
%% Converts ASCII files (swan_storm_startdate_enddate.txt) into netCDF format. 
% Data description: colomn 1: Date of measurement, format YYYYMMDD.HHMMSS;
%                   colomn 2: Wind speed (m/s);
%                   colomn 3: Wind direction (degrees);
%                   colomn 4: Waves: Hs (m);
%                   colomn 5: Waves: Tmean (sec);
%                   colomn 6: Waves: Direction mean (degrees);
%                                      
%% Remark: Wave and wind parameters are output from SWAN modelling.   

%% ---------
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);
Date = datenum(num2str(t,'%15.4f'), 'yyyymmdd.HHMM');
WinV = data(:,2); WinDir = data(:,3);
WavHs = data(:,4); WavTm = data(:,5); WavDmean = data(:,6); 

swanstorms = struct('WindV', WinV, ...
                    'WindDir', WinDir, ...                   
                    'WaveHs', WavHs, ...
                    'WaveTmean', WavTm, ...
                    'WaveDmean', WavDmean, ...
                    'Time', Date);

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

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

%% Define global attributes
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'title',                    'Wind and wave parameters');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'institution',              'Institute of Oceanology - BAS');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'source',                   'Data are output from SWAN modelling, grid point is located about 8km offshore of Shkorpilovtsi study site - Bulgaria');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'grid_point_location',      '43.03N, 27.93E, WGS84');
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','float',timedim);
netcdf.putAtt(ncid,timevar,'long_name','time');
netcdf.putAtt(ncid,timevar,'units','hours');

wdvvar = netcdf.defvar(ncid,'WindV','float', timedim);
netcdf.putAtt(ncid,wdvvar,'long_name','wind_speed');
netcdf.putAtt(ncid,wdvvar,'standard_name','wind_speed');
netcdf.putAtt(ncid,wdvvar,'units','m/sec');

wddvar = netcdf.defvar(ncid,'WindDir','double', timedim);
netcdf.putAtt(ncid,wddvar,'long_name','wind_direction');
netcdf.putAtt(ncid,wddvar,'standard_name','wind_from_direction');
netcdf.putAtt(ncid,wddvar,'units','degrees');

wvhmvar = netcdf.defvar(ncid,'WaveHs','float', timedim);
netcdf.putAtt(ncid,wvhmvar,'long_name','significant_wave_height');
netcdf.putAtt(ncid,wvhmvar,'units','metres');

wvtmvar = netcdf.defvar(ncid,'WaveTmean','float',timedim);
netcdf.putAtt(ncid,wvtmvar,'long_name','mean_wave_period');
netcdf.putAtt(ncid,wvtmvar,'units','seconds');

wvdmvar = netcdf.defvar(ncid,'WaveDmean','double',timedim);
netcdf.putAtt(ncid,wvdmvar,'long_name','mean_wave_period');
netcdf.putAtt(ncid,wvdmvar,'units','degrees');

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

%% Add data to the created variables
netcdf.putVar(ncid,wdvvar,swanstorms.WindV);
netcdf.putVar(ncid,wddvar,swanstorms.WindDir);
netcdf.putVar(ncid,wvhmvar,swanstorms.WaveHs);
netcdf.putVar(ncid,wvtmvar,swanstorms.WaveTmean);
netcdf.putVar(ncid,wvdmvar,swanstorms.WaveDmean);
netcdf.putVar(ncid,timevar,swanstorms.Time);

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