
%% 0a Set options
p = fileparts(mfilename('fullpath'));

OPT.fillvalue    = NaN;
OPT.refdatenum   = datenum(1970,1,1);
OPT.timezone     = timezone_code2iso('GMT');
OPT.outputfile   = fullfile(first_subdir(p,-1),'processed','strandlijnen.nc');
OPT.disp         = true;
OPT.pause        = false;
OPT.dump         = true;

%% 0b Read pre-processed raw data data

load(fullfile(p,'StrandLijnData.mat'));

%% 1a Create file

nc_create_empty (OPT.outputfile)

%% Add overall meta info
%  http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#description-of-file-contents
%------------------
nc_attput(OPT.outputfile, nc_global, 'title'         , 'Dutch Beach Lines between 1843 and 1980');
nc_attput(OPT.outputfile, nc_global, 'institution'   , 'Rijkswaterstaat');
nc_attput(OPT.outputfile, nc_global, 'source'        , 'Rijkswaterstaat; DID-DSDS-Data Beheer');
nc_attput(OPT.outputfile, nc_global, 'history'       , '');
nc_attput(OPT.outputfile, nc_global, 'references'    , '');
nc_attput(OPT.outputfile, nc_global, 'email'         , 'did-info@rws.nl');

nc_attput(OPT.outputfile, nc_global, 'comment'       , 'The file contains position of mean low water, mean high water, and dunefoot');
nc_attput(OPT.outputfile, nc_global, 'version'       , '0.1');

nc_attput(OPT.outputfile, nc_global, 'Conventions'   , 'CF-1.4');
nc_attput(OPT.outputfile, nc_global, 'CF:featureType', 'stationTimeSeries');  % https://cf-pcmdi.llnl.gov/trac/wiki/PointObservationConventions

nc_attput(OPT.outputfile, nc_global, 'terms_for_use' , 'These data can be used freely for research purposes provided that the following source is acknowledged: KNMI.');
nc_attput(OPT.outputfile, nc_global, 'disclaimer'    , 'This data is made available in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.');

%% 2 Create dimensions

nc_add_dimension(OPT.outputfile, 'year'        , length(D.year))
nc_add_dimension(OPT.outputfile, 'trID'        , length(D.trID))

%% 3 Create variables

clear nc
ifld = 0;

%% transect ID

ifld = ifld + 1;
nc(ifld).Name         = 'trID';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'number');
nc(ifld).Attribute(2) = struct('Name', 'standard_name'  ,'Value', 'station_id');
nc(ifld).Attribute(3) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);

%% year

ifld = ifld + 1;
nc(ifld).Name         = 'year';
nc(ifld).Nctype       = 'float';
nc(ifld).Dimension    = {'year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'year');
nc(ifld).Attribute(2) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);

%% Time
% http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#time-coordinate
% time is a dimension, so there are two options:
% * the variable name needs the same as the dimension
%   http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#id2984551
% * there needs to be an indirect mapping through the coordinates attribute
%   http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#id2984605


ifld = ifld + 1;
nc(ifld).Name         = 'time';
nc(ifld).Nctype       = 'double'; % float not sufficient as datenums are big: double
nc(ifld).Dimension    = {'year'}; % QuickPlot error: plots dimensions instead of datestr
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'time');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value',['days since ',datestr(OPT.refdatenum,'yyyy-mm-dd'),' 00:00:00 ',OPT.timezone]);
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'time');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);

%% parameter values 


%% cross shore distance
% RSP:
ifld = ifld + 1;
nc(ifld).Name         = 'DF';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'Dune Foot Line');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');
nc(ifld).Attribute(3) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);  

ifld = ifld + 1;
nc(ifld).Name         = 'MHW';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'Mean High water Line');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');
nc(ifld).Attribute(3) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);  

ifld = ifld + 1;
nc(ifld).Name         = 'MLW';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'Mean Low water Line');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');
nc(ifld).Attribute(3) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);  

%% RDx
ifld = ifld + 1;
nc(ifld).Name         = 'DF_x';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'x');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'project_x_coordinate');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
nc(ifld).Attribute(5) = struct('Name', 'coordinates'    ,'Value', 'DF_lat DF_lon');

ifld = ifld + 1;
nc(ifld).Name         = 'MHW_x';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'x');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'project_x_coordinate');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
nc(ifld).Attribute(5) = struct('Name', 'coordinates'    ,'Value', 'MHW_lat MHW_lon');

