% da_red.m - process manual flux and motcorr output data.
% Save filtered output in 10 min and hourly flux files.
% Create analysis and diagnostic plots in separate program: plot_da_red.m
% joke: "da" means "don't ask" ;) and we don't know what red means. It
% might have used to mean read. 


% RUN THIS SCRIPT AFTER RUNNING run_motcorr.m

%% initialize & set matlab script path for your system
close all;
clear all;
warning ('off','MATLAB:MKDIR:DirectoryExists');
setup_cruise;

%% set system specific paths
% edit these as necessary for your system
% data drive is the root directory for cruise data
% path prog is the directory of matlab scripts for this project
% system specific path defs
sysType = computer;
username=char(java.lang.System.getProperty('user.name'));
if strncmp(sysType,'MACI64',7)     % set Mac paths
    data_drive = '/Users/eliz/DATA/';
    path_prog = fullfile(data_drive,cruise,ship,'Scientific_analysis','programs');
elseif strncmp(sysType,'PCWIN64',7)  % set PSD DAC paths
    data_drive = 'D:\DATA\';
    path_prog = fullfile(data_drive,cruise,ship,'Scientific_analysis','programs');
end

% define path to 'Processed' folder with da files
path_proc_data = fullfile(data_drive,cruise,ship,'flux','Processed','final');

% define paths to folders for saving da plots for this cruise leg
png_path = fullfile(data_drive,cruise,ship,'flux','Processed_Images','da_red');
mkdir(png_path); % create if it doesn't exist

% set matlab script path
restoredefaultpath
cd(fullfile(path_prog,'flux'));
addpath(genpath(fullfile(path_prog,'flux')));
rehash toolboxcache;

%% COARE options
jcool = 1;  % set to 1 to compute cool skin
jwarm = 0;  % set to 1 for warm layer estimate if using ship 5m TSG data
zref = 10;  % user defined reference height

%% load 10-min data from prior processing
% load corrected 10-min data from manualflux_eval and fix_met_sea
file_b_in_version = 'v2';  % v2 for Jan 2022 tests
file_d_in_version = 'v2'; % v2 for Jan 2022 tests
file_out_version = 'v2'; % v2 for Jan 2022 tests

% 10-min corrected manfluxeval data and motcorr data
b_indir = [data_drive cruise '/' ship '/flux/Processed/' file_b_in_version '/'];
d_indir = [data_drive cruise '/' ship '/flux/Processed/motcorr/'];
e_outdir = [data_drive cruise '/' ship '/flux/Processed/final/'];

b10_infile  = [b_indir cruise '_10min_nav_met_sea_flux_' file_b_in_version '.mat'];  
b1_infile   = [b_indir cruise  '_1min_nav_met_sea_flux_' file_b_in_version '.mat'];  
d10_infile  = [d_indir cruise '_10min_motcorr_' file_d_in_version '.mat'];  
d1_infile   = [d_indir cruise  '_1min_motcorr_' file_d_in_version '.mat'];  

d10_outfile  = [d_indir cruise '_10min_motcorr_' file_d_in_version '_da.mat'];  
d1_outfile   = [d_indir cruise '_1min_motcorr_' file_d_in_version '_da.mat'];  


load(b10_infile);  % structure b10
load(b1_infile);   % structure b1
load(d10_infile);  % strcuture d10
load(d1_infile);   % strcuture d1

% output 10-min and 60-min (hourly) legacy text files
legacy_dir = [data_drive cruise '/' ship '/flux/Processed/legacy/decorr/'];
sname_10 = [cruise '_legacy_10min_' file_out_version];
sname_60 = [cruise '_legacy_60min_' file_out_version];

nt = length(b10.jd);

%% fixes

% % 1/27/2022 fixed for leaving out 1E-3 for qa unit conversion. Fixed in motcorr. Just need to rerun. 
% d10.wT_cov = ( d10.wTson_cov + 0.51*(b10.Ta+C2K).*b10.usr.*b10.qsr) ./ (1 + 0.51*b10.qa*1E-3);
% d10.hs_cov = d10.wT_cov.* b10.rhoa * cpa;

%%

% % pad d10 with half day of nan at end to match length of b10, if needed
% fmet = fields(d10);
% if strcmp(cruise,'PISTON_2019') == 1 
%    for j = 1:length(fmet)
%        if strcmp(fmet{j}, 'lag') ~= 1 && strcmp(fmet{j}, 'Fx') ~= 1
%             clear dim that this;
%             this = d10.(fmet{j});
%             [l, dim] = size(this);
%             newl = l+(12*6);
% %           disp(dim);
%             for i = 1:dim
%                 that(1:newl,i) = [this(:,i); nan(12*6, 1)];
%             end
%             d10.(fmet{j}) = that;
%        end
%     end
% end
% 
% % pad d1 with half day of nan at end to match length of b1
% fmet = fields(d1);
% if strcmp(cruise,'PISTON_2019') == 1 
%    for j = 1:length(fmet)
%         clear dim that this;
%         this = d1.(fmet{j});
%         [l, dim] = size(this);
%         newl = l+(12*60);
% %           disp(dim);
%         for i = 1:dim
%             that(1:newl,i) = [this(:,i); nan(12*60, 1)];
%         end
%         d1.(fmet{j}) = that;
%     end
% end

%% fixes before next run of run_motcorr.m, if needed
% if isfield(d10,'Tbar') ~= 1
%     d10.Tbar = d10.wbar;
%     d10.vbar = d10.wbar;
%     d10.Fx = zeros(nt,48);
%     d10.licor_qa_std = d10.licor_q_std;
%     d10 = rmfield(d10, 'licor_q_std');
% end 

%% if you wanted to load in fields by name instead of structure
% hesitate to do this before I understand how many variables are 
% getting rewritten without justification in this program
% %%% load this 1-hour segment of mean corrected 10-min data. call it
% %%% "a" so that things don't get overwritten or confused yet
% fields_f10 = fields(b10);
% nf = length(fields_f10);
% for k = 1:nf
%    this = b10.(fields_f10{k});
%    eval([fields_f10{k} ' = this;']); 
% end
% 
% fields_ff = fields(ff);
% nf = length(fields_ff);
% for k = 1:nf
%    this = d10.(fields_ff{k});
%    eval([fields_ff{k} ' = this;']); 
% end

%% save all-cruise legacy text files: da, cr, sp

da_readme = {'da 10-min variable designations as follows:  144 x 176 array';...
    ' ';
    'Note:  historical da format is retained, but Hare corrections not computed';...
    'in this version of the flux code & therefore set to NaN in the da output.';...
    ' ';
    'Also note: COARE input format is for vectorized version 3.5.';...
    ' ';
    'COL     VARIABLE        DESCRIPTION';...
    '1       jd              decimal day of year';...
    '2:10    $ship           COARE 3.5 input, ship:[Utrue,Tair,RH,slp_mb,Tseawater,';...
    '                        sw_dn,lw_dn,lat,rain_rate]';...
    '11      no_data';...
    '12      b10.wspd';...
    '13      b10.wdir';...
    '14      b10.rspd';...
    '15      b10.rdir';...
    '16:24   $psl            COARE 3.5 input, PSL, as above';...
    '25:29   no_data         To maintain historical column numbering...';...
    '30      u_ship          ship mean true wind speed (m/s)';...
    '31      u_ship_min      ship minimum true wind speed (m/s)';...
    '32      u_ship_max      ship maximum true wind speed (m/s)';...
    '33      u_ship_std      ship true wind speed stdev (m/s)';...
    '34      wdir_ship       ship mean true wind direction';...
    '35      wdir_ship_min   ship minimum true wind direction';...
    '36      wdir_ship_max   ship maximum true wind direction';...
    '37      wdir_ship_std   ship true wind direction stdev';...
    '38      rspd            streamwise relative wind speed (m/s)from PSL sonic';...
    '39      Un              wind component south-to-north like the met convention "v", corrected true windspeed, PSL sonic';...
    '40      Ue              wind component east-to-west like the met convention "-u", corrected true windspeed, PSL sonic';...
    '41      wdir            true wind direction from N,E components, PSL sonic';...
    '42      rdir            relative wind direction from PSL sonic';...
    '43      azm             azimuth angle from first rotation, radians';...
    '44      tilt            tilt angle from first rotation, radians';...
    '45      Tsnk            seasnake SST, degC';...
    '46      Tair            air temperature, PSL vaisala, degC';...
    '47      RH              RH, PSL vaisala';...
    '48      RH_ship         RH, ship sensor';...
    '49      Q               specific humidity, PSL sensors, g/kg';...
    '50      Q_ship          specific humidity, ship sensors, g/kg';...
    '51      Qs            sea surface specific humidity from seasnake SST';...
    '52      rhoa            moist air density from PSL sensors (kg/m3)';...
    '53:73   $ship           COARE 3.5 legacy output, ship data:[usr,tau,hs,hl,hlwebb,';...
    '                        tsr,qsr,zot,zoq,Cd,Ch,Ce,L,zeta,dT_skin,dq_skin,dz_skin,hrain,';...
    '                        Cdn_10,Chn_10,Cen_10]';...
    '74:94   $psl            COARE 3.5 legacy output, PSL data, as above';...
    '95      lw_dn_std       std PIR flux, PSL';...
    '96      sw_dn_std       std PSP flux, PSL';...
    '97      sog_ship        mean sog, ship gps, m/s';...
    '98      sog_ship_std    std sog, ship gps';...
    '99      sog_ship_min    minimum sog, ship gps';...
    '100     sog_ship_max    maximum sog, ship gps';...
    '101     cog_ship        mean cog, deg, ship gps';...
    '102     cog_ship_std    std cog, ship gps';...
    '103     cog_ship_min    minimum cog, ship gps';...
    '104     cog_ship_max    maximum cog, ship gps';...
    '105     cog             mean cog, deg, PSL gps';...
    '106     cog_std         std cog, PSL gps';...
    '107     cog_min         minimum cog, PSL gps';...
    '108     cog_max         maximum cog, PSL gps';...
    '109     sog             mean sog, m/s, PSL gps';...
    '110     sog_min         minimum sog, PSL gps';...
    '111     sog_max         maximum sog, PSL gps';...
    '112     hed             mean heading, deg, ship';...
    '113     hed_std         std heading, ship';...
    '114     hed_min         minimum heading, ship';...
    '115     hed_max         maximum heading, ship';...
    '116     no_data         wu, Hare correction, not computed';...
    '117     wu_cov          wu covariance, Edson correction';...
    '118     no_data         wv, Hare correction, not computed';...
    '119     wv_cov          wv covariance, Edson correction';...
    '120     no_data         wT, Hare correction, not computed';...
    '121     wt_cov          wT covariance, Edson correction';...
    '122     no_data         uvar, Hare correction, not computed';...
    '123     no_data         vvar, Hare correction, not computed';...
    '124     no_data         wvar, Hare correction, not computed';...
    '125     uvar            u variance, Edson correction';...
    '126     vvar            v variance, Edson correction';...
    '127     wvar            w variance, Edson correction';...
    '128     Tvar            T variance';...
    '129     uplat_std       U platform velocity std, Edson correction';...
    '130     vplat_std       V platform velocity std, Edson correction';...
    '131     wplat_std       W platform velocity std, Edson correction';...
    '132     shed_std        stdev sine heading';...
    '133     ched_std        stdev cosine heading';...
    '134     Cua             Cu structure func parameter, method a';...
    '135     Cub             Cu structure func parameter, method b';...
    '136     Cwa             Cw structure func parameter, method a';...
    '137     Cwb             Cw structure func parameter, method b';...
    '138     Cta             Ct structure func parameter, method a';...
    '139     Ctb             Ct structure func parameter, method b';...
    '140     Cqa             Ch2o structure func parameter, method a';...
    '141     Cqb             Ch2o structure func parameter, method b';...
    '142     usr_ida         ustar from idiss, method a';...
    '143     usr_idb         ustar from idiss, method b';...
    '144     tsr_ida         tstar from idiss, method a';...
    '145     tsr_idb         tstar from idiss, method b';...
    '146     qsr_ida         qstar from idiss, method a, kg/kg';...
    '147     qsr_idb         qstar from idiss, method b, kg/kg';...
    '148     L_ida           L from idiss, method a';...
    '149     L_idb           L from idiss, method b';...
    '150     lon_s           longitude, ship gps';...
    '151     lat_s           latitiude, ship gps';...
    '152     ubar            streamwise true windspeed from Edson correction';...
    '153     spd_log_s       ship speed through water, m/s from odec or speed log';...
    '154     spd_log_std     stdev ship speed through water, m/s from odec or speed log';...
    '155     no data         minimum ship speed through water, m/s from odec or speed log';...
    '156     no data         maximum ship speed through water, m/s from odec or speed log';...
    '157     psealevel       sea level atmospheric pressure, PSL sensor, mb';...
    '158     missingSon      missing sonic wind data lines per 10 min interval';...
    '159     badSon          # NaNs in sonic data per 10 min interval';...
    '160     hs_cov          sensible heat flux from covariance';...
    '161     hl_cov          latent heat flux from covariance, uncorrected';...
    '162     hl_covW         webb corrected hl_cov';...
    '163     Ssea_s          salinity, ship system, psu';...
    '164     Tson_noise      Sonic temperature noise variance';...
    '165     rdir_std        std dev relative wind direction';...
    '166     rdir_wxt        rel wind direction +/- 180 from bow, WXT';...
    '167     rspd_wxt        wind speed, m/s, WXT';...
    '168     Ta_wxt          air temperature, C, WXT';...
    '169     rh_wxt          RH, %, WXT';...
    '170     slp_wxt         sea level air pressure, mb or hPa, WXT';...
    '171     prate_wxt       rain rate, mm/hr, WXT';...
    '172     wspd_wxt        true wind speed, m/s, WXT';...
    '173     wdir_wxt        true wind dir, m/s, WXT';...
    '174     rUn_wxt         Un rel, m/s, WXT + south-to-north like the met convention "v"';...
    '175     rUw_wxt         Uw rel, m/s, WXT + east-to-west like the met convention "-u"';...
    '176     Tskin           PSL estimated skin SST, deg C';...
    '177     rspd_s          ship rel wind speed';...
    '178     rdir_s          ship rel wind dir, +/- 180 deg';...
    '179     rspd_s_std      ship rspd std dev';...
    '180     rdir_s_std      ship rdir std dev';...
    '181     wT_cov_sds      heat flux covariance from Simon function';...
    '182     wq_cov_sds      latent flux covariance from Simon function, with CWF instrument separation correction';...
    '183     hs_cov_sds      Simon covariance hs';...
    '184     hl_cov_sds      Simon covariance hl';...
    '185     wspd_sfc        wind speed corrected for surface currents';...
    '186     wdir_sfc        wind direction corrected for surface currents'};


    no_data = nan(nt,1);
    
    da = [b10.jd,  b10.wspd_s, b10.Ta_s, b10.rh_s, b10.psealevel_s, ...        % 1-5
            b10.Tsea_s, b10.sw_dn_s, b10.lw_dn_s, b10.lat_s, b10.prate_s, no_data, b10.wspd, ...          % 6-12
            b10.wdir, b10.rspd, b10.rdir, b10.wspd_sfc, b10.Ta, b10.rh, b10.psealevel, ...   % 13-19
            b10.Tsnk, b10.sw_dn, b10.lw_dn, b10.lat, b10.prate, no_data, no_data, ...           % 20-26
            no_data, no_data, no_data, b10.wspd_s, b10.wspd_min_s, ...             % 27-31    
            b10.wspd_max_s, b10.wspd_std_s, b10.wdir_s, ...                  % 32-34
            b10.wdir_min_s, b10.wdir_max_s, b10.wdir_std_s, ...        % 35-37
            b10.rspd, d10.Un, d10.Ue, b10.wdir, b10.rdir, d10.azm, d10.tilt, ...       % 38-44
            b10.Tsnk, b10.Ta, b10.rh, b10.rh_s, b10.qa, b10.qa_s, b10.qs, ...             % 45-51
            b10.rhoa, ...                                                        % 52
            b10.usr_s, b10.tau_s, b10.hs_s, b10.hl_s, b10.hlwebb_s, b10.tsr_s, b10.qsr_s, b10.zot_s, b10.zoq_s, b10.Cd_s,  ...  % 53-62
            b10.Ch_s, b10.Ce_s, b10.L_s, b10.zeta_s, b10.dT_skin_s, b10.dq_skin_s, b10.dz_skin_s, b10.hrain_s, b10.CdN10_s, b10.ChN10_s, b10.CeN10_s, ...  % 53-73
            b10.usr, b10.tau, b10.hs, b10.hl, b10.hlwebb, b10.tsr, b10.qsr, b10.zot,  b10.zoq, b10.Cd, ... % 74-83
            b10.Ch, b10.Ce, b10.L, b10.zeta, b10.dT_skin, b10.dq_skin, b10.dz_skin, b10.hrain, b10.CdN10, b10.ChN10, b10.CeN10, ... % 74-94
            b10.lw_dn_std, ...                                              % 95
            b10.sw_dn_std, b10.sog_s, b10.sog_std_s, ...                   % 96-98
            b10.sog_min_s, b10.sog_max_s, b10.cog_s, ...                   % 99-101
            b10.cog_std_s, b10.cog_min_s, b10.cog_max_s, ...        % 102-104
            b10.cog, d10.cog_std, d10.cog_min, ...        % 105-107
            d10.cog_max, b10.sog, d10.sog_min, ...        % 108-110
            d10.sog_max, b10.hed, d10.hed_std, ...                  % 111-113
            d10.hed_min, d10.hed_max, no_data, ...                 % 114-116
            d10.wu_cov, no_data, d10.wv_cov, no_data, ...                                   % 117-120
            d10.wT_cov, no_data, no_data, no_data, d10.uvar, ...                         % 121-125
            d10.vvar, d10.wvar, ...                                                    % 126-127
            d10.Tvar, d10.uplat_std, d10.vplat_std, d10.wplat_std, ...                                      % 128-131
            d10.shed_std, d10.ched_std, d10.Cua, d10.Cub, d10.Cwa, ...        % 132-136
            d10.Cwb, d10.Cta, d10.Ctb, d10.Cqa, d10.Cqb, d10.usr_ida, d10.usr_idb, d10.tsr_ida, d10.tsr_idb, d10.qsr_ida, ...     % 137-146
            d10.qsr_idb, d10.L_ida, d10.L_idb, b10.lon_s, b10.lat_s, d10.ubar, ...          % 147-152
            b10.spd_log_s, b10.spd_log_std_s, no_data, no_data, b10.psealevel, d10.missingSon, d10.badSon, ...          % 153-159
            d10.hs_cov, d10.hl_cov, d10.hl_cov, b10.Ssea_s, d10.Tson_noise, ...                       % 160-164
            d10.rdir_std, b10.rdir_wxt, b10.rspd_wxt, b10.Ta_wxt, b10.rh_wxt, b10.psealevel_wxt, ...         % 165-170
            b10.prate_wxt, b10.wspd_wxt, b10.wdir_wxt, b10.rUn_wxt, b10.rUw_wxt, b10.Tskin, ...             % 171-176
            b10.rspd_s, b10.rdir_s, b10.rspd_std_s, b10.rdir_std_s, ...                     % 177-180
            d10.wT_cov_sds,  d10.wq_cov_sds, d10.hs_cov_sds, d10.hl_cov_sds,  b10.wspd_sfc,  b10.wdir_sfc];       % 181-186

cr_readme = {'1 b10.jd';...
            '2  b10.licor_H2O   mmol/m3';... 
                '3  no_data';...
                '4  no_data';...
                '5  no_data';...
            '6  b10.licor_Pbox   kPa';...
            '7  b10.licor_Tbox   deg C';...
            '8  d10.wq_cov   g/kg * m/s';...
            '9  d10.hl_cov   K * m/s';...
                '10 no_data';...
                '11 no_data';...
            '12 d10.licor_H2O_std   mmol/m3';...
                '13 no_data';...
                '14 no_data';...
                '15 no_data';...
            '16 d10.licor_Pbox_std   kPa';...
            '17 b10.licor_AGC   diagnostic value'};

    cr      = [b10.jd,...
            b10.licor_H2O,... 
                no_data,...
                no_data,...
                no_data,...
            b10.licor_Pbox,...
            b10.licor_Tbox,...
            d10.wq_cov,...
            d10.hl_cov,...
                no_data,...
                no_data,...
            d10.licor_H2O_std,...
                no_data,...
                no_data,...
                no_data,...
            d10.licor_Pbox_std,...
            b10.licor_AGC];
        


