function Display_local_curvature_data
%Program to make figures of local patterns of Gaussian and Mean curvature.
%In figures with multiple rows and columns, 1st row represents the 1st 4 time points e.g. 7-10, 2nd row 4th to 7th time point e.g. DAS10-13, 
% (then 3rd row would be DAS13-16, and last row DAS16-19)'
%In figures with one column and 4 rows, each row gives the colour scale for corresponding rows in the multi-column figures
%the leaf displayed in each row in that case represents the last time point of the dataset)');
%Next program to run: Disp_3Dmeshes_characteristicleaves

days=7:19; %input('Enter DAS numbers contained in this data set ');

close all;
rangeset{1,1}=7:10;
rangeset{2,1}=10:13;
rangeset{3,1}=13:16;
rangeset{4,1}=16:19;

Path1=uigetdir(cd,'Select Reference data folder');
load([Path1 '\meanoutlines_DAS7-19.mat']);

Pathname=uigetdir(cd,'Select main project folder in  which all shape results and figures are stored');
load([Pathname '\Local curvature data and figures\Characteristicleaves_localcurvaturedata.mat']);


 PathName=[Pathname '\Local curvature data and figures'];
 cd(PathName)

%----- To register the min and max values for that period(groups of 4 DAS): ----------------------------------------------------

for cpt=1:size(rangeset,1)
    for ii=rangeset{cpt}
        gcurv_minimum(cpt,ii)=min(min(gcurv_all{ii}));
        gcurv_maximum(cpt,ii)=max(max(gcurv_all{ii}));
        mcurv_minimum(cpt,ii)=min(min(mcurv_all{ii}));
        mcurv_maximum(cpt,ii)=max(max(mcurv_all{ii}));
    end
end
%----- Display Local Gaussian Curvature Over Time : ---------------------------------------------------------------------------------------
n=0;figure;


% to close outlines
for ii=days(1):days(end)
    avx{ii}=cat(1,avx{ii},avx{ii}(1));
    avy{ii}=cat(1,avy{ii},avy{ii}(1));
end

for cpt=1:size(rangeset,1), % for each group of days
    rangecol{cpt}=getrangecol(gcurv_minimum(cpt,:),gcurv_maximum(cpt,:));
    map3=colmapset(rangecol{cpt});
    colormap(map3);
    for ii=rangeset{cpt}
        n=n+1;
        subplot(size(rangeset,1),size(rangeset{1},2),n);
        surf(XIg_all{ii},YIg_all{ii},ZIg_all{ii}*0,gcurv_all{ii}); h=line(avx{ii},avy{ii}); set(h,'Color',[0 0 0],'LineWidth',2);
        axis equal; view(0,90);caxis(rangecol{cpt}); shading('flat');grid off;axis off;
    end
end


set(gcf,'Name','Local Gaussian Curvature');
%cd('S:\Camille Girard-Bock\Shape paper_Summer2013\figures\Camille\Gaussian and Local Curv');
saveas(gcf, ['LocalGaussian3.fig']);
saveas(gcf, ['LocalGaussian3'], 'png');
saveas(gcf, ['LocalGaussian3'], 'epsc');
%save the scale bar:
figure;
for cpt=1:size(rangeset,1), % for each group of days
    map3=colmapset(rangecol{cpt});
    colormap(map3);
    subplot(size(rangeset,1),1,cpt);surf(XIg_all{ii},YIg_all{ii},ZIg_all{ii}*0,gcurv_all{ii}); h=line(avx{ii},avy{ii}); set(h,'Color',[0 0 0],'LineWidth',2);
    axis equal; view(0,90);caxis(rangecol{cpt}); shading('flat');grid off;axis off;colorbar;
end
saveas(gcf, ['LocalGaussian3scalebars.fig']);
saveas(gcf, ['LocalGaussian3scalebars'], 'png');
saveas(gcf, ['LocalGaussian3scalebars'], 'epsc');
%----- Display Local Mean Curvature Over Time : ---------------------------------------------------------------------------------------
n=0;figure;
for cpt=1:size(rangeset,1), % for each group of days
    for ii=rangeset{cpt}
        n=n+1;
        subplot(size(rangeset,1),size(rangeset{1},2),n);
        surf(XIg_all{ii},YIg_all{ii},ZIg_all{ii}*0,mcurv_all{ii}); h=line(avx{ii},avy{ii}); set(h,'Color',[0 0 0],'LineWidth',2);
        axis equal; view(0,90);caxis([min(min(mcurv_minimum(cpt,:))) max(max(mcurv_maximum(cpt,:)))]); shading('flat');grid off;axis off;
    end
end

set(gcf,'Name','Local Mean Curvature');
%cd('S:\Camille Girard-Bock\Shape paper_Summer2013\figures\Camille\Gaussian and Local Curv');
saveas(gcf, ['LocalMean3.fig']);
saveas(gcf, ['LocalMean3'], 'png');
saveas(gcf, ['LocalMean3'], 'epsc');
%save the scale bar:
figure;
for cpt=1:size(rangeset,1), % for each group of days
    subplot(size(rangeset,1),1,cpt);
    surf(XIg_all{ii},YIg_all{ii},ZIg_all{ii}*0,mcurv_all{ii}); h=line(avx{ii},avy{ii}); set(h,'Color',[0 0 0],'LineWidth',2);
    axis equal; view(0,90);caxis([min(min(mcurv_minimum(cpt,:))) max(max(mcurv_maximum(cpt,:)))]); shading('flat');grid off;axis off;colorbar;
end

saveas(gcf, ['LocalMean3scalebars.fig']);
saveas(gcf, ['LocalMean3scalebars'], 'png');
saveas(gcf, ['LocalMean3scalebars'], 'epsc');

disp(['figures have been saved in ' PathName]);
disp('In figures with multiple rows and columns, 1st row represents DAS7-10, 2nd row DAS10-13, 3rd row DAS13-16, last row DAS16-19');
disp('In figures with one column and 4 rows, each row gives the colour scale for corresponding rows in the multi-column figures...');
disp('...the leaf displayed in each row in that case represents the last time point of the dataset)');

function range=getrangecol(gcurv_min,gcurv_max)
min1_4=min(min(gcurv_min));
max1_4=max(max(gcurv_max));

if min1_4<0 && max1_4 >0
    if abs(min1_4)<abs(max1_4)
        min1_4=-max1_4 ;
    elseif abs(min1_4)>abs(max1_4)
        max1_4=-min1_4;
    end
elseif min1_4<=0 && max1_4<=0
    max1_4=-min1_4;
elseif min1_4>=0 && max1_4>=0
    min1_4=-max1_4;
end
range=[min1_4 max1_4];

function map3=colmapset(minmax)
%meanmaxis a range vector as output by getrangecol
interv=(minmax(2)-minmax(1))/100;
rangelev=minmax(1):interv:minmax(2); %To use for DAS7
%rangelev=minmax(1):0.0000001:minmax(2); %To use for DAS7
%rangelev=-0.00000004:0.000000001:0.00000004; %to use for DAS19
set1=linspace(0,1,ceil(size(rangelev,2)/2));
map1=cat(2,ones(ceil(size(rangelev,2)/2),1),set1',set1');
set2=linspace(1,0,ceil(size(rangelev,2)/2));
map2=cat(2,set2',set2',ones(ceil(size(rangelev,2)/2),1));
map1(size(map1,1),:)=[];
map2(1,:)=[];
map3=cat(1,map1,[1 1 1],map2);
map3=flipud(map3);

% caxis([rangelev(1) rangelev(end)])