%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read the raw data file of the wind measured in Porto Corsini (RA) -
% Italy and write data into a NetCDF
% 
% 20110426 Andrea Valentini avalentini@arpa.emr.it
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fname='RMN_porto_corsini_20060101_20110420_WIND.txt';
dati=load(fname,'-ascii');


aa=dati(:,1);       %year
MM=dati(:,2);       %month
dd=dati(:,3);       %day
hh=dati(:,4);       %hour
wd=dati(:,5);       %wind direction (°N)
ws=dati(:,6);       %wind speed (m/s)

date=[aa MM dd hh aa*00 aa*00];
[i,j]=size(date);

% Create new netCdf file
filename=('RMN_porto_corsini_WIND.nc');
nc_create_empty(filename)

% Add global atributes
nc_attput(filename, nc_global, 'title', 'Wind RMN-ISPRA Porto Corsini, Ravenna, Italy');
nc_attput(filename, nc_global, 'institution', 'ARPA-SIMC Emilia Romagna, Italy');
nc_attput(filename, nc_global, 'source', 'Wind measured by RMN-ISPRA at Porto Corsini (Ravenna, Italy)')
nc_attput(filename, nc_global, 'geographic_location', '44.4925N  12.282730555555556E')
nc_attput(filename, nc_global, 'conventions', 'CF-1.4');
nc_attput(filename, nc_global, 'terms_for_use', 'These data are property of ISPRA and can be used only by partners of the Micore EU Project (grant agreement n° 202798)');
nc_attput(filename, nc_global, 'disclaimer', 'These data are made available in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.');

%Add dimensions
nc_add_dimension(filename, 'Time', i)
nc_adddim(filename, 'datelength', j)

%Add variable attributes
DateVariable = struct(...
     'Name', 'DATE', ...
     'Nctype', 'double', ...
     'Dimension', {{'Time','datelength'}}, ...
     'Attribute', struct('Name', 'Date_and_Time_GMT', 'Value', '[YEAR MONTH DAY HOURS MINUTES SECONDS]') ...
     );
nc_addvar(filename, DateVariable);

WSVariable = struct(...
    'Name', 'WS', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Wind_Speed', 'Value', 'm/s') ...
    );
nc_addvar(filename, WSVariable);

WDVariable = struct(...
    'Name', 'WD', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Wind_Direction_(coming_from_convention)', 'Value', '°N') ...
    );
nc_addvar(filename, WDVariable);


% Store Variable
nc_varput(filename, 'DATE', date)
nc_varput(filename, 'WS', ws)
nc_varput(filename, 'WD', wd)

%End file
nc_dump(filename)
