%% Simple example to plot a topview of the data
clear
close all
savefigs=1; %Toggle

savemovie=1 ;  % save as movie (1) or not (0)
clear M

% Load

Mpath='F:\check_outs\tudelft\vlugtenburg\'
%Mpath='C:\Madeschipper\check_out\OpenEarthRawData\tudelft\vlugtenburg\'

    load F:\check_outs\tudelft\vlugtenburg\scripts\Matthieu\WaveHeightAnalysis\Pdaily.mat
    
load([Mpath 'AllVbeachsurveys.mat'])
load('cmapVBmin10to5.mat')

%%% images


%% plot gridded (from transects) data
if 1
    for i_date=1:length(Vsurvey)
        figure(229)
        set(gcf,'PaperPosition',[0  0  40 20])
        set(gcf,'Position',[9          86        1641         881])
        
     subplot(223)
        pk=pcolor(Vsurvey(i_date).transects.y,Vsurvey(i_date).transects.cr_sh,Vsurvey(i_date).transects.elevation)
        hold all
        
        [C,h] = contour(Vsurvey(i_date).transects.y,Vsurvey(i_date).transects.cr_sh,Vsurvey(i_date).transects.elevation,[-9:1:4],'--k');
        clabel(C,h,'LabelSpacing',72,'FontSize',10,'Color','k','Rotation',0) ;
        
        shading flat
        ylim([0 1200])
        caxis([-1 1])
        colorbar
        hold all
        
        set(gca,'Xtick',[flipud(Vsurvey(i_date).transects.y)])
        set(gca,'Xticklabel',num2str([flipud(Vsurvey(i_date).transects.y)],'%4.0f'))
        
        xlim([0 1750])
        xlabel('Alongshore distance [m]')
        
        ylim([0 1200])
        ylabel('Cross shore distance [m]')
        
        caxis([-10 5])
        % colorbar  %% Not necessary with contour lines.
         colormap(cmap)
        %set(gca,'position',[0.1300    0.1100    0.5750    0.8150])
    %    legend([pk  h], 'Elevation [m]',[ 'contour ' Vsurvey(i_date).date([4:6 1:3 7:10])] )
        title([ 'Topography survey  ' Vsurvey(i_date).date([1:3 7:10]) ''],'Fontsize',20)
                
        x0=30;           y0=1050;  theta0 =-131.65534968173412+90;
        h=100; w=100 ;
        [x_ds3,y_ds3]=northarrow(h,w,x0,y0,theta0);
      
        hold off
     
       
    subplot(222)
      % Mean profiles    
      hold on
            if i_date >=4
        Mzi_min3=nanmean(Vsurvey(i_date-3).transects.elevation(:,:),2);
     plot(Mzi_min3,Vsurvey(i_date).transects.cr_sh,'color',0.7*[1 1 1],'linewidth',2)   
            end
          if i_date >=3
        Mzi_min2=nanmean(Vsurvey(i_date-2).transects.elevation(:,:),2);
     plot(Mzi_min2,Vsurvey(i_date).transects.cr_sh,'color',0.5*[1 1 1],'linewidth',2)   
    end
   if i_date >=2
        Mzi_min1=nanmean(Vsurvey(i_date-1).transects.elevation(:,:),2);
     plot(Mzi_min1,Vsurvey(i_date).transects.cr_sh,'color',0.3*[1 1 1],'linewidth',2)   
    end  
Mzi=nanmean(Vsurvey(i_date).transects.elevation(:,:),2);
    plot(Mzi,Vsurvey(i_date).transects.cr_sh,'color',0*[1 1 1],'linewidth',2.5)  
    
    plot(Mzi*0,Vsurvey(i_date).transects.cr_sh,'b')   
    ylim([0 1200])

    xlabel ('Elevation [m]')
    title ('Mean Profile')
    
     grid on
     hold off
%      
      subplot(221)

  plot(dailyP(:,1),dailyP(:,2),'k') 
   hold on
 for iperiod=2:length(Vsurvey)
plot([Vsurvey(iperiod).matlabdate Vsurvey(iperiod).matlabdate],[0 15E8],':','color',[0.4 0.4 0.4]) % Survey date
end
      
   plot([ Vsurvey(i_date).matlabdate Vsurvey(i_date).matlabdate],[0 15E8],'r','linewidth',2)
 if i_date >=2
    xdata = [Vsurvey(i_date-1).matlabdate Vsurvey(i_date-1).matlabdate Vsurvey(i_date).matlabdate Vsurvey(i_date).matlabdate];
ydata = [0 15E8 15E8 0];
p = patch(xdata,ydata,0.7*[1 1 1])
 end
 
xlim([datenum(['06/01/2009'],'mm/dd/yyyy') datenum(['10/02/2012'],'mm/dd/yyyy')])
 datetick('x','keeplimits')
% xlabel('Date')
ylabel('Wave power')
  hold off

     subplot(222)
      set(gca,'position',[0.7701    0.1100    0.1845    0.5847])

        subplot(221)
        set(gca,'position',[0.12    0.7900    0.6155    0.1430])  
        subplot(223)
    set(gca,'position',[0.12    0.1100    0.5807    0.5847])  

     
        set(gcf,'renderer','zbuffer')
        if savefigs==1
            filename=[ 'Topography ' Vsurvey(i_date).date([7:10]) '_' Vsurvey(i_date).date([1:2]) '_' Vsurvey(i_date).date([4:5])]
            print ([ Mpath '\scripts\Matthieu\MakeTopo\images\tgrid' filename '.jpg'],'-djpeg')
        end
               if savemovie==1
    set(gcf,'Color',[1,1,1])
%set(gcf,'renderer','opengl')
                   M(i_date)=getframe(gcf);
                 %  close (gcf)
               end

    end
end



if savemovie==1
M(1,length(M)+1)=M(1,length(M))
M(1,length(M)).cdata=M(1,length(M)).cdata*0+255
    make_movie_gif(M,0.5,'MoviePresentation.gif',10)

end

