function [Catalog,PRODdata,Mode1]=...
    Data_Hand_A2M(SEIS_DATA,SEIS_FIELDS,PROD_DATA,PROD_FIELDS,Mode1)
clc

switch Mode1
    
    case 1
%% SEISMIC DATA
cd CATALOGS\
SData=load(SEIS_DATA);                                         % Seismic Data           
SFields=fileread(SEIS_FIELDS);                                 % Seismic Data Fields
cd ../

Na=length(SFields);if SFields(Na)~=' ';SFields(Na+1)=' ';end
[cou,c,Datime,Catalog]=Fields_dat(SData,SFields,1);
 ss1=SetParams(Catalog);%Catalog=Catalog(ss1);
PRODdata=[];ss=[];s2=[];ss2=[];
    case 2
%% --------------------------------------------------------------------------------------------------------------
% NON-SEISMIC DATA
cd PRODUCTION_DATA\
OData=load(PROD_DATA);                                         % Operational Data           
OFields=fileread(PROD_FIELDS);                                 % Operational Data Fields
cd ../

Na1=length(OFields);if OFields(Na1)~=' ';OFields(Na1+1)=' ';end
[cou1,c1,Datime1,PRODdata]=Fields_dat(OData,OFields,2);
ss2=SetParams(PRODdata);%PRODdata=PRODdata(ss2);
Catalog=[];s=[];s1=[];ss1=[];

    case 3
%% ------------------------------------------------------------------------------------------------------------------------------------
% BOTH -SEISMIC and NON-SEISMIC DATA

cd CATALOGS\                                             %Seismic Data
SData=load(SEIS_DATA);                                         % Seismic Data           
SFields=fileread(SEIS_FIELDS);                                 % Seismic Data Fields
cd ../

Na=length(SFields);if SFields(Na)~=' ';SFields(Na+1)=' ';end
[cou,c,Datime,Catalog]=Fields_dat(SData,SFields,1);
 ss1=SetParams(Catalog);                               %Catalog=Catalog(ss1);
 
cd PRODUCTION_DATA\                                             % non-Seismic Data
OData=load(PROD_DATA);                                         % Production (Operational) Data           
OFields=fileread(PROD_FIELDS);                                  % Production (Operational) Data Fields
cd ../

Na1=length(OFields);if OFields(Na1)~=' ';OFields(Na1+1)=' ';end
[cou1,c1,Datime1,PRODdata]=Fields_dat(OData,OFields,2);
ss2=SetParams(PRODdata);%PRODdata=PRODdata(ss2);

end
 % save('Catalog_ST2_Test','Catalog')


%% -----------------------------------------    F U N C T I O N S    -----------------------------------------
function [cou,c,Datime,OUT]=Fields_dat(indata,infields,iop)

Datime=datenum(indata(:,1),indata(:,2),indata(:,3),indata(:,4),indata(:,5),indata(:,6));       % Convert time to matlab format

% Define Fields
c=1;
for i=1:length(infields)-1
if strcmp(infields(i),' ')==1;cou(i)=0;
else cou(i)=c;end
if strcmp(infields(i),' ')==0 & strcmp(infields(i+1),' ')==1;c=c+1;end
end

if iop==1
OUT(1).field='Occurrence_Time';OUT(1).val=Datime;
elseif iop==2
OUT(1).field='Production_Time';OUT(1).val=Datime;
end

for i=2:c-1
OUT(i).field=infields(cou==i);    
OUT(i).val=indata(:,i+5);
end

%Set Field Type for Magnitude Recognition
for i=1:size(OUT,2)
    if strcmp(OUT(i).field,'ML') || strcmp(OUT(i).field,'Mw') || strcmp(OUT(i).field,'M') ...
   || strcmp(OUT(i).field,'Ms') || strcmp(OUT(i).field,'mb') || strcmp(OUT(i).field,'Md')
         OUT(i).fieldType='Magnitude';
    else
    OUT(i).fieldType=[];
    end
end

end

%% -----------------------------------------------------------------------------------------------------------


function ss1=SetParams(Data)
%Select Parameters from Seismic Catalog
s1=size(Data,2);
for i=1:length(s1)
    ss1=s1(isnan(s1)==0);
end

end
% ---------------------------------------------------------------------------------------------------------------------------------

%%  ------------------------- Function not usable in this version of the code ------------------------
function [Data,Fields,s,f]=DatLoad(dstr,da)        
% Select Seismic Catalog
if da==1;dlabel1='Select Seismic Catalog:';dlabel2='Select Catalog FIELDS file:';
elseif da==2;dlabel1='Select Production Data file:';dlabel2='Select Production FIELDS file:';end

[s,ok]=listdlg('PromptString',dlabel1,...
    'SelectionMode','single',...
    'ListString',dstr);
if ok; Data=load(dstr{s});end

[f,ok]=listdlg('PromptString',dlabel2,...
    'SelectionMode','single',...
    'ListString',dstr);
if ok; Fields=fileread(dstr{f});end

end


%%---------------------------------------------------------------------------------------------------------------




















end


