%% calculating the receiver function in time domain by Yaoyao He, 2024/7/6
clear; clc; close all;

% %% convert event set to station set
% evset_dir = "D:\SUSTech businesse\observational_seismology\XE_work\sac_eventset";
% staset_dir = "D:\SUSTech businesse\observational_seismology\XE_work\sac_stationset";
% if ~exist(staset_dir, 'dir')
%     mkdir(staset_dir);
% end
% evlist = string(ls(fullfile(evset_dir,'2*')));
% for i = 1:length(evlist)
% %     list_r_Decon_PF = textscan(fopen(fullfile(evset_dir, evlist(i), 'list_r_Decon_PF.txt'), 'r'),...
% %     '%s %f %f %f %f %s %s %f %f %f %f %f %f %f %f %f %f\n', 'HeaderLines', 1);
% %     decon_filename = char(list_r_Decon_PF{1});
%     saclist = char(ls(fullfile(evset_dir,evlist(i),'*.SAC')));
%     for j = 1:size(saclist, 1)
%         parts = split(saclist(j,:), '.');
%         sta_no = parts{2};
%         if ~exist(fullfile(staset_dir,sta_no),'dir')
%             mkdir(fullfile(staset_dir,sta_no));
%         end
%         copyfile(fullfile(evset_dir,evlist(i),saclist(j, :)), fullfile(staset_dir,sta_no));
%     end
% end


%% show the raw data 
% I/O dir
sacdir= "D:\YB\yunnan2\F0_6.0_waterlevel_0.01\3_selected_stationset_v2";
stlist = string(ls(fullfile(sacdir,'NK*')));
outputdir = fullfile(fileparts(sacdir), '3_selected_stationset_v2_multiband_RFs');
if ~exist(outputdir, 'dir')
    mkdir(outputdir);
end
% basic parameters
new_delta = 0.1;
timewin = [-30 60];  
time_cut = reshape(timewin(1):new_delta:timewin(2),[],1);
fl = 0.02; fh = 1; order = 2;
np = 1000;
mod = 'ak135'; thmax = 15;
em = refinemodel(set_vmodel_v2(mod), thmax);
R = 6371;
% decon parameters
deconwin = [-30 60];
time_decon = reshape(deconwin(1):new_delta:deconwin(2),[],1);
F0 = 6; TDEL = 30; itmax = 100; minerr = 10^(-2); waterlevel = 0.001 ;

sourcedir = "D:\YB\yunnan2\F0_6.0_waterlevel_0.01\2_z_seis_cutdata_180s_1st_selection";
for i = 1:length(stlist) 
    fid = fopen(fullfile(sacdir, stlist(i), 'list_r_Decon_PF.txt'), 'r');
    if fid == -1
        continue
    end
    list_r_Decon_PF = textscan(fid, '%s %f %f %f %f %s %s %f %f %f %f %f %f %f %f %f %f %f %f \n', ...
        'Commentstyle', 'shell', 'HeaderLines', 1); 
    fclose(fid);
    if isempty(list_r_Decon_PF{1})
        continue
    end
    if ~exist(fullfile(outputdir, stlist(i)), 'dir')
        mkdir(fullfile(outputdir, stlist(i)));
    end
    sacfiles = list_r_Decon_PF{1};
    num_file = length(sacfiles);
    
    for j = 1:num_file
        parts = string(split(sacfiles(j), '.'));
        file_R = strcat(parts(1), '.', parts(2), '.', parts(3), '.', parts(4), '.', 'R', '.', parts(6));
        file_Z = strcat(parts(1), '.', parts(2), '.', parts(3), '.', parts(4), '.', 'Z', '.', parts(6));
        [sachdr, data_raw_r] = irdsac(fullfile(sourcedir, strcat(parts(3), '.', parts(4)), file_R));
        [sachdz, data_raw_z] = irdsac(fullfile(sourcedir, strcat(parts(3), '.', parts(4)), file_Z));
        stla = sachdr.stla; stlo = sachdr.stlo;
        evla = sachdr.evla; evlo = sachdr.evlo; evdp = sachdr.evdp;
        % modified by Chunquan Yu (2019/11/09) to take into account
        [dist,baz] = distance(stla,stlo, evla,evlo, [6378.137*180/pi/6371 0.0818191908426215]);
        [rayp, taup, Xp]= y_get_p_P_Pdiff (evdp, np, em);
        tt = taup + rayp.*Xp;
        dd = Xp*180/pi;
        theo_tt = interp1db(dist, dd,tt);
        time_raw = [0:sachdr.npts-1]' * sachdr.delta + sachdr.b - sachdr.o - theo_tt;
        data_cut_r = interp1(time_raw, data_raw_r, time_cut, 'linear', nan);
        data_cut_z = interp1(time_raw, data_raw_z, time_cut, 'linear', nan);
        % filtered data
        data_cut_r_filter = filtering(data_cut_r, new_delta, fl, fh, order);
        data_cut_z_filter = filtering(data_cut_z, new_delta, fl, fh, order);

        %% Decon part
        data_decon_r = interp1(time_cut, data_cut_r, time_decon, 'linear', 0);
        data_decon_z = interp1(time_cut, data_cut_z, time_decon, 'linear', 0);
        data_decon_r = detrend(data_decon_r) .* tukeywin(length(time_decon),new_delta);
        data_decon_z = detrend(data_decon_z) .* tukeywin(length(time_decon),new_delta);
        % Frequency domian
        [eqr, rms_r, nwl_r] = decon_waterlevel_matlab_v3_trueamp( data_decon_r, data_decon_z, TDEL, new_delta, waterlevel, F0);
        [aftn, rms_z, nwl_z] = decon_waterlevel_matlab_v3_trueamp( data_decon_z, data_decon_z, TDEL, new_delta, waterlevel, F0);
        
        newsachdr = sachdr;
        newsachdr.a = sachdr.b + theo_tt;
        newsachdr.b = newsachdr.a + timewin(1);
        newsachdr.e = newsachdr.a + timewin(2);
        newsachdr.npts = length(time_decon);
        newsachdr.delta = new_delta;

        newsachdz = sachdz;
        newsachdz.a = sachdz.b + theo_tt;
        newsachdz.b = newsachdz.a + timewin(1);
        newsachdz.e = newsachdz.a + timewin(2);
        newsachdz.npts = length(time_decon);
        newsachdz.delta = new_delta;

        deconR_filename = strcat('gaussa_', num2str(F0), '_', file_R, '.PF.Decon');
        deconZ_filename = strcat('gaussa_', num2str(F0), '_', file_Z, '.PF.Decon');
        imksac(newsachdr, eqr, fullfile(outputdir, stlist(i), deconR_filename));
%         imksac(newsachdz, aftn, fullfile(outputdir, stalist(i), deconZ_filename));
    end
end