sp_readme = {'10-min spec output arrays for spectra and cospectra: 144x481';...
        ' ';...
        'column designations as follows, all spectra are 48 bin';...
        ' ';...
        'COLUMNS     VARIABLE        DESCRIPTION';...
        '1           doy             decimal day of year';...
        '2:49        Fxx             frequency bins';...
        '50:97       Puu             U spectrum';...
        '98:145      Pvv             V spectrum';...
        '146:193     Pww             W spectrum';...
        '194:241     Ptt             T spectrum';...
        '242:289     Pqq             q spectrum';...
        '290:337     Cuw             wu cospectrum';...
        '338:385     Cvw             wv cospectrum';...
        '386:433     Ctw             wt cospectrum';...
        '434:481     Cqw             wq cospectrum'};

    % reshape Fx to nt x 48 to match other arrays
    Fx2 = zeros(nt,48); % Fx is 19 x 48, so fill the rest with zeros after
    [nrowFx, ncolFx] = size(d10.Fx);
    for m = 1:nrowFx
        Fx2(m,:) = d10.Fx(m,:);
    end
    sp = [b10.jd, Fx2, d10.Pu, d10.Pv, d10.Pw, d10.Pt, d10.Pq, d10.Cuw, d10.Cvw, d10.Ctw, d10.Cqw];

    %% save all-cruise text files
    
    % reset first col to be sure missingSon hour timestamps are not NaN
    da = real(da);
    fl = [legacy_dir cruise '_da_' file_out_version '.txt'];
    save(fl,'da','-ascii','-tabs');

    cr = real(cr);
    fl = [legacy_dir cruise '_cr_' file_out_version '.txt'];
    save(fl,'cr','-ascii','-tabs');

    sp = real(sp);
    fl = [legacy_dir cruise '_sp_' file_out_version '.txt'];
    save(fl,'sp','-ascii','-tabs');

    lag = d10.lag;
    fl = [legacy_dir cruise '_lag_' file_out_version '.txt'];
    save(fl,'lag','-ascii','-tabs');

    mu = d10.mu;
    fl = [legacy_dir cruise '_mu_' file_out_version '.txt'];
    save(fl,'mu','-ascii','-tabs');

    mu_q = d10.mu_q;
    fl = [legacy_dir cruise '_mu_q_' file_out_version '.txt'];
    save(fl,'mu_q','-ascii','-tabs');


% save readme and header automatically
name_readme_da  = [legacy_dir 'fields_' cruise '_da.txt'];
name_readme_cr  = [legacy_dir 'fields_' cruise '_cr.txt'];
name_readme_sp  = [legacy_dir 'fields_' cruise '_sp.txt'];

fileID_rda = fopen(name_readme_da,'w');
fileID_rcr = fopen(name_readme_cr,'w');
fileID_rsp = fopen(name_readme_sp,'w');

for i = 1:length(da_readme)
    fprintf(fileID_rda,'%s\n',da_readme{i});
end
fclose(fileID_rda);
    
for i = 1:length(cr_readme)
    fprintf(fileID_rcr,'%s\n',cr_readme{i});
end
fclose(fileID_rcr);

for i = 1:length(sp_readme)
    fprintf(fileID_rsp,'%s\n',sp_readme{i});
end
fclose(fileID_rsp);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% choose inertial dissipation, ID, method: 'a', 'b', or 'ab'
% a: compute Cx2 from median over inertial subrange in smoothed spectra
% b: compute Cx2 from polynomial fit over inertial subrange in smoothed spectra
% ab: average of a and b methods
IDflag = 'ab'; % we don't really know whether the ab way is the way to go 

% average of a and b methods for ID fluxes
d10.Cuab = (d10.Cua+d10.Cub)/2;
d10.Cwab = (d10.Cwa+d10.Cwb)/2;
d10.Ctab = (d10.Cta+d10.Ctb)/2;
d10.Cqab = (d10.Cqa+d10.Cqb)/2;

%% set limits for various filtering criteria... more extreme than in run_motcorr.m
rdir_lo     = -60;  % rel wind direction limits, deg from bow
rdir_hi     = 60;
rain_lim    = 5;    % max rain rate, mm/hr
sp2_lim     = 0.8;  % max vplat std dev  m/s
sigu_lim    = 1.5;  % max ship speed std dev  m/s
sigh_lim    = 5;    % max ship heading std dev, deg
badSon_lim  = 50;   % max bad sonic points per 10min interval
missingSon_lim = 50;% max missingSon sonic points per 10min interval
ushp_lim    = 3.5;  % max ship speed, m/s
tilt_lim    = 10;   % max flow tilt angle, degrees
sig_rwd_lim = 15;   % max std dev rdir
AGC_lim     = 60;   % max licor AGC
CO2_LoLim   = 350;  % min CO2 ppm
CO2_HiLim   = 450;  % max CO2 ppm
sigCO2_lim  = 20;   % max sigma CO2
q_lic_LoLim = 0.1;  % min q licor
q_lic_HiLim = 26;   % max q licor
q_std_LoLim  = 0;    % min sigma q licor
q_std_HiLim  = 3;    % max sigma q licor
wq_LoLim    = -0.02;% min wq licor
wq_HiLim    = 0.1;  % max wq licor
wu_LoLim    = -5;   % min wu
wu_HiLim    = 0.05; % max wu
wv_LoLim    = -1;   % min wv
wv_HiLim    = 1;    % max wv
wT_LoLim    = -0.1; % min wT
wT_HiLim    = 0.25; % max wT % should this be lowered to 0.6? 
Tnoise_lim  = 2e-3; % max Ts noise - having issues with Tsonic
Tson_var_lim = 0.1; % max variance in Tsonic
cu_LoLim    = 0;    % min cu2
cu_HiLim    = 1.3;  % max cu2
cw_LoLim    = 0;    % min cw2
cw_HiLim    = 1;    % max cw2
ct_LoLim    = 1e-6; % min ct2
ct_HiLim    = 0.6;  % max ct2
cq_LoLim   = 1e-4;  % min cq2
cq_HiLim   = 2;     % max cq2
qvar_LoLim = 0;     % min qvar
qvar_HiLim = 0.2e4;   % max qvar


%% make adjustments, filter outliers, etc.
% Apply fixes to data following run_motcorr and for filtering
% bulk flux calculations.  Edit fixit.m to make corrections to means.

% kill all flux components where AGC is bad, licor q is out of range, when std(q) is
% out of range. AGC limit was already been applied to means, but not fluxes. 

% For PISTON 2019, the licor stopped washing itself after this point and so
% its data should just not be used. There is a drifting offset in qa past this point.
bad_licor_day = datenum(2019,9,7,22,0,0);

clear ii; 
ii = find(b10.licor_AGC>AGC_lim | ...
    b10.licor_qa<q_lic_LoLim | b10.licor_qa>q_lic_HiLim | ...
    d10.licor_qa_std>q_std_HiLim | d10.licor_qa_std<q_std_LoLim);
if strcmp(cruise,'PISTON_2019') == 1
   iii = find(b10.t > bad_licor_day);
   ii = unique([ii; iii]);
end

d10.wq_cov(ii) = nan;
d10.wq_cov_sds(ii) = nan;
d10.wH2O_cov(ii) = nan;
d10.wCO2_cov(ii) = nan;
d10.hl_cov(ii) = nan;
d10.hl_cov_sds(ii) = nan;
d10.Cqa(ii) = nan;
d10.Cqb(ii) = nan;
d10.Cqab(ii) = nan;
d10.qsr_cov(ii) = nan;
d10.qsr_cov_sds(ii) = nan;
d10.qsr_ida(ii) = nan;
d10.qsr_idb(ii) = nan;

%%% something is wrong with qvar and simon's covariance heat and moisture
% fluxes
bad_qvar = find(d10.qvar > qvar_HiLim | d10.qvar < qvar_LoLim);

%%% test with a plot
% hs_cov_sds = d10.hs_cov_sds;
% hs_cov_sds(bad_qvar) = nan;
% figure
% plot(d10.t, d10.hs_cov_sds,'o');
% hold on;
% plot(d10.t, hs_cov_sds,'x');
% % plot(d10.t, d10.hs_cov,'.')
% legend('og hs cov sds','new hs cov sds','hs cov');
% grid on;

d10.hs_cov_sds(bad_qvar) = nan;
d10.tsr_cov_sds(bad_qvar) = nan;
d10.qsr_cov_sds(bad_qvar) = nan;
d10.wq_cov_sds(bad_qvar) = nan;
d10.wT_cov_sds(bad_qvar) = nan;

%% filter other covariances and ID variables for outlier values
% NOTE: these rules are more strict than the ones applied in runmotcorr
% first, remove all instances with bad wind direction or excessive missing data
clear kk; kk = find(d10.good_motion ~= 1 | ...
    b10.rdir>rdir_hi | b10.rdir<rdir_lo | ...
    d10.badSon>badSon_lim | d10.missingSon>missingSon_lim);
% these are different from motcorr... it changes cruise to cruise.
% kk in run_motcorr used more variables to perform filtering. Here we use
% fewer variables to choose from, but stricter missingSon (was 100) and
% rdri (was +/-90) rules

d10.wTson_cov(kk) = nan;
d10.wT_cov(kk) = nan; 
d10.wq_cov(kk) = nan;
d10.wq_cov_sds(kk) = nan; 
d10.wH2O_cov(kk) = nan;
d10.wCO2_cov(kk) = nan;
d10.wT_cov_sds(kk) = nan; 
d10.wu_cov(kk) = nan;
d10.wv_cov(kk) = nan;
d10.hl_cov(kk) = nan;
d10.hl_cov_sds(kk) = nan;
d10.hs_cov(kk) = nan;
d10.hs_cov_sds(kk) = nan;
d10.tau_cov(kk) = nan;
d10.tau_cov_cross(kk) = nan;

d10.uvar(kk) = nan; 
d10.vvar(kk) = nan;
d10.wvar(kk) = nan; 
d10.Tvar(kk) = nan; 

d10.usr_ida(kk) = nan; 
d10.usr_idb(kk) = nan; 
d10.tsr_id_son(kk) = nan;
d10.tsr_ida(kk) = nan; 
d10.tsr_idb(kk) = nan; 
d10.qsr_ida(kk) = nan;
d10.qsr_idb(kk) = nan;

d10.hs_ida(kk) = nan;
d10.hs_idb(kk) = nan;
d10.hl_ida(kk) = nan;
d10.hl_idb(kk) = nan;
d10.tau_ida(kk) = nan;
d10.tau_idb(kk) = nan;

d10.Cua(kk) = nan; 
d10.Cub(kk) = nan; 
d10.Cwa(kk) = nan; 
d10.Cwb(kk) = nan; 
d10.Cta(kk) = nan; 
d10.Ctb(kk) = nan; 
d10.Cqa(kk) = nan; 
d10.Cqb(kk) = nan; 

%%% NOTE why would we do this? licor does not depend on sonic
% licor_qa(kk) = Nan; 
% licor_H2O(kk) = nan;
% hl_cov(kk) = nan;

%% general covariance limits
d10.wu_cov(d10.wu_cov<wu_LoLim | d10.wu_cov>wu_HiLim) = nan;
d10.tau_cov(d10.wu_cov<wu_LoLim | d10.wu_cov>wu_HiLim) = nan;

d10.wv_cov(d10.wv_cov<wv_LoLim | d10.wv_cov>wv_HiLim) = nan;

d10.wTson_cov(d10.wTson_cov<wT_LoLim | d10.wTson_cov>wT_HiLim) = nan;
d10.wT_cov(d10.wT_cov<wT_LoLim | d10.wT_cov>wT_HiLim) = nan;
d10.hs_cov(d10.wT_cov<wT_LoLim | d10.wT_cov>wT_HiLim) = nan;

d10.wT_cov_sds(d10.wT_cov_sds<wT_LoLim | d10.wT_cov_sds>wT_HiLim) = nan;
d10.hs_cov_sds(d10.wT_cov_sds<wT_LoLim | d10.wT_cov_sds>wT_HiLim) = nan;

d10.wq_cov(d10.wq_cov<wq_LoLim | d10.wq_cov>wq_HiLim) = nan;
d10.hl_cov(d10.wq_cov<wq_LoLim | d10.wq_cov>wq_HiLim) = nan;

d10.wq_cov_sds(d10.wq_cov_sds<wq_LoLim | d10.wq_cov_sds>wq_HiLim) = nan;
d10.hl_cov_sds(d10.wq_cov_sds<wq_LoLim | d10.wq_cov_sds>wq_HiLim) = nan;

d10.wH2O_cov(d10.wq_cov<wq_LoLim | d10.wq_cov>wq_HiLim) = nan;
d10.wCO2_cov(d10.wq_cov<wq_LoLim | d10.wq_cov>wq_HiLim) = nan;

% cu2,cw2,ct2 and cq2 limits
bad_cua = find(d10.Cua<cu_LoLim | d10.Cua >cu_HiLim);
bad_cwa = find(d10.Cwa<cw_LoLim | d10.Cwa >cw_HiLim);
bad_cta = find(d10.Cta<ct_LoLim | d10.Cta>ct_HiLim);
bad_cqa = find(d10.Cqa<cq_LoLim | d10.Cqa>cq_HiLim);

bad_cub = find(d10.Cub<cu_LoLim | d10.Cub >cu_HiLim);
bad_cwb = find(d10.Cwb<cw_LoLim | d10.Cwb >cw_HiLim);
bad_ctb = find(d10.Ctb<ct_LoLim | d10.Ctb>ct_HiLim);
bad_cqb = find(d10.Cqb<cq_LoLim | d10.Cqb>cq_HiLim);

bad_cuab = find(d10.Cuab<cu_LoLim | d10.Cuab >cu_HiLim);
bad_cwab = find(d10.Cwab<cw_LoLim | d10.Cwab >cw_HiLim);
bad_ctab = find(d10.Ctab<ct_LoLim | d10.Ctab>ct_HiLim);
bad_cqab = find(d10.Cqab<cq_LoLim | d10.Cqab>cq_HiLim);

d10.Cua(bad_cua) = nan;
d10.Cub(bad_cub) = nan;
d10.Cuab(bad_cuab) = nan;

d10.Cwa(bad_cwa) = nan;
d10.Cwb(bad_cwb) = nan;
d10.Cwab(bad_cwab) = nan;

d10.Cta(bad_cta) = nan;
d10.Ctb(bad_ctb) = nan;
d10.Ctab(bad_ctab) = nan;

d10.Cqa(bad_cqa) = nan;
d10.Cqb(bad_cqb) = nan;
d10.Cqab(bad_cqab) = nan;

% filter for Ts noise limit and variance
noisy_ct = (d10.Tson_noise > Tnoise_lim | d10.Tvar>Tson_var_lim);
d10.Cta(noisy_ct) = nan;
d10.Ctb(noisy_ct) = nan;
d10.Ctab(noisy_ct) = nan;
d10.hs_cov(noisy_ct) = nan;
d10.hs_cov_sds(noisy_ct) = nan;
d10.wTson_cov(noisy_ct) = nan;
d10.wT_cov(noisy_ct) = nan; 
d10.wT_cov_sds(noisy_ct) = nan; 

% Cs is the weighted average of cu and cw for smoother structure function
% cs = (cu+.75*cw)/2;
d10.Csa = (d10.Cua+.75*d10.Cwa)/2;
d10.Csb = (d10.Cub+.75*d10.Cwb)/2;
d10.Csab = (d10.Cuab+.75*d10.Cwab)/2;

d10.Csa(isnan(d10.Cua)) = .75*d10.Cwa(isnan(d10.Cua));
d10.Csa(isnan(d10.Cwa)) = d10.Cua(isnan(d10.Cwa));
d10.Csb(isnan(d10.Cub)) = .75*d10.Cwb(isnan(d10.Cub));
d10.Csb(isnan(d10.Cwb)) = d10.Cub(isnan(d10.Cwb));
d10.Csab(isnan(d10.Cuab)) = .75*d10.Cwab(isnan(d10.Cuab));
d10.Csab(isnan(d10.Cwab)) = d10.Cuab(isnan(d10.Cwab));


% ship plume contam index based on wind variances, 0 = good, 1 = bad
jplume = ones(nt,1);
clear ii; 
ii = (sqrt(d10.wvar)./d10.ugw < 2+0.0015*b10.wspd.^2) & (sqrt(d10.vvar)./d10.ugu < 2);
jplume(ii) = 0;
jplume(isnan(jplume)) = 1;
wh_bad_plume = find(jplume == 1);

%%% NOTE - IS IT OKAY IF I CHANGED THIS? DEALING WITH DECIMAL PLACE FLAGS
%%% IS NOT A GOOD IDEA. IT WAS TOO HARD TO INTERPOLATE FROM TIME BASE TO
%%% TIME BASE
% ship maneuver index, 0 = good, 1 = bad
jmanuv = ones(nt,1);
clean_sog_std = ones(nt,1);
clear ii; 
ii = (d10.hed_std<sigh_lim) & (d10.sog_std<sigu_lim) & (d10.vplat_std<sp2_lim);
clean_sog_std(ii) = d10.sog_std(ii);

jmanuv(clean_sog_std>0.8) = 1;
jmanuv(clean_sog_std<0.8) = 0;
jmanuv(isnan(clean_sog_std)) = 1;
wh_bad_manuv = find(jmanuv == 1);

d10.jmanuv = jmanuv;
d10.jplume = jplume;

% interpolate to 1-min

% assume bad values to start
d1.jmanuv = ones(length(d1.jd),1);
d1.plume = ones(length(d1.jd),1);

% can't do interp for nan jd values... 
jd_good1 = find(isfinite(d1.jd) == 1);
jd_good10 = find(isfinite(d10.jd) == 1);

d1.jmanuv = interp1(d10.jd(jd_good10), d10.jmanuv(jd_good10), d1.jd);
d1.jplume = interp1(d10.jd(jd_good10), d10.jplume(jd_good10), d1.jd);

d1.jmanuv = round(d1.jmanuv);
d1.jplume = round(d1.jplume);

% recompute ID fluxes... with new smoothed and/or filtered cs, ct, cq structures
% choosing method ab for now... but will test a, b, ab methods later
% zu / L is b10.zeta and is output by COARE
% air-sea temp difference: DT = Tskin - b10.Ta; 
% was used prior for sign of tsr... technically it should be dtheta
% DT was this before: Tsbest + dT_skin - Ta... 
% which would have been a positive number added to Tsnk... not the skin temp
usr_id_ab   = sqrt(d10.Csab     .*zu^0.667./psi_fu(b10.zeta)); 
usr_id_a    = sqrt(d10.Csa      .*zu^0.667./psi_fu(b10.zeta)); 
usr_id_b    = sqrt(d10.Csb      .*zu^0.667./psi_fu(b10.zeta)); 

usr_id_ab = real(usr_id_ab);
usr_id_a = real(usr_id_a);
usr_id_b = real(usr_id_b);

usr_id = usr_id_ab;


sign_tsr = sign(b10.tsr);
sign_dtheta = sign(b10.dtheta); % looks worse and opposite sign compared to bulk, ppposite to tsr_bulk
sign_dT = sign(b10.Tskin - b10.Ta); % looks worse and opposite sign compared to bulk, ppposite to tsr_bulk
sign_dT_OG = sign(b10.Tsnk - b10.Ta + b10.dT_skin);  % doesn't make sense because Tskin = Ts_best - dT_skin;
sign_wtv = -sign(d10.wtv);

the_sign = sign_wtv;

