
%% Converts ASCII files (bathymetry*date*.txt) into netCDF format. 
% Data description: colomn 1: Date of measurement, format YYYYMMDD;
%                   colomn 2: Y coordinate (Latitude), Northing, WGS84, format DD.DDDDD
%                   colomn 3: X coordinate (Longitude), Easting, WGS84, format DD.DDDDD
%                   colomn 4: Depth (in metres);
%                   
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);
depth = data(:,4);

depth = struct('Latitude', north, ...
               'Longitude', east, ...                   
               'Depth', depth, ...
               'Time', date);

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

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

%% Define global attributes
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'title',             'Beach bathymetry');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'institution',       'Institute of Oceanology - BAS');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'source',            'Measurements are taken 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');

depvar = netcdf.defvar(ncid,'Depth','double', [timedim, pointdim]);
netcdf.putAtt(ncid,depvar,'units','metre');
netcdf.putAtt(ncid,depvar,'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,depth.Longitude);
netcdf.putVar(ncid,northvar,depth.Latitude);
netcdf.putVar(ncid,depvar,depth.Depth);
netcdf.putVar(ncid,timevar,depth.Time);


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