% a18_concat_metcity_OOT_results.m
% concatenates OOT flux and met parameters into tables

%% initialize
clear
close all
warning('off','MATLAB:MKDIR:DirectoryExists');

%% paths
root_data_path = '/Users/byronb/Raw_Data_Files.noindex/MOSAIC_2019';
path_prog = fullfile(root_data_path,'_matlab');
path_oot = fullfile(root_data_path,'_processed','metcity_flux_v3','flux_merged_OOT_v2');

%% set matlab script path
cd(path_prog);
addpath(genpath(path_prog));
cd(fullfile(path_prog,'co2_ch4_o3_flux'));

%% loop through daily oot output folders and concat output cell arrays

fldrs = dir(fullfile(path_oot,'data_out','20*')); % list of all output folders '20YY_MM_DD'

flx_dvec = 1:6; % oot cell columns with date/time
param_dat = 7:22; % oot cell columns with met data
flx_dat = [7:10,15:30]; % oor cell columns with flux result data

oot_mat = []; % init output table (will be N x 37)
vars = {'dnum','u','std_u','w','std_w','wdir','wdir_range_left','wdir_range_right',...
        'std_v','wv_cov','ustar','q_v','Tair','c_d0','rhoa_d','Tair_potv','z_L',...
        'Hs_OO','Fco2_OO','Fch4_OO','tau_OO','Hs_fit_err_pct','Fco2_fit_err_pct',...
        'Fch4_fit_err_pct','tau_fit_err_pct','Hs','Fco2','Fch4','tau','Hs_std',...
        'Fco2_std','Fch4_std','tau_std','Hs_2_98th','Fco2_2_98th','Fch4_2_98th',...
        'tau_2_98th'};

for yyyy = 2019:2020
    YYYY = sprintf('%4i',yyyy);
    for mm = 1:12
        MM = sprintf('%02i',mm);
        for dd = 1:31
            DD = sprintf('%02i',dd);
            DATE = [YYYY,'_',MM,'_',DD];
            % look for a folder with this date
            for ii = 1:length(fldrs)
                jj = strcmp(fldrs(ii).name,DATE);
                if jj; break; end
            end
            % go to next day if no folder is found
            if ~jj; continue; end
            disp(['processing ',DATE])

            % load FLUX and PARAMETERS cell arrays from RESULTS folder
            res_files = dir(fullfile(fldrs(ii).folder,fldrs(ii).name,'**','RESULTS'));
            try
                load(fullfile(res_files(1).folder,'FLUXES.mat')); % loads 'FLUXES_CELL'
                load(fullfile(res_files(1).folder,'PARAMETERS.mat')); % loads 'PARAMETERS_CELL'
            catch
                disp('no results')
            end
            % concat selected results to oot array
            dn = datenum(cell2mat(FLUXES_CELL(2:end,flx_dvec)));
            param = cell2mat(PARAMETERS_CELL(2:end,param_dat));
            flx = cell2mat(FLUXES_CELL(2:end,flx_dat));
            oot_mat = [oot_mat;[dn,param,flx]];
        end
    end
end

% convert wdir to degrees
oot_mat(:,6:8) = oot_mat(:,6:8)*180/pi;

% remove all flux results for periods when Tair > 10.
% an OOT bug gives nonsense results for parameters and fluxes.
% looks like sign on Tair is dropped and other errors appear.
ii = find(oot_mat(:,13)>10); % look for Tair > 10°C
oot_mat(ii,13:37) = NaN;

oot = array2table(oot_mat,'VariableNames',vars);
save(fullfile(path_oot,'flx_metcity_oot.mat'),"oot");

disp('DONE')