clearvars -except cmaps
clear global
close all

nikki='Dt80';
mconfig = 'sedonly';
load(sprintf('../pfm_summary/%s_%s_pfm.mat',nikki,mconfig))
load('../indiv_proc/amp_struct_sedonly.mat')
load('../indiv_proc/bin_struct_sedonly.mat')


global_var

var_name_mod={'diagM0_rain','diagM3_rain'};
var_name_output={'number','mass'};
nvar=length(var_name_mod);
rho = bin_struct.tau.density;
sedsum=struct;

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
      rain_i(ivar) = nansum(amp_struct.(bintype{its}).(var_name_mod{ivar})(1,:).*rho(1,:)*dz);
      for it = 1:length(time)
         amp_rain_path{ivar}(it) = nansum(amp_struct.(bintype{its}).(var_name_mod{ivar})(it,:).*rho(it,:)*dz);
         bin_rain_path{ivar}(it) = nansum(bin_struct.(bintype{its}).(var_name_mod{ivar})(it,:).*rho(it,:)*dz);
      end

      sedsum.(var_name_output{ivar})((its-1)*2+1,:)=...
         amp_rain_path{ivar}/rain_i(ivar);
      sedsum.(var_name_output{ivar})((its-1)*2+2,:)=...
         bin_rain_path{ivar}(1:length(time))/rain_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 % ivar
end % its

%% figures
figure('Position',[1291 631 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,sedsum.(var_name_output{ivar})(iline,:),...
         'LineWidth',2,...
         'LineStyle',style_order{iline},...
         'color',color_order{ceil(iline/2)})
      ylim([0 1])
   end % iline
   grid
   hold off
   legend('AMP-TAU','bin-TAU','AMP-SBM','bin-SBM','Location','best')
   title(['(' char(96+ivar) ') ' 'Rain ' var_name_output{ivar}],'fontweight','normal')
   set(gca,'FontSize',16)
end % ivar

nexttile
str1 = {'(a) Rain 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) Rain 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,'Sed. only - D_m = 600 \mum, \nu = 3',...
   'fontsize',18,'fontweight','bold')

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