% co2_ch4_ship_1min_V1.m
% concatenate 10 sec filtered ship co2/co4 data and average to 1-min table

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

dnum0 = datenum(2019,10,26);  % start datenum for timeseries
dnum1 = datenum(2020,9,30);   % end datenum for timeseries
bins = dnum0:1/1440:dnum1; bins = bins(:);    % bins for averaging

save_fig = true;    % set to save daily plot

root_data_path = '/Users/byronb/Raw_Data_Files.noindex/MOSAIC_2019';
path_prog = fullfile(root_data_path,'_matlab');
cd(fullfile(path_prog,'co2_ch4_o3_flux'));
addpath(genpath(path_prog));
rehash toolboxcache;

data_path = fullfile(root_data_path,'_processed','co2_ch4_ship_V1','data_10sec');
save_path = fullfile(root_data_path,'_processed','co2_ch4_ship_V1','data_1min');
mkdir(save_path);

%% load and concat all 10sec result files

flist = dir(fullfile(data_path,'*.mat'));
temp = NaN(length(bins),8);
for ii = 1:length(flist)
    dfl = fullfile(data_path,flist(ii).name);
    disp(['loading ',flist(ii).name])
    load(dfl); % loads pic_ship_10sec
    dnum = datenum(pic_ship_10sec.yr(1),pic_ship_10sec.mon(1),pic_ship_10sec.day(1));
    bins = dnum:1/1440:dnum+1; bins = bins(:);
    A = table2array(pic_ship_10sec);
    temp2 = interval_avg(A(:,1),A(:,8:14),bins);
    xx = find(isnan(temp(:,1)),1,'first');
    temp(xx:xx+length(temp2)-1,:) = temp2;
end

clear pic_ship_10sec A temp2 bins dnum

% remove any NaNs at the end of the array
xx = find(isnan(temp(:,1)),1,'first');
if ~isempty(xx)
    temp = temp(1:xx-1,:);
end

%% build table and save

disp('saving table')
[Y,M,D,H,MN,S] = datevec(temp(:,1));
pic_ship_1min = table;
pic_ship_1min.datenum = temp(:,1);
pic_ship_1min.yr = Y;
pic_ship_1min.mon = M;
pic_ship_1min.day = D;
pic_ship_1min.hr = H;
pic_ship_1min.min = MN;
pic_ship_1min.sec = S;
pic_ship_1min.Pc_torr = temp(:,2);
pic_ship_1min.Tc_C = temp(:,3);
pic_ship_1min.ch4w_ppm = temp(:,4);
pic_ship_1min.ch4d_ppm = temp(:,5);
pic_ship_1min.co2w_ppm = temp(:,6);
pic_ship_1min.co2d_ppm = temp(:,7);
pic_ship_1min.h2o_pct = temp(:,8);

sfl = fullfile(save_path,'co2_ch4_ship_1min_legs1-5.mat');
save(sfl,'pic_ship_1min');

%% save as ascii file

disp('saving ascii')
sfl = fullfile(save_path,['co2_ch4_ship_1min_legs1-5.txt']);
A = table2array(pic_ship_1min); A = A';
vars = pic_ship_1min.Properties.VariableNames;

fid = fopen(sfl,'w');
fprintf(fid,'%s\n','MOSAiC 1-min CO2/CH4, ship bow tower, Picarro CRDS 2311-f');
fprintf(fid,'%s\n','version 1.0, Feb 2021, B. Blomquist, CU/CIRES');
for qq = 1:length(vars)-1
    fprintf(fid,'%-20s',cell2mat(vars(qq)));
end
fprintf(fid,'%-20s\n',cell2mat(vars(length(vars))));

fmtstr = ['%-20.12e',repmat('%-20i',1,6),repmat('%-20.8e',1,6),'%-20.8e\n'];
fprintf(fid,fmtstr,A(:,1:end));
fclose(fid);

%% plot

dt = datetime(pic_ship_1min.datenum,'convertfrom','datenum');
dt0 = datetime(pic_ship_1min.datenum(1)-7,'convertfrom','datenum');
dt1 = datetime(pic_ship_1min.datenum(end)+7,'convertfrom','datenum');
figure('position',[500,500,1000,800],'Name','co2_ch4_10sec'); % create a figure window for plots
subplot(2,1,1)
    plot(dt,pic_ship_1min.co2d_ppm,'.','markersize',4); grid minor;
    ylabel('CO_2 ppm','fontsize',12); xlabel('date / time','fontsize',12);
    xlim([dt0,dt1]);
    title('MOSAiC Ship Bow Tower CO_2 & CH_4, Legs 1–5','fontsize',18);
subplot(2,1,2)
    plot(dt,pic_ship_1min.ch4d_ppm,'.','markersize',4); grid minor;
    ylabel('CH_4 ppm','fontsize',12); xlabel('date / time','fontsize',12);
    xlim([dt0,dt1]);
if save_fig
    ppath = fullfile(save_path,['MOSAiC_Ship_CO2_CH4_Legs1-5.png']);
    print('-dpng',ppath);
end