% should this be sign of tsr_son? or tsr? or dT skin - air? or dtheta?
tsr_id_son_ab    = sqrt(d10.Ctab     .*zu^0.667./psi_ft(b10.zeta)).*the_sign; 
tsr_id_son_a     = sqrt(d10.Cta      .*zu^0.667./psi_ft(b10.zeta)).*the_sign; 
tsr_id_son_b      = sqrt(d10.Ctb      .*zu^0.667./psi_ft(b10.zeta)).*the_sign; 

tsr_id_son_ab    = real(tsr_id_son_ab);
tsr_id_son_a     = real(tsr_id_son_a);
tsr_id_son_b      = real(tsr_id_son_b);

tsr_id_ab       = tsr_id_son_ab  -0.51*(b10.Ta+C2K).*b10.qsr; % moisture corrrected tsr_id
tsr_id_a        = tsr_id_son_a   -0.51*(b10.Ta+C2K).*b10.qsr; % moisture corrrected tsr_id
tsr_id_b        = tsr_id_son_b    -0.51*(b10.Ta+C2K).*b10.qsr; % moisture corrrected tsr_id

tsr_id_son = tsr_id_son_ab;
tsr_id = tsr_id_ab;

% figure; plot(b10.t, b10.tsr, b10.t(wh_good_id), tsr_id_ab(wh_good_id), 'o', ...
%     b10.t(wh_good_id), tsr_id_a(wh_good_id), 'x',...
%     b10.t(wh_good_id), tsr_id_b(wh_good_id),'s'); 
% grid on; legend('bulk','id ab','id a','id b');

zet_q = zq./b10.L;
qsr_id_ab       = sqrt(d10.Cqab     .*zq^0.667./psi_ft(zet_q))*1e-3.*sign(b10.qsr); % qsr in kg/kg
qsr_id_a        = sqrt(d10.Cqa      .*zq^0.667./psi_ft(zet_q))*1e-3.*sign(b10.qsr); % qsr in kg/kg
qsr_id_b        = sqrt(d10.Cqb      .*zq^0.667./psi_ft(zet_q))*1e-3.*sign(b10.qsr); % qsr in kg/kg
qsr_id_ab       = real(qsr_id_ab);
qsr_id_a        = real(qsr_id_a);
qsr_id_b        = real(qsr_id_b);

qsr_id = qsr_id_ab;

hs_id_ab    = -b10.rhoa*cpa.*tsr_id_ab  .*usr_id_ab;
hs_id_a     = -b10.rhoa*cpa.*tsr_id_a   .*usr_id_a;
hs_id_b     = -b10.rhoa*cpa.*tsr_id_b   .*usr_id_b;

hs_id = hs_id_ab;

hl_id_ab        = -b10.rhoa.*d10.Le_w.*qsr_id_ab  .*usr_id_ab +b10.hlwebb; % Webb corrected, g/kg
hl_id_a         = -b10.rhoa.*d10.Le_w.*qsr_id_a   .*usr_id_a  +b10.hlwebb; % Webb corrected, g/kg
hl_id_b         = -b10.rhoa.*d10.Le_w.*qsr_id_b   .*usr_id_b  +b10.hlwebb; % Webb corrected, g/kg

hl_id = hl_id_ab;

tau_id_ab = b10.rhoa .*usr_id_ab .^2;
tau_id_a = b10.rhoa .*usr_id_a .^2;
tau_id_b = b10.rhoa .*usr_id_b .^2;

tau_id = tau_id_ab;

d10.usr_id = usr_id;
d10.usr_ida = usr_id_a;
d10.usr_idb = usr_id_b;
d10.tsr_id = tsr_id;
d10.tsr_ida = tsr_id_a;
d10.tsr_idb = tsr_id_b;
d10.tsr_id_son = tsr_id_son;
d10.tsr_id_sona = tsr_id_son_a;
d10.tsr_id_sonb = tsr_id_son_b;
d10.qsr_id = qsr_id;
d10.qsr_ida = qsr_id_a;
d10.qsr_idb = qsr_id_b;
d10.hs_id = hs_id;
d10.hs_ida = hs_id_a;
d10.hs_idb = hs_id_b;
d10.hl_id = hl_id;
d10.hl_ida = hl_id_a;
d10.hl_idb = hl_id_b;
d10.tau_id = tau_id;
d10.tau_ida = tau_id_a;
d10.tau_idb = tau_id_b;

wh_good_id = find((b10.rdir<rdir_hi & b10.rdir>rdir_lo) & d10.hed_std<sigh_lim & ...
       d10.sog_std<sigu_lim & d10.vplat_std<sp2_lim & b10.prate<rain_lim & ...
       (sqrt(d10.wvar)./d10.ugw < 2+0.0015*b10.wspd.^2) & d10.badSon<badSon_lim & ...
       d10.missingSon<missingSon_lim & (sqrt(d10.vvar)./d10.ugu < 2));
good_id = zeros(nt, 1);
good_id(wh_good_id) = 1;
bad_id = ones(nt,1);
bad_id(wh_good_id) = 0;
wh_bad_id = find(bad_id == 1);

wh_good_usr_id = find(good_id == 1 & isfinite(usr_id) == 1);
wh_good_qsr_id = find(good_id == 1 & isfinite(qsr_id) == 1);
wh_good_tsr_id = find(good_id == 1 & isfinite(tsr_id) == 1);
d10.good_usr_id = zeros(nt, 1);
d10.good_tsr_id = zeros(nt, 1);
d10.good_qsr_id = zeros(nt, 1);
d10.good_usr_id(wh_good_usr_id) = 1;
d10.good_tsr_id(wh_good_tsr_id) = 1;
d10.good_qsr_id(wh_good_qsr_id) = 1;

% use the thresholds

d10.usr_id(wh_bad_id) = nan;
d10.usr_ida(wh_bad_id) = nan;
d10.usr_idb(wh_bad_id) = nan;
d10.tsr_id(wh_bad_id) = nan;
d10.tsr_ida(wh_bad_id) = nan;
d10.tsr_idb(wh_bad_id) = nan;
d10.tsr_id_son(wh_bad_id) = nan;
d10.tsr_id_sona(wh_bad_id) = nan;
d10.tsr_id_sonb(wh_bad_id) = nan;
d10.qsr_id(wh_bad_id) = nan;
d10.qsr_ida(wh_bad_id) = nan;
d10.qsr_idb(wh_bad_id) = nan;
d10.hs_id(wh_bad_id) = nan;
d10.hs_ida(wh_bad_id) = nan;
d10.hs_idb(wh_bad_id) = nan;
d10.hl_id(wh_bad_id) = nan;
d10.hl_ida(wh_bad_id) = nan;
d10.hl_idb(wh_bad_id) = nan;
d10.tau_id(wh_bad_id) = nan;
d10.tau_ida(wh_bad_id) = nan;
d10.tau_idb(wh_bad_id) = nan;

% note: everywhere that manuv is bad, plume is usually also bad. So this is 
% a bit redundant usually but good to check just in case.
wh_bad_ship = unique([wh_bad_plume; wh_bad_manuv]);

d10.usr_id(wh_bad_ship) = nan;
d10.usr_ida(wh_bad_ship) = nan;
d10.usr_idb(wh_bad_ship) = nan;
d10.tsr_id(wh_bad_ship) = nan;
d10.tsr_ida(wh_bad_ship) = nan;
d10.tsr_idb(wh_bad_ship) = nan;
d10.tsr_id_son(wh_bad_ship) = nan;
d10.tsr_id_sona(wh_bad_ship) = nan;
d10.tsr_id_sonb(wh_bad_ship) = nan;
d10.qsr_id(wh_bad_ship) = nan;
d10.qsr_ida(wh_bad_ship) = nan;
d10.qsr_idb(wh_bad_ship) = nan;
d10.hs_id(wh_bad_ship) = nan;
d10.hs_ida(wh_bad_ship) = nan;
d10.hs_idb(wh_bad_ship) = nan;
d10.hl_id(wh_bad_ship) = nan;
d10.hl_ida(wh_bad_ship) = nan;
d10.hl_idb(wh_bad_ship) = nan;
d10.tau_id(wh_bad_ship) = nan;
d10.tau_ida(wh_bad_ship) = nan;
d10.tau_idb(wh_bad_ship) = nan;

d10.hl_cov(wh_bad_ship) = nan;
d10.hl_cov_sds(wh_bad_ship) = nan;
d10.hs_cov(wh_bad_ship) = nan;
d10.hs_cov_sds(wh_bad_ship) = nan;
d10.tau_cov(wh_bad_ship) = nan;
d10.tau_cov_cross(wh_bad_ship) = nan;
d10.wTson_cov(wh_bad_ship) = nan;
d10.wT_cov(wh_bad_ship) = nan; 
d10.wT_cov_sds(wh_bad_ship) = nan; 
d10.wq_cov(wh_bad_ship) = nan;
d10.wq_cov_sds(wh_bad_ship) = nan; 
d10.wu_cov(wh_bad_ship) = nan;
d10.wv_cov(wh_bad_ship) = nan;
d10.wCO2_cov(wh_bad_ship) = nan;

% examine stability functions for velocity and temp/humidity structure
% functions
zeta_fake = -100:.1:10;
cuxx = psi_fu(zeta_fake);
ctxx = psi_ft(zeta_fake);

clear jjj; jjj = wh_good_id;     
   
% examine wind speed structure function parameter z/L dependence: gets plotted
zeta_fake_log = [-60 -30 -20 -8 -5 -4 -3 -2 -1 -.7 -.5 -.3 -.2 -.1 -.05 0 .05 .1 1];
nn_zeta = length(zeta_fake_log)-1;
zeta_log_bin = nan(nn_zeta,1); 
cs_plot = nan(nn_zeta,1); 
cnt = nan(nn_zeta,1); 
for i=1:nn_zeta
   clear ii; ii = find(b10.zeta(jjj)>zeta_fake_log(i) & ...
       b10.zeta(jjj)<zeta_fake_log(i+1) & isfinite(d10.Csab(jjj)));
   if isfinite(jjj(ii))
      zeta_log_bin(i) = nanmedian(b10.zeta(jjj(ii)));
      cs_plot(i) = nanmedian(d10.Csab(jjj(ii))./b10.usr(jjj(ii)).^2*zu^.667);
      cnt(i) = length(jjj(ii));
   else
      zeta_log_bin(i) = nan;
      cs_plot(i) = nan;
      cnt(i) = nan;
   end
end

% select ID Hl as best latent heat flux... for now it's method a
hl_id_plot = nan(nt,1);
hl_id_plot(jjj) = d10.hl_id(jjj);
hl_id_plot(hl_id_plot>1000) = nan;

%%% NOTE: for now we use the ab method but really we should compare again
%%% and choose what is best as a research effort.
%%% other way of doing it: avg of ID and cov..
% hlm(jjj) = nanmean(hl_id(jjj), hl_cov(jjj)); 
% maybe b is better... if you use structure functions to compute
% stabilitday... that's what used to be done... but it can create a numerical
% problem. Spectral fit method is better ideallday... but also subject to
% going wrong. The other mean/median method is better. It's reliable. If
% you have a spectrum w/o a -5/3 region, you still get an estimate from
% mean/median method, and tends to be overestimate. Carry both methods
% through. Simon's method hasn't been proven or time tested yet. Don't know
% what problem it's solving. It can vary for q and T, Tsonic noise is high
% so fitting 5/3 slope is difficult. But in tropics, q signal is high so
% fit should be good. 

% wind speed dependence of Hl: gets plotted
u_bin = 0:20; % wind speed bins
hl_id_u = nan(length(u_bin),1);
hl_u = nan(length(u_bin),1); 
dq_u = nan(length(u_bin),1); 
ynn_u = nan(length(u_bin),1);
for i=1:length(u_bin)
    clear ii; ii = find(floor(b10.U10N)==u_bin(i));
    
    clear ppp; ppp = find(b10.sog(ii)<ushp_lim & (b10.rdir(ii)<rdir_hi & b10.rdir(ii)>rdir_lo) &...
       d10.hed_std(ii)<sigh_lim & d10.sog_std(ii)<sigu_lim & d10.vplat_std(ii)<sp2_lim &...
       b10.prate(ii)<rain_lim & (sqrt(d10.wvar(ii))./d10.ugw(ii) < 2+0.0015*b10.wspd(ii).^2) & ...
       (sqrt(d10.vvar(ii))./d10.ugu(ii) < 2+0.05*b10.wspd(ii)) & isfinite(hl_id_plot(ii)) & d10.badSon(ii)<badSon_lim);
   if isempty(ppp)
        hl_id_u(i) = nan;
        hl_u(i) = nan;
        ynn_u(i) = 0;
        dq_u(i) = nan;
   else
        hl_id_u(i) = nanmedian(hl_id_plot(ii(ppp)));
        hl_u(i) = nanmedian(b10.hl(ii(ppp)));
        ynn_u(i) = length(ppp);
        dq_u(i) = nanmedian(b10.qs(ii(ppp))-b10.qa(ii(ppp)))-nanmean(b10.dq_skin(ii(ppp)));
    end
end
 
%% compute 10min u*, t* and q* from covariance measurements: gets plotted or used
% compare cov and ID results with bulk model
% wu_cov is in the direction of the wind, streamwise, approx the stress
% wv_cov is cross stream, should be approx zero but still has sampling noise
% So the average is squaring noise and adding it...  which is worst at low
% wind with noise is bigger than signal
% usr_cov = (wu_cov.^2 + wv_cov.^2).^(1/4); % this is a common mistake, 
d10.usr_cov = (d10.wu_cov.^2).^(1/4); % wu_cov has neg numbers so need to do it this way for algebra's sake
d10.qsr_cov = -1E-3*d10.wq_cov./d10.usr_cov;
d10.tsr_cov = -d10.wT_cov./d10.usr_cov;
d10.tsr_cov_sds = -d10.wT_cov_sds./d10.usr_cov;
d10.qsr_cov_sds = -1E-3*d10.wq_cov_sds./d10.usr_cov;

clear jjjj; jjjj =  find(good_id == 1 & isfinite(usr_id)); 
   
% binning
[usr_bulk_bin, usr_cov_bin_mean, usr_cov_bin_med, usr_cov_bin_std, nn_cov] = ...
    binave2(b10.usr(jjjj),d10.usr_cov(jjjj),0.05,0,0.75);

[~, tsr_cov_bin_mean, tsr_cov_bin_med, tsr_cov_std, nn_tsr_cov] = ...
    binave2(b10.tsr(jjjj),d10.tsr_cov(jjjj),0.025,-0.15,0.15);

[~, tsr_cov_sds_bin_mean, tsr_cov_sds_bin_med, tsr_cov_sds_std, nn_tsr_cov_sds] = ...
    binave2(b10.tsr(jjjj),d10.tsr_cov_sds(jjjj),0.025,-0.15,0.15);

[~, usr_id_bin_mean, usr_id_bin_med, usr_id_bin_std, nn_usr_id] = ...
    binave2(b10.usr(jjjj),d10.usr_id(jjjj),0.05, 0,0.75);   
 
[tsr_bulk_bin, tsr_id_bin_mean, tsr_id_bin_med, tsr_id_bin_std, nn_tsr_id] = ...
    binave2(b10.tsr(jjjj),d10.tsr_id(jjjj),0.025,-0.15,0.15);

[hs_bulk_bin, hs_id_bin_mean, hs_id_bin_med, hs_id_std, nn_hs_id] = ...
    binave2(b10.hs(jjj),d10.hs_id(jjj),5,-30,30);

[U10N_bin, Cd_id_bin_mean, Cd_id_bin_med, Cd_id_bin_std, Cd_id_bin_nn] = ...
    binave2(b10.U10N(jjj),(d10.usr_id(jjj)./b10.U10N(jjj)).^2,2,1,15);


%% ocean warm layer profile (could be checked with updated COARE output) 
% this only works if the warm layer bulk model is run using sea T as input...
% otherwise zw = 0 and T5_no_warm = T from tsg
% from COARE: dT_warm = warming across entire warm layer
%             dT_warm_at_input = warming from surface to input T level

dtz = zeros(nt,round(zsea2_ship)+1);
depth_inc=0:round(zsea2_ship);
for i=1:round(zsea2_ship)+1
    clear ii;
    ii = find(b10.dz_warm>depth_inc(i));
    dtz(ii,i) = b10.dT_warm(ii).*(1-depth_inc(i)./b10.dz_warm(ii));  % warming from mixed layer to z
end
% save these vars for daily averages
d10.dT5_warm = dtz(:,round(zsea2_ship)+1); % amount of warming expected at 5 m due to presence of warm layer
d10.T5_no_warm = b10.Tsea_s - dtz(:,round(zsea2_ship)+1);  % "ML" or 5 m temp: tsg with with 5-m warm layer removed

d10 = orderfields(d10);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%% daily and hourly time bases
% some are plotted, some are saved, but should save all in the future

% daily time base
jd_d_bin = floor(b10.jd(1)):ceil(b10.jd(end)); % daily bin edges
jd_d_bin = jd_d_bin'; % daily bin edges
day.jd = jd_d_bin(1:end-1); % just days
day.t = datenum(yr,0,0,0,0,0) + day.jd;

% hourly time base
jd_h_bin = floor(b10.jd(1)):(1/24):ceil(b10.jd(end)); % hourly bin edges
jd_h_bin = jd_h_bin'; % hourly bin edges
hr.jd = jd_h_bin(1:end-1); % just days
hr.t = datenum(yr,0,0,0,0,0) + hr.jd;

%% daily averages
day.hnet = interval_avg_var(b10.jd, b10.hnet, jd_d_bin);
day.lat = interval_avg_var(b10.jd, b10.lat, jd_d_bin);
day.lon = interval_avg_var(b10.jd, b10.lon, jd_d_bin);
day.sw_dn = interval_avg_var(b10.jd, b10.sw_dn, jd_d_bin);
day.sw_dn_clr = interval_avg_var(b10.jd, b10.sw_dn_clr, jd_d_bin);
day.lw_dn = interval_avg_var(b10.jd, b10.lw_dn, jd_d_bin);
day.lw_dn_clr = interval_avg_var(b10.jd, b10.lw_dn_clr, jd_d_bin);
day.prate = interval_avg_var(b10.jd, b10.prate, jd_d_bin);
temp = day.prate;
temp(isnan(temp)) = 0;
day.paccum = cumsum(temp)*24; % 
%     paccum_10 = cumsum(temp)/6;  % 6 X 10 min segments per hour
%     paccum_1  = cumsum(temp)/60; % 60 X 1 min segments per hour

%%     straight average
% could combine b10 and d10 first in the future. That way you could combine
% and see all variables in one structure. However, keeping them separate
% sort of helps by being able to trace which program produced them. b10
% comes from eval_flux.m and fixit.m. These are used to create d10 with
% run_motcorr.m and da_red_et.m ... d10 is added to in da_red_et.m. 


% variables in b10 structure
var_avg_b = fields(b10);
wh_bt = find(strcmp(var_avg_b,'t') == 1);
wh_bjd = find(strcmp(var_avg_b,'jd') == 1);
var_avg_b(wh_bt) = [];
var_avg_b(wh_bjd) = [];

