disp('save_flux_10_FATIMA_2022');

%{
    Saves filtered 10-min output variable table flx10 as follows:
         Variable     Units   Description
    1     dnum         none    matlab datenum timestamp (decimal day)
    2     wspd         m/s     true wind speed
    3     wdir         deg     true wind direction
    4     rwspd        m/s     relative wind speed
    5     rwdir        deg     relative wind direction
    6     ta           degC    air temperature
    7     ta_wxt       degC    air temperature, WXT sensor
    8     rh           percent relative humidity
    9     rh_wxt       percent relative humidity, WXT sensor
    10    sst          degC    sea surface temperature
    11    sst_rosr     degC    radiometruc sea surface temperature
    12    Pmb          mb      sea level air pressure
    13    qs           g/kg    surface saturation specific humidity
    14    qa           g/kg    specific humidity
    15    rs           W/m2    downwelling solar radiative flux
    16    rl           W/m2    downwelling IR flux
    17    rain         mm/hr    rain rate
    18    hsc          W/m2    turbulent sensible heat flux
    19    hsib         W/m2    inertial dissipation sensible heat flux
    20    hsb          W/m2    bulk sensible heat flux
    21    hlc          W/m2    turbulent latent heat flux
    22    hlib         W/m2    inertial dissipation latent heat flux
    23    hlb          W/m2    bulk latent heat flux
    24    tauc         N/m2    turbulent streamwise wind stress
    25    tauxc        N/m2    turbulent cross-stream wind stress
    26    tauib        N/m2    inertial dissipation wind stress
    27    taub         N/m2    bulk wind stress
    28    ct           none    Ct structure func parameter
    29    cq           none    Cq structure func parameter
    30    cu           none    Cu structure func parameter
    31    cw           none    Cw structure func parameter
    32    jplume       none    exhaust plume filter flag
    33    jmanuv       none    ship maneuver filter flag
    34    tilt         deg     streamline tilt in wind at location of sonic anemometer
    35    RF           W/m2    rain heat flux
    36    hlwebb       W/m2    latent heat 'Webb' correction
    37    rlclr        W/m2    clear sky IR downwelling radiative flux model
    38    rsclr        W/m2    clear sky solar downwelling radiative flux model
    39    u10n         m/s     10m neutral wind speed
    40    t10n         degC    10m neutral air temperature
    41    q10n         g/kg    10m neutral specific humidity
    42    usid_flag    none    quality flag for ID ustar
    43    qsid_flag    none    quality flag for ID qstar
    44    tsid_flag    none    quality flag for ID tstar
    45    zu           m       wind measurement height
    46    zt           m       air temperature measurement height
    47    zq           m       humidity measurement height
    48    hed          deg     ship heading
    49    sog          m/s     ship speed over ground from gps
    50    cog          deg     ship course over ground from gps
    51    lat          degN    latitude
    52    lon          degE    longitude
    53    wspd_ship    m/s     true wind speed from ship sensor
    54    wdir_ship    deg     true wind direction from ship sensor
    55    ta_ship      degC    air temperature from ship sensor
    56    rh_ship      percent relative humidity from ship sensor
    57    sst_ship     degC    sea surface temperature from ship sensor
    58    Pmb_ship     mb      sea level air pressure from ship sensor
    59    qs_ship      g/kg    surface saturation specific humidity from ship sensors
    60    qa_ship      g/kg    specific humidity from ship sensor
    61    rs_ship      W/m2    downwelling solar radiative flux from ship sensor
    62    rl_ship      W/m2    downwelling IR flux from ship sensor
    63    q_lic        g/kg    specific humidity from Licor 7500
    64    q_lic_std    g/kg    std dev specific humidity from Licor 7500
    65    co2_lic      ppm     co2 ppm from Licor 7500
    66    co2_lic_std  ppm     std dev co2 ppm from Licor 7500
%}
fclose all;
clear flx10;

%% initialize
path_f10_vars = fullfile(path_prog,'f10_vars.xlsx');
f10_params = readcell(path_f10_vars); % load variable names, units and descriptions
f10_vars = f10_params(:,2);
f10_units = f10_params(:,3);
f10_descript = f10_params(:,4);

% prepare empty table for output
% names, units and descriptions are read in from an excel spreadsheet
% to add variables or adjust metadata, edit the excel sheet
npz = height(da);       % number of rows of data
nnn = length(f10_vars); % number of columns of output
flx10 = array2table(NaN(npz,nnn),'VariableNames',f10_vars);
flx10.Properties.VariableUnits = f10_units;
flx10.Properties.VariableDescriptions = f10_descript;

