% concatenate all flights to one structure, adding RF and drop index
% variables

%% initialize
clear
close all
plotit = true;
LatLonLim = [299,312,9,19];  % for map plots
warning('off','MATLAB:MKDIR:DirectoryExists');

% matlab script path
path_prog = '/Users/byronb/Documents/MATLAB/2020_ATOMIC';
cd(path_prog);                  % cd to matlab directory
addpath(genpath(path_prog));	% add scripts to path
rehash;

% path for saving plots from this script
path_plots = '/Users/byronb/Raw_Data_Files.noindex/ATOMIC_2020/plots/multiFlight_Pgrid';
mkdir(path_plots);

cd('/Users/byronb/Raw_Data_Files.noindex/ATOMIC_2020/drops/raw');

%% load p-interp data for all flights
for ii=1:11   % all flights
    file = dir(['*_RF',sprintf('%02.0f',ii),'_pinterp.mat']);
    load(file.name);
    if ii==1
        rf = ones(size(snd_p.jd))*ii;
        drop_idx = 1:length(snd_p.jd);
        jd_all = snd_p.jd;
        lat_all = snd_p.lat;
        lon_all = snd_p.lon;
        P_ref = snd_p.P;
        z_all = snd_p.z;
        qa_all = snd_p.qa;
        rh_all = snd_p.rh;
        ta_all = snd_p.ta;
        udir_all = snd_p.udir;
        U_all = snd_p.U;
        Un_all = snd_p.Un;
        Ue_all = snd_p.Ue;
    else
        rf = [rf,ones(size(snd_p.jd))*ii];
        drop_idx = [drop_idx,1:length(snd_p.jd)];
        jd_all = [jd_all,snd_p.jd];
        lat_all = [lat_all,snd_p.lat];
        lon_all = [lon_all,snd_p.lon];
        z_all = [z_all,snd_p.z];
        qa_all = [qa_all,snd_p.qa];
        rh_all = [rh_all,snd_p.rh];
        ta_all = [ta_all,snd_p.ta];
        udir_all = [udir_all,snd_p.udir];
        U_all = [U_all,snd_p.U];
        Un_all = [Un_all,snd_p.Un];
        Ue_all = [Ue_all,snd_p.Ue];
    end
end

%% create index for circle drops over all flights
% 14 circle patterns over 11 flights
%{
    circle drop_indexes for each flight
    RF01 [7:16]
    RF02 [8:16]
    RF03 [5:12,14:16] & [27:36]
    RF05 [1:12]
    RF06 [5:16]
    RF07 [6:17]
    RF08 [5:16]
    RF09 [5:16] & [17:22,24,26:28]
    RF10 [5:13] & [14:25]
    RF11 [5:16] & [17:27]
%}
circ_select = zeros(size(drop_idx));
circ_select(rf==1 & ismember(drop_idx,7:16)) = 1;
circ_select(rf==2 & ismember(drop_idx,8:16)) = 2;
circ_select(rf==3 & ismember(drop_idx,[5:12,14:16])) = 3;
circ_select(rf==3 & ismember(drop_idx,27:36)) = 4;
circ_select(rf==5 & ismember(drop_idx,1:12)) = 5;
circ_select(rf==6 & ismember(drop_idx,5:16)) = 6;
circ_select(rf==7 & ismember(drop_idx,6:17)) = 7;
circ_select(rf==8 & ismember(drop_idx,5:16)) = 8;
circ_select(rf==9 & ismember(drop_idx,5:16)) = 9;
circ_select(rf==9 & ismember(drop_idx,[17:22,24,26:28])) = 10;
circ_select(rf==10 & ismember(drop_idx,5:13)) = 11;
circ_select(rf==10 & ismember(drop_idx,14:25)) = 12;
circ_select(rf==11 & ismember(drop_idx,5:16)) = 13;
circ_select(rf==11 & ismember(drop_idx,17:27)) = 14;

%% plot each flight path, showing circle drops
for ii = 1:11
    jj = find(rf==ii);
    RF = ['RF',sprintf('%02i',ii)];
    lat = lat_all(jj);
    lon = lon_all(jj);
    jd = jd_all(jj);
    ddd = floor(jd(1));
    % idx for all valid drops
    kk = isfinite(lat_all(jj)) & isfinite(lon_all(jj));
    % idx for all circle drops
    cc = isfinite(lat_all(jj)) & isfinite(lon_all(jj)) & circ_select(jj);
    map_ATOMIC_2020(lon+360,lat,kk,cc,LatLonLim,ddd,RF);
    if plotit
        ppath = fullfile(path_plots,['ATOMIC_P3_',RF,'_track_plot.png']);
        print(ppath,'-dpng');
    end
    close all
end

%% save merged data to structure
sname = 'ATOMIC_P3_sonde_all_pinterp.mat';
snd_pinterp = struct('RF',rf,'jd',jd_all,'drop_idx',drop_idx,'circ_select',circ_select,...
    'lon',lon_all,'lat',lat_all,'P',P_ref,'U',U_all,'udir',udir_all,...
    'Un',Un_all,'Ue',Ue_all,'ta',ta_all,'rh',rh_all,'qa',qa_all,'z',z_all);
save(sname,'snd_pinterp');

clear
load('ATOMIC_P3_sonde_all_pinterp.mat');