% var_avg_b = {'Cd';'CdN10';'Ce';'CeN10';'Ch';'ChN10';'Edis';...
%     'Cd_s';'CdN10_s';'Ce_s';'CeN10_s';'Ch_s';'ChN10_s';'Edis_s';...
%     'Ssea_s';'Ta';'Ta_s';'Ta_wxt';'Tskin';'Tskin_s';'Tsonic';...
%     'Tskin_ir';'Tsnk';'Tsea_s';'Ta10';'Ta10N';'Ta10_s';'Ta10N_s';...
%     'U10';'U10N';'UN';'U10_s';'U10N_s';'UN_s';...
%     'cspd';'dT_skin';'dT_warm';'dq_skin';'dz_skin';'dz_warm';...
%     'dT_skin_s';'dT_warm_s';'dq_skin_s';'dz_skin_s';'dz_warm_s';...
%     'dT_warm_to_skin';'dT_warm_to_skin_s';'du_warm';'du_warm_s';...
%     'erate';'hb';'hb_son';'hl';'hlwebb';'hrain';'hs';...
%     'erate_s';'hb_s';'hl_s';'hlwebb_s';'hrain_s';'hs_s';...
%     'lat';'lon';'lat_s';'lon_s';...
%     'licor_AGC';'licor_CO2';'licor_qa';
%     'lw_dn';'lw_net';'lw_up';'lw_dn_clr';...
%     'lw_dn_s';'lw_net_s';' lw_up_s';'paccum';'prate';'prate_wxt';...
%     'prate_s';'paccum_s';
%     'psealevel';'psealevel_s';'psealevel_wxt';
%     'qa10';'qa10_s';'qa10N';'qa10N_s';'qa';'qa_s';'qa_wxt';'qs';'qs_s';...
%     'rh';'rh_s';'rh_wxt';'rh10';'rh10_s';'rhoa';'rhoa_s';'rhoa_wxt';...
%     'rhoa10';'rhoa10_s';'rspd';'rspd_s';'rspd_wxt';'sog';'sog_s';...
%     'cog';'cog_s';'sw_dn';'sw_dn_s';'sw_dn_clr';...
%     'sw_net';'sw_up';'sw_net_s';'sw_up_s';'theta0';'theta10';'tau';'tau_s';...
%     'tsr';'tsr_s';'tsr_son';'usr';'usr_s';'wc_frac';'wc_frac_s';...
%     'wspd';'wspd_s';'wspd_sfc';'zeta';'zeta_s';...
%     'zo';'zoq';'zot';'zo_s';'zoq_s';'zot_s';'hnet';'hnet_s';...
%     'sogN';'sogE';'hedN';'hedE';'sogN_s';'sogE_s';'hedN_s';'hedE_s';...
%     'spd_log_u_s';'spd_log_v_s'};


% variables in d10 structure
% var_avg_d = {'L_ida';'L_idb';'Le_sw';'Le_w';'dT5_warm';'T5_no_warm';...
%     'wbar';'Tbar';'ubar';'vbar';...
%     'wvar';'Tvar';'uvar';'vvar';'qvar';...
%     'Cqa';'Cqb';'Cqw';'Cta';'Ctb';'Ctw';'Cua';'Cub';'Cuw';'Cwa';'Cwb';...
%     'Cqab';'Csab';'Ctab';'Cuab';'Cqab';'Cwab';...
%     'qsr_ida';'qsr_idb';'qsr_id';'qsr_cov';'qsr_cov_sds';...
%     'tsr_ida';'tsr_idb';'tsr_id';'tsr_id_son';'tsr_cov';'tsr_cov_sds';...
%     'usr_ida';'usr_idb';'usr_id';'usr_cov';...
%     'wT_cov';'wT_cov_sds';'wTson_cov';'wCO2_cov';...
%     'wq_cov';'wq_cov_sds';'wu_cov';'wv_cov';'wtv';'ws';...
%     'hl_cov';'hl_cov_sds';'hl_ida';'hl_idb';'hl_id';... 
%     'hs_cov';'hs_cov_sds';'hs_ida';'hs_idb';'hs_id';...
%     'tau_cov';'tau_cov_cross';'tau_ida';'tau_idb';'tau_id';... 
%     'good_motion';'good_motion_id';'good_motion_id_licor';'good_motion_licor';...
%     'jplume';'jmanuv'};

% isolate just the time-based d10 variables
all_var_d = fields(d10);
nd = length(all_var_d);
time_vars_d = ones(nd,1);
d10_c = nan(nd,1);
d10_r = nan(nd,1);
nt_d = length(d10.Tvar);
for i = 1:length(all_var_d)
    eval(['[d10_c(i), d10_r(i)] = size(d10.' all_var_d{i} ');']);
    if d10_c(i) ~= nt_d || d10_r(i) ~= 1
        time_vars_d(i) = 0;
    end
    if strcmp(all_var_d{i},'t') == 1 | strcmp(all_var_d{i},'jd')
        time_vars_d(i) = 0;
    end
end
wh_non_time_vars_d = find(time_vars_d == 0);
var_avg_d = all_var_d;
var_avg_d(wh_non_time_vars_d) = [];
    
%%% combine the 10-min time series first
clear e10;
e10.t = b10.t;
e10.jd = b10.jd;
e10.hnet_cov    = b10.sw_net + b10.lw_net - d10.hs_cov - d10.hl_cov - b10.hrain;
e10.hnet_id     = b10.sw_net + b10.lw_net - d10.hs_id - d10.hl_id - b10.hrain;

for i = 1:length(var_avg_b)
    eval(['e10.' var_avg_b{i} ' = b10.' var_avg_b{i} ';']);
end
for i = 1:length(var_avg_d)
    eval(['e10.' var_avg_d{i} ' = d10.' var_avg_d{i} ';']);
end

%%% perfrom a straight average second.
for i = 1:length(var_avg_b)
    eval(['hr.' var_avg_b{i} ' = interval_avg_var(b10.jd, b10.' var_avg_b{i} ', jd_h_bin);']);
end
for i = 1:length(var_avg_d)
    eval(['hr.' var_avg_d{i} ' = interval_avg_var(d10.jd, d10.' var_avg_d{i} ', jd_h_bin);']);
end

%%  special values, directions, and vectors
 
hr.minute = zeros(length(hr.t),1);
hr.missingSon = interval_sum_var(b10.jd, d10.missingSon, jd_h_bin);
hr.badSon = interval_sum_var(b10.jd, d10.badSon, jd_h_bin);
hr.tilt = interval_avg_dir(d10.jd,d10.tilt,jd_h_bin); 


[hr.cspd, hr.cdir]            =  interval_avg_vect(b10.jd, b10.cspd, b10.cdir, jd_h_bin);
[hr.cspd_adcp, hr.cdir_adcp]  =  interval_avg_vect(b10.jd, b10.cspd_adcp, b10.cdir_adcp, jd_h_bin);
[hr.cspd_spdlog, hr.cdir_spdlog]=  interval_avg_vect(b10.jd, b10.cspd_spdlog, b10.cdir_spdlog, jd_h_bin);
[hr.wspd, hr.wdir]            =  interval_avg_vect(b10.jd, b10.wspd, b10.wdir, jd_h_bin);
[hr.wspd_new, hr.wdir_new]    =  interval_avg_vect(b10.jd, d10.wspd_new, d10.wdir_new, jd_h_bin);
[hr.wspd_s, hr.wdir_s]        =  interval_avg_vect(b10.jd, b10.wspd_s, b10.wdir_s, jd_h_bin);
[hr.wspd_wxt, hr.wdir_wxt]    =  interval_avg_vect(b10.jd, b10.wspd_wxt, b10.wdir_wxt, jd_h_bin);
[hr.wspd_sfc, hr.wdir_sfc]    =  interval_avg_vect(b10.jd, b10.wspd_sfc, b10.wdir_sfc, jd_h_bin);

%%% check to make sure this is resulting in +/- 180 for all
% rdir_new, rspd_new2, rspd_raw
[hr.rspd, hr.rdir]            =  interval_avg_rvect(b10.jd, b10.rspd, b10.rdir, jd_h_bin);
[hr.rspd_new, hr.rdir_new]    =  interval_avg_rvect(b10.jd, d10.rspd_new, d10.rdir_new, jd_h_bin);
[hr.rspd_s, hr.rdir_s]        =  interval_avg_rvect(b10.jd, b10.rspd_s, b10.rdir_s, jd_h_bin);
[hr.rspd_wxt, hr.rdir_wxt]    =  interval_avg_rvect(b10.jd, b10.rspd_wxt, b10.rdir_wxt, jd_h_bin);
 
plot_rdir = 0;
if plot_rdir == 1
   figure;
   plot(b10.t, b10.rdir,'x',hr.t, hr.rdir,'o'); 
   legend('10-min','hr');    
   title('rdir check');
end

%% fix hed, cog, sog if needed
% These are equivalent to the ones below, and so are not needed. 
hr2.spd_log_s = sqrt(hr.spd_log_u_s.^2 + hr.spd_log_v_s.^2);  % recompute sog from speed components

hr2.sog_s = sqrt(hr.sogN_s.^2 + hr.sogE_s.^2);  % recompute sog from speed components
hr2.sog = sqrt(hr.sogN.^2 + hr.sogE.^2);  % recompute sog from speed components

hr2.cog_s = atan2(hr.sogE_s, hr.sogN_s)*r2d;  % recompute cog from speed components
hr2.cog_s = mod(hr.cog_s + 360, 360); % reorient units
hr2.cog = atan2(hr.sogE, hr.sogN)*r2d;  % recompute cog from speed components
hr2.cog = mod(hr.cog + 360, 360); % reorient units2

hr2.hed_s = atan2(hr.hedE_s, hr.hedN_s)*r2d; % recompute avg hed from avg N/E components
hr2.hed_s = mod(hr.hed_s + 360, 360); % reorient units    
hr2.hed = atan2(hr.hedE, hr.hedN)*r2d; % recompute avg hed from avg N/E components
hr2.hed = mod(hr.hed + 360, 360); % reorient units    