%% prepare a few of the output variables
reldir2 = rwdir_best;
reldir2(reldir2<0) = reldir2(reldir2<0)+360;
% define good ID data flags
filt_u = isfinite(usib);              % wind
filt_q = isfinite(qsib_lic);          % Licor
filt_t = isfinite(tsib);              % sonic T
taucx = -(da.rhoa_psl.*da.wv_csp);    % x-stream stress
tauib = (da.rhoa_psl.*usib.^2);       % ID stress

% copy variables into table
flx10.dnum = da.dnum;
flx10.wspd = wspd_best;
flx10.wdir = wdir_best;
flx10.rwspd = da.rwspd_psl;
flx10.rwdir = rwdir_best;
flx10.ta = ta_best;
flx10.ta_wxt = da.ta_wxt;
flx10.rh = rh_best;
flx10.rh_wxt = da.rh_wxt;
flx10.sst = ts_best;
flx10.sst_rosr = da.sst_rosr;
flx10.Pmb = P_best;
flx10.qs = qs_best;
flx10.qa = qa_best;
flx10.rs = rs_best;
flx10.rl = rl_best;
flx10.rain = rain_best;
flx10.hsc = hsc;
flx10.hsib = hsib;
flx10.hsb = da.hsb_psl;
flx10.hlc = hlc_lic;
flx10.hlib = hlib_lic;
flx10.hlb = da.hlb_psl;
flx10.tauc = tauc;
flx10.taucx = taucx;
flx10.tauib = tauib;
flx10.taub = da.taub_psl;
flx10.ct = ct;
flx10.cq = cql;
flx10.cu = cu;
flx10.cw = cw;
flx10.jplume = jplume;
flx10.jmanuv = jmanuv;
flx10.tilt = da.tilt;
flx10.RF = da.RF_psl;
flx10.hlwebb = da.hlwebb_psl;
flx10.rlclr = rlclr;
flx10.rsclr = rsclr;
flx10.u10n = c35.U10N;
flx10.t10n = c35.T10N;
flx10.q10n = c35.Q10N;
flx10.usid_flag = filt_u;
flx10.qsid_flag = filt_q;
flx10.tsid_flag = filt_t;
flx10.zu = da.zu_psl;
flx10.zt = da.zt_psl;
flx10.zq = da.zq_psl;
flx10.hed = hed_best;
flx10.sog = sog_best;
flx10.cog = cog_best;
flx10.lat = da.lat_psl;
flx10.lon = da.lon_psl;
flx10.wspd_ship = da.wspd_ship;
flx10.wdir_ship = da.wdir_ship;
flx10.ta_ship = da.ta_ship;
flx10.rh_ship = da.rh_ship;
flx10.sst_ship = da.sst_ship;
flx10.Pmb_ship = da.Pmb_ship;
flx10.qs_ship = da.qs_ship;
flx10.qa_ship = da.qa_ship;
flx10.rs_ship = da.rs_ship;
flx10.rl_ship = da.rl_ship;
flx10.q_lic = da.q_lic;
flx10.q_lic_std = sqrt(da.qvar);
flx10.co2_lic = da.co2_lic_good;
flx10.co2_lic_std = da.co2_std_good;

% save in .mat format
dfl = fullfile(da_path,['FATIMA_2022_flux_10_',dcname,'.mat']);
save(dfl,'flx10');

%% save output in ascii format with variable name and unit header lines
% format header strings
hdr1 = 'FATIMA 10-min flux Rev 1, July, 2022, BWB\n';
hdr2 = ''; hdr3 = '';
for ii = 1:length(f10_vars)
    n = f10_vars{ii}; ln = length(n); padn = repmat(' ',1,21-ln);
    hdr2 = [hdr2,n,padn];
    u = f10_units{ii}; lu = length(u); padu = repmat(' ',1,21-lu);
    hdr3 = [hdr3,u,padu];
end
hdr2 = [hdr2,'\n']; hdr3 = [hdr3,'\n'];

temp = table2array(flx10);
% save results to ascii format with header lines
fout = fopen(fullfile(da_path,['FATIMA_2022_flux_10_',dcname,'.txt']),'w');
fprintf(fout,hdr1);
fprintf(fout,hdr2);
fprintf(fout,hdr3);
fprintf(fout,[repmat('%20.10e ',1,nnn),' \n'],temp');
fclose all;
