function Display_Deformations_DaytoNext
%Program used to display deformation grids from 1 day to the next user has to indicate whether 
%Compute_deformation_patterns or Compute_deformation_patterns_lwm was used at the previous step.
%
%Next program to run: warpleaves_ShapePaper

days=7:19;

PathName=uigetdir(cd,'Select main project folder in  which all shape results and figures are stored');

trsfo=input('If you used Compute_deformation_patterns Press 1, if you used Compute_deformation_patterns_lwm press 2');
%PathName=uigetdir(cd,'Select folder in  which to save mean grids'' data and figures');
resdir='Grid deformations in 2D';
PathName=[PathName '\' resdir];

close all
maingrid=5;
mainthick=1.5;%thickness of main grid lines
secondthick=0.2;
secondcol=[0.7 0.7 0.7];
scalegrid=1;
loaddata=2;

%dispside=input('Display pre and post def side by side? y/n','s');
dispside='y';

opt1=0; % showing grids only

% [fn4 pn4]=uigetfile(pwd,['Load data to be displayed','*.mat']);
% load([pn4 '\' fn4]);
if trsfo==1
    load([PathName '\Meangrid_day_by_day_deform07-19.mat']);
    nt1='pol3';
else
    load([PathName '\Meangrid_day_by_day_deform_lwm07-19.mat']);
    nt1='lwm';
end
%PathName=uigetdir(pn4,'Select folder in  which to save mean grids');
cd(PathName)
%% Plot
% saveimagesto=uigetdir(cd,'Select folder where you want to save images');
% cd(saveimagesto)


fignum=0;

for t=days(1):days(end-1)
    figure; hold on
    set(gcf, 'color', 'white');
    axis equal;
    
    if scalegrid==1
        rangex=max(max(avxpredef{t}))-min(min(avxpredef{t}));
        scfac=1000/rangex;
    else scfac=1;
    end
    
    
    for k=1:size(gX{t},1) %drawing lines instead of mesh b/c mesh won't save to eps files
        if rem(k-1,maingrid)==0
            plot(gX{t}(k,:)*scfac,gY{t}(k,:)*scfac,'k','LineWidth',mainthick);
        else
            hh=plot(gX{t}(k,:)*scfac,gY{t}(k,:)*scfac,'LineWidth',secondthick);
            set(hh,'Color',secondcol);
        end
    end
    for j=1:size(gX{t},2)
        
        if rem(j-1,maingrid)==0
            plot(gX{t}(:,j)*scfac,gY{t}(:,j)*scfac,'k','LineWidth',mainthick);
        else
            hh=plot(gX{t}(:,j)*scfac,gY{t}(:,j)*scfac,'k','LineWidth',secondthick);
            set(hh,'Color',secondcol);
        end
    end
    
    %plot predeformation mean outline based on samples used for trsfo
    %computation
    plot([avxpredef{t};avxpredef{t}(1)]*scfac ,[avypredef{t};avypredef{t}(1)]*scfac,'color',[0 0 0],'LineWidth',1)
    
    title(['DAS' num2str(t,'%02.0f') 'before deformation']);
    axis([-3000 3000 -3000 3000]);
    
    %set(gcf,'WindowStyle','Docked');
    set(gcf, 'color', 'white');
        
    %NOTE: here can add grid colour-coded for growth
    %grid pre-deformation = gX{t}, gY{t}
    %grid post deformation = xm{t+1}, ym{t+1}
    
    
    fignum=fignum+1;
    %     print('-depsc2',['Grid DAS' num2str(days(1)) '-' num2str(days(end)) '   predefDAS' num2str(t,'%02.0f')]);
    %     saveas(gcf,['Grid DAS' num2str(days(1)) '-' num2str(days(end)) '   predefDAS' num2str(t,'%02.0f')],'png');
    axis off;
    %AG: removed 'if' Aug 29
    %     if loaddata==3 && opt2==3
    
    %     end
    if dispside=='n'
        saveas(gcf,['Figure' nt1 '_' num2str(fignum)],'fig');
        close gcf %if opt1==0, we will display the 2 grids side by side
        figure
    end

        
        hold on
        
        
        if dispside=='y'
            if scfac==1
                cst=(max(max(xm{t+1}))-min(min(xm{t+1}))/2)+(max(max(gX{t}))-min(min(gX{t})/2))+100;
            else
                cst=(max(max(xm{t+1}))-min(min(xm{t+1}))/2)*scfac+(max(max(gX{t}))-min(min(gX{t})/2))*scfac+100;
            end
        else
            cst=0;
        end
        plot([avxpostdef{t+1};avxpostdef{t+1}(1)]*scfac+cst ,[avypostdef{t+1};avypostdef{t+1}(1)]*scfac,'color',[0 0 0],'LineWidth',1.5);
        plot([olxw{t+1};olxw{t+1}(1)]*scfac+cst,[olyw{t+1};olyw{t+1}(1)]*scfac,'color', [0.7 0.7 0.7],'LineWidth',1.5);
        
        
        
        if dispside=='y'
            if scfac==1
                cst=(max(max(xm{t+1}))-min(min(xm{t+1}))/2)+(max(max(gX{t}))-min(min(gX{t})/2))+100;
            else
                cst=(max(max(xm{t+1}))-min(min(xm{t+1}))/2)*scfac+(max(max(gX{t}))-min(min(gX{t})/2))*scfac+100;
            end
        else
            cst=0;
        end
        
        for k=1:size(xm{t+1},1) %drawing lines instead of mesh b/c mesh won't save to eps files
            if rem(k-1,maingrid)==0
                plot(xm{t+1}(k,:)*scfac+cst,ym{t+1}(k,:)*scfac,'k','LineWidth',mainthick);
            else
                hh=plot(xm{t+1}(k,:)*scfac+cst,ym{t+1}(k,:)*scfac,'LineWidth',secondthick);
                set(hh,'Color',secondcol);
            end
        end
        for j=1:size(xm{t+1},2)
            if rem(j-1,maingrid)==0
                plot(xm{t+1}(:,j)*scfac+cst,ym{t+1}(:,j)*scfac,'k','LineWidth',mainthick);
            else
                hh=plot(xm{t+1}(:,j)*scfac+cst,ym{t+1}(:,j)*scfac,'LineWidth',secondthick);
                set(hh,'Color',secondcol);
            end
            
        end
        
        
        title(['DAS' num2str(t+1,'%02.0f') 'deformed from DAS' num2str(t,'%02.0f')]);
        axis([-3000 3000 -3000 3000]);
        
        %set(gcf,'WindowStyle','Docked');
        
        set(gcf, 'color', 'white');
        axis equal;
        
        
        fignum=fignum+1;
        %axis([-3000 3000 -3000 3000]);
        %         print('-depsc2',['Grid DAS' num2str(days(1)) '-' num2str(days(end)) '   postdefDAS' num2str(t+1,'%02.0f')]);
        %         saveas(gcf,['Grid DAS' num2str(days(1)) '-' num2str(days(end)) '   postdefDAS' num2str(t,'%02.0f')],'png');
        axis equal
        axis on
        axis([-1000 4000 -2000 2000])
        nscale=1000;
        %title('click where you want the scale bar to be')
        %[x1,y1]=ginput(1);
        x1=min(gX{t}(1,:))*scfac;
        y1=max(ym{t+1}(:,1)*scfac)+50;
        hold on,
        h=line([x1;x1+nscale*scfac],[y1;y1]);
        set(h,'Color',[0 0 0]);
        
        %axis([-1000 1000 -600 600])
        
        axis off
        %print('-depsc2',['Fig' num2str(fignum)]);
        %saveas(gcf,['Figure' num2str(fignum)],'fig');
        saveas(gcf,['Figure' nt1 '_' num2str(fignum)],'fig');

        %        end
        close gcf;
        
        %keyboard
end

disp(['Figures of deformation patterns in 2D from one day to the next have been saved in ' PathName]);