%% fix any negative angles (NOT the rdirs... which are now +/- 180 on purpose
% is this necessary? there weren't any bad angles in PISTON 2019
angles1 = {'wdir';'wdir_s';'wdir_wxt'};
angles2 = {'cog';'hed'};
for i = 1:length(angles1)
    this = hr.(angles1{i});
    bad_ang = find(this<0);
    if ~isempty(bad_ang)
        disp(['bad angles found for ' angles1{i}]);
        this(bad_ang) = this(bad_ang)+360;
        hr.(angles1{i}) = this;
    end
end
for i = 1:length(angles2)
    this = hr2.(angles2{i});
    bad_ang = find(this<0);
    if ~isempty(bad_ang)
        disp(['bad angles found for ' angles2{i}]);
        this(bad_ang) = this(bad_ang)+360;
        hr2.(angles2{i}) = this;
    end
end

%% standard deviations... first square all standard deviations, then
% % average, then take square root
hr.rdir_std = sqrt(interval_avg_var(b10.jd, d10.rdir_std.^2, jd_h_bin));         
hr.wspd_std_s = sqrt(interval_avg_var(b10.jd, b10.wspd_std_s.^2, jd_h_bin));  
hr.wdir_std_s = sqrt(interval_avg_var(b10.jd, b10.wdir_std_s.^2, jd_h_bin));     
hr.rspd_std_s = sqrt(interval_avg_var(b10.jd, b10.rspd_std_s.^2, jd_h_bin));  
hr.rdir_std_s = sqrt(interval_avg_var(b10.jd, b10.rdir_std_s.^2, jd_h_bin)); 
hr.sw_dn_std = sqrt(interval_avg_var(b10.jd, b10.sw_dn_std.^2, jd_h_bin));    
hr.lw_dn_std = sqrt(interval_avg_var(b10.jd, b10.lw_dn_std.^2, jd_h_bin));    
hr.sog_std_s = sqrt(interval_avg_var(b10.jd, b10.sog_std_s.^2, jd_h_bin));     
hr.cog_std_s = sqrt(interval_avg_var(b10.jd, b10.cog_std_s.^2, jd_h_bin));         
hr.hed_std_s = sqrt(interval_avg_var(b10.jd, b10.hed_std_s.^2, jd_h_bin));        
hr.sog_std = sqrt(interval_avg_var(b10.jd, d10.sog_std.^2, jd_h_bin));         
hr.cog_std = sqrt(interval_avg_var(b10.jd, d10.cog_std.^2, jd_h_bin));       
hr.hed_std = sqrt(interval_avg_var(b10.jd, d10.hed_std.^2, jd_h_bin));        
hr.uplat_std = sqrt(interval_avg_var(b10.jd, d10.uplat_std.^2, jd_h_bin));
hr.vplat_std = sqrt(interval_avg_var(b10.jd, d10.vplat_std.^2, jd_h_bin));
hr.wplat_std = sqrt(interval_avg_var(b10.jd, d10.wplat_std.^2, jd_h_bin));
hr.shed_std = sqrt(interval_avg_var(b10.jd, d10.shed_std.^2, jd_h_bin));
hr.ched_std = sqrt(interval_avg_var(b10.jd, d10.ched_std.^2, jd_h_bin));
hr.shed_std_s = sqrt(interval_avg_var(b10.jd, b10.shed_std_s.^2, jd_h_bin));
hr.ched_std_s = sqrt(interval_avg_var(b10.jd, b10.ched_std_s.^2, jd_h_bin));
hr.spd_log_std_s = sqrt(interval_avg_var(b10.jd, b10.spd_log_std_s.^2, jd_h_bin));         
hr.licor_H2O_std = sqrt(interval_avg_var(b10.jd, d10.licor_H2O_std.^2, jd_h_bin));
hr.licor_qa_std = sqrt(interval_avg_var(b10.jd, d10.licor_qa_std.^2, jd_h_bin));
hr.licor_CO2_std = sqrt(interval_avg_var(b10.jd, d10.licor_CO2_std.^2, jd_h_bin));
hr.licor_Pbox_std = sqrt(interval_avg_var(b10.jd, d10.licor_Pbox_std.^2, jd_h_bin));

%%     special special or recalculations after averaging

temp = hr.prate;
temp(isnan(temp)) = 0;
hr.paccum = cumsum(temp); % already in increments of hours

ref = 2;
B=coare36vnWarm_et(hr.jd, hr.wspd_sfc, zu, hr.Ta, zt, hr.rh, zq, hr.psealevel, hr.Tsnk, hr.sw_dn, hr.lw_dn, hr.lat, hr.lon, 600, hr.prate, zsnk, hr.Ssea_s, nan, nan, ref, ref, ref);
C=coare36vnWarm_et(hr.jd, hr.wspd_s, zu_ship, hr.Ta_s, zt_ship, hr.rh_s, zq_ship, hr.psealevel_s, hr.Tsea_s, hr.sw_dn_s, hr.lw_dn_s, hr.lat, hr.lon, 600, hr.prate, zsea2_ship, hr.Ssea_s, nan, nan, ref, ref, ref);

cfields = {'usr';'tau';'hs';'hl';'hb';'hb_son';'hlwebb';'tsr';'qsr';'zo';'zot';'zoq';...
    'Cd';'Ch';'Ce';'L';'zeta';'dT_skin';'dq_skin';'dz_skin';'U2';'Ta2';'qa2';...
    'rh2';'U2N';'Ta2N';'qa2N';'lw_net';'sw_net';'Le';'rhoa';'UN';'U10';'U10N';'CdN10';'ChN10';'CeN10';...
    'hrain';'qs';'erate';'Ta10';'Ta10N';'qa10';'qa10N';'rh10';'pa10';'rhoa10';'gust';'wc_frac';'Edis';...
    'dT_warm';'dz_warm';'dT_warm_to_skin';'du_warm'};

for i = 1:length(cfields)
%     if contains(cfields(i),'substring') ~= 1   %% to not rewrite certain variables, if needed
        eval(['hr.' cfields{i} '_s = C(:,i);']);
        eval(['hr.' cfields{i} '   = B(:,i);']);
%     end
end

hr.Tskin       = hr.Tsnk + hr.dT_warm_to_skin - hr.dT_skin;
hr.lw_up       = hr.lw_net - hr.lw_dn;
hr.lw_up_s     = hr.lw_net_s - hr.lw_dn_s;
hr.sw_up       = hr.sw_net - hr.sw_dn;
hr.sw_up_s     = hr.sw_net_s - hr.sw_dn_s;
hr.hnet        = hr.sw_net + hr.lw_net - hr.hs - hr.hl - hr.hrain;
hr.hnet_s      = hr.sw_net_s + hr.lw_net_s - hr.hs_s - hr.hl_s - hr.hrain_s;
hr.hnet_cov    = hr.sw_net + hr.lw_net - hr.hs_cov - hr.hl_cov - hr.hrain;
hr.hnet_id     = hr.sw_net + hr.lw_net - hr.hs_id - hr.hl_id - hr.hrain;
hr.theta10     = (hr.Ta10N+C2K).*(1000./hr.pa10).^(Rgas/cpa);  
hr.theta0      = (hr.Tskin+C2K).*(1000./hr.psealevel).^(Rgas/cpa);  
hr.dtheta      = hr.theta0 - hr.theta10;
hr.tsr_son     = hr.tsr + 0.51*(hr.Ta+C2K).*hr.qsr;
hr.zeta        = zu./hr.L;

% for some reason the warm layer looks weird at hourly levels... so just
% average 10-min output instead

hr.dt_warm_to_skin = interval_avg_var(b10.jd, b10.dT_warm_to_skin, jd_h_bin);
hr.dt_warm = interval_avg_var(b10.jd, b10.dT_warm, jd_h_bin);
hr.dz_warm = interval_avg_var(b10.jd, b10.dz_warm, jd_h_bin);

%% recomputed fluxes and covariances and functions... 
% The averaged structure functions could make for smoother averaged fluxes 
% and stars. These have been tested and show zero improvement or change.

hr2.Cuab = (hr.Cua + hr.Cub)/ 2; % mean of methods a & b
hr2.Cwab = (hr.Cwa + hr.Cwb)/ 2; % mean of methods a & b
hr2.Ctab = (hr.Cta + hr.Ctb)/ 2; % mean of methods a & b
hr2.Cqab = (hr.Cqa + hr.Cqb)/ 2; % mean of methods a & b

clear ii; ii = hr2.Cqab<1e-4 | hr2.Cqab>1; hr.Cqab(ii) = nan;

% avg cu and cw for smoother structure function
hr2.Csab = (hr2.Cuab+(.75*hr2.Cwab))/2;
hr2.Csab(isnan(hr2.Cuab)) = .75*hr2.Cwab(isnan(hr2.Cuab));
hr2.Csab(isnan(hr2.Cwab)) = hr2.Cuab(isnan(hr2.Cwab) == 1);

% hr2.wtv     = -hr.usr.*(hr.tsr+.61.*(hr.Ta+C2K).*hr.qsr); % virtual temp covariance. EXACT SAME as straight avg
% hr2.ws      = (9.83./(hr.Ta+C2K).*abs(hr2.wtv)*600).^.333; % convective velocity. EXACT SAME as straight avg

% ID stars and fluxes
% convert complex numbers from psi_fu(zetay)
a = psi_fu(hr.zeta);
a(isnan(a)) = nanmedian(a);
hr2.usr_id = sqrt(hr2.Csab.*zu^0.667./a);  
% hr2.tau_id = hr.rhoa.*hr2.usr_id.*hr2.usr_id./hr.gust;  % the bulk method
% includes gustiness but the ID method doesn't need it. Taken gustiness
% variance spectra and related it to the stress already.
hr2.tau_id = hr.rhoa.*hr2.usr_id.^2;
hr2.tsr_id_son = sqrt(hr2.Ctab.*zu^0.667./psi_ft(hr.zeta)).*sign(hr.tsr);
hr2.tsr_id = hr2.tsr_id_son-0.51*(hr.Ta+C2K).*hr.qsr;
hr2.hs_id = -hr.rhoa*cpa.*hr.tsr_id.*hr2.usr_id;


hr2_zet_q = zq./hr.L;
hr2.qsr_id = sqrt(hr2.Cqab.*zu^.667./psi_ft(hr2_zet_q))*1e-3.*sign(hr.qsr);
clear ii; ii = hr2.qsr_id<-3e-4 | hr2.qsr_id>0; hr2.qsr_id(ii) = nan;
hr2.hl_id = -hr.rhoa.*hr.Le_w.*hr2.qsr_id.*hr2.usr_id + hr.hlwebb;

% cov fluxes

% from motcorr:        
% wT_cov = ( wTson_cov + 0.51*(x.Ta+C2K).*x.usr.*x.qsr) ./ (1 + 0.51*x.qa*1E-3);
% hr2.wT_cov = hr.wTson_cov+0.51*(hr.Ta+C2K).*hr.usr.*hr.qsr ./ (1 + 0.51*x.qa*1E-3);
% this is basically equivalent to hr.wT_cov but lacks the thresholding/filtering from before
% so deem it unnecessary.

hr2.hs_cov = hr.rhoa*cpa.*hr.wT_cov;
hr2.hs_cov_sds = hr.rhoa*cpa.*hr.wT_cov_sds;
hr2.hl_cov = 1E-3*hr.rhoa.*hr.Le_sw.*hr.wq_cov + hr.hlwebb;
hr2.hl_cov_sds = 1E-3*hr.rhoa.*hr.Le_sw.*hr.wq_cov_sds; % simon's version is already webb corrected

bad_hl_cov_sds = find(isnan(hr.hl_cov_sds) == 1);
hr2.hl_cov_sds(bad_hl_cov_sds) = nan;

hr2.tau_cov = -hr.rhoa.*hr.wu_cov;
hr2.tau_cov_cross = -hr.rhoa.*hr.wv_cov;

hr2 = orderfields(hr2);

%% plots to check hourly data
plot_hr2_checks = 0;
if plot_hr2_checks == 1

figure;
counter = 1;
thefields = fields(hr2);
nc = length(thefields);
for i = 1:4:nc
    clf;
    for j = 1:4
       if (i+j-1) <= nc
               subplot(2,2,j); hold on;
               eval(['var2 = hr2.' thefields{i+j-1} ';']);
               eval(['var1 = hr.' thefields{i+j-1} ';']);
               plot(hr.t, var2,'o');
               plot(hr.t, var1,'x');
               grid on;
               var_name = {strrep(thefields{i+j-1},'_',' ')};
               title(var_name);
               xlim([min(hr.t) max(hr.t)]);
               datetick('x','mm/dd','keeplimits');
               grid on;
               legend('v2','v1','location','southoutside','orientation','horizontal');
       end
    end
   print(graphdevice,[png_path '/check_hr2_' sprintf('%i',counter) '_' cruise graphformat]);
   counter = counter + 1;
end  % for: all vars
end  % if : plotting hr2 checks

%% replace hr2 fields into hr
% since these were deemed to show zero change to these variables, do not 
% change with hr2 fields. 
swap_hr2 = 0;
if swap_hr2 == 1
fhr2 = fields(hr2);
    for i = 1:length(fhr2)
        eval(['hr.' fhr2{i} ' = hr2. ' fhr2{i} ';']);
    end
end

%% fix integers data flags... set to 0 if nan or decimal place after avg-ing
ints = {'good_motion';'good_motion_id';'jplume';'jmanuv';...
    'good_motion_id_licor';'good_motion_licor';...
    'ID_Fsea1_s';'ID_Fsea2_s';'ID_bad_TSG';'ID_bad_wind';...
    'ID_mod_wind';'ID_no_otter';'ID_otter';'ID_tsg_s';...
    'good_qsr_id';'good_tsr_id';'good_usr_id'};

for i = 1:length(ints)
    this = hr.(ints{i});
    bad_int = find(isnan(this) == 1 | mod(this,1) ~= 0);
    if ~isempty(bad_int)
        % could try this to save more data
        % this(bad_int) = round(this(bad_int)); 
        this(bad_int) = 0; % just says anything not perfect is bad
        hr.(ints{i}) = this;
    end
end


%% median hourly hl, hs, stress vs bulk & vs U
hr_u_bin=0:15; % wind bins for both sets of plots / fluxes

%%%%% latent
hr_hl_id_mean_u = nan(length(hr_u_bin),1);
hr_hl_id_med_u = nan(length(hr_u_bin),1);
hr_hl_mean_u = nan(length(hr_u_bin),1);
hr_hl_med_u = nan(length(hr_u_bin),1);
hr_hl_cov_mean_u = nan(length(hr_u_bin),1);
hr_hl_cov_med_u = nan(length(hr_u_bin),1);
hr_nh_u = nan(length(hr_u_bin),1);
% nu = nan(length(u_bin),1);  % just a repeat of u_bin... not needed.

for i=1:length(hr_u_bin)
    ii = find(floor(hr.wspd)==hr_u_bin(i));
    jj = find(isfinite(hr.hl_id(ii)) & isfinite(hr.hl(ii)) );
    if isempty(jj)
        hr_hl_id_mean_u(i) = nan;
        hr_hl_id_med_u(i) = nan;
        hr_hl_mean_u(i) = nan;
        hr_hl_med_u(i) = nan;
        hr_hl_cov_mean_u(i) = nan;
        hr_hl_cov_med_u(i) = nan;
        hr_nh_u(i) = 0;
%        nu(i) = nan;
    else
        hr_hl_id_mean_u(i) = nanmean(hr.hl_id(ii(jj)));
        hr_hl_id_med_u(i) = nanmedian(hr.hl_id(ii(jj)));
        hr_hl_cov_mean_u(i) = nanmean(hr.hl_cov(ii(jj)));
        hr_hl_cov_med_u(i) = nanmedian(hr.hl_cov(ii(jj)));
        hr_hl_mean_u(i) = nanmean(hr.hl(ii(jj)));
        hr_hl_med_u(i) = nanmedian(hr.hl(ii(jj)));
        hr_nh_u(i) = length(jj);
%        nu(i) = nanmean(ywspd(ii(jj)));
    end
end
% 

%%%%% sensible
hr_hs_id_mean_u = nan(length(hr_u_bin),1);
hr_hs_id_med_u = nan(length(hr_u_bin),1);
hr_hs_mean_u = nan(length(hr_u_bin),1);
hr_hs_med_u = nan(length(hr_u_bin),1);
hr_hs_cov_mean_u = nan(length(hr_u_bin),1);
hr_hs_cov_med_u = nan(length(hr_u_bin),1);
hr_ns_u = nan(length(hr_u_bin),1);
% nu = nan(length(u_bin),1);  % just a repeat of u_bin... not needed.

for i=1:length(hr_u_bin)
    ii = find(floor(hr.wspd)==hr_u_bin(i));
    jj = find(isfinite(hr.hs_id(ii)) & isfinite(hr.hs(ii)) );
    if isempty(jj)
        hr_hs_id_mean_u(i) = nan;
        hr_hs_id_med_u(i) = nan;
        hr_hs_mean_u(i) = nan;
        hr_hs_med_u(i) = nan;
        hr_hs_cov_mean_u(i) = nan;
        hr_hs_cov_med_u(i) = nan;
        hr_ns_u(i) = 0;
%        nu(i) = nan;
    else
        hr_hs_id_mean_u(i) = nanmean(hr.hs_id(ii(jj)));
        hr_hs_id_med_u(i) = nanmedian(hr.hs_id(ii(jj)));
        hr_hs_cov_mean_u(i) = nanmean(hr.hs_cov(ii(jj)));
        hr_hs_cov_med_u(i) = nanmedian(hr.hs_cov(ii(jj)));
        hr_hs_mean_u(i) = nanmean(hr.hs(ii(jj)));
        hr_hs_med_u(i) = nanmedian(hr.hs(ii(jj)));
        hr_ns_u(i) = length(jj);
%        nu(i) = nanmean(ywspd(ii(jj)));
    end
end
% 

%%%%% stress

hr_tau_med_u = nan(length(hr_u_bin),1); 
hr_tau_cov_med_u = nan(length(hr_u_bin),1); 
hr_tau_cov_cross_med_u = nan(length(hr_u_bin),1); 
hr_tau_id_med_u = nan(length(hr_u_bin),1); 
hr_tau_mean_u = nan(length(hr_u_bin),1); 
hr_tau_cov_mean_u = nan(length(hr_u_bin),1); 
hr_tau_cov_cross_mean_u = nan(length(hr_u_bin),1); 
hr_tau_id_mean_u = nan(length(hr_u_bin),1); 
hr_wu_med_u = nan(length(hr_u_bin),1); 
hr_wv_med_u = nan(length(hr_u_bin),1); 
hr_nt_u = nan(length(hr_u_bin),1); 
hr_nu_u = nan(length(hr_u_bin),1); 

for i=1:length(hr_u_bin)
    clear ii; ii = find(floor(hr.wspd)==hr_u_bin(i));
    clear ppp; ppp = find(isfinite(hr.tau_cov(ii)) & isfinite(hr.tau_cov_cross(ii)) & ...
        isfinite(hr.tau(ii)) & (hr.rdir(ii)>rdir_lo & hr.rdir(ii)<rdir_hi) & ...
        hr.tilt(ii)<tilt_lim);
    if isempty(ppp)
      hr_tau_med_u(i) = nan;
      hr_tau_cov_med_u(i) = nan;
      hr_tau_cov_cross_med_u(i) = nan;
      hr_tau_id_med_u(i) = nan;
      hr_wu_med_u(i) = nan;
      hr_wv_med_u(i) = nan;
      hr_nt_u(i) = 0;
      hr_nu_u(i) = nan;
      hr_tau_mean_u(i) = nan;
      hr_tau_cov_mean_u(i) = nan;
      hr_tau_cov_cross_mean_u(i) = nan;
      hr_tau_id_mean_u(i) = nan;
    else
      hr_tau_med_u(i) = nanmedian(hr.tau(ii(ppp)));
      hr_tau_cov_med_u(i) = nanmedian(hr.tau_cov(ii(ppp)));
      hr_tau_cov_cross_med_u(i) = nanmedian(hr.tau_cov_cross(ii(ppp)));
      hr_tau_id_med_u(i) = nanmedian(hr.tau_id(ii(ppp)));
      hr_wu_med_u(i) = nanmedian(hr.wu_cov(ii(ppp)));
      hr_wv_med_u(i) = nanmedian(hr.wv_cov(ii(ppp)));
      hr_nt_u(i) = length(ppp);
      hr_tau_mean_u(i) = nanmean(hr.tau(ii(ppp)));
      hr_tau_cov_mean_u(i) = nanmean(hr.tau_cov(ii(ppp)));
      hr_tau_cov_cross_mean_u(i) = nanmean(hr.tau_cov_cross(ii(ppp)));
      hr_tau_id_mean_u(i) = nanmean(hr.tau_id(ii(ppp)));
    end
end

% non-dimensional sigma-W vs. z/L
hr_phi_w_cov = sqrt(hr.wvar)./sqrt(-hr.wu_cov);  % nondim sigma-W from cov ustar
hr_phi_w_b = sqrt(hr.wvar)./hr.usr;              % nondim sigma-W from bulk ustar
hr_aa = hr.zeta>1e-4;      % selectors for stable
hr_bb = hr.zeta<-1e-4;     % selectors for convective
hr_zeta_a = 10.^(-3:0.25:2);            % stable z/L for K&F function
hr_zeta_b = -1*10.^(-3:0.25:2);         % convective z/L for K&F function
% non-dimensional sigma w from Kaimal & Finnigan, 1994, Eq. 1.33
hr_phi_w_af = 1.25*(1+0.2*hr_zeta_a);       % stable
hr_phi_w_bf = 1.25*(1-3*hr_zeta_b).^(1/3);  % convective

%% save all 1-min, 10-min, 60-min, and 1-day file

clear e1 e60 eday;

e60 = hr;
eday = day;

% save everything we changed in d10 (a lot) and d1 (just flags);
save(d10_outfile, 'd10');
save(d1_outfile, 'd1');

% e10 already exists. It is everything in d10 + b10 except the non time
% series fields from d10

%%%% alternatively, save e10 as everything in d10
% % combine b10 and [updated] d10.... everything, including multidimensional arrays! 
% bfields = fields(b10);
% 
% % copy d10 to e10
% e10 = d10;
% % add all fields from b10 to e10;
% for i = 1:length(bfields)
%     eval(['e10.' bfields{i} ' = b10.' bfields{i} ';']);
% end 

% copy d1 to e1
e1 = d1;
% add all fields from b1 to e1;
bfields = fields(b1);
for i = 1:length(bfields)
    eval(['e1.' bfields{i} ' = b1.' bfields{i} ';']);
end 

hr = orderfields(hr);

e1 = orderfields(e1);
e10 = orderfields(e10);
e60 = orderfields(e60);
eday = orderfields(eday);

%%% remove nan time periods at beginning / end due to EEZ
    % for eday, might want to exclude first and last because they are incomplete days
EEZ_day = find(eday.t < datenum(2019,9,6,0,0,0) | eday.t >= datenum(2019,9,25,12,0,0));
EEZ_60 = find(e60.t < datenum(2019,9,6,5,0,0) | e60.t >= datenum(2019,9,25,12,0,0));
EEZ_10 = find(e10.t < datenum(2019,9,6,5,0,0) | e10.t >= datenum(2019,9,25,12,0,0));
EEZ_1 = find(e1.t < datenum(2019,9,6,5,0,0) | e1.t >= datenum(2019,9,25,12,0,0));

fe1 = fields(e1);
fe10 = fields(e10);
fe60 = fields(e60);
fday = fields(day);

for i = 1:length(fe1)
    eval(['e1.' fe1{i} '(EEZ_1) = [];']);
end
for i = 1:length(fe10)
    eval(['e10.' fe10{i} '(EEZ_10) = [];']);
end
for i = 1:length(fe60)
    eval(['e60.' fe60{i} '(EEZ_60) = [];']);
end
for i = 1:length(fday)
    eval(['eday.' fday{i} '(EEZ_day) = [];']);
end

% check for nan time... should not be there
bad_time_1 = find(isnan(e1.t) == 1);
bad_time_10 = find(isnan(e10.t) == 1);
bad_time_60 = find(isnan(e60.t) == 1);
bad_time_day = find(isnan(eday.t) == 1);
if isempty(bad_time_1) == 0
    disp('WARNING -> time in e1 has NaN values, which should be fixed');
elseif isempty(bad_time_10) == 0
    disp('WARNING -> time in e10 has NaN values, which should be fixed');
elseif isempty(bad_time_60) == 0
    disp('WARNING -> time in e60 has NaN values, which should be fixed');
elseif isempty(bad_time_day) == 0
    disp('WARNING -> time in eday has NaN values, which should be fixed');
else
    disp('good news -> time has no NaN values');
end

disp(['  e1 lasts from ' datestr(e1.t(1),0)    ' - ' datestr(e1.t(end),0) ]);
disp([' e10 lasts from ' datestr(e10.t(1),0)   ' - ' datestr(e10.t(end),0) ]);
disp([' e60 lasts from ' datestr(e60.t(1),0)   ' - ' datestr(e60.t(end),0) ]);
disp(['eday lasts from ' datestr(eday.t(1),0)  ' - ' datestr(eday.t(end),0) ]);

%%% save
ename_1 = [e_outdir cruise '_1min_' file_out_version '.mat'];
ename_10 = [e_outdir cruise '_10min_' file_out_version '.mat'];
ename_hr = [e_outdir cruise '_60min_' file_out_version '.mat'];
ename_day = [e_outdir cruise '_daily_' file_out_version '.mat'];

save(ename_1, 'e1'); 
save(ename_10, 'e10'); 
save(ename_hr, 'e60'); 
save(ename_day,'eday'); 

%% save legacy text files %%%% DO NOT CHANGE ORDER OR CONTENTS
%%% used to be done with prt_flux_hr.m and prt_flux_10.m
%%% use the new e10 and e60 fields because they have had EEZ times with no
%%% data removed
 
%%%% DO NOT CHANGE ORDER OR CONTENTS
save_vars_60 =  ...
    {'jd';'sog';'wspd';'wdir';'rspd';'rdir';'hed';'Tsnk';'Ta';'qs';...
    'qa';'hs_cov';'hs_id';'hs';'hl_cov';'hl_id';'hl';'tau_cov';'tau_cov_cross';...
    'tau_id';'tau';'sw_dn';'lw_dn';'prate';
    'jplume';'tilt';'jmanuv';'Ctab';'Cqab';'Cuab';'Cwab';
    'hrain';'hlwebb';'lat';'lon';'zu';'zt';'zq';'sog_s';'wspd_s';'wdir_s';...
    'cog_s';'Tskin';'Tsea_s';'qs_s';'qa_s';'sw_dn_s';'lw_dn_s';'wCO2_cov';...
    'licor_qa';'licor_qa_std';'licor_CO2';'licor_CO2_std';...
    'psealevel'; 'wspd_sfc'; 'wdir_sfc'; 
    'lw_net'; 'hnet'; 'hnet_cov'; ...  % these entries change between 10 min and 1 hr files
    'rh_s';'psealevel_s';...
    'wspd_wxt';'wdir_wxt';'Ta_wxt';'rh_wxt';'psealevel_wxt';'prate_wxt';...
    'wT_cov_sds';'wq_cov_sds';'hs_cov_sds';'hl_cov_sds'};

%%%% DO NOT CHANGE ORDER OR CONTENTS
save_vars_10 = ...
    {'jd';'sog';'wspd';'wdir';'rspd';'rdir';...
    'hed';'Tsnk';'Ta';'qs';...
    'qa';'hs_cov';'hs_id';'hs';...
    'hl_cov';'hl_id';'hl';'tau_cov';'tau_cov_cross';...
    'tau_id';'tau';'sw_dn';'lw_dn';'prate';
    'jplume';'tilt';'jmanuv';'Ctab';'Cqab';'Cuab';'Cwab';
    'hrain';'hlwebb';'lat';'lon';'zu';'zt';'zq';...
    'sog_s';'wspd_s';'wdir_s';...
    'cog_s';'Tskin';'Tsea_s';'qs_s';'qa_s';...
    'sw_dn_s';'lw_dn_s';'wCO2_cov';...
    'licor_qa';'licor_qa_std';'licor_CO2';'licor_CO2_std';...
    'psealevel'; 'wspd_sfc'; 'wdir_sfc'; ...
    'lw_dn_clr';'sw_dn_clr';'U10N';'Ta10N';'qa10N';...   % these entries change between 10 min and 1 hr files
    'rh_s';'psealevel_s';...
    'rh_wxt';'good_usr_id';'good_qsr_id';'good_tsr_id'; ...
    'wspd_sfc'; 'wdir_sfc';
    'hs_cov_sds';'hl_cov_sds';'Tskin'};

clear s60 s10;

for i = 1:length(save_vars_10)
    the_var_10 = char(save_vars_10{i});
    if strcmp(the_var_10,'zu') == 1 || strcmp(the_var_10,'zt') == 1 || ...
            strcmp(the_var_10,'zq') == 1
        %%% for heights/depths: s10.zu = ones(length(s10.jd),1)*zu;
        eval(['s10.' the_var_10 ' = ones(length(s10.jd),1)*' the_var_10 ';']);
    else  %%% variable that don't start with z
        %%% for time array variables
        eval(['s10.' the_var_10 ' = e10.' the_var_10 ';']);
    end
end 

for i = 1:length(save_vars_60)
    the_var_60 = char(save_vars_60{i});
    if strcmp(the_var_60,'zu') == 1 || strcmp(the_var_60,'zt') == 1 || ...
            strcmp(the_var_60,'zq') == 1
        %%% for heights/depths: s60.zu = ones(length(s60.jd),1)*zu;
        eval(['s60.' the_var_60 ' = ones(length(s60.jd),1)*' the_var_60 ';']);
    else  %%% variable that don't start with z
        %%% for time array variables
        eval(['s60.' the_var_60 ' = e60.' the_var_60 ';']);
    end
end 

mat2txt(sname_10,  'n', legacy_dir , s10);
mat2txt(sname_60,  'n', legacy_dir , s60);


% save readme and header automatically
name_readme_s10  = [legacy_dir 'fields_' cruise '_legacy_10min.txt'];
name_readme_s60  = [legacy_dir 'fields_' cruise '_legacy_60min.txt'];
name_header_s10  = [legacy_dir 'header_' cruise '_legacy_10min.txt'];
name_header_s60  = [legacy_dir 'header_' cruise '_legacy_60min.txt'];

fileID_r10 = fopen(name_readme_s10,'w');
fileID_r60 = fopen(name_readme_s60,'w');
fileID_h10 = fopen(name_header_s10,'w');
fileID_h60 = fopen(name_header_s60,'w');

n10 = length(save_vars_10);
for j = 1:n10
%     formatSpec = 'i(%i) = %20s %-60s\n'; % if you want to add a long name
    formatSpec = 'i(%i) = %20s\n';
    fprintf(fileID_r10, formatSpec, j, string(save_vars_10(j))); %, string(l_1(j)) ); % if you want to add a long name
    fprintf(fileID_h10,'%20s',string(save_vars_10(j)) );
end
fclose(fileID_r10);
fclose(fileID_h10);

