%LDB2NC  rewrites Deltares landboundary file with segment names file (line segments) to nc file
%
%See also: SNCTOOLS, LANDBOUNDARY, POLYSPLIT, POLYJOIN, POLY_FUN

% $Id: ldb2nc.m 
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

%% Initialize

   OPT.fillvalue         = nan; % NaNs do work in netcdf API
   OPT.dump              = 1;
   
   OPT.directory_raw     = [first_subdir(mfilename('fullpath'),-1),filesep,'raw'      ,filesep];
   OPT.directory_nc      = [first_subdir(mfilename('fullpath'),-1),filesep,'processed',filesep];

%% Set (1)

   OPT.file.landboundary = [OPT.directory_raw,'Netherlands_inclBelGer_RD.ldb'];
   OPT.file.names        = '';
   OPT.file.nc           = [OPT.directory_nc ,'holland_fillable'];
   OPT.title             = 'Fillable vector coastline of the Netherlands';
   OPT.epsg              = 28992;

%% 0 Read raw data
   
  [D.xpar,D.ypar]        = landboundary('read',OPT.file.landboundary);% ,999.999
  
   D.xpar(D.xpar==999.999)=nan;
   D.ypar(D.ypar==999.999)=nan;
  
  [D.lon ,D.lat ]        = convertCoordinates(D.xpar,D.ypar,'CS1.name','Amersfoort / RD New','CS2.code',4326);
%    D.names               = textread(OPT.file.names,'%s','delimiter','','commentstyle','matlab');
%    D.names               = char(D.names);
   D.version             = '$Revision$';
   
%% 0 Create file
   
   outputfile    = fullfile([OPT.file.nc,'.nc']);
   
   nc_create_empty (outputfile)
   
%% 1 Add global meta-info to file
% Add overall meta info:
% http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#description-of-file-contents
   
   nc_attput(outputfile, nc_global, 'title'           , OPT.title);
   nc_attput(outputfile, nc_global, 'institution'     , 'Deltares');
   nc_attput(outputfile, nc_global, 'source'          , 'Deltares');
   nc_attput(outputfile, nc_global, 'history'         , ['Original filename: ',filename(OPT.file.landboundary),...  % note any windows backward slash (\) not allowed in TDS 4.2
                                  ', version:' ,D.version,...
                                  ', tranformation to NetCDF: $HeadURL$']);
   nc_attput(outputfile, nc_global, 'references'      , '<http://openearth.eu>');
   nc_attput(outputfile, nc_global, 'email'           , '');
   
   nc_attput(outputfile, nc_global, 'comment'         , '');
   nc_attput(outputfile, nc_global, 'version'         , D.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
   
   nc_attput(outputfile, nc_global, 'terms_for_use'   , 'These data can be used freely for research purposes provided that the following source is acknowledged: Deltares.');
   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, 'segments'   , size(D.names,1));
%    nc_add_dimension(outputfile, 'name_strlen', size(D.names,2)); % 
   nc_add_dimension(outputfile, 'points'     , length(D.lon))  ; % incl nan-separators
   
%% 3 Create variables
   
   clear nc
   ifld = 0;
   
%    % Segment name
%    
%    ifld = ifld + 1;
%    nc(ifld).Name         = 'description';
%    nc(ifld).Nctype       = 'char';
%    nc(ifld).Dimension    = {'segments','name_strlen'};
%    nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'description');
%    nc(ifld).Attribute(2) = struct('Name', 'comment'        ,'Value', 'description of nan-separated segments');
%   %nc(ifld).Attribute(2) = struct('Name', 'standard_name'  ,'Value', 'station_id'); % standard name
   
%% WGS84
   
     ifld = ifld + 1;
   nc(ifld).Name         = 'WGS84';
   nc(ifld).Nctype       = 'int';
   nc(ifld).Dimension    = {}; % no dimension, dummy variable
   nc(ifld).Attribute    = nc_cf_grid_mapping(4326);

   % Longitude:
   % http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#longitude-coordinate
   
   ifld = ifld + 1;
   nc(ifld).Name         = 'lon';
   nc(ifld).Nctype       = 'float'; % no double needed
   nc(ifld).Dimension    = {'points'};
   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', 'comment'        ,'Value', 'segments are separated by NaN');
   nc(ifld).Attribute(5) = struct('Name', 'grid_mapping'   ,'Value', 'WGS84');
   
   % Latitude:
   % http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#latitude-coordinate
   
   ifld = ifld + 1;
   nc(ifld).Name         = 'lat';
   nc(ifld).Nctype       = 'float'; % no double needed
   nc(ifld).Dimension    = {'points'};
   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', 'comment'        ,'Value', 'segments are separated by NaN');
   nc(ifld).Attribute(5) = struct('Name', 'grid_mapping'   ,'Value', 'WGS84');
   
%% Coordinate system
%  http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#appendix-grid-mappings
   
     ifld = ifld + 1;
   nc(ifld).Name         = 'Oblique_Stereographic';
   nc(ifld).Nctype       = 'int';
   nc(ifld).Dimension    = {}; % no dimension, dummy variable
   nc(ifld).Attribute    = nc_cf_grid_mapping(OPT.epsg);
   
   % x:
   
   ifld = ifld + 1;
   nc(ifld).Name         = 'x';
   nc(ifld).Nctype       = 'float'; % no double needed
   nc(ifld).Dimension    = {'points'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'x coordinate');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'm');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'projection_x_coordinate');
   nc(ifld).Attribute(4) = struct('Name', 'comment'        ,'Value', 'segments are separated by NaN');
   nc(ifld).Attribute(5) = struct('Name', 'grid_mapping'   ,'Value', 'Oblique_Stereographic');
   
   % y:
   
   ifld = ifld + 1;
   nc(ifld).Name         = 'y';
   nc(ifld).Nctype       = 'float'; % no double needed
   nc(ifld).Dimension    = {'points'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'y coordinate');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'm');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'projection_y_coordinate');
   nc(ifld).Attribute(4) = struct('Name', 'comment'        ,'Value', 'segments are separated by NaN');
   nc(ifld).Attribute(5) = struct('Name', 'grid_mapping'   ,'Value', 'Oblique_Stereographic');

   % Parameters with standard names:
   % * http://cf-pcmdi.llnl.gov/documents/cf-standard-names/standard-name-table/current/standard-name-table/
   
%% 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)
       nc_addvar(outputfile, nc(ifld));
   end
   
%% 5 Fill variables
    
%    nc_varput(outputfile, 'description'          , D.names);
   nc_varput(outputfile, 'lon'                  , D.lon);
   nc_varput(outputfile, 'lat'                  , D.lat);
   nc_varput(outputfile, 'x'                    , D.xpar);
   nc_varput(outputfile, 'y'                    , D.ypar);
   nc_varput(outputfile, 'Oblique_Stereographic', OPT.epsg);
   nc_varput(outputfile, 'WGS84'                , 4326);
   
%% 6 Check
   
   if OPT.dump
       nc_dump(outputfile);
   end
   
%    x        = nc_varget(outputfile,'x');
%    y        = nc_varget(outputfile,'y');
%    fillPolygon([x y],'k','y',1000,1000);
%     lon        = nc_varget(outputfile,'lon');
%     lat        = nc_varget(outputfile,'lat');
%     kmlline(lat ,lon ,'fileName','KMLline_test.kml','lineColor',jet(3) ,'kmlName','horizontal')
   
%% EOF
