% convertMKL2nc
%
% Generates MKL.nc which contains the following coastal indicator: 
% MKL   (Momentane Kustlijn)
%                              
%
% MKL is obtained using Morphan/Winkust boundaries (Rijkswaterstaat).
%     The information is available from Deltares repository @
%     https://repos.deltares.nl/repos/OpenEarthRawData/trunk/rijkswaterstaat/kustlijnkaartenboek/raw/mkl/
%
% old:
% % % MKL is evaluated using OpenEarthTools.
% % %     The value of MLW is gathered from Jarkus dataset @
% % %     http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/jarkus/profiles/transect.nc
%
%	---------------------------------------
% 
%       Giorgio Santinelli
%
%       Giorgio.Santinelli@deltares.nl
%
%       Deltares
%       P.O. Box 177
%       2600 MH Delft
%       The Netherlands
%
%	---------------------------------------
%
% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$


%% settings

% jarkus database for transect info
OPT.ncpath      = 'C:\Users\fuentesm\Marilu\Deltares\Projects\Coastviewer\jarkus\scripts\';
OPT.Filename    = 'transect_r20240405.nc';

% general settings
OPT.fillvalue    = NaN;
OPT.refdatenum   = datenum(1970,1,1);
OPT.outputfile   = 'MKL.nc';
OPT.disp         = true;
OPT.dump         = true;


%% Read data form jarkus transect.nc
id          = ncread([OPT.ncpath OPT.Filename],'id');
%MHW         = ncread([OPT.ncpath OPT.Filename],'mean_high_water');
%MLW         = ncread([OPT.ncpath OPT.Filename],'mean_low_water');
lat         = ncread([OPT.ncpath OPT.Filename],'lat');
lon         = ncread([OPT.ncpath OPT.Filename],'lon');
cross       = ncread([OPT.ncpath OPT.Filename],'cross_shore');
along       = ncread([OPT.ncpath OPT.Filename],'alongshore');
areaname    = ncread([OPT.ncpath OPT.Filename],'areaname');
areacode    = ncread([OPT.ncpath OPT.Filename],'areacode');
time        = ncread([OPT.ncpath OPT.Filename],'time');
time_bounds = ncread([OPT.ncpath OPT.Filename],'time_bounds');

jarkusyear = str2num(datestr(OPT.refdatenum+ncread([OPT.ncpath OPT.Filename],'time'),'yyyy')); %#ok<ST2NM>

x        = ncread([OPT.ncpath OPT.Filename],'x');
y        = ncread([OPT.ncpath OPT.Filename],'y');
rsp_x    = ncread([OPT.ncpath OPT.Filename],'rsp_x');
rsp_y    = ncread([OPT.ncpath OPT.Filename],'rsp_y');

%% Read MKL, time_MKL, [High, Low, x_sea (boundaries)] for different years (format is always different)

[filepath,~,~] = fileparts(mfilename('fullpath'));
rawpath = fullfile(filepath, '..', 'raw');

% rekenschijf verandert, dus alleen de laatste file is good,
% anders vergelijken wij peren met appels.
% s2011 = readMKLtil2012(fullfile(rawpath, 'MKL 2011 data'));
% save('s2011.mat','s2011');
% load('s2011.mat');
% s2012 = readMKL2012(fullfile(rawpath, 'MKL2012'));
% save('s2012.mat','s2012');
% load('s2012.mat');
% s2013 = readMKL2013(fullfile(rawpath, 'MKL2013'));
% save('s2013.mat','s2013');
% load('s2013.mat');
% s2014 = readMKL2014(fullfile(rawpath, 'MKL 2014 bestanden'));
% save('s2014.mat','s2014');
% load('s2014.mat');
% s2015 = readMKL2015(fullfile(rawpath, 'Totale kust MKL TKL BKL Trend uit toetsing september 2015'));
% save('s2015.mat','s2015');
% load('s2015.mat');
% s2016 = readMKL2016(fullfile(rawpath, 'BKL-MKL2016-TKL2017-trend export 2016 alle kustvakken'));
% save('s2016.mat','s2016');
% load('s2016.mat');
% s2017 = readMKL2017(fullfile(rawpath, 'MKL2017'));
% save('s2017.mat','s2017');
% load('s2017.mat');
% s2018 = readMKL2018(fullfile(rawpath, 'MKL2018'));
% save('s2018.mat','s2018');
% load('s2018.mat');
% s2019 = readMKL2019(fullfile(rawpath, 'MKL2019'));
% save('s2019.mat','s2019');
% load('s2019.mat');
% s2020 = readMKL2020(fullfile(rawpath, 'MKL2020'));
% save('s2020.mat','s2020');
%s2021 = readMKL2021(fullfile(rawpath, 'MKL2021'));
%save('s2021.mat','s2021');
%s2022 = readMKL2022(fullfile(rawpath, 'MKL2022'));
%save('s2022.mat','s2022');

