%MONSTERPUNTEN2NC  run after MONSTERPUNTEN2KML
%
%See also: MONSTERPUNTEN2KML

%% 1. Create file

   outputfile = 'Monsterpunten_overview.nc';
   nc_create_empty (outputfile);
   
%% 2. Add overall meta info

   %%  CF convention   
   % <http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#description-of-file-contents>

   nc_attput(outputfile, nc_global, 'title'         , OPT.description);
   nc_attput(outputfile, nc_global, 'institution'   , 'Ecoshape');
   nc_attput(outputfile, nc_global, 'source'        , '');
   nc_attput(outputfile, nc_global, 'history'       , ['tranformation to NetCDF: $$']);
   % Provide web-link to originator of dataset (can be url in OpenEarthRawData).
   nc_attput(outputfile, nc_global, 'references'    , '<http://www.ecoshape.nl>');
   nc_attput(outputfile, nc_global, 'email'         , '');
   
   nc_attput(outputfile, nc_global, 'comment'       , '');
   nc_attput(outputfile, nc_global, 'version'       , OPT.version);
						    
   nc_attput(outputfile, nc_global, 'Conventions'   , 'CF-1.4');
   nc_attput(outputfile, nc_global, 'CF:featureType', '');  % https://cf-pcmdi.llnl.gov/trac/wiki/PointObservationConventions
   
   %%  OpenEarth convention

   nc_attput(outputfile, nc_global, 'terms_for_use' , 'These data can be used freely for research purposes provided that the following source is acknowledged: Ecoshape.');
   nc_attput(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(outputfile, 'locations', sum([D(:).n]));
   
   strlen = 0;
   for ifld=1:length(OPT.fields)
      strlen = max(length(OPT.fields{ifld}),strlen);
   end
   
   nc_add_dimension(outputfile, 'string_length', strlen);

%% 3. Create variables

   clear nc
   ifld = 0;
   
   %%% Latitude
   % Prescribed as dimension associated with variable by CF convention in:
   % <http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#latitude-coordinate>
   
      ifld = ifld + 1;
   nc(ifld).Name         = 'lat';   % This name is required when extracing the data with nc_varget(,'lat').
   nc(ifld).Nctype       = 'float'; % no double needed
   nc(ifld).Dimension    = {'locations'}; % Should conform with dimension lat defined above
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'latitude');      % Name free of choice, will appear in plots
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degrees_north'); % Note: 1st type of degrees, chosen from UDUNITS list.
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'latitude');      % Prescribed by CF convention and CF standard name table.
   
   
   %%% Longitude
   % Prescribed as dimension associated with variable by CF convention in:
   % <http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#longitude-coordinate>
   
      ifld = ifld + 1;
   nc(ifld).Name         = 'lon';   % This name is required when extracing the data with nc_varget(,'lat').
   nc(ifld).Nctype       = 'float'; % no double needed
   nc(ifld).Dimension    = {'locations'}; % Should conform with dimension lat defined above
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'longitude');    % Name free of choice, will appear in plots
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degrees_east'); % Note: 2nd type of degrees, chosen from UDUNITS list.
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'longitude');    % Prescribed by CF convention and CF standard name table.

   %%% local x
   % Should be associated with a coordinate system.
  
      ifld = ifld + 1;
   nc(ifld).Name         = 'x';
   nc(ifld).Nctype       = 'float'; % no double needed
   nc(ifld).Dimension    = {'locations'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'x');                       % Name free of choice, will appear in plots
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');                   % Chosen from UDUNITS list.
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'projection_x_coordinate'); % Prescribed by CF standard name table.

   %%% local y
   % Should be associated with a coordinate system.
   
      ifld = ifld + 1;
   nc(ifld).Name         = 'y';
   nc(ifld).Nctype       = 'float'; % no double needed
   nc(ifld).Dimension    = {'locations'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'y');                       % Name free of choice, will appear in plots
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'meter');                   % Chosen from UDUNITS list.
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'projection_y_coordinate'); % Prescribed by CF standard name table.

   %%% label
   
      ifld = ifld + 1;
   nc(ifld).Name         = 'label';
   nc(ifld).Nctype       = 'float'; % no double needed
   nc(ifld).Dimension    = {'locations'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'label');

   %%% zone
   
      ifld = ifld + 1;
   nc(ifld).Name         = 'name';
   nc(ifld).Nctype       = 'char'; % no double needed
   nc(ifld).Dimension    = {'locations','string_length'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'name');

%% 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. So there is no 
% need to use NC_PADHEADER.

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

%% 5 Fill variables

ncum = 0;
for ifld=1:length(OPT.fields)

   index = [ncum:ncum+D(ifld).n-1]';

   nc_varput(outputfile, 'lat'  , D(ifld).lat  ,ncum,D(ifld).n);
   nc_varput(outputfile, 'lon'  , D(ifld).lon  ,ncum,D(ifld).n);
   nc_varput(outputfile, 'x'    , D(ifld).X    ,ncum,D(ifld).n);
   nc_varput(outputfile, 'y'    , D(ifld).Y    ,ncum,D(ifld).n);
   nc_varput(outputfile, 'label', D(ifld).label,ncum,D(ifld).n);
   string = pad(OPT.fields{ifld},strlen);
   string = repmat(string,[D(ifld).n 1]);
   nc_varput(outputfile, 'name' , string       ,[ncum 0],[D(ifld).n strlen]);

   ncum = ncum + D(ifld).n;

end


%% 6 Check

   nc_dump(outputfile);
   
%% For more information see: <OpenEarth.Deltares.nl>

   D2 = nc2struct('Monsterpunten_overview.nc')
   [char(D2.name) repmat(' ',[sum([D(:).n]) 1]) num2str([D2.lat D2.lon D2.x D2.y D2.label])]