
%% Converts ASCII files (nshwaves_date_N.txt) into netCDF format, N - file number
% Data description: colomn 1: Date of measurement, format DDMMYYYY.HHMMSSS;
%                   colomn 2: Wave data from wave gauge 1 (metres);
%                    . . . . . . . . . . . . . . . . . . . .
%                   colomn 8: Wave data from wave gauge 7 (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);
WaveDate = datenum(num2str(t,'%18.9f'), 'ddmmyyyy.HHMMSSFFF');
WG1 = data(:,2); WG2 = data(:,3);
WG3 = data(:,4); WG4 = data(:,5);
WG5 = data(:,6); WG6 = data(:,7);
WG7 = data(:,8); 

stormwaves = struct('WaveGauge1', WG1, ...
                   'WaveGauge2', WG2, ...                   
                   'WaveGauge3', WG3, ...
                   'WaveGauge4', WG4, ...                   
                   'WaveGauge5', WG5, ...
                   'WaveGauge6', WG6, ...                   
                   'WaveGauge7', WG7, ...
                   'Time', WaveDate);

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

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

%% Define global attributes
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'title',                    'Wave ordinates');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'institution',              'Institute of Oceanology - BAS, Bulgaria');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'source',                   'Measurements are taken by wave gauges installed on the pier deck at Kamchia-Shkorpilovtsi study site - Bulgaria');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'wave_gauge_location',     ['WG1: 42.9578899N  27.8988921E', ...
                                                                                 'WG2: 42.9579262N  27.8991275E', ...
                                                                                 'WG3: 42.9579645N	27.8993746E', ...
                                                                                 'WG4: 42.9580201N	27.8997328E', ... 
                                                                                 'WG5: 42.9580754N	27.9000904E', ...
                                                                                 'WG6: 42.9581310N	27.9004520E', ...  
                                                                                 'WG7: 42.9581917N	27.9008113E', ...
                                                                                 'coordinate system: WGS84', 'format DD.DDDDDDDDD']);
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');

wg1var = netcdf.defvar(ncid,'WaveGauge1','float', timedim);
netcdf.putAtt(ncid,wg1var,'long_name','wave ordinates');
netcdf.putAtt(ncid,wg1var,'units','metres');

wg2var = netcdf.defvar(ncid,'WaveGauge2','float', timedim);
netcdf.putAtt(ncid,wg2var,'long_name','wave ordinates');
netcdf.putAtt(ncid,wg2var,'missing_value','-9999');
netcdf.putAtt(ncid,wg2var,'units','metres');

wg3var = netcdf.defvar(ncid,'WaveGauge3','float', timedim);
netcdf.putAtt(ncid,wg3var,'long_name','wave ordinates');
netcdf.putAtt(ncid,wg3var,'units','metres');

wg4var = netcdf.defvar(ncid,'WaveGauge4','float', timedim);
netcdf.putAtt(ncid,wg4var,'long_name','wave ordinates');
netcdf.putAtt(ncid,wg4var,'units','metres');

wg5var = netcdf.defvar(ncid,'WaveGauge5','float', timedim);
netcdf.putAtt(ncid,wg5var,'long_name','wave ordinates');
netcdf.putAtt(ncid,wg5var,'units','metres');

wg6var = netcdf.defvar(ncid,'WaveGauge6','float', timedim);
netcdf.putAtt(ncid,wg6var,'long_name','wave ordinates');
netcdf.putAtt(ncid,wg6var,'units','metres');

wg7var = netcdf.defvar(ncid,'WaveGauge7','float', timedim);
netcdf.putAtt(ncid,wg7var,'long_name','wave ordinates');
netcdf.putAtt(ncid,wg7var,'units','metres');


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

%% Add data to the created variables
netcdf.putVar(ncid,wg1var,stormwaves.WaveGauge1);
netcdf.putVar(ncid,wg2var,stormwaves.WaveGauge2);
netcdf.putVar(ncid,wg3var,stormwaves.WaveGauge3);
netcdf.putVar(ncid,wg4var,stormwaves.WaveGauge4);
netcdf.putVar(ncid,wg5var,stormwaves.WaveGauge5);
netcdf.putVar(ncid,wg6var,stormwaves.WaveGauge6);
netcdf.putVar(ncid,wg7var,stormwaves.WaveGauge7);
netcdf.putVar(ncid,timevar,stormwaves.Time);

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