
%% load
% struct2xls('FW2010.xls',D,'units',{'deg','deg','100*kg/kg','100*kg/kg','100*kg/kg'})
% E = xls2struct('FW2010.xls')
%D = load('2010deep30cmsed')
D = load('2010surfsed')

%% make kml's
KMLscatter(D.lat1,D.lon1,D.cl,'fileName','cl.kml','kmlName','clay','cLim',[0 100],'CBcolorTitle','clay [%]','scalenormalState',1.0,'markerAlpha',1)
KMLscatter(D.lat1,D.lon1,D.si,'fileName','si.kml','kmlName','silt','cLim',[0 100],'CBcolorTitle','silt [%]','scalenormalState',0.8,'markerAlpha',1)
KMLscatter(D.lat1,D.lon1,D.sa,'fileName','sa.kml','kmlName','sand','cLim',[0 100],'CBcolorTitle','sand [%]','scalenormalState',0.6,'markerAlpha',1)

KMLmerge_files('sourceFiles',{'sa.kml','si.kml','cl.kml'},'filename','2010surfsed.kml','description','work of Steven ..... Ref:  www.tudelft.nl')

%% make netCDF
% struct2nc('FW2010.nc',D) % NIET NETJES: GEEN META INFO
% see: nc_cf_grid_write_lat_lon_curvilinear_tutorial

%% 1.a Create netCDF file

ncfile = '2010surfsed.nc';
nc_create_empty(ncfile)

%% 1.b Add overall meta info
%  http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#description-of-file-contents

   OPT.title                  = 'bodemn droehoek';
   OPT.institution            = 'TU Delft';
   OPT.source                 = 'gemeten op ';
   OPT.history                = ['tranformation to netCDF: $HeadURL$'];
   OPT.references             = 'intercoh';
   OPT.email                  = 'stgeven.@@@tudelft.nl';
   OPT.comment                = 'nog niet gechekct';
   OPT.version                = '0';
   OPT.acknowledge            =['These data can be used freely for research purposes provided that the following source is acknowledged: ',OPT.institution];
   OPT.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.';

   nc_attput(ncfile, nc_global, 'title'         , OPT.title);
   nc_attput(ncfile, nc_global, 'institution'   , OPT.institution);
   nc_attput(ncfile, nc_global, 'source'        , OPT.source);
   nc_attput(ncfile, nc_global, 'history'       , OPT.history);
   nc_attput(ncfile, nc_global, 'references'    , OPT.references);
   nc_attput(ncfile, nc_global, 'email'         , OPT.email);
   
   nc_attput(ncfile, nc_global, 'comment'       , OPT.comment);
   nc_attput(ncfile, nc_global, 'version'       , OPT.version);
   					   
   nc_attput(ncfile, nc_global, 'Conventions'   , 'CF-1.4');
   nc_attput(ncfile, nc_global, 'CF:featureType', 'Grid');  % https://cf-pcmdi.llnl.gov/trac/wiki/PointObservationConventions
   
   nc_attput(ncfile, nc_global, 'terms_for_use' , OPT.acknowledge);
   nc_attput(ncfile, nc_global, 'disclaimer'    , OPT.disclaimer);
   
%% 2 Create matrix span dimensions
%    http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#dimensions   

   nc_add_dimension(ncfile, 'locations', length(D.lat1));
   
%% 3.a Create coordinate variables: longitude
%      http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#longitude-coordinate

   clear nc;ifld = 1;
   nc(ifld).Name             = 'lon';
   nc(ifld).Nctype           = 'double';
   nc(ifld).Dimension        = {'locations'}; % !!!
   nc(ifld).Attribute(    1) = struct('Name', 'long_name'      ,'Value', 'longitude');
   nc(ifld).Attribute(end+1) = struct('Name', 'units'          ,'Value', 'degrees_east');
   nc(ifld).Attribute(end+1) = struct('Name', 'standard_name'  ,'Value', 'longitude');
   
   ifld = ifld + 1;
   nc(ifld).Name             = 'lat';
   nc(ifld).Nctype           = 'double';
   nc(ifld).Dimension        = {'locations'}; % !!!
   nc(ifld).Attribute(    1) = struct('Name', 'long_name'      ,'Value', 'latitude');
   nc(ifld).Attribute(end+1) = struct('Name', 'units'          ,'Value', 'degrees_north');
   nc(ifld).Attribute(end+1) = struct('Name', 'standard_name'  ,'Value', 'latitude');
   
   ifld = ifld + 1;
   nc(ifld).Name             = 'sand';
   nc(ifld).Nctype           = 'double';
   nc(ifld).Dimension        = {'locations'}; % !!!
   nc(ifld).Attribute(    1) = struct('Name', 'long_name'      ,'Value', 'sand percentage');
   nc(ifld).Attribute(end+1) = struct('Name', 'units'          ,'Value', '100*kg/kg');
   nc(ifld).Attribute(end+1) = struct('Name', 'coordinates'    ,'Value', 'lat lon');
   
   ifld = ifld + 1;
   nc(ifld).Name             = 'silt';
   nc(ifld).Nctype           = 'double';
   nc(ifld).Dimension        = {'locations'}; % !!!
   nc(ifld).Attribute(    1) = struct('Name', 'long_name'      ,'Value', 'silt percentage');
   nc(ifld).Attribute(end+1) = struct('Name', 'units'          ,'Value', '100*kg/kg');
   nc(ifld).Attribute(end+1) = struct('Name', 'coordinates'    ,'Value', 'lat lon');
   
   ifld = ifld + 1;
   nc(ifld).Name             = 'clay';
   nc(ifld).Nctype           = 'double';
   nc(ifld).Dimension        = {'locations'}; % !!!
   nc(ifld).Attribute(    1) = struct('Name', 'long_name'      ,'Value', 'clay percentage');
   nc(ifld).Attribute(end+1) = struct('Name', 'units'          ,'Value', '100*kg/kg');
   nc(ifld).Attribute(end+1) = struct('Name', 'coordinates'    ,'Value', 'lat lon');
   
%% 5.a Create all variables with attributes
   
   for ifld=1:length(nc)
      nc_addvar(ncfile, nc(ifld));   
   end   
   
%% 5.b Fill all variables

   nc_varput(ncfile, 'lon'          , D.lon1       );
   nc_varput(ncfile, 'lat'          , D.lat1       );
   nc_varput(ncfile, 'sand'         , D.sa);
   nc_varput(ncfile, 'silt'         , D.si);
   nc_varput(ncfile, 'clay'         , D.cl);

nc_dump(ncfile)   
   
E = nc2struct('2010surfsed.nc')

   
