% pondmovieGRLS2.m - script to produce an image stack for making movie S2 of
% radiatively heated convection in a fresh pond. 

% code has only been tested on original author's system, 
% and we accept no responsibility for adverse consequences if used on other systems. 
% Use at your own risk.

% author: Andrew Wells, Oxford
% contact andrew.wells@physics.ox.ac.uk for further information

Nsamp=1;
Pr=13; Ra=5e6; 

ploton=1;

nstart=1;
nend=16;


%filenamestr='constheat/F0p5/Test/snapshots/snapshots_s2.h5';

dirstr='sourcedirectory/'; % need to update this with your source directory

graphics_save=1;% flag to produce output


% plot formatting

set(0, 'DefaultTextFontName','Helvetica')
set(0, 'DefaultAxesFontName','Helvetica')
set(0, 'DefaultAxesFontSize',14)
set(0, 'DefaultTextFontSize',14)
set(0,'DefaultLineLineWidth',2)
set(0,'DefaultLineMarkerSize',8)

% ENTER FILE NAME HERE
%file_stem=''; 

file_stem='ConvectionRa5e6n';

if graphics_save==1 % if producing output
    figZ=1;
    
     % set up figure frame and properties  
    frame=[];
    close all
    figure(figZ); clf; set(gcf,'DockControls','off','Position',[ 13 282 800 1200],'Renderer','Painters')
   fnum=0;
  
      set(gcf, 'PaperPosition', [2 1 12 3.2]);
  
    
end

% generate arrays for storing data
maxTseries=[];
timeseries=[];
surfTseries=[];
surfFluxseries=[];
baseFluxseries=[];

Tdata2=[]; xdata2=[]; zdata2=[]; timedata2=[]; 
Tslice=[]; Tmax=[]; Tmaxvec=[]; Tave=[]; Tsurf=[]; 
Fluxtmp=[];

% loop over all input files and read data
for jj=nstart:1:nend
    
clear Tdata2, xdata2, zdata2, timedata2, Tslice, Tmax, Tmaxvec, Tave, Tsurf, Fluxtmp;

filenamestr=strcat(dirstr,'FreshRa5000000snapshots_s',num2str(jj),'.h5');

% read in data (all dimensionless)
Tdata2=h5read(filenamestr,'/tasks/T');
Tzdata2=h5read(filenamestr,'/tasks/Tz');
xdata2=h5read(filenamestr,'/scales/x/1.0');
zdata2=h5read(filenamestr,'/scales/z/1.0');

timedata2=h5read(filenamestr,'/scales/sim_time');

arrsize=size(Tdata2);
Nt=arrsize(3);

% loop over time for each matrix of T(x,z,t) values
for ii=1:1:Nt

    
currt=timedata2(ii);    
Tslice=Tdata2(:,:,ii);

Tfac=20*0.1/0.6 ; % temp conversion factor (QH)*H/k for 20 W/m^2 absorption in 0.1m pond

timefac=0.1^2/1.5e-7/(13*5e6)^(1/3)/60; % H^2/kappa/(Pr*Ra)^(1/3)/60s 
% time conversion factor to minutes for 0.1m deep pond, Ra=5e6;


% produce image
%pause(0.01)
figure(1); imagesc(xdata2,zdata2,Tfac*(Tslice)/(Ra*Pr)^(1/3) )
%colorbar

% add labels
xlabel('x (m)')
ylabel('z (m)')


axis equal


   htitle = title(strcat('Temperature (deg. C) at time  t = ',num2str(currt*timefac,'%3.0f'),' min; Fresh Pond'),'FontSize',14);
    
    %set(gca,'Visible','off');
    set(htitle,'Visible','on');
    %colorbar('EastOutside','FontSize',14)
    colorbar
    
        cbarhandleST=colorbar;




     caxis([0.0 0.17])

% set colour bar and ticks
set(cbarhandleST,'Ytick',[0 0.05 0.1 0.15],'FontSize',14)
set(cbarhandleST,'YtickLabel',{'0.00','0.05','0.10','0.15'})



colormap(coolwarmcmap)


set(gca,'XLim',[0 4],'YLim',[0 1],'XTick',0:1:8,'YTick',[0 1],'FontSize',14)
set(gca,'XTickLabel',0.1*(0:1:8) ,'YTickLabel',0.1*[1 0],'FontSize',14)

% output to file
if graphics_save==1
            fnum=fnum+1;


             fname=['graphics/GRLFreshRa5e6HiRes/',file_stem,'_',num2str(fnum,'%04d'),'.png'];
            %print('-dpng','-painters','-r100',fname);
            print('-dpng','-painters','-r400',fname);
            %print('-dpng','-r100',fname);
end

  
end

end