
%% Converts ASCII files (sediments_date.txt) into netCDF format. 
% Data description: column 1: date of measuremets, format YYYYMMDD;
%                   colomn 2: Y coordinate (Latitude), Northing, WGS84, format DD.DDDDDD;
%                   colomn 3: X coordinate (Longitude), Easting, WGS84, format DD.DDDDDD;
%                   colomn 4: shells content [ % ];
%                   colomn 5: coarse sand    [ % ];
%                   colomn 6: medium sand    [ % ];
%                   colomn 7: fine sand      [ % ];
%                   colomn 8: dust           [ % ].
%
% The columns 3-7 give sand sample grain size distribution (in %) 
% according to ASTM classification (Coastal Engineering manual, 2002,
% vol.III, Chapter 1, p.III-1-8).
%
% LEGEND:	              phi size	       size in [mm]
% fine gravel (shells)	   x > -2.25	   x > 4.76
% coarse sand	       -2.25 ÷ -1	    4.76 ÷ 2
% medium sand	          -1 ÷ 1.25	       2 ÷ 0.42
% fine sand	            1.25 ÷ 3.75	    0.42 ÷ 0.074
% dust	                   x < 3.75 	   x < 0.074

clear all

%% Fill in the path to the raw data
aa = input('Input the path and the name to raw data file');
data = load(aa); clear(aa);

%% Store data in transect series
t = data(:,1);
npoint = length(t);
date = t(1,1);
north = data(:,2); east = data(:,3); sh = data(:,4); crs = data(:,5); 
mds = data(:,6); fns = data(:,7); dst = data(:,8); 

sediments = struct('Time',       date, ...
                   'Latitude',   north, ...
                   'Longitude',  east,  ...                   
                   'Shells',     sh,    ...
                   'CoasreSand', crs,   ...
                   'MediumSand', mds,   ...
                   'FineSand',   fns,   ...
                   'Dust',       dst);
                   

%% Enter netCDF file name according to the name of the input raw data file or else...

%----- Create NetCDF file. 
ncid = netcdf.create('sediments_20100422.nc','nc_write');  

%% Define global attributes
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'title',             'Beach sediments');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'institution',       'Institute of Oceanology - BAS');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'source',            'Sand sampling is made during topographic surveys along profiles 1, 3, 5, 7, 9, 11 at beach shoreline, beach shoreface, middle beach and beach rearline at Kamchia-Shkorpilovtsi study site - Bulgaria');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'project_name',      'MICORE');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'project_url',       'www.micore.eu');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'e-mail',            'n.andreeva@io-bas.bg');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'conventions',       'CF-1.4');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'coordinate_system', ...
['PROJCS["WGS 84 / UTM zone 35N"',...
      'GEOGCS["WGS 84"',...
      '  DATUM["WGS_1984"',...
      '      SPHEROID["WGS 84",6378137,298.257223563',...
      '          AUTHORITY["EPSG","7030"]]',...
      '      AUTHORITY["EPSG","6326"]]',...
      '  PRIMEM["Greenwich",0',...
      '      AUTHORITY["EPSG","8901"]]',...
      '  UNIT["degree",0.01745329251994328',...
      '      AUTHORITY["EPSG","9122"]]',...
      '  AUTHORITY["EPSG","4326"]]',...
      '  UNIT["metre",1',...
      '  AUTHORITY["EPSG","9001"]]',...
      ' PROJECTION["Transverse_Mercator"]',...
      ' PARAMETER["latitude_of_origin",0]',...
      ' PARAMETER["central_meridian",27]',...
      ' PARAMETER["scale_factor",0.9996]',...
      ' PARAMETER["false_easting",500000]',...
      ' PARAMETER["false_northing",0]',...
      ' AUTHORITY["EPSG","32635"]',...
      ' AXIS["Easting",EAST]',...
      ' AXIS["Northing",NORTH]]']);

netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'Vertical_Datum', 'Baltic sea datum; Black sea MSL is 0.28m below the level at Kronshtadt pegel');
 
%% Define dimensions
pointdim = netcdf.defDim(ncid,'Npoint', length(t));
timedim = netcdf.defDim(ncid,'Time', 1);

%% Create variables and their attributes
northvar = netcdf.defvar(ncid,'Latitude','float',pointdim);
netcdf.putAtt(ncid,northvar,'long_name','latitude');
netcdf.putAtt(ncid,northvar,'units','degrees_north');
netcdf.putAtt(ncid,northvar,'standard_name','latitude');

eastvar = netcdf.defvar(ncid,'Longitude','float',pointdim);
netcdf.putAtt(ncid,eastvar,'long_name','longitude');
netcdf.putAtt(ncid,eastvar,'units','degrees_east');
netcdf.putAtt(ncid,eastvar,'standard_name','longitude');

shellvar = netcdf.defvar(ncid,'Shells','float', [timedim, pointdim]);
netcdf.putAtt(ncid,shellvar,'units','percent');
netcdf.putAtt(ncid,shellvar,'missing_value','-9999');
netcdf.putAtt(ncid,shellvar,'coordinates','Latitude Longitude');

crsvar = netcdf.defvar(ncid,'CoasreSand','float', [timedim, pointdim]);
netcdf.putAtt(ncid,crsvar,'units','percent');
netcdf.putAtt(ncid,crsvar,'missing_value','-9999');
netcdf.putAtt(ncid,crsvar,'coordinates','Latitude Longitude');

mdsvar = netcdf.defvar(ncid,'MediumSand','float', [timedim, pointdim]);
netcdf.putAtt(ncid,mdsvar,'units','percent');
netcdf.putAtt(ncid,mdsvar,'missing_value','-9999');
netcdf.putAtt(ncid,mdsvar,'coordinates','Latitude Longitude');

fnsvar = netcdf.defvar(ncid,'FineSand','float', [timedim, pointdim]);
netcdf.putAtt(ncid,fnsvar,'units','percent');
netcdf.putAtt(ncid,fnsvar,'missing_value','-9999');
netcdf.putAtt(ncid,fnsvar,'coordinates','Latitude Longitude');

dstvar = netcdf.defvar(ncid,'Dust','float', [timedim, pointdim]);
netcdf.putAtt(ncid,dstvar,'units','percent');
netcdf.putAtt(ncid,dstvar,'missing_value','-9999');
netcdf.putAtt(ncid,dstvar,'coordinates','Latitude Longitude');

timevar = netcdf.defvar(ncid,'Time','double',timedim);
netcdf.putAtt(ncid,timevar,'long_name','time');
netcdf.putAtt(ncid,timevar,'units','date in YYYYMMDD');


%% Exit define mode
netcdf.endDef(ncid);

%% Add data to the created variables
netcdf.putVar(ncid,eastvar,sediments.Longitude);
netcdf.putVar(ncid,northvar,sediments.Latitude);
netcdf.putVar(ncid,shellvar,sediments.Shells);
netcdf.putVar(ncid,crsvar,sediments.CoasreSand);
netcdf.putVar(ncid,mdsvar,sediments.MediumSand);
netcdf.putVar(ncid,fnsvar,sediments.FineSand);
netcdf.putVar(ncid,dstvar,sediments.Shells);
netcdf.putVar(ncid,timevar,sediments.Time);


%% Close netcdf file
netcdf.close(ncid);
