% b5_plotlags_8192_metcity.m

% this script just plots results from b3_analyze_lag_times_metcity_8192.m
% saves plots in lag_analysis subfolder
% assumes b3_analyze_lag_times_metcity_8192.m has run to completion and
% workspace is populated with all variables

% load the excel spreadsheet with lagtable flags prior to running script
% the lagtable flags are determined by manual examination of the hourly
% lag correlation plots and indicate which hours have a likely significant
% correlation between W and gas concentration.

ll = find(mc_flx.datenum<datenum(2020,10,1));
dt = datetime(mc_flx.datenum(ll),'convertfrom','datenum');
savefigs = 1;
edges1 = 0:0.1:5;   % histogram bin edges
edges2 = 0:0.2:10;

clear h9 h10 ah11 dh12 h13 h14 h15 h16 h17

% q lags
q_lag = mc_flx.q_pic_lag(ll);
lagflagq = lagtable.q_lag_good(ll);
jj = lagflagq>1 | lagflagq<0;
q_lag(jj) = q_lag(jj) + lagflagq(jj);

kk = lagflagq~=0; % select hours with a real XCorr peak
h1 = figure('Name','Dq_lag_ts');
plot(dt(kk),q_lag(kk)/10-mc_flx.puff_lag_co2(kk)/10,'.','markersize',8); grid on; grid minor;
xlim([dt(1),dt(end)]);
ax=gca; set(ax,'fontsize',14);
ylabel('lag seconds'); xlabel('date/time');
title('water vapor \Deltalag (sec)');

h2 = figure('Name','q_lag_v_puff_lag');
plot([0,150],[0,150],'k-',mc_flx.puff_lag_co2(kk),q_lag(kk),'.','markersize',10);
xlim([0,150]); ylim([0,150]); grid on; grid minor; ax=gca; set(ax,'fontsize',14);
xlabel('puff response lag points'); ylabel('optimal q lag points');
title('q lag points vs. puff lag points');

h3 = figure('Name','Dq_lag_hist');
histogram(q_lag(kk)/10-mc_flx.puff_lag_co2(kk)/10, edges2); grid on; grid minor;
xlabel('\Deltaq lag, seconds'); ax=gca; set(ax,'fontsize',14);
title('Distribution of \Deltaq lag (beyond puff lag) sec');

h4 = figure('Name','Dq_v_tair');
plot(mc_flx.temp_10m(kk),q_lag(kk)/10-mc_flx.puff_lag_co2(kk)/10,'.','markersize',10);
grid on; grid minor; xlabel('T_{air}, °C'); ylabel('\Deltaq lag, sec');
title('\Deltaq lag vs. air temperature');

h5 = figure('Name','Dq_v_rh');
plot(mc_flx.rh_10m(kk),q_lag(kk)/10-mc_flx.puff_lag_co2(kk)/10,'.','markersize',10);
grid on; grid minor; xlabel('RH, %'); ylabel('\Deltaq lag, sec');
title('\Deltaq lag vs. RH');

% fit Delta lag v tau for q to get default value for Delta lag when xcorr is not good
fitx = [1,4]; fity = [3.2,8];
h6 = figure('Name','Dq_v_tau');
plot(fitx,fity,'r-','linewidth',2); hold on;
plot(mc_flx.q_pic_tau(kk),q_lag(kk)/10-mc_flx.puff_lag_co2(kk)/10,'b.','markersize',10); hold off;
grid on; grid minor; xlabel('\tau O_3, sec'); ylabel('\Deltaq lag, sec');
title('\Deltaq lag vs. low-pass time const. \tau');

q = qair_p([mc_flx.temp_10m,mc_flx.rh_10m,mc_flx.atmos_pressure_2m]);  % use mc_flx.q_pic when available
h7 = figure('Name','Dq_v_q');
plot(q(kk),q_lag(kk)/10-mc_flx.puff_lag_co2(kk)/10,'.','markersize',10);
grid on; grid minor; xlabel('q, g/kg'); ylabel('\Deltaq lag, sec');
title('\Deltaq lag vs. q');

% co2 lags
co2_lag = mc_flx.co2_lag(ll);
lagflagc = lagtable.co2_lag_good(ll);
jj = lagflagc>1 | lagflagc<0;
co2_lag(jj) = co2_lag(jj) + lagflagc(jj);

mm = lagflagc~=0;
h8 = figure('Name','Dco2_lag_ts');
plot(dt(mm),co2_lag(mm)/10-mc_flx.puff_lag_co2(mm)/10,'.','markersize',8); grid on; grid minor;
xlim([dt(1),dt(end)]);
ax=gca; set(ax,'fontsize',14);
ylabel('\Deltalag seconds'); xlabel('date/time');
title('CO_2 \Deltalag (sec)');

h9 = figure('Name','Dco2_lag_hist');
histogram(co2_lag(mm)/10-mc_flx.puff_lag_co2(mm)/10,edges1); grid on; grid minor;
xlabel('\DeltaCO_2 lag, seconds'); ax=gca; set(ax,'fontsize',14);
title('Distribution of \DeltaCO_2 lag (beyond puff lag) sec');

h10 = figure('Name','co2_lag_v_puff_lag');
plot([0,150],[0,150],'k-',mc_flx.puff_lag_co2(mm),co2_lag(mm),'.','markersize',10);
xlim([0,150]); ylim([0,150]); grid on; grid minor; ax=gca; set(ax,'fontsize',14);
xlabel('puff response lag points'); ylabel('optimal co2/ch4 lag points');
title('CO_2 lag points vs. puff lag points');

h11 = figure('Name','co2_puff_lag_ts');
plot(dt,mc_flx.puff_lag_co2(ll)/10,'.','markersize',8);
grid on; grid minor; ax=gca; set(ax,'fontsize',14);
ylabel('lag seconds'); xlim([dt(1),dt(end)]); xlabel('date/time');
title('Picarro raw puff lag seconds');


%% save all figures if desired

if savefigs
    disp('saving figures')
    figs = findall(0,'type','figure');
    sorted_figs = sortfighandlearray(figs); % put in ascending order
    N = length(sorted_figs);
    fig_path = '/Users/byronb/Raw_Data_Files.noindex/MOSAIC_2019/_processed/metcity_flux_v1/flux_8192/plots/lag_analysis';
    mkdir(fig_path);
    if N>0
        for ii=1:N
            fname = sorted_figs(ii).Name;
            figure(sorted_figs(ii));
            ppath = fullfile(fig_path,['fig',sprintf('%02i',ii),'_',fname,'.fig']);
            savefig(ppath);
            ppath = fullfile(fig_path,['fig',sprintf('%02i',ii),'_',fname,'.png']);
            print('-dpng',ppath);
        end
    end
end
