% MKLminuBKL_Plots 
%
% Coast line indicators and nourishment plots along the Dutch coast
% MKLminuBKL = MKL(t) - BKL(t0)        t0 = year of reference;
% 
% NB. time in BKL (TKL, TND) ends in 2013 (defined and predicted coast lines)
%     time in MKL (DF) ends in 2012 (measured coast line)
%
% giorgio.santinelli@deltares.nl

clear all
close all

%% read .nc files
KLpath         = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/BKL_TKL_MKL/';
NOURpath       = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/suppleties/';

MKLurl         = 'MKL.nc';
BKL_TKL_TNDurl = 'BKL_TKL_TND.nc';
NOURurl        = 'suppleties.nc';

id       = nc_varget([KLpath MKLurl], 'id');

% read area
areacode = nc_varget([KLpath MKLurl], 'areacode');
areaname = nc_varget([KLpath MKLurl], 'areaname');

% read time
MKLyear  = nc_cf_time([KLpath MKLurl]); % year
MKLtime  = nc_varget([KLpath MKLurl], 'time_MKL') + datenum('1970','yyyy') ; % actual date
BKLtime  = nc_cf_time([KLpath BKL_TKL_TNDurl]); % fairly 1 more than MKL !

MKL      = nc_varget([KLpath MKLurl], 'momentary_coastline');
TKL      = nc_varget([KLpath BKL_TKL_TNDurl], 'testing_coastline');
BKL      = nc_varget([KLpath BKL_TKL_TNDurl], 'basal_coastline');

% time re-definition to yyyy.mm...
for ii = 1:numel(MKLtime) 
    if ~isnan(MKLtime(ii))
        MKLtime(ii) = str2num(datestr(MKLtime(ii),'yyyy')) + (MKLtime(ii) - datenum(str2num(datestr(MKLtime(ii),'yyyy')),1,1))/365;
    else
        MKLtime(ii) = NaN;
    end
end
BKLtime  = str2num(datestr(BKLtime,'yyyy'));

%% read .nc file of nourishments (m3/m)
NOUR_ntime = nc_varget([NOURpath NOURurl], 'n_time') + datenum('1970','yyyy'); % year of beg_date

NOUR_beach = nc_varget([NOURpath NOURurl], 'vol_beach');
NOUR_shore = nc_varget([NOURpath NOURurl], 'vol_shoreface');
NOUR_dune  = nc_varget([NOURpath NOURurl], 'vol_dune');
NOUR_other = nc_varget([NOURpath NOURurl], 'vol_other');

TIME_start_beach_n     = nc_varget([NOURpath NOURurl], 'time_start_beach') + datenum('1970','yyyy'); % actual date;
TIME_end_beach_n       = nc_varget([NOURpath NOURurl], 'time_end_beach') + datenum('1970','yyyy');
TIME_start_shore_n     = nc_varget([NOURpath NOURurl], 'time_start_shoreface') + datenum('1970','yyyy');
TIME_end_shore_n       = nc_varget([NOURpath NOURurl], 'time_end_shoreface') + datenum('1970','yyyy');
TIME_start_dune_n      = nc_varget([NOURpath NOURurl], 'time_start_dune') + datenum('1970','yyyy');
TIME_end_dune_n        = nc_varget([NOURpath NOURurl], 'time_end_dune') + datenum('1970','yyyy');
TIME_start_other_n     = nc_varget([NOURpath NOURurl], 'time_start_other') + datenum('1970','yyyy');
TIME_end_other_n       = nc_varget([NOURpath NOURurl], 'time_end_other') + datenum('1970','yyyy');

%% Figures...
KLfolder = 'MKLminusBKL_Plots';
mkdir(KLfolder);

% definition in yyyy to avoid issues for datetick command
dateini = 1960;       
datefin = 2015;

TIME_start_beach = NaN(size(TIME_start_beach_n,1),size(TIME_start_beach_n,2)); 
TIME_end_beach   = NaN(size(TIME_end_beach_n,1),size(TIME_end_beach_n,2));
TIME_start_shore = NaN(size(TIME_start_shore_n,1),size(TIME_start_shore_n,2)); 
TIME_end_shore   = NaN(size(TIME_end_shore_n,1),size(TIME_end_shore_n,2)); 
TIME_start_dune  = NaN(size(TIME_start_dune_n,1),size(TIME_start_dune_n,2));   
TIME_end_dune    = NaN(size(TIME_end_dune_n,1),size(TIME_end_dune_n,2)); 
TIME_start_other = NaN(size(TIME_start_other_n,1),size(TIME_start_other_n,2)); 
TIME_end_other   = NaN(size(TIME_end_other_n,1),size(TIME_end_other_n,2)); 

    
% reference BKL = 2012;
BKL2012  = BKL(end,:);
MKLminusBKL = MKL - repmat(BKL2012,size(MKL,1),1);


