% This is a Wrapper Script for performing Transformation
% to Equivalent Dimensions [function "T2ED_V2_*"]. The
% comments for tranformation process application can be 
% found within 'T2ED_V2_*' code. Here the input data & 
% parameters (function's arguments) are defined by the 
% User. Please modify the script arguments accordingly. 
% The User can set the values of input arguments at the 
% beginning of the script, at LINES 18-39. PLEASE REFER 
% ALSO TO APPLICATION DOCUMENTATION:
% "READ_ME_App_1A_v2_Description_T2ED.docx"

clc
clear; 
close all;

% PLEASE SET INPUT ARGUMENTS [LINES 18-39]
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SEIS_DATA='ST2_SEIS_Data.txt';                       % Seismic Data File - NOTE: SEIS_DATA=[] is valid as well
SEIS_FIELDS='ST2_SEIS_Fields.txt';                    % Seismic Data Fields File
PROD_DATA='ST2_PROD_Data.txt';                  % Production Data (non-seismic) File - NOTE: PROD_DATA=[] is valid as well
PROD_FIELDS='ST2_PROD_Fields.txt';               % Production Data (non-seismic) Fields File
MScale='ML';                                                          % Magnitude Scale (e.g. 'ML', 'Mw' etc). NOTE: MScale=[] is also valid
Mc=1.0;                                                                 % Select Mc
randmode='norm';                                                     %EQ_DIM parameter: Randomization Mode: 'exp', 'uni', 'norm', 'no'              
sample_mult='both';                                                 %EQ_DIM parameter: Sample Multiplication Mode: 'no', 'left', 'right', 'both' 
DT=0;                                                                   % Time Lag  (i.e. response of Seismicity to Operational Activities)                                                                          
opt1='1';                                                                % '1', '2', '3' - for Seismic Events, Time, or All Data, respectively) - Mode for Background & Testing Data
    %  ------------------------------------------------ Background (BG) and Testing (TS) Data:--------------------------------------------------
                                                                           if opt1=='1'
BG=[1 1800];                                                            % starting - ending point for Background (BG) Data                                           
TS=[1000 1500];                                                           % starting - ending point for Testing (TS) Data 
                                                                           elseif opt1=='2'
BG=[737000 737100];                                              % staring - ending time for Background (BG) Data 
TS=[737100 737200];                                              % staring - ending time Testing (TS) Data 
                                                                           elseif opt1=='3'                   
BG=[];TS=[];                                                         % All data selected as Backgroung and Testing Data/ PLEASE DO NOT MODIFY 
                                                                           end 
   %  ------------------------------------------------------------------------------------------------------------------------------------------------------------
PLOT='ON';                                                            % 'ON' for Enable - 'OFF' for Disable plotting 
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%% ************************ STEPS OF ANALYSIS ************************ 
 %* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
%% STEP 1.  DATA Upload, please type the files for the upcoming analysis (at Lines 18-19):
% THE FILES must be located in the 'Data' directory and must be in IS-EPOS 
% ASCII format.
if isempty(PROD_DATA)                                            % ONLY SEISMIC DATA IS SELECTED
Mode1=1;
elseif isempty(SEIS_DATA)                                        % ONLY PRODUCTION DATA IS SELECTED
Mode1=2;
else                                                                      % BOTH SEISMIC AND PRODUCTION DATA IS SELECTED
Mode1=3;
end

%% STEP 2.  SEISMIC and Production Data Conversion to compatible format:
[Catalog,PRODdata,Mode1]=Data_Hand_A2M(SEIS_DATA,SEIS_FIELDS,PROD_DATA,PROD_FIELDS,Mode1);


%% STEP 3.  Time and Magnitude Columns importing (seismic data):  
% Select time vector from Seismic Catalog
for i=1:size(Catalog,2)
    if strcmp(Catalog(i).field,'Occurrence_Time')==1
          Ctime=Catalog(i).val;end                                % Catalog Time Vector
    if strcmp(Catalog(i).field,MScale)==1                      % Select Magnitude Scale      
        CMag=Catalog(i).val;end 
end


%% STEP 4.  Mc filtering:  
%Filter Data for M>=Mc
if Mode1~=2;
if isempty(MScale)~=1
Ctime=Ctime(CMag>=Mc);                                 %           
for i=1:size(Catalog,2)
    Catalog(i).val=Catalog(i).val(CMag>=Mc);
end
else Mc=-20;
end
end
%% STEP 5.  Date Column importing (Production data):  
% Select time vector from Production Dataset
if Mode1~=1
for i=1:size(PRODdata,2)
    if strcmp(PRODdata(i).field,'Production_Time')==1,nn=1;
          PRODtime=PRODdata(i).val;                                % ProductionTime Vector
    end
end    
end
% ----------------------------------------------------------------------------------------
%% STEP 6. DATA Selection (define vectors for analysis and reshape data):
% Select vectors (data columns) to be analyzed
Cvector=[1:size(Catalog,2)];PRODvector=[1:size(PRODdata,2)];      %   This is for all vectors, - PLEASE Modify if needed
% Set input datasets
Data=Catalog(Cvector);for i=1:size(Data,2);Data(i).source='Catalog';end
Data2=PRODdata(PRODvector);for i=1:size(Data2,2);Data2(i).source='Production';end

%% STEP 7.  Parameter Values Selection (SET at Lines 24-26) : 
randmode;                                                             %       
sample_mult;                                                          %       
DT;                                                                      %       

%% STEP 8. Select Mode for Background and Testing Data (1 - for 'Seismic Events', 2 - for 'Time', 3 - for 'All Data'):
option=opt1;    %                                                                                        

%% STEP 9. Background (BG) and Testing (TS) Data (1: starting - ending point/ 2: staring - ending time):
BG;TS;                                                                  % SET at Lines 30-36       
if Mode1~=2;NData=numel(Data(1).val);
if  strcmp(opt1,'1') && NData<max([BG TS]) 
error(['The events range exceed Data size, please select different Backgound and/or Testing Data'...
    ' (for the selected parameters Data size=',num2str(NData),')']);end;end
%% STEP 10. RUN Function after selecting a scenario
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
scenario=Mode1;        % SCENARIO ('1' - for Seismic Data, '2' - for Production Data, or '3' - for Both)    

switch scenario
    case 1
% SCENARIO 1. SEISMIC DATA
Data2=[];PRODtime=[];DT=[];                       % empty production data  
[Tdata]=T2ED_V2_8(Data,Data2,Ctime,PRODtime,randmode,sample_mult,DT,option,BG,TS,Mc);

    case 2 
            % SCENARIO 2. PRODUCTION DATA
Data=[];Ctime=[];DT=[];Mc=[];                   % empty seismic data  
[Tdata]=T2ED_V2_8(Data,Data2,Ctime,PRODtime,randmode,sample_mult,DT,option,BG,TS,Mc);

    case 3 
            % SCENARIO 3. Both SEISMIC/PRODUCTION DATA
[Tdata]=T2ED_V2_8(Data,Data2,Ctime,PRODtime,randmode,sample_mult,DT,option,BG,TS,Mc);
end

%% STEP 11 Display (optional) and Save results (results are saved already at directory 'Outpus_ED')
%% plot all histograms
if strcmp(PLOT,'ON')==1;                                           % Set at Line  39          
  for j=1:length(Tdata)
      figure
      nn=j;plottesth,cd Outputs_ED;saveas(gcf,[Tdata(j).field,'.jpg']);cd ../
  end
end
