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

clear all
close all
printit = false;

% matlab script path
path_prog = '/Users/byronb/Documents/MATLAB/2016_ENRR';
cd(path_prog);                  % cd to matlab directory
restoredefaultpath              % reset matlab path
addpath(genpath(path_prog));	% add scripts to path
rehash;

cd('/Users/byronb/Raw_Data_Files.noindex/ENRR_2016/Fairall_processing/data2');

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

% create logical mask for good drops on all flights
%{
    drop indexes for each flight
    RF01 [6:16,18:21,23:27]
    RF02 [5:7,9,11:16]
    RF03 5:29
    RF04 4:29
    RF05 4:16
    RF07 4:22
    RF08 4:27
    RF09 4:26
    RF10 5:24
    RF11 5:27
    RF12 [3:23,25:30]
    RF13 3:21
    RF14 1:35
    RF15 [6:14,16:21]
    RF16 6:23
    RF18 5:27
    RF19 [3:11,13:17]
%}
drop_select = (RF==1 & ismember(drop_idx,[6:16,18:21,23:27]));
drop_select = (drop_select | (RF==2 & ismember(drop_idx,[5:7,9,11:16])));
drop_select = (drop_select | (RF==3 & ismember(drop_idx,5:29)));
drop_select = (drop_select | (RF==4 & ismember(drop_idx,4:29)));
drop_select = (drop_select | (RF==5 & ismember(drop_idx,4:16)));
drop_select = (drop_select | (RF==7 & ismember(drop_idx,4:22)));
drop_select = (drop_select | (RF==8 & ismember(drop_idx,4:27)));
drop_select = (drop_select | (RF==9 & ismember(drop_idx,4:26)));
drop_select = (drop_select | (RF==10 & ismember(drop_idx,5:24)));
drop_select = (drop_select | (RF==11 & ismember(drop_idx,5:27)));
drop_select = (drop_select | (RF==12 & ismember(drop_idx,[3:23,25:30])));
drop_select = (drop_select | (RF==13 & ismember(drop_idx,3:21)));
drop_select = (drop_select | (RF==14 & ismember(drop_idx,1:35)));
drop_select = (drop_select | (RF==15 & ismember(drop_idx,[6:14,16:21])));
drop_select = (drop_select | (RF==16 & ismember(drop_idx,6:23)));
drop_select = (drop_select | (RF==18 & ismember(drop_idx,5:27)));
drop_select = (drop_select | (RF==19 & ismember(drop_idx,[3:11,13:17])));

% save to structure
sname = 'ENRR_G4_sonde_all_zinterp.mat';
snd_zinterp = struct('RF',RF,'jd',jd_all,'drop_idx',drop_idx,'drop_select',drop_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('ENRR_G4_sonde_all_zinterp.mat');
