% b7_stationarity_distributions_metcity.m


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

savefigs = 1;

%% paths
root_data_path = '/Users/byronb/Raw_Data_Files.noindex/MOSAIC_2019';
path_prog = fullfile(root_data_path,'_matlab');
save_path = fullfile(root_data_path,'_processed','metcity_flux_v2','flux_8192');
plot_path = fullfile(root_data_path,'_processed','metcity_flux_v2','flux_8192','plots','stationarity');
mkdir(plot_path);

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

%% load data

%13-min flux table 'mc_flx' computed in b5_compute_fluxes_metcity_8192.m
load(fullfile(save_path,'wins_met_gases_lags_flux_8192_all.mat'));

%% normal distribution of dco2_dt

data = mc_flx.dco2_dt;
ii = abs(data)<2;
edges = -2:0.05:2;
bins = -2+0.025:0.05:2-0.025;
hist = histcounts(data,edges);
Nhist = hist/sum(hist);
pd = fitdist(data(ii),'Normal');
dist = pdf(pd,bins);
Ndist = dist/sum(dist);
figure('Position',[300,300,600,400],'Name','dco2_dt_Ndist');
plot(bins,Nhist,'r.','MarkerSize',10); hold on;
plot(bins,Ndist,'b-','LineWidth',1); hold off; grid on; grid minor;
xlabel('dco2_dt, ppm/hr, Normal Dist.'); ylabel('Norm PDF');
dim = [.2 .5 .3 .3];
str = ['std dev = ',sprintf('%4.2f',std(pd))];
annotation('textbox',dim,'String',str,'FitBoxToText','on','FontSize',14);

%% normal distribution of uco2_mM

data = mc_flx.uco2_mM;
ii = abs(data)<50;
edges = -50:1:50;
bins = -50+0.5:1:50-0.5;
hist = histcounts(data,edges);
Nhist = hist/sum(hist);
pd = fitdist(data(ii),'Normal');
dist = pdf(pd,bins);
Ndist = dist/sum(dist);
figure('Position',[300,300,600,400],'Name','uco2_Ndist');
plot(bins,Nhist,'r.','MarkerSize',10); hold on;
plot(bins,Ndist,'b-','LineWidth',1); hold off; grid on; grid minor;
xlabel('u''co2'', mM/m2/d, Normal Dist.'); ylabel('Norm PDF');
dim = [.2 .5 .3 .3];
str = ['std dev = ',sprintf('%4.2f',std(pd))];
annotation('textbox',dim,'String',str,'FitBoxToText','on','FontSize',14);

%% normal distribution of vco2_mM

data = mc_flx.vco2_mM;
ii = abs(data)<50;
edges = -50:1:50;
bins = -50+0.5:1:50-0.5;
hist = histcounts(data,edges);
Nhist = hist/sum(hist);
pd = fitdist(data(ii),'Normal');
dist = pdf(pd,bins);
Ndist = dist/sum(dist);
figure('Position',[300,300,600,400],'Name','vco2_Ndist');
plot(bins,Nhist,'r.','MarkerSize',10); hold on;
plot(bins,Ndist,'b-','LineWidth',1); hold off; grid on; grid minor;
xlabel('v''co2'', mM/m2/d, Normal Dist.'); ylabel('Norm PDF');
dim = [.2 .5 .3 .3];
str = ['std dev = ',sprintf('%4.2f',std(pd))];
annotation('textbox',dim,'String',str,'FitBoxToText','on','FontSize',14);

%% THE NORMAL DISTRIBUTION DOESN'T DESCRIBE THE DISTRIBUTION VERY WELL
% SO WE WILL USE WEIBULL

%% weibull distribution of dco2_dt