for i = 1:length(id)
    
    figure('Visible','Off')

    %% Nourishments + add axis

    hvol_st = []; hvol_on = []; hvol_dn = []; hvol_ot = [];
    
    for j = 1:length(NOUR_ntime)
        if ~isnan(NOUR_beach(i,j)) % beach nourishment
            if ~isnan(TIME_start_beach_n(i,j))
                TIME_start_beach(i,j) = str2double(datestr(TIME_start_beach_n(i,j),'yyyy')) + (str2double(datestr(TIME_start_beach_n(i,j),'mm'))-1)/12;
                TIME_end_beach(i,j) = str2double(datestr(TIME_end_beach_n(i,j),'yyyy')) + (str2double(datestr(TIME_end_beach_n(i,j),'mm'))-1)/12;
            end
            hvol_st = fill([TIME_start_beach(i,j), TIME_start_beach(i,j),...
                TIME_end_beach(i,j), TIME_end_beach(i,j)],...
                [0, NOUR_beach(i,j), NOUR_beach(i,j), 0],[1 0.69 0.39]); % fill the plot
            set(hvol_st,'EdgeColor',[1 0.69 0.39],'EdgeAlpha',0.2);
            set(gca,'XLimMode','manual','Xlim',[dateini datefin]);
            set(gca,'XTick', [],'XTickLabel', []);   
            hold on        
        end
        if ~isnan(NOUR_shore(i,j)) % shoreface nourishment
            if ~isnan(TIME_start_shore_n(i,j))
                TIME_start_shore(i,j) = str2double(datestr(TIME_start_shore_n(i,j),'yyyy')) + (str2double(datestr(TIME_start_shore_n(i,j),'mm'))-1)/12;
                TIME_end_shore(i,j) = str2double(datestr(TIME_end_shore_n(i,j),'yyyy')) + (str2double(datestr(TIME_end_shore_n(i,j),'mm'))-1)/12;
            end
            hvol_on = fill([TIME_start_shore(i,j), TIME_start_shore(i,j),...
                TIME_end_shore(i,j), TIME_end_shore(i,j)],...
                [0, NOUR_shore(i,j), NOUR_shore(i,j), 0],[0.04 0.52 0.78]);
            set(hvol_on,'EdgeColor',[0.04 0.52 0.78],'EdgeAlpha',0.2);
            set(gca,'XLimMode','manual','Xlim',[dateini datefin]);
            set(gca,'XTick', [],'XTickLabel', []);        
            hold on          
        end
        if ~isnan(NOUR_dune(i,j))  % dune nourishment
            if ~isnan(TIME_start_dune_n(i,j))
                TIME_start_dune(i,j) = str2double(datestr(TIME_start_dune_n(i,j),'yyyy')) + (str2double(datestr(TIME_start_dune_n(i,j),'mm'))-1)/12;
                TIME_end_dune(i,j) = str2double(datestr(TIME_end_dune_n(i,j),'yyyy')) + (str2double(datestr(TIME_end_dune_n(i,j),'mm'))-1)/12;
            end            
            hvol_dn = fill([TIME_start_dune(i,j), TIME_start_dune(i,j),...
                TIME_end_dune(i,j), TIME_end_dune(i,j)],...
                [0, NOUR_dune(i,j), NOUR_dune(i,j), 0],[0.5 0.5 0]);
            set(hvol_dn,'EdgeColor',[0.5 0.5 0],'EdgeAlpha',0.2);
            set(gca,'XLimMode','manual','Xlim',[dateini datefin]);
            set(gca,'XTick', [],'XTickLabel', []);        
            hold on           
        end
        if ~isnan(NOUR_other(i,j)) % other type of nourishment
            if ~isnan(TIME_start_other_n(i,j))
                TIME_start_other(i,j) = str2double(datestr(TIME_start_other_n(i,j),'yyyy')) + (str2double(datestr(TIME_start_other_n(i,j),'mm'))-1)/12;
                TIME_start_other(i,j) = str2double(datestr(TIME_start_other_n(i,j),'yyyy')) + (str2double(datestr(TIME_start_other_n(i,j),'mm'))-1)/12;
            end            
            hvol_ot = fill([TIME_start_other(i,j), TIME_start_other(i,j),...
                TIME_end_other(i,j), TIME_end_other(i,j)],...
                [0, NOUR_other(i,j), NOUR_other(i,j), 0],[1 0 0]);
            set(hvol_ot,'EdgeColor',[1 0 0],'EdgeAlpha',0.2);
            set(gca,'XLimMode','manual','Xlim',[dateini datefin]);
            set(gca,'XTick', [],'XTickLabel', []);
            hold on            
        end    
    end
    ylabel('Nourishment volume [m  ^3/m]','FontSize',12)
    set(gca,'XTick', [],'XTickLabel', []);    

    % routine to display legend [dumb...]
    Mhvol     = [' Beach    ';' Shoreface';' Dune     ';' Other    '];
    if isempty(hvol_st); Mhvol(find(ismember(Mhvol,' Beach    ', 'rows')),:) = []; end
    if isempty(hvol_on); Mhvol(find(ismember(Mhvol,' Shoreface', 'rows')),:) = []; end
    if isempty(hvol_dn); Mhvol(find(ismember(Mhvol,' Dune     ', 'rows')),:) = []; end
    if isempty(hvol_ot); Mhvol(find(ismember(Mhvol,' Other    ', 'rows')),:) = []; end

    % do not display ticks and labels if no nourishment occurred
    if isempty(hvol_st) && isempty(hvol_on) && isempty(hvol_dn) && isempty(hvol_ot)
        set(gca,'YTick', [],'YTickLabel', []);
    end

    hvol      = ([hvol_st; hvol_on; hvol_dn; hvol_ot]); 
    legend(hvol, char(Mhvol), 'Location', 'SouthWest');
    HandleNou = gca;

    % set # of ticks to 10
    ylimits = get(gca,'YLim');
    yinc    = (ylimits(2)-ylimits(1))/10;
    set(gca,'YTick', ylimits(1):yinc:ylimits(2));
    
    %% MKLminusBKL

    HandleKL = axes('Position',get(HandleNou,'Position'));
    
    M = plot(MKLtime(:,i), MKLminusBKL(:,i),'bx','LineWidth',2,'MarkerSize',8); % plot(MKLyear, MKL,'bo');
    hold on
    
    set(HandleKL,'YAxisLocation','right','Color','none');
    set(HandleKL,'XLimMode','manual','Xlim',[dateini datefin]);

    % set # of ticks to 10
    ylimits = get(gca,'YLim');
    yinc    = (ylimits(2)-ylimits(1))/10;
    set(gca,'YTick', ylimits(1):yinc:ylimits(2));

    % figure opt
    set(gcf,'Position',[5 35 1040 760]); 
    set(gca,'FontSize',12); grid on
    xlabel('Year','FontSize',12); ylabel('MKL - BKL2012 [m]','FontSize',12)
    title(['MKL - BKL2012 : ' char(cellstr(areaname(i,:))) ' (' num2str(areacode(i)) '), transect '...
        num2str(id(i) - areacode(i)*1000000)],'FontSize',12,'FontWeight','bold')
    legendKL = legend(M, {' MKL - BKL2012'}, 'Location', 'NorthWest');     
    print('-dpng','-r300',[KLfolder filesep char(cellstr(areaname(i,:))) '_' num2str(id(i))])

    close all
    clear('hvol_st','hvol_on','hvol_dn','hvol_ot','M');
    
    %% save values for periods
    
    % 1965 - 1990
    MKLminusBKL1   = MKLminusBKL(MKLtime(:,i) >= 1965 & MKLtime(:,i) < 1991,i);
    MKLminusBKL_mean1(i) = nanmean(MKLminusBKL1);    
    
    % 1991 - 2000    
    MKLminusBKL2   = MKLminusBKL(MKLtime(:,i) >= 1991 & MKLtime(:,i) < 2001,i);
    MKLminusBKL_mean2(i) = nanmean(MKLminusBKL2);    
    
    % 2001 - 2010    
    MKLminusBKL3   = MKLminusBKL(MKLtime(:,i) >= 2001 & MKLtime(:,i) < 2050,i); % take the rest
    MKLminusBKL_mean3(i) = nanmean(MKLminusBKL3);
    
    save('MKLminusBKL2012periods','MKLminusBKL_mean1','MKLminusBKL_mean2','MKLminusBKL_mean3');
    
    clear('MKLminusBKL1','MKLminusBKL2','MKLminusBKL3')
    
end

save('MKLminusBKL2012','MKLminusBKL');

