clearvars -except cmaps
clear global
close all

nikki='Dt80';
mconfig = 'evaponly';
global_var

load(sprintf('../pfm_summary/%s_%s_pfm.mat',nikki,mconfig))
load('../indiv_proc/amp_struct_evaponly.mat')
load('../indiv_proc/bin_struct_evaponly.mat')

%%

var_name_mod={'diagM0_cloud','diagM3_cloud'};
var_name_output={'number','mass'};
nvar=length(var_name_mod);
evapsum=struct;

rho = bin_struct.tau.density;

for its=1:length(bintype)
   time = amp_struct.(bintype{its}).time;
   z = amp_struct.(bintype{its}).z;
   dz = z(2) - z(1);
   for ivar=1:nvar
      cloud_i(ivar) = nansum(amp_struct.(bintype{its}).(var_name_mod{ivar})(1,:).*rho(1,:)*dz);
      for it = 1:length(time)
         amp_cloud_path{ivar}(it) = nansum(amp_struct.(bintype{its}).(var_name_mod{ivar})(it,:).*rho(it,:)*dz);
         bin_cloud_path{ivar}(it) = nansum(bin_struct.(bintype{its}).(var_name_mod{ivar})(it,:).*rho(it,:)*dz);
      end
      evapsum.(var_name_output{ivar})((its-1)*2+1,:)=...
         amp_cloud_path{ivar}/cloud_i(ivar);
      evapsum.(var_name_output{ivar})((its-1)*2+2,:)=...
         bin_cloud_path{ivar}(1:length(time))/cloud_i(ivar);
      ratios(ivar,its) = mean(pfm.(var_name_mod{ivar}).(bintype{its}).mr(:));
      rsq(ivar,its) = mean(pfm.(var_name_mod{ivar}).(bintype{its}).rsq(:));
   end
end

%% figures
figure('Position',[0 0 850 346])
tl=tiledlayout(1,5);

style_order={'-',':','-',':'};

for ivar=1:nvar
   nexttile((ivar-1)*2+1, [1 2])
   for iline=1:4
      hold on
      plot(time,evapsum.(var_name_output{ivar})(iline,:),...
         'LineWidth',2,...
         'LineStyle',style_order{iline},...
         'color',color_order{ceil(iline/2)})
      % ylim([0 1])
   end
   grid
   hold off
   legend('AMP-TAU','bin-TAU','AMP-SBM','bin-SBM','Location','best')
   title(['(' char(96+ivar) ')' ' Cloud ' var_name_output{ivar}],'fontweight','normal')
   set(gca,'FontSize',16)
end

nexttile
str1 = {'(a) Cloud number', ... 
   '% difference / R^2', ...
   sprintf('TAU: %0.1f%% / %0.3f', (ratios(1, 1)-1)*100, rsq(1, 1)), ...
   sprintf('SBM: %0.1f%% / %0.3f', (ratios(1, 2)-1)*100, rsq(1, 2)), ...
   };
annotation('textbox',[0.75 0.5 0.20 0.3],'String', str1, ...
           'FontSize', 14, 'HorizontalAlignment', 'center', ...
           'VerticalAlignment', 'middle', 'edgecolor', [.8 .8 .8])
str1 = {'(b) Cloud mass', ... 
   '% difference / R^2', ...
   sprintf('TAU: %0.1f%% / %0.3f', (ratios(2, 1)-1)*100, rsq(2, 1)), ...
   sprintf('SBM: %0.1f%% / %0.3f', (ratios(2, 2)-1)*100, rsq(2, 2)), ...
   };
annotation('textbox',[0.75 0.2 0.20 0.3],'String', str1, ...
           'FontSize',14, 'HorizontalAlignment', 'center', ...
           'VerticalAlignment', 'middle', 'edgecolor', [.8 .8 .8])
set(gca,'xcolor','none')
set(gca,'ycolor','none')

xlabel(tl,'Time [s]','fontsize',16)
ylabel(tl,'Fraction left','fontsize',16)
title(tl,'Evap. only - RH = 99%, D_m = 27\mum',...
   'fontsize',18,'fontweight','bold')

% exportgraphics(gcf,['plots/9 evaponly_massnum.png'],'Resolution',300)
exportgraphics(gcf,'plots/9 evaponly_massnum.pdf')