data = abs(mc_flx.dco2_dt);
ii = data<2;
edges = 0:0.05:2;
bins = 0.025:0.05:2-0.025;
hist = histcounts(data,edges);
Nhist = hist/sum(hist);
pd = fitdist(data(ii),'Weibull');
dist = pdf(pd,bins);
cdist = cdf(pd,bins);
lim = bins(find(cdist>=0.99,1,'first'));
Ndist = dist/sum(dist);
figure('Position',[300,300,600,400],'Name','dco2_dt_WeiDist');
plot(bins,Nhist,'r.','MarkerSize',10); hold on;
plot(bins,Ndist,'b-','LineWidth',1); hold off; grid on; grid minor;
xlabel('dco2/dt, ppm/hr, Weibull Dist.'); ylabel('Norm PDF');
dim = [.3 .5 .3 .3];
str = ['99% limit = ',sprintf('%4.2f',lim)];
annotation('textbox',dim,'String',str,'FitBoxToText','on','FontSize',14);


%% weibull distribution of uco2_mM

data = abs(mc_flx.uco2_mM);
ii = data<50;
edges = 0:1:50;
bins = 0+0.5:1:50-0.5;
hist = histcounts(data,edges);
Nhist = hist/sum(hist);
pd = fitdist(data(ii),'Weibull');
dist = pdf(pd,bins);
cdist = cdf(pd,bins);
lim = bins(find(cdist>=0.99,1,'first'));
Ndist = dist/sum(dist);
figure('Position',[300,300,600,400],'Name','uco2_WeiDist');
plot(bins,Nhist,'r.','MarkerSize',10); hold on;
plot(bins,Ndist,'b-','LineWidth',1); hold off; grid on; grid minor;
xlabel('u''co2'', mM/m2/d, Weibull Dist.'); ylabel('Norm PDF');
dim = [.3 .5 .3 .3];
str = ['99% limit = ',sprintf('%4.2f',lim)];
annotation('textbox',dim,'String',str,'FitBoxToText','on','FontSize',14);

%% weibull distribution of vco2_mM

data = abs(mc_flx.vco2_mM);
ii = data<50;
edges = 0:1:50;
bins = 0+0.5:1:50-0.5;
hist = histcounts(data,edges);
Nhist = hist/sum(hist);
pd = fitdist(data(ii),'Weibull');
dist = pdf(pd,bins);
cdist = cdf(pd,bins);
lim = bins(find(cdist>=0.99,1,'first'));
Ndist = dist/sum(dist);
figure('Position',[300,300,600,400],'Name','vco2_WeiDist');
plot(bins,Nhist,'r.','MarkerSize',10); hold on;
plot(bins,Ndist,'b-','LineWidth',1); hold off; grid on; grid minor;
xlabel('v''co2'', mM/m2/d, Weibull Dist.'); ylabel('Norm PDF');
dim = [.3 .5 .3 .3];
str = ['99% limit = ',sprintf('%4.2f',lim)];
annotation('textbox',dim,'String',str,'FitBoxToText','on','FontSize',14);

%% weibull distribution of dch4_dt

data = abs(mc_flx.dch4_dt);
ii = data<10;
edges = 0:0.1:10;
bins = 0.05:0.1:10-0.05;
hist = histcounts(data,edges);
Nhist = hist/sum(hist);
pd = fitdist(data(ii),'Weibull');
dist = pdf(pd,bins);
cdist = cdf(pd,bins);
lim = bins(find(cdist>=0.99,1,'first'));
Ndist = dist/sum(dist);
figure('Position',[300,300,600,400],'Name','dch4_dt_WeiDist');
plot(bins,Nhist,'r.','MarkerSize',10); hold on;
plot(bins,Ndist,'b-','LineWidth',1); hold off; grid on; grid minor;
xlabel('dch4/dt, ppb/hr, Weibull Dist.'); ylabel('Norm PDF');
dim = [.3 .5 .3 .3];
str = ['99% limit = ',sprintf('%4.2f',lim)];
annotation('textbox',dim,'String',str,'FitBoxToText','on','FontSize',14);


%% weibull distribution of uch4_uM