s2023 = readMKL2023(fullfile(rawpath, 'MKL2023'));
save('s2023.mat','s2023');

% mkl file from 2013 already contains all the information until 2013 included. This will then be used as reference 
%% combine MKL from different years
% s.MKL_lat   = [s2013.MKL_lat(1:49,:);  s2014.MKL_lat(50,:);  s2015.MKL_lat(51,:);  s2016.MKL_lat(52,:); s2017.MKL_lat(53,:); s2018.MKL_lat(54,:); s2019.MKL_lat(55,:); s2020.MKL_lat(56,:)]; 
% s.MKL_lon   = [s2013.MKL_lon(1:49,:);  s2014.MKL_lon(50,:);  s2015.MKL_lon(51,:);  s2016.MKL_lon(52,:);  s2017.MKL_lon(53,:); s2018.MKL_lon(54,:); s2019.MKL_lon(55,:); s2020.MKL_lon(56,:)]; 
% s.MKL_x     = [s2013.MKL_x(1:49,:);    s2014.MKL_x(50,:);    s2015.MKL_x(51,:);  s2016.MKL_x(52,:); s2017.MKL_x(53,:); s2018.MKL_x(54,:); s2019.MKL_x(55,:); s2020.MKL_x(56,:)]; 
% s.MKL_y     = [s2013.MKL_y(1:49,:);    s2014.MKL_y(50,:);    s2015.MKL_y(51,:);  s2016.MKL_y(52,:);  s2017.MKL_y(53,:); s2018.MKL_y(54,:); s2019.MKL_y(55,:); s2020.MKL_y(56,:)]; 
% s.Xmkl      = [s2013.Xmkl(1:49,:);     s2014.Xmkl(50,:);     s2015.Xmkl(51,:);  s2016.Xmkl(52,:); s2017.Xmkl(53,:); s2018.Xmkl(54,:); s2019.Xmkl(55,:); s2020.Xmkl(56,:)]; 
% s.Volume    = [s2013.Volume(1:49,:);   s2014.Volume(50,:);   s2015.Volume(51,:);  s2016.Volume(52,:);  s2017.Volume(53,:); s2018.Volume(54,:); s2019.Volume(55,:); s2020.Volume(56,:)]; 
% s.timeMKL   = [s2013.timeMKL(1:49,:);  s2014.timeMKL(50,:);  s2015.timeMKL(51,:);  s2016.timeMKL(52,:); s2017.timeMKL(53,:); s2018.timeMKL(54,:);  s2019.timeMKL(55,:); s2020.timeMKL(56,:)]; 
% s.hoog      = [s2013.hoog(1:49,:);     s2014.hoog(50,:);     s2015.hoog(51,:);  s2016.hoog(52,:);  s2017.hoog(53,:); s2018.hoog(54,:);  s2019.hoog(55,:); s2020.hoog(56,:)]; 
% s.laag      = [s2013.laag(1:49,:);     s2014.laag(50,:);     s2015.laag(51,:);  s2016.laag(52,:);  s2017.laag(53,:); s2018.laag(54,:); s2019.laag(55,:); s2020.laag(56,:)]; 
% s.XzeeMKL   = [s2013.XzeeMKL(1:49,:);  s2014.XzeeMKL(50,:);  s2015.XzeeMKL(51,:);  s2016.XzeeMKL(52,:); s2017.XzeeMKL(53,:); s2018.XzeeMKL(54,:); s2019.XzeeMKL(55,:); s2020.XzeeMKL(56,:)]; 
% s.XMinLand   = [s2013.XMinLand(1:49,:); s2014.XMinLand(50,:); s2015.XMinLand(51,:);  s2016.XMinLand(52,:);  s2017.XMinLand(53,:); s2018.XMinLand(54,:); s2019.XMinLand(55,:); s2020.XMinLand(56,:)];
% s.MKL_lat   = [s2013.MKL_lat(1:49,:);  s2014.MKL_lat(50,:);  s2015.MKL_lat(51,:);  s2016.MKL_lat(52,:); s2017.MKL_lat(53,:); s2018.MKL_lat(54,:); s2019.MKL_lat(55,:); s2020.MKL_lat(56,:)]; 
% s.MKL_lon   = [s2013.MKL_lon(1:49,:);  s2014.MKL_lon(50,:);  s2015.MKL_lon(51,:);  s2016.MKL_lon(52,:);  s2017.MKL_lon(53,:); s2018.MKL_lon(54,:); s2019.MKL_lon(55,:); s2020.MKL_lon(56,:)]; 
% s.MKL_x     = [s2013.MKL_x(1:49,:);    s2014.MKL_x(50,:);    s2015.MKL_x(51,:);  s2016.MKL_x(52,:); s2017.MKL_x(53,:); s2018.MKL_x(54,:); s2019.MKL_x(55,:); s2020.MKL_x(56,:)]; 
% s.MKL_y     = [s2013.MKL_y(1:49,:);    s2014.MKL_y(50,:);    s2015.MKL_y(51,:);  s2016.MKL_y(52,:);  s2017.MKL_y(53,:); s2018.MKL_y(54,:); s2019.MKL_y(55,:); s2020.MKL_y(56,:)]; 
% s.Xmkl      = [s2013.Xmkl(1:49,:);     s2014.Xmkl(50,:);     s2015.Xmkl(51,:);  s2016.Xmkl(52,:); s2017.Xmkl(53,:); s2018.Xmkl(54,:); s2019.Xmkl(55,:); s2020.Xmkl(56,:)]; 
% s.Volume    = [s2013.Volume(1:49,:);   s2014.Volume(50,:);   s2015.Volume(51,:);  s2016.Volume(52,:);  s2017.Volume(53,:); s2018.Volume(54,:); s2019.Volume(55,:); s2020.Volume(56,:)]; 
% s.timeMKL   = [s2013.timeMKL(1:49,:);  s2014.timeMKL(50,:);  s2015.timeMKL(51,:);  s2016.timeMKL(52,:); s2017.timeMKL(53,:); s2018.timeMKL(54,:);  s2019.timeMKL(55,:); s2020.timeMKL(56,:)]; 
% s.hoog      = [s2013.hoog(1:49,:);     s2014.hoog(50,:);     s2015.hoog(51,:);  s2016.hoog(52,:);  s2017.hoog(53,:); s2018.hoog(54,:);  s2019.hoog(55,:); s2020.hoog(56,:)]; 
% s.laag      = [s2013.laag(1:49,:);     s2014.laag(50,:);     s2015.laag(51,:);  s2016.laag(52,:);  s2017.laag(53,:); s2018.laag(54,:); s2019.laag(55,:); s2020.laag(56,:)]; 
% s.XzeeMKL   = [s2013.XzeeMKL(1:49,:);  s2014.XzeeMKL(50,:);  s2015.XzeeMKL(51,:);  s2016.XzeeMKL(52,:); s2017.XzeeMKL(53,:); s2018.XzeeMKL(54,:); s2019.XzeeMKL(55,:); s2020.XzeeMKL(56,:)]; 
% s.XMinLand   = [s2013.XMinLand(1:49,:); s2014.XMinLand(50,:); s2015.XMinLand(51,:);  s2016.XMinLand(52,:);  s2017.XMinLand(53,:); s2018.XMinLand(54,:); s2019.XMinLand(55,:); s2020.XMinLand(56,:)];
s.MKL_lat   = s2023.MKL_lat; 
s.MKL_lon   = s2023.MKL_lon; 
s.MKL_x     = s2023.MKL_x; 
s.MKL_y     = s2023.MKL_y; 
s.Xmkl      = s2023.Xmkl; 
s.Volume    = s2023.Volume; 
s.timeMKL   = s2023.timeMKL; 
s.hoog      = s2023.hoog; 
s.laag      = s2023.laag; 
s.XzeeMKL   = s2023.XzeeMKL; 
s.XMinLand  = s2023.XMinLand;

% include relevant information from Jarkus
s.id   = id;   
s.cross = cross; 
s.along = along;
s.time        = time; 
s.time_bounds = time_bounds;
s.areaname = areaname; 
s.areacode = areacode;
% add for convenience
s.jarkusyear = jarkusyear; 


%% create MKL.nc
mkl2netcdf(OPT.outputfile, s,'processing_level', 'final');

