
%% Converts ASCII files (wind_startdate_enddate.txt) into netCDF format. 
% Data description: colomn 1: Date of measurement, format YYYYMMDD.HHMMSS;
%                   colomn 2: Wind direction - min (degrees);
%                   colomn 3: Wind direction - mean (degrees);
%                   colomn 4: Wind direction - max (degrees);
%                   colomn 5: Wind speed - min (m/s);
%                   colomn 6: Wind speed - mean (m/s);
%                   colomn 7: Wind speed - max (m/s);
%                   
%% ---------
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);
WindDate = datenum(num2str(t,'%15.6f'), 'yyyymmdd.HHMMSS');
WiDmin = data(:,2); WiDmean = data(:,3); WiDmax = data(:,4); 
WiVmin = data(:,5); WiVmean = data(:,6); WiVmax = data(:,7);

stormwinds = struct('WindDmin', WiDmin, ...
                   'WindDmean', WiDmean, ...                   
                   'WindDmax', WiDmax, ...
                   'WindVmin', WiVmin, ...
                   'WindVmean', WiVmean, ...
                   'WindVmax', WiVmax, ...
                   'Time', WindDate);

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

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

%% Define global attributes
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'title',                    'Wind 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 Vaisala weather station installed on the pier at Shkorpilovtsi study site - Bulgaria');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'weather_station_location', '42.958038263N, 27.899852161E , 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');

wdminvar = netcdf.defvar(ncid,'WindDmin','double', timedim);
netcdf.putAtt(ncid,wdminvar,'long_name','Minimum wind from direction');
netcdf.putAtt(ncid,wdminvar,'missing_value','-999');
netcdf.putAtt(ncid,wdminvar,'units','degrees');

wdmeanvar = netcdf.defvar(ncid,'WindDmean','double', timedim);
netcdf.putAtt(ncid,wdmeanvar,'long_name','Mean wind from direction');
netcdf.putAtt(ncid,wdmeanvar,'missing_value','-999');
netcdf.putAtt(ncid,wdmeanvar,'units','degrees');

wdmaxvar = netcdf.defvar(ncid,'WindDmax','double', timedim);
netcdf.putAtt(ncid,wdmaxvar,'long_name','Maximum wind from direction');
netcdf.putAtt(ncid,wdmaxvar,'missing_value','-999');
netcdf.putAtt(ncid,wdmaxvar,'units','degrees');

wvminvar = netcdf.defvar(ncid,'WindVmin','float',timedim);
netcdf.putAtt(ncid,wvminvar,'long_name','Minimum wind speed');
netcdf.putAtt(ncid,wvminvar,'missing_value','-999');
netcdf.putAtt(ncid,wvminvar,'units','m/sec');

wvmeanvar = netcdf.defvar(ncid,'WindVmean','float',timedim);
netcdf.putAtt(ncid,wvmeanvar,'long_name','Mean wind speed');
netcdf.putAtt(ncid,wvmeanvar,'missing_value','-999');
netcdf.putAtt(ncid,wvmeanvar,'units','m/sec');

wvmaxvar = netcdf.defvar(ncid,'WindVmax','float',timedim);
netcdf.putAtt(ncid,wvmaxvar,'long_name','Maximum wind speed');
netcdf.putAtt(ncid,wvmaxvar,'missing_value','-999');
netcdf.putAtt(ncid,wvmaxvar,'units','m/sec');

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

%% Add data to the created variables
netcdf.putVar(ncid,wdminvar,stormwinds.WindDmin);
netcdf.putVar(ncid,wdmeanvar,stormwinds.WindDmean);
netcdf.putVar(ncid,wdmaxvar,stormwinds.WindDmax);
netcdf.putVar(ncid,wvminvar,stormwinds.WindVmin);
netcdf.putVar(ncid,wvmeanvar,stormwinds.WindVmean);
netcdf.putVar(ncid,wvmaxvar,stormwinds.WindVmax);
netcdf.putVar(ncid,timevar,stormwinds.Time);

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