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

clear
close all
printit = false;
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;

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

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

% create index for drops in each circle over all flights
% 14 circle patterns over 11 flights
%{
    circle drop indexes for each flight
    RF01 [7:16]
    RF02 [7:15]
    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:26]
%}
circ_select = zeros(size(drop_idx));
circ_select(RF==1 & ismember(drop_idx,7:16)) = 1;
circ_select(RF==2 & ismember(drop_idx,7:15)) = 2;
circ_select(RF==3 & ismember(drop_idx,5:12)) = 3;
circ_select(RF==3 & ismember(drop_idx,14:16)) = 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:26)) = 14;


% save to structure
sname = 'ATOMIC_P3_sonde_all_zinterp.mat';
snd_zinterp = struct('RF',RF,'jd',jd_all,'drop_idx',drop_idx,'circ_select',circ_select,...
    'lon',lon_all,'lat',lat_all,'z',z_ref,'U',U_all,'udir',udir_all,...
    'Un',Un_all,'Ue',Ue_all,'ta',ta_all,'rh',rh_all,'P',P_all,'qa',qa_all);
save(sname,'snd_zinterp');

clear
load('ATOMIC_P3_sonde_all_zinterp.mat');