n60 = length(save_vars_60);
for j = 1:n60
%     formatSpec = 'i(%i) = %20s %-60s\n'; % if you want to add a long name
    formatSpec = 'i(%i) = %20s\n';
    fprintf(fileID_r60, formatSpec, j, string(save_vars_60(j))); %, string(l_1(j)) ); % if you want to add a long name
    fprintf(fileID_h60,'%20s',string(save_vars_60(j)) );
end
fclose(fileID_r60);
fclose(fileID_h60);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plot_checks = 0;
if plot_checks == 1
%%% now check to see before after of fluxes. 
figure;
counter = 1;
thefields = fields(e60);
nc = length(thefields);
for i = 1:4:nc
    clf;
    for j = 1:4
       if (i+j-1) <= nc
           if strcmp(thefields{i+j-1},'hnet_cov') ~= 1 && ...
                   strcmp(thefields{i+j-1},'hnet_id') ~= 1 && ...
                   strcmp(thefields{i+j-1},'Cuw') ~= 1 && ...
                   strcmp(thefields{i+j-1},'Ctw') ~= 1 && ...
                   strcmp(thefields{i+j-1},'Cqw') ~= 1 
               subplot(2,2,j); hold on;
               eval(['newvar = e60.' thefields{i+j-1} ';']);
               eval(['oldvar = e10.' thefields{i+j-1} ';']);
               plot(e10.t, oldvar,'x');
               plot(e60.t, newvar,'o');
               grid on;
               var_name = {strrep(thefields{i+j-1},'_',' ')};
               title(var_name);
               xlim([min(hr.t) max(hr.t)]);
               datetick('x','mm/dd','keeplimits');
               grid on;
               legend('10-min','hour','location','southoutside','orientation','horizontal');
           end
       end
    end
   print(graphdevice,[png_path '/check_' sprintf('%i',counter) '_' cruise graphformat]);
   counter = counter + 1;
end  % for: all vars
end % if: plotting

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plot_checks_all = 0;
if plot_checks_all == 1
%%% now check to see before after of fluxes. 
figure;
counter = 1;
thefields = fields(hr);
nc = length(thefields);
for i = 1:4:nc
    clf;
    for j = 1:4
       if (i+j-1) <= nc
               subplot(2,2,j); hold on;
               eval(['newvar = hr.' thefields{i+j-1} ';']);
               eval(['oldvar = e10.' thefields{i+j-1} ';']);
               plot(e10.t, oldvar,'x');
               plot(hr.t, newvar,'o');
               grid on;
               var_name = {strrep(thefields{i+j-1},'_',' ')};
               title(var_name);
               xlim([min(hr.t) max(hr.t)]);
               datetick('x','mm/dd','keeplimits');
               grid on;
               legend('10-min','hour','location','southoutside','orientation','horizontal');
       end
    end
   print(graphdevice,[png_path '/allcheck_' sprintf('%i',counter) '_' cruise graphformat]);
   counter = counter + 1;
end  % for: all vars
end % if: plotting

%% END and MAKE LEGACY PLOTS
disp(['FINISHED da_red PROCESSING ',cruise]);

plot_it = 0;
if plot_it == 1
     plot_da_red;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% OUTPUTS:


% %%% da_red adds to d10 from rumotcorr. Things that get changed or added in da-red:
% 
% wTson_cov
% wT_cov 
% wT_cov_sds 
% wq_cov
% wq_cov_sds 
% wu_cov
% wv_cov
% wCO2_cov
% 
% hl_cov
% hl_cov_sds
% 
% hs_cov
% hs_cov_sds
% 
% tau_cov
% tau_cov_cross
% 
% uvar 
% vvar
% wvar 
% Tvar 
% 
% Cua 
% Cub 
% Cuab
% Cwa 
% Cwb 
% Cwab
% Cta 
% Ctb 
% Ctab
% Cqa 
% Cqb 
% Cqab
% 
% Csa
% Csb
% Csab
% 
% jmanuv
% jplume
% 
% usr_id
% usr_ida
% usr_idb
% 
% tsr_id
% tsr_ida
% tsr_idb
% 
% tsr_id_son
% tsr_id_sona
% tsr_id_sonb
% 
% qsr_id
% qsr_ida
% qsr_idb
% 
% hs_id
% hs_ida
% hs_idb
% 
% hl_id
% hl_ida
% hl_idb
% 
% tau_id 
% tau_ida
% tau_idb
% 
% good_usr_id(wh_good_usr_id) = 1;
% good_tsr_id(wh_good_tsr_id) = 1;
% good_qsr_id(wh_good_qsr_id) = 1;
% 
% usr_cov = (wu_cov.^2).^(1/4); % wu_cov has neg numbers so need to do it this way for algebra's sake
% qsr_cov = -1E-3*wq_cov./usr_cov;
% tsr_cov = -wT_cov./usr_cov;
% tsr_cov_sds = -wT_cov_sds./usr_cov;
% qsr_cov_sds = -1E-3*wq_cov_sds./usr_cov;
% 
% dT5_warm = dtz(:,round(zsea2_ship)+1); % amount of warming expected at 5 m due to presence of warm layer
% T5_no_warm = b10.Tsea_s - dtz(:,round(zsea2_ship)+1);  % "ML" or 5 m temp: tsg with with 5-m warm layer removed
% 
% flags = {'good_motion';'good_motion_id';'jplume';'jmanuv';...
%     'good_motion_id_licor';'good_motion_licor'; ...
%     'good_usr_id';'good_qsr_id';'good tsr_id'};
% 
% %%% filters:
% 
% wh_good_id = find((b10.rdir<rdir_hi & b10.rdir>rdir_lo) & d10.hed_std<sigh_lim & ...
%        d10.sog_std<sigu_lim & d10.vplat_std<sp2_lim & b10.prate<rain_lim & ...
%        (sqrt(d10.wvar)./d10.ugw < 2+0.0015*b10.wspd.^2) & d10.badSon<badSon_lim & ...
%        d10.missingSon<missingSon_lim & (sqrt(d10.vvar)./d10.ugu < 2));
%    
% ii = (d10.hed_std<sigh_lim) & (d10.sog_std<sigu_lim) & (d10.vplat_std<sp2_lim);
% 
% ii = (sqrt(d10.wvar)./d10.ugw < 2+0.0015*b10.wspd.^2) & (sqrt(d10.vvar)./d10.ugu < 2);
%    
% clear kk; kk = find(d10.good_motion ~= 1 | ...
%     b10.rdir>rdir_hi | b10.rdir<rdir_lo | ...
%     d10.badSon>badSon_lim | d10.missingSon>missingSon_lim);  
% 
% ii = find(b10.licor_AGC>AGC_lim | ...
%     b10.licor_qa<q_lic_LoLim | b10.licor_qa>q_lic_HiLim | ...
%     d10.licor_qa_std>q_std_HiLim | d10.licor_qa_std<q_std_LoLim);
% if strcmp(cruise,'PISTON_2019') == 1
%    iii = find(b10.t > bad_licor_day);
%    ii = unique([ii; iii]);
   
   
%==========================================================================   
   
% d10 = 
% 
%   struct with 133 fields:
% 
%                      Cqa: [2778×1 double]
%                     Cqab: [2778×1 double]
%                      Cqb: [2778×1 double]
%                      Cqw: [2778×48 double]
%                      Csa: [2778×1 double]
%                     Csab: [2778×1 double]
%                      Csb: [2778×1 double]
%                      Cta: [2778×1 double]
%                     Ctab: [2778×1 double]
%                      Ctb: [2778×1 double]
%                      Ctw: [2778×48 double]
%                      Cua: [2778×1 double]
%                     Cuab: [2778×1 double]
%                      Cub: [2778×1 double]
%                      Cuw: [2778×48 double]
%                      Cvw: [2778×48 double]
%                      Cwa: [2778×1 double]
%                     Cwab: [2778×1 double]
%                      Cwb: [2778×1 double]
%                       Fx: [19×48 double]
%                    L_ida: [2778×1 double]
%                    L_idb: [2778×1 double]
%                    Le_sw: [2778×1 double]
%                     Le_w: [2778×1 double]
%                       Pq: [2778×48 double]
%                       Pt: [2778×48 double]
%                       Pu: [2778×48 double]
%                       Pv: [2778×48 double]
%                       Pw: [2778×48 double]
%               T5_no_warm: [2778×1 double]
%                     Tbar: [2778×1 double]
%               Tson_noise: [2778×1 double]
%                 Tson_std: [2778×1 double]
%                     Tvar: [2778×1 double]
%                       Ue: [2778×1 double]
%                       Un: [2778×1 double]
%                      azm: [2778×1 double]
%                   badSon: [2778×1 double]
%                 ched_std: [2778×1 double]
%                  cog_max: [2778×1 double]
%                  cog_min: [2778×1 double]
%                  cog_std: [2778×1 double]
%                 dT5_warm: [2778×1 double]
%              good_motion: [2778×1 double]
%           good_motion_id: [2778×1 double]
%     good_motion_id_licor: [2778×1 double]
%        good_motion_licor: [2778×1 double]
%              good_qsr_id: [2778×1 double]
%              good_tsr_id: [2778×1 double]
%              good_usr_id: [2778×1 double]
%                  hed_max: [2778×1 double]
%                  hed_min: [2778×1 double]
%                  hed_std: [2778×1 double]
%                   hl_cov: [2778×1 double]
%               hl_cov_sds: [2778×1 double]
%                    hl_id: [2778×1 double]
%                   hl_ida: [2778×1 double]
%                   hl_idb: [2778×1 double]
%                   hs_cov: [2778×1 double]
%               hs_cov_sds: [2778×1 double]
%                    hs_id: [2778×1 double]
%                   hs_ida: [2778×1 double]
%                   hs_idb: [2778×1 double]
%                       jd: [2778×1 double]
%                   jmanuv: [2778×1 double]
%                   jplume: [2778×1 double]
%                      lag: [11112×2 double]
%            licor_CO2_std: [2778×1 double]
%            licor_H2O_std: [2778×1 double]
%           licor_Pbox_std: [2778×1 double]
%             licor_qa_std: [2778×1 double]
%               missingSon: [2778×1 double]
%                       mu: [2778×17 double]
%                     mu_q: [2778×13 double]
%                  qsr_cov: [2778×1 double]
%              qsr_cov_sds: [2778×1 double]
%                   qsr_id: [2778×1 double]
%                  qsr_ida: [2778×1 double]
%                  qsr_idb: [2778×1 double]
%                     qvar: [2778×1 double]
%                 rdir_new: [2778×1 double]
%                 rdir_std: [2778×1 double]
%                 rspd_new: [2778×1 double]
%                rspd_new2: [2778×1 double]
%                 rspd_raw: [2778×1 double]
%                 shed_std: [2778×1 double]
%                  sog_max: [2778×1 double]
%                  sog_min: [2778×1 double]
%                  sog_std: [2778×1 double]
%                        t: [2778×1 double]
%                  tau_cov: [2778×1 double]
%            tau_cov_cross: [2778×1 double]
%                   tau_id: [2778×1 double]
%                  tau_ida: [2778×1 double]
%                  tau_idb: [2778×1 double]
%                     tilt: [2778×1 double]
%                  tsr_cov: [2778×1 double]
%              tsr_cov_sds: [2778×1 double]
%                   tsr_id: [2778×1 double]
%               tsr_id_son: [2778×1 double]
%              tsr_id_sona: [2778×1 double]
%              tsr_id_sonb: [2778×1 double]
%                  tsr_ida: [2778×1 double]
%                  tsr_idb: [2778×1 double]
%                     ubar: [2778×1 double]
%                       ug: [2778×1 double]
%                      ugu: [2778×1 double]
%                      ugw: [2778×1 double]
%                uplat_std: [2778×1 double]
%                  usr_cov: [2778×1 double]
%                   usr_id: [2778×1 double]
%                  usr_ida: [2778×1 double]
%                  usr_idb: [2778×1 double]
%                     uvar: [2778×1 double]
%                     vbar: [2778×1 double]
%                vplat_std: [2778×1 double]
%                     vvar: [2778×1 double]
%                 wCO2_cov: [2778×1 double]
%                 wH2O_cov: [2778×1 double]
%                   wT_cov: [2778×1 double]
%               wT_cov_sds: [2778×1 double]
%                wTson_cov: [2778×1 double]
%                     wbar: [2778×1 double]
%                 wdir_new: [2778×1 double]
%                wplat_std: [2778×1 double]
%                   wq_cov: [2778×1 double]
%               wq_cov_sds: [2778×1 double]
%                       ws: [2778×1 double]
%                 wspd_new: [2778×1 double]
%                      wtv: [2778×1 double]
%                   wu_cov: [2778×1 double]
%                   wv_cov: [2778×1 double]
%                     wvar: [2778×1 double]
%                     


%==========================================================================   


% e1 = 
% 
%   struct with 302 fields:
% 
%                       Cd: [27780×1 double]
%                    CdN10: [27780×1 double]
%                  CdN10_s: [27780×1 double]
%                     Cd_s: [27780×1 double]
%                       Ce: [27780×1 double]
%                    CeN10: [27780×1 double]
%                  CeN10_s: [27780×1 double]
%                     Ce_s: [27780×1 double]
%                       Ch: [27780×1 double]
%                    ChN10: [27780×1 double]
%                  ChN10_s: [27780×1 double]
%                     Ch_s: [27780×1 double]
%                     Edis: [27780×1 double]
%                   Edis_s: [27780×1 double]
%                  Fsea1_s: [27780×1 double]
%                  Fsea2_s: [27780×1 double]
%                      H2O: [27780×1 double]
%                  H2O_wxt: [27780×1 double]
%               ID_Fsea1_s: [27780×1 double]
%               ID_Fsea2_s: [27780×1 double]
%               ID_bad_TSG: [27780×1 double]
%              ID_bad_wind: [27780×1 double]
%              ID_mod_wind: [27780×1 double]
%              ID_no_otter: [27780×1 double]
%                 ID_otter: [27780×1 double]
%                 ID_tsg_s: [27780×1 double]
%                        L: [27780×1 double]
%                      L_s: [27780×1 double]
%                       Le: [27780×1 double]
%                     Le_s: [27780×1 double]
%                   S_0p85: [27780×1 double]
%                  Ssea1_s: [27780×1 double]
%                  Ssea2_s: [27780×1 double]
%                   Ssea_s: [27780×1 double]
%                  Stsg1_s: [27780×1 double]
%                       Ta: [27780×1 double]
%                     Ta10: [27780×1 double]
%                    Ta10N: [27780×1 double]
%                  Ta10N_s: [27780×1 double]
%                   Ta10_s: [27780×1 double]
%                      Ta2: [27780×1 double]
%                     Ta2N: [27780×1 double]
%                   Ta2N_s: [27780×1 double]
%                    Ta2_s: [27780×1 double]
%                     Ta_s: [27780×1 double]
%                   Ta_wxt: [27780×1 double]
%                  Tsea1_s: [27780×1 double]
%                  Tsea2_s: [27780×1 double]
%                   Tsea_s: [27780×1 double]
%                    Tskin: [27780×1 double]
%                 Tskin_ir: [27780×1 double]
%                  Tskin_s: [27780×1 double]
%                     Tsnk: [27780×1 double]
%                   Tsonic: [27780×1 double]
%                  Ttsg1_s: [27780×1 double]
%                     Tvar: [27780×1 double]
%                      U10: [27780×1 double]
%                     U10N: [27780×1 double]
%                   U10N_s: [27780×1 double]
%                    U10_s: [27780×1 double]
%                       U2: [27780×1 double]
%                      U2N: [27780×1 double]
%                    U2N_s: [27780×1 double]
%                     U2_s: [27780×1 double]
%                       UN: [27780×1 double]
%                     UN_s: [27780×1 double]
%                aspir_TRH: [27780×1 double]
%                     cdir: [27780×1 double]
%                cdir_adcp: [27780×1 double]
%              cdir_spdlog: [27780×1 double]
%                 ched_std: [27780×1 double]
%               ched_std_s: [27780×1 double]
%                      cog: [27780×1 double]
%                  cog_max: [27780×1 double]
%                cog_max_s: [27780×1 double]
%                  cog_min: [27780×1 double]
%                cog_min_s: [27780×1 double]
%                    cog_s: [27780×1 double]
%                  cog_std: [27780×1 double]
%                cog_std_s: [27780×1 double]
%                     cspd: [27780×1 double]
%                cspd_adcp: [27780×1 double]
%              cspd_spdlog: [27780×1 double]
%              cspd_u_adcp: [27780×1 double]
%              cspd_v_adcp: [27780×1 double]
%                  dT_skin: [27780×1 double]
%                dT_skin_s: [27780×1 double]
%                  dT_warm: [27780×1 double]
%                dT_warm_s: [27780×1 double]
%          dT_warm_to_skin: [27780×1 double]
%        dT_warm_to_skin_s: [27780×1 double]
%                      day: [27780×1 double]
%                  dq_skin: [27780×1 double]
%                dq_skin_s: [27780×1 double]
%                   dtheta: [27780×1 double]
%                  du_warm: [27780×1 double]
%                du_warm_s: [27780×1 double]
%                  dz_skin: [27780×1 double]
%                dz_skin_s: [27780×1 double]
%                  dz_warm: [27780×1 double]
%                dz_warm_s: [27780×1 double]
%                    erate: [27780×1 double]
%                  erate_s: [27780×1 double]
%                     gust: [27780×1 double]
%                   gust_s: [27780×1 double]
%                       hb: [27780×1 double]
%                     hb_s: [27780×1 double]
%                   hb_son: [27780×1 double]
%                 hb_son_s: [27780×1 double]
%                      hed: [27780×1 double]
%                     hedE: [27780×1 double]
%                   hedE_s: [27780×1 double]
%                     hedN: [27780×1 double]
%                   hedN_s: [27780×1 double]
%                  hed_max: [27780×1 double]
%                hed_max_s: [27780×1 double]
%                  hed_min: [27780×1 double]
%                hed_min_s: [27780×1 double]
%                    hed_s: [27780×1 double]
%                  hed_std: [27780×1 double]
%                hed_std_s: [27780×1 double]
%                       hl: [27780×1 double]
%                     hl_s: [27780×1 double]
%                   hlwebb: [27780×1 double]
%                 hlwebb_s: [27780×1 double]
%                     hnet: [27780×1 double]
%                   hnet_s: [27780×1 double]
%                     hour: [27780×1 double]
%                    hrain: [27780×1 double]
%                  hrain_s: [27780×1 double]
%                       hs: [27780×1 double]
%                     hs_s: [27780×1 double]
%                       jd: [27780×1 double]
%                   jmanuv: [27780×1 double]
%                   jplume: [27780×1 double]
%                      lat: [27780×1 double]
%                    lat_s: [27780×1 double]
%                licor_AGC: [27780×1 double]
%                licor_CO2: [27780×1 double]
%             licor_CO2_MR: [27780×1 double]
%                licor_H2O: [27780×1 double]
%             licor_H2O_MR: [27780×1 double]
%               licor_Pbox: [27780×1 double]
%               licor_Tbox: [27780×1 double]
%                 licor_qa: [27780×1 double]
%             licor_qa_dry: [27780×1 double]
%                 licor_rh: [27780×1 double]
%           licor_rhoa_dry: [27780×1 double]
%                      lon: [27780×1 double]
%                    lon_s: [27780×1 double]
%              lw_T_case_1: [27780×1 double]
%              lw_T_case_2: [27780×1 double]
%              lw_T_case_s: [27780×1 double]
%              lw_T_dome_1: [27780×1 double]
%              lw_T_dome_2: [27780×1 double]
%              lw_T_dome_s: [27780×1 double]
%                    lw_dn: [27780×1 double]
%                  lw_dn_1: [27780×1 double]
%                  lw_dn_2: [27780×1 double]
%                lw_dn_clr: [27780×1 double]
%                  lw_dn_s: [27780×1 double]
%                lw_dn_std: [27780×1 double]
%                   lw_net: [27780×1 double]
%                 lw_net_s: [27780×1 double]
%               lw_therm_1: [27780×1 double]
%               lw_therm_2: [27780×1 double]
%               lw_therm_s: [27780×1 double]
%                    lw_up: [27780×1 double]
%                  lw_up_s: [27780×1 double]
%                   minute: [27780×1 double]
%                    month: [27780×1 double]
%                     orgV: [27780×1 double]
%                orgV_desp: [27780×1 double]
%                       pa: [27780×1 double]
%                     pa10: [27780×1 double]
%                   pa10_s: [27780×1 double]
%                     pa_s: [27780×1 double]
%                   pa_wxt: [27780×1 double]
%                   paccum: [27780×1 double]
%                 paccum_s: [27780×1 double]
%               paccum_wxt: [27780×1 double]
%                    pitch: [27780×1 double]
%                    plume: [27780×1 double]
%                    prate: [27780×1 double]
%               prate_orig: [27780×1 double]
%                  prate_s: [27780×1 double]
%                prate_wxt: [27780×1 double]
%                   psea_s: [27780×1 double]
%                psealevel: [27780×1 double]
%     psealevel_reported_s: [27780×1 double]
%              psealevel_s: [27780×1 double]
%            psealevel_wxt: [27780×1 double]
%                       qa: [27780×1 double]
%                     qa10: [27780×1 double]
%                    qa10N: [27780×1 double]
%                  qa10N_s: [27780×1 double]
%                   qa10_s: [27780×1 double]
%                      qa2: [27780×1 double]
%                     qa2N: [27780×1 double]
%                   qa2N_s: [27780×1 double]
%                    qa2_s: [27780×1 double]
%                     qa_s: [27780×1 double]
%                   qa_wxt: [27780×1 double]
%                       qs: [27780×1 double]
%                     qs_s: [27780×1 double]
%                     qsnk: [27780×1 double]
%                      qsr: [27780×1 double]
%                    qsr_s: [27780×1 double]
%                     qvar: [27780×1 double]
%                  rUn_wxt: [27780×1 double]
%                  rUw_wxt: [27780×1 double]
%                     rdir: [27780×1 double]
%                   rdir_s: [27780×1 double]
%                 rdir_std: [27780×1 double]
%               rdir_std_s: [27780×1 double]
%                 rdir_wxt: [27780×1 double]
%                       rh: [27780×1 double]
%                     rh10: [27780×1 double]
%                   rh10_s: [27780×1 double]
%                      rh2: [27780×1 double]
%                    rh2_s: [27780×1 double]
%                     rh_s: [27780×1 double]
%                   rh_wxt: [27780×1 double]
%                     rhoa: [27780×1 double]
%                   rhoa10: [27780×1 double]
%                 rhoa10_s: [27780×1 double]
%                   rhoa_s: [27780×1 double]
%                 rhoa_wxt: [27780×1 double]
%                 rhosea_s: [27780×1 double]
%                     rspd: [27780×1 double]
%                   rspd_s: [27780×1 double]
%               rspd_std_s: [27780×1 double]
%                 rspd_wxt: [27780×1 double]
%                 shed_std: [27780×1 double]
%               shed_std_s: [27780×1 double]
%                      sog: [27780×1 double]
%                     sogE: [27780×1 double]
%                   sogE_s: [27780×1 double]
%                     sogN: [27780×1 double]
%                   sogN_s: [27780×1 double]
%                  sog_max: [27780×1 double]
%                sog_max_s: [27780×1 double]
%                  sog_min: [27780×1 double]
%                sog_min_s: [27780×1 double]
%                    sog_s: [27780×1 double]
%                  sog_std: [27780×1 double]
%                sog_std_s: [27780×1 double]
%                spd_log_s: [27780×1 double]
%            spd_log_std_s: [27780×1 double]
%              spd_log_u_s: [27780×1 double]
%              spd_log_v_s: [27780×1 double]
%                    sw_dn: [27780×1 double]
%                  sw_dn_1: [27780×1 double]
%                  sw_dn_2: [27780×1 double]
%                sw_dn_clr: [27780×1 double]
%                  sw_dn_s: [27780×1 double]
%                sw_dn_std: [27780×1 double]
%                   sw_net: [27780×1 double]
%                 sw_net_s: [27780×1 double]
%                    sw_up: [27780×1 double]
%                  sw_up_s: [27780×1 double]
%                        t: [27780×1 double]
%                      tau: [27780×1 double]
%                    tau_s: [27780×1 double]
%                   theta0: [27780×1 double]
%                  theta10: [27780×1 double]
%                      tsr: [27780×1 double]
%                    tsr_s: [27780×1 double]
%                  tsr_son: [27780×1 double]
%                uplat_std: [27780×1 double]
%                      usr: [27780×1 double]
%                    usr_s: [27780×1 double]
%                     uvar: [27780×1 double]
%                vplat_std: [27780×1 double]
%                     vvar: [27780×1 double]
%                  wc_frac: [27780×1 double]
%                wc_frac_s: [27780×1 double]
%                     wdir: [27780×1 double]
%               wdir_max_s: [27780×1 double]
%               wdir_min_s: [27780×1 double]
%                   wdir_s: [27780×1 double]
%                 wdir_sfc: [27780×1 double]
%               wdir_std_s: [27780×1 double]
%                 wdir_wxt: [27780×1 double]
%                wplat_std: [27780×1 double]
%                     wspd: [27780×1 double]
%               wspd_max_s: [27780×1 double]
%               wspd_min_s: [27780×1 double]
%                   wspd_s: [27780×1 double]
%                 wspd_sfc: [27780×1 double]
%               wspd_std_s: [27780×1 double]
%                 wspd_wxt: [27780×1 double]
%                     wvar: [27780×1 double]
%                     year: [27780×1 double]
%                     zeta: [27780×1 double]
%                   zeta_s: [27780×1 double]
%                       zo: [27780×1 double]
%                     zo_s: [27780×1 double]
%                      zoq: [27780×1 double]
%                    zoq_s: [27780×1 double]
%                      zot: [27780×1 double]
%                    zot_s: [27780×1 double]
% 


