%
% ver 1  11/2016 within EPOS-IP project.
%
% ver 1.01 - error handling for case where a user tries to load a txt
% file before providing number of variables (12.10.2021 by ML)
%
% DESCRIPTION: Program to create the catalogue v2.0x in the 
%              Matlab format file from txt file.
%
% INSTRUCTION:
% 1. Input data to txt file. 
%    - First two columns must be ‘ID’ and ‘Time’ respectively. 
%      Time will be converted in the Matlab format serial time.  
%      Input time format define inside the ‘Time_format.txt’ file
%    - Next columns are the rest catalog parameters. All defined 
%      parameters are listen in  ‘Catalog_description_v2_0.docx’ – 
%      if it is possible please use them
%    - Don't remove the first line with names of the column. 
%      The separator between columns is defined by ‘;'
%    - Example input file is ‘example_dataset.txt’ where 6 variables 
%      are defined with 2 seismic events
% 2. Run the program ‘Catalog_create_v2_0x.m’. Follow the steps 1-5 
%    in program window. 
% 3. The result file is ‘Catalog.mat’
%
% INPUT FILES:
% *.txt - the file which contains variables name and events



h.catFig = figure('position', [120 120 1000 600],...
    'menubar','none',...
    'Name',datestr(now));
set(h.catFig, 'resize', 'off');

%% Panel covers full figure
h.Main_Panel = uipanel('Parent', h.catFig,...
    'units','normalized',...
    'position', [0 0 1 1],...
    'visible','on');

%% Oversized sub-panel
h.ThisPanelMoves = uipanel('parent',h.Main_Panel,...
    'units','normalized',...
    'position', [0 -1 0.99 1.99]);


%% Add input of file panel
panel0 = uipanel('parent', h.ThisPanelMoves ,...
    'Title','1. READ DATA',...
    'Position', [.01 .94 0.95 0.06]);

s0_control = uicontrol('parent',panel0,'Style','text',...
    'String', 'Input number of variables:',...
    'Position', [5 12 125 30]);

s1_control = uicontrol('parent',panel0,'Style','text','ForegroundColor','r',...
    'String', 'First enter the number of variables and then read txt file',...
    'Position', [320 18 300 15]);

t0_control = uicontrol('parent',panel0,'Style','edit',...
    'String', 'No',...
    'Position', [135 15 50 20]);

b0_control = uicontrol('parent',panel0,'Style','pushbutton',...
    'String', 'Read txt Data',...
    'Position', [195 15 100 20],...
    'callback','[plik,sciezka]=uigetfile(''*.txt'',''Dane 1'');f1=get(t0_control,''String'');set(gcbf, ''Name'', datestr(now))');

waitfor(h.catFig,'Name');

