
%% Converts ASCII files (waves_startdate_enddate.txt) into netCDF format. 
% Data description: colomn 1: Date of measurement, format YYYYMMDD.HHMM;
%                   colomn 2: Hs - significant wave height (meters), Hs=4*sqrt(m0), where m0 - zeroth spectral moment;
%                   colomn 3: Tp - peak wave period (seconds); period associated with largest peak in the power spectrum;
%                   colomn 4: Dp - peak wave direction (degrees); peak direction at the peak period;
%                   colomn 5: Depth - water level from pressure sensor (millimeters);
%                   colomn 6: H1/10 - (10% highest waves), H1/10 = 1.27*Hs;
%                   colomn 7: Tmean - mean wave period (seconds); T01 = Tmean = m0/m1;
%                   colomn 8: Dmean - mean peak wave direction (degrees);
%
%% Remark: Wave parameters are calculated from wave records registered by the ACDP once per hour for 20min.   

%% ---------
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);
WaveDate = datenum(num2str(t,'%15.4f'), 'yyyymmdd.HHMM');
Hs = data(:,2); Tp = data(:,3); Dp = data(:,4); depth = data(:,5);
Hten = data(:,6); Tm = data(:,7); Dm = data(:,8);

stormwaves = struct('Hsig', Hs, ...
                   'Tpeak', Tp, ...                   
                   'Dpeak', Dp, ...
                   'Depth', depth, ...
                   'Hten', Hten, ...
                   'Tmean', Tm, ...
                   'Dmean', Dm, ....
                   'Time', WaveDate);

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

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

%% Define global attributes
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'title',             'Wave parameters');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'institution',       'Institute of Oceanology - BAS');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'source',            'Measurements are taken by TeledyneRDI WH 600kHz ADCP deployed at 18m depth offshore of Shkorpilovtsi study site - Bulgaria');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'adcp_location',     '42.960716666N, 27.908016666E, WGS84, format DD.DDDDDDDDD');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'history',           'The parameters are calculated from ADCP wave records processed by TeledyneRDI WavesMon v.3.04');
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');
 
%% Define dimensions
timedim = netcdf.defDim(ncid,'Time', length(t));

%% Create variables and their attributes
timevar = netcdf.defvar(ncid,'Time','double',timedim);
netcdf.putAtt(ncid,timevar,'long_name','time');
netcdf.putAtt(ncid,timevar,'units','hours');

hsvar = netcdf.defvar(ncid,'Hs','float',timedim);
netcdf.putAtt(ncid,hsvar,'long_name','Significant wave height');
netcdf.putAtt(ncid,hsvar,'standard_name','sea_surface_wave_significant_height');
netcdf.putAtt(ncid,hsvar,'missing_value','-999');
netcdf.putAtt(ncid,hsvar,'units','metre');

tpvar = netcdf.defvar(ncid,'Tpeak','float',timedim);
netcdf.putAtt(ncid,tpvar,'long_name','Peak wave period');
netcdf.putAtt(ncid,tpvar,'missing_value','-999');
netcdf.putAtt(ncid,tpvar,'units','second');

dpvar = netcdf.defvar(ncid,'Dpeak','double', timedim);
netcdf.putAtt(ncid,dpvar,'long_name','Peak wave direction');
netcdf.putAtt(ncid,dpvar,'missing_value','-999');
netcdf.putAtt(ncid,dpvar,'units','degrees');

depthvar = netcdf.defvar(ncid,'Depth','double', timedim);
netcdf.putAtt(ncid,depthvar,'long_name','Depth above pressure sensor');
netcdf.putAtt(ncid,depthvar,'units','millimetre');

htenvar = netcdf.defvar(ncid,'H_1_10','float', timedim);
netcdf.putAtt(ncid,htenvar,'long_name','10 percent highest waves');
netcdf.putAtt(ncid,htenvar,'missing_value','-999');
netcdf.putAtt(ncid,htenvar,'units','metre');

tmvar = netcdf.defvar(ncid,'Tmean','float', timedim);
netcdf.putAtt(ncid,tmvar,'long_name','Mean wave period');
netcdf.putAtt(ncid,tmvar,'missing_value','-999');
netcdf.putAtt(ncid,tmvar,'units','second');

dmvar = netcdf.defvar(ncid,'Dmean','double', timedim);
netcdf.putAtt(ncid,dmvar,'long_name','Mean peak wave direction');
netcdf.putAtt(ncid,dmvar,'missing_value','-999');
netcdf.putAtt(ncid,dmvar,'units','degree');

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

%% Add data to the created variables
netcdf.putVar(ncid,hsvar,stormwaves.Hsig);
netcdf.putVar(ncid,tpvar,stormwaves.Tpeak);
netcdf.putVar(ncid,dpvar,stormwaves.Dpeak);
netcdf.putVar(ncid,depthvar,stormwaves.Depth);
netcdf.putVar(ncid,htenvar,stormwaves.Hten);
netcdf.putVar(ncid,tmvar,stormwaves.Tmean);
netcdf.putVar(ncid,dmvar,stormwaves.Dmean);
netcdf.putVar(ncid,timevar,stormwaves.Time);

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