ifld = ifld + 1;
nc(ifld).Name         = 'MLW_x';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'x');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'project_x_coordinate');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
nc(ifld).Attribute(5) = struct('Name', 'coordinates'    ,'Value', 'MLW_lat MLW_lon');

%% RDy
ifld = ifld + 1;
nc(ifld).Name         = 'DF_y';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'y');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'project_y_coordinate');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
nc(ifld).Attribute(5) = struct('Name', 'coordinates'    ,'Value', 'DF_lat DF_lon');

ifld = ifld + 1;
nc(ifld).Name         = 'MHW_y';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'y');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'project_y_coordinate');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
nc(ifld).Attribute(5) = struct('Name', 'coordinates'    ,'Value', 'MHW_lat MHW_lon');

ifld = ifld + 1;
nc(ifld).Name         = 'MLW_y';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'y');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'project_y_coordinate');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
nc(ifld).Attribute(5) = struct('Name', 'coordinates'    ,'Value', 'MLW_lat MLW_lon');

%% Latitude
% http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#latitude-coordinate
ifld = ifld + 1;
nc(ifld).Name         = 'DF_lat';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'latitude');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degrees_north');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'latitude');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);

ifld = ifld + 1;
nc(ifld).Name         = 'MHW_lat';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'latitude');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degrees_north');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'latitude');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);

ifld = ifld + 1;
nc(ifld).Name         = 'MLW_lat';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'latitude');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degrees_north');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'latitude');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);

%% Longitude
% http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#longitude-coordinate
ifld = ifld + 1;
nc(ifld).Name         = 'DF_lon';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'longitude');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degrees_east');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'longitude');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);

ifld = ifld + 1;
nc(ifld).Name         = 'MHW_lon';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'longitude');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degrees_east');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'longitude');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);

ifld = ifld + 1;
nc(ifld).Name         = 'MLW_lon';
nc(ifld).Nctype       = 'float'; % no double needed
nc(ifld).Dimension    = {'trID','year'};
nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'longitude');
nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degrees_east');
nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'longitude');
nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);

%% crs (Rijskdriehoek)

%ifld = ifld + 1;
%nc(ifld).Name         = 'projection';
%nc(ifld).Nctype       = 'float'; % no double needed
%nc(ifld).Attribute(1) = struct('Name', 'projection'      ,'Value', '');

%% Parameters with standard names
% * http://cf-pcmdi.llnl.gov/documents/cf-standard-names/standard-name-table/current/

%% 4 Create variables with attibutes
% When variable definitons are created before actually writing the
% data in the next cell, netCDF can nicely fit all data into the
% file without the need to relocate any info.

for ifld=1:length(nc)
    if OPT.disp;disp(['adding ',num2str(ifld),' ',nc(ifld).Name]);end
    nc_addvar(OPT.outputfile, nc(ifld));
end

%% 5 Fill variables

nc_varput(OPT.outputfile, 'trID'   , D.trID);
nc_varput(OPT.outputfile, 'year'   , D.year);
nc_varput(OPT.outputfile, 'time'   , datenum(D.year,1,1)-OPT.refdatenum);

nc_varput(OPT.outputfile, 'DF'     , D.DF);
nc_varput(OPT.outputfile, 'MHW'    , D.MHW);
nc_varput(OPT.outputfile, 'MLW'    , D.MLW);

nc_varput(OPT.outputfile, 'DF_x'   , D.xDF);
nc_varput(OPT.outputfile, 'MHW_x'  , D.xMHW);
nc_varput(OPT.outputfile, 'MLW_x'  , D.xMLW);

nc_varput(OPT.outputfile, 'DF_y'   , D.yDF);
nc_varput(OPT.outputfile, 'MHW_y'  , D.yMHW);
nc_varput(OPT.outputfile, 'MLW_y'  , D.yMLW);

nc_varput(OPT.outputfile, 'DF_lat' , D.latDF);
nc_varput(OPT.outputfile, 'MHW_lat', D.latMHW);
nc_varput(OPT.outputfile, 'MLW_lat', D.latMLW);

nc_varput(OPT.outputfile, 'DF_lon' , D.lonDF);
nc_varput(OPT.outputfile, 'MHW_lon', D.lonMHW);
nc_varput(OPT.outputfile, 'MLW_lon', D.lonMLW);

%% 6 Check

if OPT.dump
    nc_dump(OPT.outputfile);
end

%% Pause

if OPT.pause
    pausedisp
end


%% EOF