%==========================================================================   


% e10 = 
% 
%   struct with 399 fields:
% 
%                       Cd: [2778×1 double]
%                    CdN10: [2778×1 double]
%                  CdN10_s: [2778×1 double]
%                     Cd_s: [2778×1 double]
%                       Ce: [2778×1 double]
%                    CeN10: [2778×1 double]
%                  CeN10_s: [2778×1 double]
%                     Ce_s: [2778×1 double]
%                       Ch: [2778×1 double]
%                    ChN10: [2778×1 double]
%                  ChN10_s: [2778×1 double]
%                     Ch_s: [2778×1 double]
%                      Cqa: [2778×1 double]
%                     Cqab: [2778×1 double]
%                      Cqb: [2778×1 double]
%                      Csa: [2778×1 double]
%                     Csab: [2778×1 double]
%                      Csb: [2778×1 double]
%                      Cta: [2778×1 double]
%                     Ctab: [2778×1 double]
%                      Ctb: [2778×1 double]
%                      Cua: [2778×1 double]
%                     Cuab: [2778×1 double]
%                      Cub: [2778×1 double]
%                      Cwa: [2778×1 double]
%                     Cwab: [2778×1 double]
%                      Cwb: [2778×1 double]
%                     Edis: [2778×1 double]
%                   Edis_s: [2778×1 double]
%                  Fsea1_s: [2778×1 double]
%                  Fsea2_s: [2778×1 double]
%                      H2O: [2778×1 double]
%                  H2O_wxt: [2778×1 double]
%               ID_Fsea1_s: [2778×1 double]
%               ID_Fsea2_s: [2778×1 double]
%               ID_bad_TSG: [2778×1 double]
%              ID_bad_wind: [2778×1 double]
%              ID_mod_wind: [2778×1 double]
%              ID_no_otter: [2778×1 double]
%                 ID_otter: [2778×1 double]
%                 ID_tsg_s: [2778×1 double]
%                        L: [2778×1 double]
%                    L_ida: [2778×1 double]
%                    L_idb: [2778×1 double]
%                      L_s: [2778×1 double]
%                       Le: [2778×1 double]
%                     Le_s: [2778×1 double]
%                    Le_sw: [2778×1 double]
%                     Le_w: [2778×1 double]
%                   S_0p85: [2778×1 double]
%                  Ssea1_s: [2778×1 double]
%                  Ssea2_s: [2778×1 double]
%                   Ssea_s: [2778×1 double]
%                  Stsg1_s: [2778×1 double]
%               T5_no_warm: [2778×1 double]
%                       Ta: [2778×1 double]
%                     Ta10: [2778×1 double]
%                    Ta10N: [2778×1 double]
%                  Ta10N_s: [2778×1 double]
%                   Ta10_s: [2778×1 double]
%                      Ta2: [2778×1 double]
%                     Ta2N: [2778×1 double]
%                   Ta2N_s: [2778×1 double]
%                    Ta2_s: [2778×1 double]
%                     Ta_s: [2778×1 double]
%                   Ta_wxt: [2778×1 double]
%                     Tbar: [2778×1 double]
%                  Tsea1_s: [2778×1 double]
%                  Tsea2_s: [2778×1 double]
%                   Tsea_s: [2778×1 double]
%                    Tskin: [2778×1 double]
%                 Tskin_ir: [2778×1 double]
%                  Tskin_s: [2778×1 double]
%                     Tsnk: [2778×1 double]
%               Tson_noise: [2778×1 double]
%                 Tson_std: [2778×1 double]
%                   Tsonic: [2778×1 double]
%                  Ttsg1_s: [2778×1 double]
%                     Tvar: [2778×1 double]
%                      U10: [2778×1 double]
%                     U10N: [2778×1 double]
%                   U10N_s: [2778×1 double]
%                    U10_s: [2778×1 double]
%                       U2: [2778×1 double]
%                      U2N: [2778×1 double]
%                    U2N_s: [2778×1 double]
%                     U2_s: [2778×1 double]
%                       UN: [2778×1 double]
%                     UN_s: [2778×1 double]
%                       Ue: [2778×1 double]
%                       Un: [2778×1 double]
%                aspir_TRH: [2778×1 double]
%                      azm: [2778×1 double]
%                   badSon: [2778×1 double]
%                     cdir: [2778×1 double]
%                cdir_adcp: [2778×1 double]
%              cdir_spdlog: [2778×1 double]
%                 ched_std: [2778×1 double]
%               ched_std_s: [2778×1 double]
%                      cog: [2778×1 double]
%                  cog_max: [2778×1 double]
%                cog_max_s: [2778×1 double]
%                  cog_min: [2778×1 double]
%                cog_min_s: [2778×1 double]
%                    cog_s: [2778×1 double]
%                  cog_std: [2778×1 double]
%                cog_std_s: [2778×1 double]
%                     cspd: [2778×1 double]
%                cspd_adcp: [2778×1 double]
%              cspd_spdlog: [2778×1 double]
%              cspd_u_adcp: [2778×1 double]
%              cspd_v_adcp: [2778×1 double]
%                 dT5_warm: [2778×1 double]
%                  dT_skin: [2778×1 double]
%                dT_skin_s: [2778×1 double]
%                  dT_warm: [2778×1 double]
%                dT_warm_s: [2778×1 double]
%          dT_warm_to_skin: [2778×1 double]
%        dT_warm_to_skin_s: [2778×1 double]
%                      day: [2778×1 double]
%                  dq_skin: [2778×1 double]
%                dq_skin_s: [2778×1 double]
%                   dtheta: [2778×1 double]
%                  du_warm: [2778×1 double]
%                du_warm_s: [2778×1 double]
%                  dz_skin: [2778×1 double]
%                dz_skin_s: [2778×1 double]
%                  dz_warm: [2778×1 double]
%                dz_warm_s: [2778×1 double]
%                    erate: [2778×1 double]
%                  erate_s: [2778×1 double]
%              good_motion: [2778×1 double]
%           good_motion_id: [2778×1 double]
%     good_motion_id_licor: [2778×1 double]
%        good_motion_licor: [2778×1 double]
%              good_qsr_id: [2778×1 double]
%              good_tsr_id: [2778×1 double]
%              good_usr_id: [2778×1 double]
%                     gust: [2778×1 double]
%                   gust_s: [2778×1 double]
%                       hb: [2778×1 double]
%                     hb_s: [2778×1 double]
%                   hb_son: [2778×1 double]
%                 hb_son_s: [2778×1 double]
%                      hed: [2778×1 double]
%                     hedE: [2778×1 double]
%                   hedE_s: [2778×1 double]
%                     hedN: [2778×1 double]
%                   hedN_s: [2778×1 double]
%                  hed_max: [2778×1 double]
%                hed_max_s: [2778×1 double]
%                  hed_min: [2778×1 double]
%                hed_min_s: [2778×1 double]
%                    hed_s: [2778×1 double]
%                  hed_std: [2778×1 double]
%                hed_std_s: [2778×1 double]
%                       hl: [2778×1 double]
%                   hl_cov: [2778×1 double]
%               hl_cov_sds: [2778×1 double]
%                    hl_id: [2778×1 double]
%                   hl_ida: [2778×1 double]
%                   hl_idb: [2778×1 double]
%                     hl_s: [2778×1 double]
%                   hlwebb: [2778×1 double]
%                 hlwebb_s: [2778×1 double]
%                     hnet: [2778×1 double]
%                 hnet_cov: [2778×1 double]
%                  hnet_id: [2778×1 double]
%                   hnet_s: [2778×1 double]
%                     hour: [2778×1 double]
%                    hrain: [2778×1 double]
%                  hrain_s: [2778×1 double]
%                       hs: [2778×1 double]
%                   hs_cov: [2778×1 double]
%               hs_cov_sds: [2778×1 double]
%                    hs_id: [2778×1 double]
%                   hs_ida: [2778×1 double]
%                   hs_idb: [2778×1 double]
%                     hs_s: [2778×1 double]
%                       jd: [2778×1 double]
%                   jmanuv: [2778×1 double]
%                   jplume: [2778×1 double]
%                      lat: [2778×1 double]
%                    lat_s: [2778×1 double]
%                licor_AGC: [2778×1 double]
%                licor_CO2: [2778×1 double]
%             licor_CO2_MR: [2778×1 double]
%            licor_CO2_std: [2778×1 double]
%                licor_H2O: [2778×1 double]
%             licor_H2O_MR: [2778×1 double]
%            licor_H2O_std: [2778×1 double]
%               licor_Pbox: [2778×1 double]
%           licor_Pbox_std: [2778×1 double]
%               licor_Tbox: [2778×1 double]
%                 licor_qa: [2778×1 double]
%             licor_qa_dry: [2778×1 double]
%             licor_qa_std: [2778×1 double]
%                 licor_rh: [2778×1 double]
%           licor_rhoa_dry: [2778×1 double]
%                      lon: [2778×1 double]
%                    lon_s: [2778×1 double]
%              lw_T_case_1: [2778×1 double]
%              lw_T_case_2: [2778×1 double]
%              lw_T_case_s: [2778×1 double]
%              lw_T_dome_1: [2778×1 double]
%              lw_T_dome_2: [2778×1 double]
%              lw_T_dome_s: [2778×1 double]
%                    lw_dn: [2778×1 double]
%                  lw_dn_1: [2778×1 double]
%                  lw_dn_2: [2778×1 double]
%                lw_dn_clr: [2778×1 double]
%                  lw_dn_s: [2778×1 double]
%                lw_dn_std: [2778×1 double]
%                   lw_net: [2778×1 double]
%                 lw_net_s: [2778×1 double]
%               lw_therm_1: [2778×1 double]
%               lw_therm_2: [2778×1 double]
%               lw_therm_s: [2778×1 double]
%                    lw_up: [2778×1 double]
%                  lw_up_s: [2778×1 double]
%                   minute: [2778×1 double]
%               missingSon: [2778×1 double]
%                    month: [2778×1 double]
%                     orgV: [2778×1 double]
%                orgV_desp: [2778×1 double]
%                       pa: [2778×1 double]
%                     pa10: [2778×1 double]
%                   pa10_s: [2778×1 double]
%                     pa_s: [2778×1 double]
%                   pa_wxt: [2778×1 double]
%                   paccum: [2778×1 double]
%                 paccum_s: [2778×1 double]
%               paccum_wxt: [2778×1 double]
%                    pitch: [2778×1 double]
%                    prate: [2778×1 double]
%               prate_orig: [2778×1 double]
%                  prate_s: [2778×1 double]
%                prate_wxt: [2778×1 double]
%                   psea_s: [2778×1 double]
%                psealevel: [2778×1 double]
%     psealevel_reported_s: [2778×1 double]
%              psealevel_s: [2778×1 double]
%            psealevel_wxt: [2778×1 double]
%                       qa: [2778×1 double]
%                     qa10: [2778×1 double]
%                    qa10N: [2778×1 double]
%                  qa10N_s: [2778×1 double]
%                   qa10_s: [2778×1 double]
%                      qa2: [2778×1 double]
%                     qa2N: [2778×1 double]
%                   qa2N_s: [2778×1 double]
%                    qa2_s: [2778×1 double]
%                     qa_s: [2778×1 double]
%                   qa_wxt: [2778×1 double]
%                       qs: [2778×1 double]
%                     qs_s: [2778×1 double]
%                     qsnk: [2778×1 double]
%                      qsr: [2778×1 double]
%                  qsr_cov: [2778×1 double]
%              qsr_cov_sds: [2778×1 double]
%                   qsr_id: [2778×1 double]
%                  qsr_ida: [2778×1 double]
%                  qsr_idb: [2778×1 double]
%                    qsr_s: [2778×1 double]
%                     qvar: [2778×1 double]
%                  rUn_wxt: [2778×1 double]
%                  rUw_wxt: [2778×1 double]
%                     rdir: [2778×1 double]
%                 rdir_new: [2778×1 double]
%                   rdir_s: [2778×1 double]
%                 rdir_std: [2778×1 double]
%               rdir_std_s: [2778×1 double]
%                 rdir_wxt: [2778×1 double]
%                       rh: [2778×1 double]
%                     rh10: [2778×1 double]
%                   rh10_s: [2778×1 double]
%                      rh2: [2778×1 double]
%                    rh2_s: [2778×1 double]
%                     rh_s: [2778×1 double]
%                   rh_wxt: [2778×1 double]
%                     rhoa: [2778×1 double]
%                   rhoa10: [2778×1 double]
%                 rhoa10_s: [2778×1 double]
%                   rhoa_s: [2778×1 double]
%                 rhoa_wxt: [2778×1 double]
%                 rhosea_s: [2778×1 double]
%                     rspd: [2778×1 double]
%                 rspd_new: [2778×1 double]
%                rspd_new2: [2778×1 double]
%                 rspd_raw: [2778×1 double]
%                   rspd_s: [2778×1 double]
%               rspd_std_s: [2778×1 double]
%                 rspd_wxt: [2778×1 double]
%                 shed_std: [2778×1 double]
%               shed_std_s: [2778×1 double]
%                      sog: [2778×1 double]
%                     sogE: [2778×1 double]
%                   sogE_s: [2778×1 double]
%                     sogN: [2778×1 double]
%                   sogN_s: [2778×1 double]
%                  sog_max: [2778×1 double]
%                sog_max_s: [2778×1 double]
%                  sog_min: [2778×1 double]
%                sog_min_s: [2778×1 double]
%                    sog_s: [2778×1 double]
%                  sog_std: [2778×1 double]
%                sog_std_s: [2778×1 double]
%                spd_log_s: [2778×1 double]
%            spd_log_std_s: [2778×1 double]
%              spd_log_u_s: [2778×1 double]
%              spd_log_v_s: [2778×1 double]
%                    sw_dn: [2778×1 double]
%                  sw_dn_1: [2778×1 double]
%                  sw_dn_2: [2778×1 double]
%                sw_dn_clr: [2778×1 double]
%                  sw_dn_s: [2778×1 double]
%                sw_dn_std: [2778×1 double]
%                   sw_net: [2778×1 double]
%                 sw_net_s: [2778×1 double]
%                    sw_up: [2778×1 double]
%                  sw_up_s: [2778×1 double]
%                        t: [2778×1 double]
%                      tau: [2778×1 double]
%                  tau_cov: [2778×1 double]
%            tau_cov_cross: [2778×1 double]
%                   tau_id: [2778×1 double]
%                  tau_ida: [2778×1 double]
%                  tau_idb: [2778×1 double]
%                    tau_s: [2778×1 double]
%                   theta0: [2778×1 double]
%                  theta10: [2778×1 double]
%                     tilt: [2778×1 double]
%                      tsr: [2778×1 double]
%                  tsr_cov: [2778×1 double]
%              tsr_cov_sds: [2778×1 double]
%                   tsr_id: [2778×1 double]
%               tsr_id_son: [2778×1 double]
%              tsr_id_sona: [2778×1 double]
%              tsr_id_sonb: [2778×1 double]
%                  tsr_ida: [2778×1 double]
%                  tsr_idb: [2778×1 double]
%                    tsr_s: [2778×1 double]
%                  tsr_son: [2778×1 double]
%                     ubar: [2778×1 double]
%                       ug: [2778×1 double]
%                      ugu: [2778×1 double]
%                      ugw: [2778×1 double]
%                uplat_std: [2778×1 double]
%                      usr: [2778×1 double]
%                  usr_cov: [2778×1 double]
%                   usr_id: [2778×1 double]
%                  usr_ida: [2778×1 double]
%                  usr_idb: [2778×1 double]
%                    usr_s: [2778×1 double]
%                     uvar: [2778×1 double]
%                     vbar: [2778×1 double]
%                vplat_std: [2778×1 double]
%                     vvar: [2778×1 double]
%                 wCO2_cov: [2778×1 double]
%                 wH2O_cov: [2778×1 double]
%                   wT_cov: [2778×1 double]
%               wT_cov_sds: [2778×1 double]
%                wTson_cov: [2778×1 double]
%                     wbar: [2778×1 double]
%                  wc_frac: [2778×1 double]
%                wc_frac_s: [2778×1 double]
%                     wdir: [2778×1 double]
%               wdir_max_s: [2778×1 double]
%               wdir_min_s: [2778×1 double]
%                 wdir_new: [2778×1 double]
%                   wdir_s: [2778×1 double]
%                 wdir_sfc: [2778×1 double]
%               wdir_std_s: [2778×1 double]
%                 wdir_wxt: [2778×1 double]
%                wplat_std: [2778×1 double]
%                   wq_cov: [2778×1 double]
%               wq_cov_sds: [2778×1 double]
%                       ws: [2778×1 double]
%                     wspd: [2778×1 double]
%               wspd_max_s: [2778×1 double]
%               wspd_min_s: [2778×1 double]
%                 wspd_new: [2778×1 double]
%                   wspd_s: [2778×1 double]
%                 wspd_sfc: [2778×1 double]
%               wspd_std_s: [2778×1 double]
%                 wspd_wxt: [2778×1 double]
%                      wtv: [2778×1 double]
%                   wu_cov: [2778×1 double]
%                   wv_cov: [2778×1 double]
%                     wvar: [2778×1 double]
%                     year: [2778×1 double]
%                     zeta: [2778×1 double]
%                   zeta_s: [2778×1 double]
%                       zo: [2778×1 double]
%                     zo_s: [2778×1 double]
%                      zoq: [2778×1 double]
%                    zoq_s: [2778×1 double]
%                      zot: [2778×1 double]
%                    zot_s: [2778×1 double]
                   
                   
%==========================================================================   


