% b0_concat_mc_met.m

% concatenates met city tower data from daily 1-min nc mosmet files
% into matlab table


%% 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','1min_met_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,'mosmet.metcity.level2.4.1min.*.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);
    % compute timestamp as datenum
    dnum = datenum(1970,1,1) + double(temp.base_time)/86400 + temp.time/86400;
    temp = rmfield(temp,{'base_time','time_offset','time'});
    temp = setfield(temp,'datenum',dnum); % add datenum field to end of temp
    % reorder fields so datenum is first
    N = length(fieldnames(temp));
    temp = orderfields(temp,[N,1:N-1]); % move datenum to first field in temp
    % convert singles into doubles
    temp = structfun(@double, temp, 'uniformoutput', 0);
    % convert structure to table
    data = struct2table(temp);
    % concat tables
    if ii==1
        mc_met = data;
    else
        mc_met = cat_tables(mc_met,data);
    end
end
clear temp data fdir fin dnum

% replace -9999 values with NaN
vars = mc_met.Properties.VariableNames;
mc_met = standardizeMissing(mc_met,{-9999},'DataVariables',vars);

% save output table
sfl = fullfile(save_path,'metcity_1min_met_all.mat');
save(sfl,'mc_met');

disp('DONE')

