%SEDIMENTATLAS_KORREL2NC  This is a first test to get scatter data into NetCDF
%
%  Scatter data are trajectories without a time vector, see examples:
%  http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#id2986393
%  http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#id2984845
%
%See also: SNCTOOLS, KNMI_POTWIND2NC_TEST

%% Initialize

OPT.dump              = 0;
OPT.disp              = 0;
OPT.debug             = 0;

%% 0 Read raw data

   D = sedimentatlas_korrel_read('korrel.txt');
   
   [D.lon,D.lat] = convertcoordinates(D.X_COORD,D.Y_COORD,'CS1.code',28992,'CS2.code',4326)
   
   if OPT.debug
   plot(d.lon,d.lat,'.')
   axislat(52)
   end

%% 0 Create file

   outputfile = 'korrel.nc';

   %% 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_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'      , '');
   nc_attput(outputfile, nc_global, 'institution', 'Rijkswaterstaat');
   nc_attput(outputfile, nc_global, 'source'     , 'surface observation');
   nc_attput(outputfile, nc_global, 'history'    , ['Tranformed to NetCDF by $Id$ $HeadURL$']);
   nc_attput(outputfile, nc_global, 'references' , ['http://www.waddenzee.nl/Sedimentatlas.729.0.html,','http://dx.doi.org/10.1016/j.ecss.2008.01.009']);
   nc_attput(outputfile, nc_global, 'comment'    , '''Korrelgrootteverdeling'' was exported from Sedimentatlas to ASCII file before conversion to NetCDF.');

   nc_attput(outputfile, nc_global, 'Conventions', 'CF-1.4');
   
     %% Discovery information:
     %  http://www.unidata.ucar.edu/software/netcdf-java/formats/DataDiscoveryAttConvention.html (draft)
     %  http://www.unidata.ucar.edu/projects/THREDDS/tech/catalog/InvCatalogSpec11.html (v1.0.1)
     
%TO DO :Metadata_Conventions = "Unidata Dataset Discovery v1.0";      

   nc_attput(outputfile, nc_global, 'geospatial_lat_min'         , min(D.lat(:)));
   nc_attput(outputfile, nc_global, 'geospatial_lat_max'         , max(D.lat(:)));
   nc_attput(outputfile, nc_global, 'geospatial_lon_min'         , min(D.lon(:)));
   nc_attput(outputfile, nc_global, 'geospatial_lon_max'         , max(D.lon(:)));
   
   nc_attput(outputfile, nc_global, 'geospatial_lat_units'       , 'degrees_north');
   nc_attput(outputfile, nc_global, 'geospatial_lon_units'       , 'degrees_east' );

%% 2 Create dimensions

   nc_add_dimension(outputfile, 'particle_diameter', size(D.PHI,1));
   nc_add_dimension(outputfile, 'locations'        , size(D.PHI,2));

%% 3 Create variables

   clear nc
   ifld = 0;

   % 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';
   nc(ifld).Dimension    = {'locations'};
   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');
    
   % 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';
   nc(ifld).Dimension    = {'locations'};
   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');
   
   ifld = ifld + 1;
   nc(ifld).Name         = 'ID';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'locations'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'       ,'Value', 'ID');
   nc(ifld).Attribute(2) = struct('Name', 'units'           ,'Value', '');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'   ,'Value', 'ID');
    
   ifld = ifld + 1;
   nc(ifld).Name         = 'phi';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'locations','particle_diameter'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      , 'Value', 'percentage material on sieve by weight');
   nc(ifld).Attribute(2) = struct('Name', 'units'          , 'Value', '100*kilogram/kilogram');
   nc(ifld).Attribute(3) = struct('Name', 'comment'        , 'Value', 'percentage material on sieve by weight');
   nc(ifld).Attribute(4) = struct('Name', 'coordinates'    , 'Value', 'lat lon'); % y first
    
   ifld = ifld + 1;
   nc(ifld).Name         = 'cumphi';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'locations','particle_diameter'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'   , 'Value', 'cumulative percentage material on sieve by weight');
   nc(ifld).Attribute(2) = struct('Name', 'units'       , 'Value', '100*kilogram/kilogram');
   nc(ifld).Attribute(3) = struct('Name', 'comment'     , 'Value', 'cumulative percentage material on sieve by weight');
   nc(ifld).Attribute(4) = struct('Name', 'coordinates' , 'Value', 'lat lon'); % y first

   ifld = ifld + 1;
   nc(ifld).Name         = 'diameter';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'particle_diameter'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name' , 'Value', 'grain size diameter');
   nc(ifld).Attribute(2) = struct('Name', 'units'     , 'Value', 'micrometer');
   nc(ifld).Attribute(3) = struct('Name', 'comment'   , 'Value', 'grain size diameter');

   ifld = ifld + 1;
   nc(ifld).Name         = 'phi_value';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'particle_diameter'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name', 'Value', 'phi');
   nc(ifld).Attribute(2) = struct('Name', 'units'    , 'Value', '');
   nc(ifld).Attribute(3) = struct('Name', 'comment'  , 'Value', 'phi');

  % ifld = ifld + 1;
  % nc(ifld).Name         = 'D50';
  % nc(ifld).Nctype       = 'float';
  % nc(ifld).Dimension    = {'particle_diameter'};
  % nc(ifld).Attribute(1) = struct('Name', 'long_name', 'Value', 'mean particle diameter');
  % nc(ifld).Attribute(2) = struct('Name', 'units'    , 'Value', 'micrometer');
  % nc(ifld).Attribute(3) = struct('Name', 'comment'  , 'Value', '');
  %
  % ifld = ifld + 1;
  % nc(ifld).Name         = 'D90';
  % nc(ifld).Nctype       = 'float';
  % nc(ifld).Dimension    = {'particle_diameter'};
  % nc(ifld).Attribute(1) = struct('Name', 'long_name', 'Value', '90-percentile particle diameter');
  % nc(ifld).Attribute(2) = struct('Name', 'units'    , 'Value', 'micrometer');
  % nc(ifld).Attribute(3) = struct('Name', 'comment'  , 'Value', '');

   ifld = ifld + 1;
   nc(ifld).Name         = 'pmud16_from_16';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'locations'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name', 'Value', 'mud content');
   nc(ifld).Attribute(2) = struct('Name', 'units'    , 'Value', 'percent');
   nc(ifld).Attribute(3) = struct('Name', 'comment'  , 'Value', D.pmud16_from_16_info);

   ifld = ifld + 1;
   nc(ifld).Name         = 'pmud16_from_63';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'locations'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name', 'Value', 'mud content');
   nc(ifld).Attribute(2) = struct('Name', 'units'    , 'Value', 'percent');
   nc(ifld).Attribute(3) = struct('Name', 'comment'  , 'Value', D.pmud16_from_63);

%% 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([num2str(ifld),' ',nc(ifld).Name]);end
       nc_addvar(outputfile, nc(ifld));
   end

%% 5 Fill variables

   nc_varput(outputfile, 'lon'           , D.lon);
   nc_varput(outputfile, 'lat'           , D.lat);
   nc_varput(outputfile, 'ID'            , D.KORREL_ID);
   nc_varput(outputfile, 'phi'           , D.PHI');
   nc_varput(outputfile, 'cumphi'        , D.cumPHI');
   nc_varput(outputfile, 'diameter'      , D.D);
   nc_varput(outputfile, 'phi_value'     , D.PHIvalue);
  %nc_varput(outputfile, 'D50'           , D.D50);
  %nc_varput(outputfile, 'D90'           , D.D90);
   nc_varput(outputfile, 'pmud16_from_16', D.pmud16_from_16);
   nc_varput(outputfile, 'pmud16_from_63', D.pmud16_from_63);
   
%% 6 Check

   if OPT.dump
       nc_dump(outputfile);
   end

%% EOF