f1=str2double(f1);
system=computer;
if strcmp(system(1:5),'PCWIN')
    dane1 = textread([sciezka '\' plik], '%s','delimiter',';');
else
    dane1 = textread([sciezka '/' plik], '%s','delimiter',';');
end

for i=1:f1
    if isnan(i)
        s2_control = uicontrol('parent',panel0,'Style','text','ForegroundColor','r',...
            'String', 'Number of variables must be entered before loading the file! Please close, CLEAR WORKSPACE and start again!',...
            'Position', [320 18 600 15]);
        error('NUMBER OF VARIABLES MUST BE ENTERED BEFORE LOADING THE FILE! PLEASE CLOSE, CLEAR WORKSPACE AND START AGAIN!');
    else    
        cnames{i,1}=dane1{i,1};
    end
end

clear b0_control  panel0 plik s0_control sciezka t0_control

%% Add subpanels
panel1 = uipanel('parent', h.ThisPanelMoves ,...
    'Title','2. CHOOSE VARIABLES',...
    'Position', [.01 .03 .19 0.90]);

panel2 = uipanel('parent', h.ThisPanelMoves ,...
    'Title','3. CHECK DESCRIPTIONS',...
    'Position', [.21 .03 .20 0.90]);

panel3 = uipanel('parent', h.ThisPanelMoves ,...
    'Title','4. CHECK UNIT',...
    'Position', [.43 .03 .16 0.90]);

panel4 = uipanel('parent', h.ThisPanelMoves ,...
    'Title','5. MAGNITUDE SELECTION',...
    'Position', [.61 .03 .16 0.90]);
panel5 = uipanel('parent', h.ThisPanelMoves ,...
    'Title','6. DATA FORMAT',...
    'Position', [.79 .03 .15 0.90]);


%% Add slider
h.Tab1_Slider = uicontrol('Parent', h.ThisPanelMoves,...
    'units','normalized',...
    'position',[.98, 0 .02 .95],...
    'style', 'slider',...
    'value', 1,...
    'callback', {@SliderCallback, h});

guidata(h.catFig,h)


%% Add catalog variables from input file
%variable names
for i = 1:f1
    if i<3
        a(i,1)=uicontrol('parent',panel1,'style','text',...
            'string',cnames{i,1},...
            'position',[8 1040-i*25 50 20],...
            'Value',1);
    else
        a(i,1)=uicontrol('parent',panel1,'style','checkbox',...
            'string',cnames{i,1},...
            'position',[5 1040-i*25 180 20],...
            'Value',1,...
            'callback','table1=get(a,''Value'')') ;
    end
end
a_control = uicontrol('parent',panel1,'Style','pushbutton',...
    'String', 'Confirm your selection',...
    'Position', [5 1040-(i+2)*25 120 20],...
    'callback','table1=get(a,''Value'');set(gcbf, ''Name'', datestr(now))');

waitfor(h.catFig,'Name');

%variable descriptions
list =lista_v3();
for k=1:f1
    for i=1:length(list)
        por_k1(i,k)=strcmp(cnames{k,1} ,list{i,1});
    end
    
    if length(find(por_k1(:,k)==1))==1
        vnames{k,1}=list{find(por_k1(:,k)==1),5};
        vunit{k,1}=list{find(por_k1(:,k)==1),4};
        vtype{k,1}=list{find(por_k1(:,k)==1),3};
    else
        vnames{k,1}=cnames{k,1};
        vunit{k,1}={'input/delete unit'};
        vtype{k,1}=1;
    end
end


for i = 1:f1
    if table1{i}==1
        b(i,1)=uicontrol('parent',panel2,'style','edit',...
            'string',vnames{i,1},...
            'position',[5 1040-i*25 180 20],...
            'Value',1,...
            'callback','table2=get(b,''String'')') ;
    else
        b(i,1)=uicontrol('parent',panel2,'style','edit',...
            'string','',...
            'position',[5 1040-i*25 180 20],...
            'Value',1,...
            'callback','table2=get(b,''String'')') ;
    end
end
b_control = uicontrol('parent',panel2,'Style','pushbutton',...
    'String', 'OK',...
    'Position', [5 1040-(i+2)*25 180 20],...
    'callback','table2=get(b,''String'');set(gcbf, ''Name'', datestr(now))');

waitfor(h.catFig,'Name');

%unit
for i = 1:f1
    if i<3
        c(i,1)=uicontrol('parent',panel3,'style','text',...
            'string','',...
            'position',[8 1040-i*25 50 20],...
            'Value',1);
    else
        if table1{i}==1
            c(i,1)=uicontrol('parent',panel3,'style','edit',...
                'string',vunit{i,1},...
                'position',[5 1040-i*25 140 20],...
                'Value',1,...
                'callback','table3=get(c,''String'')') ;
        else
            c(i,1)=uicontrol('parent',panel3,'style','edit',...
                'string','',...
                'position',[5 1040-i*25 140 20],...
                'Value',1,...
                'callback','table3=get(c,''String'')') ;
        end
    end
    
end
b_control = uicontrol('parent',panel3,'Style','pushbutton',...
    'String', 'OK',...
    'Position', [5 1040-(i+2)*25 140 20],...
    'callback','table3=get(c,''String'');set(gcbf, ''Name'', datestr(now))');

waitfor(h.catFig,'Name');

for i=1:length(table3)
    if strcmp(table3{i,1} ,'')==1;
        table3{i,1}=[];
    end
    if iscell(table3{i})==1
        table3{i,1}=table3{i}{1}
    end
end

%% Magnitude choosing
for i = 1:f1
    if i<3
        d(i,1)=uicontrol('parent',panel4,'style','text',...
            'string','',...
            'position',[8 1040-i*25 50 20],...
            'Value',0);
    else
        if table1{i}==0
            d(i,1)=uicontrol('parent',panel4,'style','text',...
                'string','',...
                'position',[5 1040-i*25 100 20]) ;
        else
            d(i,1)=uicontrol('parent',panel4,'style','checkbox',...
                'string','Magnitude',...
                'position',[5 1040-i*25 100 20],...
                'Value',0) ;
        end
    end
end

d1_control = uicontrol('parent',panel4,'Style','pushbutton',...
    'String', 'OK',...
    'Position', [5 1040-(i+2)*25 140 20],...
    'callback','table4=get(d,''Value'');set(gcbf, ''Name'', datestr(now))');

waitfor(h.catFig,'Name');


%% DATA TYPE
for i = 1:f1
    if vtype{i,1}==3
    vtype_text{i,1}='text';
    else
        vtype_text{i,1}='double';
    end
end

for i = 1:f1
    if table1{i}==1
        e(i,1)=uicontrol('parent',panel5,'style','edit',...
            'string',vtype_text{i,1},...
            'position',[5 1040-i*25 130 20],...
            'Value',1,...
            'callback','table5=get(e,''String'')') ;
    else
        e(i,1)=uicontrol('parent',panel2,'style','edit',...
            'string','',...
            'position',[5 1040-i*25 130 20],...
            'Value',1,...
            'callback','table5=get(e,''String'')') ;
    end
end
e_control = uicontrol('parent',panel5,'Style','pushbutton',...
    'String', 'OK',...
    'Position', [5 1040-(i+2)*25 130 20],...
    'callback','table5=get(e,''String'');set(gcbf, ''Name'', datestr(now))');

waitfor(h.catFig,'Name');

e2_control = uicontrol('parent',panel5,'Style','pushbutton',...
    'String', 'Generate catalogue',...
    'Position', [5 1040-(i+4)*25 130 40],...
    'callback','set(gcbf, ''Name'', datestr(now))');

waitfor(h.catFig,'Name');

for i=1:length(table4)
    if length(find(table4{i,1}==1))==0;
        vfieldType{i,1}=[];
    else
        vfieldType{i,1}='Magnitude';
    end
end


%% Catalog generate
% Time conversion
formatIn = textread('Time_format.txt', '%s', 'headerlines', 1,'delimiter',';');
formatIn = [formatIn{1,1}];
for i = 1:((length(dane1)/f1)-1)
    Time(i,1)=datenum([cell2mat({dane1{(f1*i)+2,1}(1:10)}) ' ' strrep(cell2mat({dane1{(f1*i)+2,1}(12:end)}),',',',')],formatIn)  ;
    
end

%
k=1;
for i=1:f1
    if table1{i,1}==1
        Catalog(k).field=cnames{i,1};
        Catalog(k).type=vtype{i,1};
        if table4{i,1}==1
            Catalog(k).type=4;
        end
        if k==1 %ID values
            for z=1:(length(dane1)/f1)-1
                Catalog(k).val{z,1}=dane1{(f1*z)+1,1}
            end
        end
        if k==2 %time values
            Catalog(k).val=Time;
        end
        if k>2
            if strcmp(table5{i,1},'text')==1 %vtype_text
                for z=1:(length(dane1)/f1)-1
                    Catalog(k).val{z,1}=dane1{(f1*z)+k,1};
                    
                end
                Catalog(k).type=3;
            else
                for z=1:(length(dane1)/f1)-1
                    Catalog(k).val(z,1)=str2num(dane1{(f1*z)+k,1});
                    
                end
            end
        end
        Catalog(k).unit=table3{i};
        Catalog(k).description=table2{i};
        Catalog(k).fieldType=vfieldType{i};
        
        k=k+1;
    end
end

save Catalog Catalog
close(h.catFig)
clear all;clc;

load Catalog
clear panel1 panel2 panel3 panel4