data = abs(mc_flx.uch4_uM);
ii = data<250;
edges = 0:2:250;
bins = 0+1:2:250-1;
hist = histcounts(data,edges);
Nhist = hist/sum(hist);
pd = fitdist(data(ii),'Weibull');
dist = pdf(pd,bins);
cdist = cdf(pd,bins);
lim = bins(find(cdist>=0.99,1,'first'));
Ndist = dist/sum(dist);
figure('Position',[300,300,600,400],'Name','uch4_WeiDist');
plot(bins,Nhist,'r.','MarkerSize',10); hold on;
plot(bins,Ndist,'b-','LineWidth',1); hold off; grid on; grid minor;
xlabel('u''ch4'' uM/m2/d, Weibull Dist.'); ylabel('Norm PDF');
dim = [.3 .5 .3 .3];
str = ['99% limit = ',sprintf('%4.2f',lim)];
annotation('textbox',dim,'String',str,'FitBoxToText','on','FontSize',14);

%% weibull distribution of vch4_uM

data = abs(mc_flx.vch4_uM);
ii = data<250;
edges = 0:2:250;
bins = 0+1:2:250-1;
hist = histcounts(data,edges);
Nhist = hist/sum(hist);
pd = fitdist(data(ii),'Weibull');
dist = pdf(pd,bins);
cdist = cdf(pd,bins);
lim = bins(find(cdist>=0.99,1,'first'));
Ndist = dist/sum(dist);
figure('Position',[300,300,600,400],'Name','vch4_WeiDist');
plot(bins,Nhist,'r.','MarkerSize',10); hold on;
plot(bins,Ndist,'b-','LineWidth',1); hold off; grid on; grid minor;
xlabel('v''ch4'' uM/m2/d, Weibull Dist.'); ylabel('Norm PDF');
dim = [.3 .5 .3 .3];
str = ['99% limit = ',sprintf('%4.2f',lim)];
annotation('textbox',dim,'String',str,'FitBoxToText','on','FontSize',14);

%% distribution of sigma co2

data = mc_flx.sig_co2;
ii = data<1;
edges = 0:0.005:0.5;
bins = 0+0.0025:0.005:0.5-0.0025;
hist = histcounts(data,edges);
Nhist = hist/sum(hist);
cdist = NaN(size(Nhist));
for jj = 1:length(Nhist)
    cdist(jj) = sum(Nhist(1:jj));
end
lim = bins(find(cdist>=0.99,1,'first'));
Ndist = dist/sum(dist);
figure('Position',[300,300,600,400],'Name','sigco2_RayDist');
plot(bins,Nhist,'r.','MarkerSize',10); grid on; grid minor;
xlabel('std dev co2, ppm, Rayleigh Dist.'); ylabel('Norm Dist');
dim = [.3 .5 .3 .3];
str = ['99% limit = ',sprintf('%4.2f',lim)];
annotation('textbox',dim,'String',str,'FitBoxToText','on','FontSize',14);

%% distribution of sigma ch4

data = mc_flx.sig_ch4;
ii = data<1;
edges = 0:0.01:1;
bins = 0+0.005:0.01:1-0.005;
hist = histcounts(data,edges);
Nhist = hist/sum(hist);
cdist = NaN(size(Nhist));
for jj = 1:length(Nhist)
    cdist(jj) = sum(Nhist(1:jj));
end
lim = bins(find(cdist>=0.99,1,'first'));
Ndist = dist/sum(dist);
figure('Position',[300,300,600,400],'Name','sigch4_RayDist');
plot(bins,Nhist,'r.','MarkerSize',10); grid on; grid minor;
xlabel('std dev ch4, ppb, Rayleigh Dist.'); ylabel('Norm Dist');
dim = [.3 .5 .3 .3];
str = ['99% limit = ',sprintf('%4.2f',lim)];
annotation('textbox',dim,'String',str,'FitBoxToText','on','FontSize',14);

%% save plots if desired

if savefigs
    disp('saving figures')
    figs = findall(0,'type','figure');
    sorted_figs = sortfighandlearray(figs); % put in ascending order
    N = length(sorted_figs);
    if N>0
        for ii=1:N
            fname = sorted_figs(ii).Name;
            figure(sorted_figs(ii));
            ppath = fullfile(plot_path,['fig',sprintf('%02i',ii),'_',fname,'_metcity.png']);
            print('-dpng',ppath);
        end
    end
end

disp('DONE')
