% b0_concat_mc_flux_10min.m
% concatenates 10-min met city tower flux data from nc files into matlab table

% generates the 'metcity_10min_flux_all.mat' file used in e_mc_bulk_model_run_v2.m

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

root_data_path = '/Users/byronb/Raw_Data_Files.noindex/MOSAIC_2019';

path(pathdef);
path_prog = fullfile(root_data_path,'_matlab');
cd(fullfile(path_prog,'co2_ch4_o3_flux'));
addpath(genpath(path_prog));

met_path = fullfile(root_data_path,'_processed','metcity_tower_level2_v4','10min_flux_L2V4');
save_path = fullfile(root_data_path,'_processed','metcity_tower_level2_v4');

%% read and concat nc files into a single table then save to .mat
fdir = dir(fullfile(met_path,'mosseb.metcity.level2.4.10min.*.nc'));
Nfiles = length(fdir);

for ii = 1:Nfiles
    display(['File ',fdir(ii).name])
    fin = fullfile(met_path,fdir(ii).name);
    % read nc file into structure
    temp = ncstruct(fin);
    % remove spectra fields
    fvars = fieldnames(temp);
    temp = rmfield(temp,fvars(346:394));
    % remove QC int32 variables
%     temp = rmfield(temp,fvars(95:195));
    % convert i32s into doubles
    temp = structfun(@double, temp, 'uniformoutput', 0);
    % compute timestamp as datenum
    dnum = datenum(1970,1,1) + double(temp.base_time)/86400 + double(temp.time)/86400;
    temp = rmfield(temp,{'base_time','time_offset','time'});
    temp = setfield(temp,'datenum',dnum); % add datenum field to end of temp
    N = length(fieldnames(temp));
    temp = orderfields(temp,[N,1:N-1]); % move datenum to first field in temp

    % convert structure to table
    data = struct2table(temp);
    % concat tables
    if ii==1
        mc_flux = data;
    else
        mc_flux = cat_tables(mc_flux,data);
    end
end

clear temp data fdir fin dnum


vars = mc_flux.Properties.VariableNames;
% replace -9999 values with NaN
mc_flux = standardizeMissing(mc_flux,{-9999},'DataVariables',vars);
% remove spurious values of mast temperature warmer than -1°C
ii = mc_flux.temp_mast > -1;
mc_flux.temp_mast(ii) = NaN;
dt = datetime(mc_flux.datenum,'convertfrom','datenum');

% save output table
sfl = fullfile(save_path,'metcity_10min_flux_all.mat');
save(sfl,'mc_flux');

%% average to hourly timebase
dnum_start = datenum(2019,10,26,00,00,00);
dnum_end = datenum(2020,10,2,00,00,00);
bins = dnum_start:1/24:dnum_end; bins = bins(:);

% average everything
mc_tower_hr = interval_avg(mc_flux{:,1},mc_flux{:,2:end},bins);
dt_hr = datetime(mc_tower_hr(:,1),'ConvertFrom','datenum');

% fix angles averaging for compass directions
rho_default = ones(height(mc_flux),1);
% tower heading
[~,temp,~] = interval_avg_vectors(mc_flux{:,1},mc_flux{:,4},rho_default,bins);
mc_tower_hr(:,4) = temp;
% mast heading
[~,temp,~] = interval_avg_vectors(mc_flux{:,1},mc_flux{:,5},rho_default,bins);
mc_tower_hr(:,5) = temp;
% azimuth
[~,temp,~] = interval_avg_vectors(mc_flux{:,1},mc_flux{:,10},rho_default,bins);
mc_tower_hr(:,10) = temp;
% ship bearing
[~,temp,~] = interval_avg_vectors(mc_flux{:,1},mc_flux{:,11},rho_default,bins);
mc_tower_hr(:,11) = temp;
% 2m vector mean wspd/wdir
[~,tempdir,tempspd] = interval_avg_vectors(mc_flux{:,1},mc_flux{:,48},mc_flux{:,44},bins);
mc_tower_hr(:,44) = tempspd; mc_tower_hr(:,48) = tempdir;
% 6m vector mean wspd/wdir
[~,tempdir,tempspd] = interval_avg_vectors(mc_flux{:,1},mc_flux{:,49},mc_flux{:,45},bins);
mc_tower_hr(:,45) = tempspd; mc_tower_hr(:,49) = tempdir;
% 10m vector mean wspd/wdir
[~,tempdir,tempspd] = interval_avg_vectors(mc_flux{:,1},mc_flux{:,50},mc_flux{:,46},bins);
mc_tower_hr(:,46) = tempspd; mc_tower_hr(:,50) = tempdir;
% mast vector mean wspd/wdir
[~,tempdir,tempspd] = interval_avg_vectors(mc_flux{:,1},mc_flux{:,51},mc_flux{:,47},bins);
mc_tower_hr(:,47) = tempspd; mc_tower_hr(:,51) = tempdir;

% figure; plot(dt_hr,mc_tower_hr(:,48),'o'); hold on;
% plot(dt,mc_flux.wdir_vec_mean_2m,'.','MarkerSize',8); hold off;
% ylabel('2m wdir'); ylim([0,360]); grid on; grid minor;

mc_tower_hr = array2table(mc_tower_hr,'VariableNames',vars);
sfl = fullfile(save_path,'metcity_hr_flux_all.mat');
save(sfl,'mc_tower_hr');

