% Example:
%
%
%
%See also:

OPT.fillvalue    = NaN;
OPT.refdatenum   = datenum(1970,1,1);
OPT.outputfile   = 'KustLijn.nc';
OPT.timezone     = timezone_code2iso('GMT');
OPT.disp         = true;
OPT.pause        = false;
OPT.dump         = true;

%% 0 Read raw data

load('KustLijnData.mat');
varName = {'BKL','TKL','TND'};
longName = {'Basis Kust Lijn','te Toetsen Kustlijn','Trend Kustlijn'};

%% 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'         , '');
nc_attput(OPT.outputfile, nc_global, 'institution'   , 'Rijkswaterstaat');
nc_attput(OPT.outputfile, nc_global, 'source'        , '');
nc_attput(OPT.outputfile, nc_global, 'history'       , '');
nc_attput(OPT.outputfile, nc_global, 'references'    , '');
nc_attput(OPT.outputfile, nc_global, 'email'         , '');

nc_attput(OPT.outputfile, nc_global, 'comment'       , '');
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: doubble
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 
for ii=1:length(varName)
    %% Longitude
    % http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#longitude-coordinate
    ifld = ifld + 1;
    nc(ifld).Name         = [varName{ii} '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);

    %% Latitude
    % http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#latitude-coordinate
    ifld = ifld + 1;
    nc(ifld).Name         = [varName{ii} '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);

    %% x
    ifld = ifld + 1;
    nc(ifld).Name         = [varName{ii} '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', [varName{ii} 'Lon ' varName{ii} 'Lat']);

    %% y
    ifld = ifld + 1;
    nc(ifld).Name         = [varName{ii} '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', [varName{ii} 'Lon ' varName{ii} 'Lat']);

    %% cross shore distance
    % RWS:
    ifld = ifld + 1;
    nc(ifld).Name         = varName{ii};
    nc(ifld).Nctype       = 'float'; % no double needed
    nc(ifld).Dimension    = {'trID','year'};
    nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', longName{ii});
    nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');
    nc(ifld).Attribute(3) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
end
%% 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);
for ii = 1:length(varName)
    eval(['nc_varput(OPT.outputfile, ''' varName{ii} '''          , D.'    varName{ii} ')']);
    eval(['nc_varput(OPT.outputfile, ''' varName{ii} 'Lat''       , D.Lat' varName{ii} ')']);
    eval(['nc_varput(OPT.outputfile, ''' varName{ii} 'Lon''       , D.Lon' varName{ii} ')']);
    eval(['nc_varput(OPT.outputfile, ''' varName{ii} 'x''         , D.x'   varName{ii} ')']);
    eval(['nc_varput(OPT.outputfile, ''' varName{ii} 'y''         , D.y'   varName{ii} ')']);
end
%% 6 Check

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

%% Pause

if OPT.pause
    pausedisp
end


%% EOF