% e60 = hr = 
% 
%   struct with 399 fields:
% 
%                       Cd: [480×1 double]
%                    CdN10: [480×1 double]
%                  CdN10_s: [480×1 double]
%                     Cd_s: [480×1 double]
%                       Ce: [480×1 double]
%                    CeN10: [480×1 double]
%                  CeN10_s: [480×1 double]
%                     Ce_s: [480×1 double]
%                       Ch: [480×1 double]
%                    ChN10: [480×1 double]
%                  ChN10_s: [480×1 double]
%                     Ch_s: [480×1 double]
%                      Cqa: [480×1 double]
%                     Cqab: [480×1 double]
%                      Cqb: [480×1 double]
%                      Csa: [480×1 double]
%                     Csab: [480×1 double]
%                      Csb: [480×1 double]
%                      Cta: [480×1 double]
%                     Ctab: [480×1 double]
%                      Ctb: [480×1 double]
%                      Cua: [480×1 double]
%                     Cuab: [480×1 double]
%                      Cub: [480×1 double]
%                      Cwa: [480×1 double]
%                     Cwab: [480×1 double]
%                      Cwb: [480×1 double]
%                     Edis: [480×1 double]
%                   Edis_s: [480×1 double]
%                  Fsea1_s: [480×1 double]
%                  Fsea2_s: [480×1 double]
%                      H2O: [480×1 double]
%                  H2O_wxt: [480×1 double]
%               ID_Fsea1_s: [480×1 double]
%               ID_Fsea2_s: [480×1 double]
%               ID_bad_TSG: [480×1 double]
%              ID_bad_wind: [480×1 double]
%              ID_mod_wind: [480×1 double]
%              ID_no_otter: [480×1 double]
%                 ID_otter: [480×1 double]
%                 ID_tsg_s: [480×1 double]
%                        L: [480×1 double]
%                    L_ida: [480×1 double]
%                    L_idb: [480×1 double]
%                      L_s: [480×1 double]
%                       Le: [480×1 double]
%                     Le_s: [480×1 double]
%                    Le_sw: [480×1 double]
%                     Le_w: [480×1 double]
%                   S_0p85: [480×1 double]
%                  Ssea1_s: [480×1 double]
%                  Ssea2_s: [480×1 double]
%                   Ssea_s: [480×1 double]
%                  Stsg1_s: [480×1 double]
%               T5_no_warm: [480×1 double]
%                       Ta: [480×1 double]
%                     Ta10: [480×1 double]
%                    Ta10N: [480×1 double]
%                  Ta10N_s: [480×1 double]
%                   Ta10_s: [480×1 double]
%                      Ta2: [480×1 double]
%                     Ta2N: [480×1 double]
%                   Ta2N_s: [480×1 double]
%                    Ta2_s: [480×1 double]
%                     Ta_s: [480×1 double]
%                   Ta_wxt: [480×1 double]
%                     Tbar: [480×1 double]
%                  Tsea1_s: [480×1 double]
%                  Tsea2_s: [480×1 double]
%                   Tsea_s: [480×1 double]
%                    Tskin: [480×1 double]
%                 Tskin_ir: [480×1 double]
%                  Tskin_s: [480×1 double]
%                     Tsnk: [480×1 double]
%               Tson_noise: [480×1 double]
%                 Tson_std: [480×1 double]
%                   Tsonic: [480×1 double]
%                  Ttsg1_s: [480×1 double]
%                     Tvar: [480×1 double]
%                      U10: [480×1 double]
%                     U10N: [480×1 double]
%                   U10N_s: [480×1 double]
%                    U10_s: [480×1 double]
%                       U2: [480×1 double]
%                      U2N: [480×1 double]
%                    U2N_s: [480×1 double]
%                     U2_s: [480×1 double]
%                       UN: [480×1 double]
%                     UN_s: [480×1 double]
%                       Ue: [480×1 double]
%                       Un: [480×1 double]
%                aspir_TRH: [480×1 double]
%                      azm: [480×1 double]
%                   badSon: [480×1 double]
%                     cdir: [480×1 double]
%                cdir_adcp: [480×1 double]
%              cdir_spdlog: [480×1 double]
%                 ched_std: [480×1 double]
%               ched_std_s: [480×1 double]
%                      cog: [480×1 double]
%                  cog_max: [480×1 double]
%                cog_max_s: [480×1 double]
%                  cog_min: [480×1 double]
%                cog_min_s: [480×1 double]
%                    cog_s: [480×1 double]
%                  cog_std: [480×1 double]
%                cog_std_s: [480×1 double]
%                     cspd: [480×1 double]
%                cspd_adcp: [480×1 double]
%              cspd_spdlog: [480×1 double]
%              cspd_u_adcp: [480×1 double]
%              cspd_v_adcp: [480×1 double]
%                 dT5_warm: [480×1 double]
%                  dT_skin: [480×1 double]
%                dT_skin_s: [480×1 double]
%                  dT_warm: [480×1 double]
%                dT_warm_s: [480×1 double]
%          dT_warm_to_skin: [480×1 double]
%        dT_warm_to_skin_s: [480×1 double]
%                      day: [480×1 double]
%                  dq_skin: [480×1 double]
%                dq_skin_s: [480×1 double]
%                   dtheta: [480×1 double]
%                  du_warm: [480×1 double]
%                du_warm_s: [480×1 double]
%                  dz_skin: [480×1 double]
%                dz_skin_s: [480×1 double]
%                  dz_warm: [480×1 double]
%                dz_warm_s: [480×1 double]
%                    erate: [480×1 double]
%                  erate_s: [480×1 double]
%              good_motion: [480×1 double]
%           good_motion_id: [480×1 double]
%     good_motion_id_licor: [480×1 double]
%        good_motion_licor: [480×1 double]
%              good_qsr_id: [480×1 double]
%              good_tsr_id: [480×1 double]
%              good_usr_id: [480×1 double]
%                     gust: [480×1 double]
%                   gust_s: [480×1 double]
%                       hb: [480×1 double]
%                     hb_s: [480×1 double]
%                   hb_son: [480×1 double]
%                 hb_son_s: [480×1 double]
%                      hed: [480×1 double]
%                     hedE: [480×1 double]
%                   hedE_s: [480×1 double]
%                     hedN: [480×1 double]
%                   hedN_s: [480×1 double]
%                  hed_max: [480×1 double]
%                hed_max_s: [480×1 double]
%                  hed_min: [480×1 double]
%                hed_min_s: [480×1 double]
%                    hed_s: [480×1 double]
%                  hed_std: [480×1 double]
%                hed_std_s: [480×1 double]
%                       hl: [480×1 double]
%                   hl_cov: [480×1 double]
%               hl_cov_sds: [480×1 double]
%                    hl_id: [480×1 double]
%                   hl_ida: [480×1 double]
%                   hl_idb: [480×1 double]
%                     hl_s: [480×1 double]
%                   hlwebb: [480×1 double]
%                 hlwebb_s: [480×1 double]
%                     hnet: [480×1 double]
%                 hnet_cov: [480×1 double]
%                  hnet_id: [480×1 double]
%                   hnet_s: [480×1 double]
%                     hour: [480×1 double]
%                    hrain: [480×1 double]
%                  hrain_s: [480×1 double]
%                       hs: [480×1 double]
%                   hs_cov: [480×1 double]
%               hs_cov_sds: [480×1 double]
%                    hs_id: [480×1 double]
%                   hs_ida: [480×1 double]
%                   hs_idb: [480×1 double]
%                     hs_s: [480×1 double]
%                       jd: [480×1 double]
%                   jmanuv: [480×1 double]
%                   jplume: [480×1 double]
%                      lat: [480×1 double]
%                    lat_s: [480×1 double]
%                licor_AGC: [480×1 double]
%                licor_CO2: [480×1 double]
%             licor_CO2_MR: [480×1 double]
%            licor_CO2_std: [480×1 double]
%                licor_H2O: [480×1 double]
%             licor_H2O_MR: [480×1 double]
%            licor_H2O_std: [480×1 double]
%               licor_Pbox: [480×1 double]
%           licor_Pbox_std: [480×1 double]
%               licor_Tbox: [480×1 double]
%                 licor_qa: [480×1 double]
%             licor_qa_dry: [480×1 double]
%             licor_qa_std: [480×1 double]
%                 licor_rh: [480×1 double]
%           licor_rhoa_dry: [480×1 double]
%                      lon: [480×1 double]
%                    lon_s: [480×1 double]
%              lw_T_case_1: [480×1 double]
%              lw_T_case_2: [480×1 double]
%              lw_T_case_s: [480×1 double]
%              lw_T_dome_1: [480×1 double]
%              lw_T_dome_2: [480×1 double]
%              lw_T_dome_s: [480×1 double]
%                    lw_dn: [480×1 double]
%                  lw_dn_1: [480×1 double]
%                  lw_dn_2: [480×1 double]
%                lw_dn_clr: [480×1 double]
%                  lw_dn_s: [480×1 double]
%                lw_dn_std: [480×1 double]
%                   lw_net: [480×1 double]
%                 lw_net_s: [480×1 double]
%               lw_therm_1: [480×1 double]
%               lw_therm_2: [480×1 double]
%               lw_therm_s: [480×1 double]
%                    lw_up: [480×1 double]
%                  lw_up_s: [480×1 double]
%                   minute: [480×1 double]
%               missingSon: [480×1 double]
%                    month: [480×1 double]
%                     orgV: [480×1 double]
%                orgV_desp: [480×1 double]
%                       pa: [480×1 double]
%                     pa10: [480×1 double]
%                   pa10_s: [480×1 double]
%                     pa_s: [480×1 double]
%                   pa_wxt: [480×1 double]
%                   paccum: [480×1 double]
%                 paccum_s: [480×1 double]
%               paccum_wxt: [480×1 double]
%                    pitch: [480×1 double]
%                    prate: [480×1 double]
%               prate_orig: [480×1 double]
%                  prate_s: [480×1 double]
%                prate_wxt: [480×1 double]
%                   psea_s: [480×1 double]
%                psealevel: [480×1 double]
%     psealevel_reported_s: [480×1 double]
%              psealevel_s: [480×1 double]
%            psealevel_wxt: [480×1 double]
%                       qa: [480×1 double]
%                     qa10: [480×1 double]
%                    qa10N: [480×1 double]
%                  qa10N_s: [480×1 double]
%                   qa10_s: [480×1 double]
%                      qa2: [480×1 double]
%                     qa2N: [480×1 double]
%                   qa2N_s: [480×1 double]
%                    qa2_s: [480×1 double]
%                     qa_s: [480×1 double]
%                   qa_wxt: [480×1 double]
%                       qs: [480×1 double]
%                     qs_s: [480×1 double]
%                     qsnk: [480×1 double]
%                      qsr: [480×1 double]
%                  qsr_cov: [480×1 double]
%              qsr_cov_sds: [480×1 double]
%                   qsr_id: [480×1 double]
%                  qsr_ida: [480×1 double]
%                  qsr_idb: [480×1 double]
%                    qsr_s: [480×1 double]
%                     qvar: [480×1 double]
%                  rUn_wxt: [480×1 double]
%                  rUw_wxt: [480×1 double]
%                     rdir: [480×1 double]
%                 rdir_new: [480×1 double]
%                   rdir_s: [480×1 double]
%                 rdir_std: [480×1 double]
%               rdir_std_s: [480×1 double]
%                 rdir_wxt: [480×1 double]
%                       rh: [480×1 double]
%                     rh10: [480×1 double]
%                   rh10_s: [480×1 double]
%                      rh2: [480×1 double]
%                    rh2_s: [480×1 double]
%                     rh_s: [480×1 double]
%                   rh_wxt: [480×1 double]
%                     rhoa: [480×1 double]
%                   rhoa10: [480×1 double]
%                 rhoa10_s: [480×1 double]
%                   rhoa_s: [480×1 double]
%                 rhoa_wxt: [480×1 double]
%                 rhosea_s: [480×1 double]
%                     rspd: [480×1 double]
%                 rspd_new: [480×1 double]
%                rspd_new2: [480×1 double]
%                 rspd_raw: [480×1 double]
%                   rspd_s: [480×1 double]
%               rspd_std_s: [480×1 double]
%                 rspd_wxt: [480×1 double]
%                 shed_std: [480×1 double]
%               shed_std_s: [480×1 double]
%                      sog: [480×1 double]
%                     sogE: [480×1 double]
%                   sogE_s: [480×1 double]
%                     sogN: [480×1 double]
%                   sogN_s: [480×1 double]
%                  sog_max: [480×1 double]
%                sog_max_s: [480×1 double]
%                  sog_min: [480×1 double]
%                sog_min_s: [480×1 double]
%                    sog_s: [480×1 double]
%                  sog_std: [480×1 double]
%                sog_std_s: [480×1 double]
%                spd_log_s: [480×1 double]
%            spd_log_std_s: [480×1 double]
%              spd_log_u_s: [480×1 double]
%              spd_log_v_s: [480×1 double]
%                    sw_dn: [480×1 double]
%                  sw_dn_1: [480×1 double]
%                  sw_dn_2: [480×1 double]
%                sw_dn_clr: [480×1 double]
%                  sw_dn_s: [480×1 double]
%                sw_dn_std: [480×1 double]
%                   sw_net: [480×1 double]
%                 sw_net_s: [480×1 double]
%                    sw_up: [480×1 double]
%                  sw_up_s: [480×1 double]
%                        t: [480×1 double]
%                      tau: [480×1 double]
%                  tau_cov: [480×1 double]
%            tau_cov_cross: [480×1 double]
%                   tau_id: [480×1 double]
%                  tau_ida: [480×1 double]
%                  tau_idb: [480×1 double]
%                    tau_s: [480×1 double]
%                   theta0: [480×1 double]
%                  theta10: [480×1 double]
%                     tilt: [480×1 double]
%                      tsr: [480×1 double]
%                  tsr_cov: [480×1 double]
%              tsr_cov_sds: [480×1 double]
%                   tsr_id: [480×1 double]
%               tsr_id_son: [480×1 double]
%              tsr_id_sona: [480×1 double]
%              tsr_id_sonb: [480×1 double]
%                  tsr_ida: [480×1 double]
%                  tsr_idb: [480×1 double]
%                    tsr_s: [480×1 double]
%                  tsr_son: [480×1 double]
%                     ubar: [480×1 double]
%                       ug: [480×1 double]
%                      ugu: [480×1 double]
%                      ugw: [480×1 double]
%                uplat_std: [480×1 double]
%                      usr: [480×1 double]
%                  usr_cov: [480×1 double]
%                   usr_id: [480×1 double]
%                  usr_ida: [480×1 double]
%                  usr_idb: [480×1 double]
%                    usr_s: [480×1 double]
%                     uvar: [480×1 double]
%                     vbar: [480×1 double]
%                vplat_std: [480×1 double]
%                     vvar: [480×1 double]
%                 wCO2_cov: [480×1 double]
%                 wH2O_cov: [480×1 double]
%                   wT_cov: [480×1 double]
%               wT_cov_sds: [480×1 double]
%                wTson_cov: [480×1 double]
%                     wbar: [480×1 double]
%                  wc_frac: [480×1 double]
%                wc_frac_s: [480×1 double]
%                     wdir: [480×1 double]
%               wdir_max_s: [480×1 double]
%               wdir_min_s: [480×1 double]
%                 wdir_new: [480×1 double]
%                   wdir_s: [480×1 double]
%                 wdir_sfc: [480×1 double]
%               wdir_std_s: [480×1 double]
%                 wdir_wxt: [480×1 double]
%                wplat_std: [480×1 double]
%                   wq_cov: [480×1 double]
%               wq_cov_sds: [480×1 double]
%                       ws: [480×1 double]
%                     wspd: [480×1 double]
%               wspd_max_s: [480×1 double]
%               wspd_min_s: [480×1 double]
%                 wspd_new: [480×1 double]
%                   wspd_s: [480×1 double]
%                 wspd_sfc: [480×1 double]
%               wspd_std_s: [480×1 double]
%                 wspd_wxt: [480×1 double]
%                      wtv: [480×1 double]
%                   wu_cov: [480×1 double]
%                   wv_cov: [480×1 double]
%                     wvar: [480×1 double]
%                     year: [480×1 double]
%                     zeta: [480×1 double]
%                   zeta_s: [480×1 double]
%                       zo: [480×1 double]
%                     zo_s: [480×1 double]
%                      zoq: [480×1 double]
%                    zoq_s: [480×1 double]
%                      zot: [480×1 double]
%                    zot_s: [480×1 double]


%==========================================================================   


% day = 
% 
%   struct with 11 fields:
% 
%            jd: [20×1 double]
%             t: [20×1 double]
%          hnet: [20×1 double]
%           lat: [20×1 double]
%           lon: [20×1 double]
%         sw_dn: [20×1 double]
%     sw_dn_clr: [20×1 double]
%         lw_dn: [20×1 double]
%     lw_dn_clr: [20×1 double]
%         prate: [20×1 double]
%        paccum: [20×1 double]