%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read the raw data file of the sea state measured by the Emilia-Romagna
% (Italy) wave bouy Nausicaa located in front of Cesenatico town and write 
% data into a NetCDF
% 
% 20110426 Andrea Valentini avalentini@arpa.emr.it
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fname='Nausicaa_20090101_20110420.txt';
dati=load(fname,'-ascii');
% name='Nausicaa_20090101_20110420';

aa=dati(:,1);       %year
MM=dati(:,2);       %month
dd=dati(:,3);       %day
hh=dati(:,4);       %hour
mm=dati(:,5);       %minute

swh=dati(:,6);      %significant wave height (m)
mwd=dati(:,7);      %mean wave direction (°N)
tm=dati(:,8);       %mean period (s)
tp=dati(:,9);       %peak period (s)
temp=dati(:,10);    %sea surface water temperature (°C)

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

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

% Add global atributes
nc_attput(filename, nc_global, 'title', 'Emilia-Romagna wave data, Cesenatico, Italy');
nc_attput(filename, nc_global, 'institution', 'ARPA-SIMC Emilia Romagna, Italy');
nc_attput(filename, nc_global, 'source', 'Nausicaa wave buoy (Cesenatico, Italy)');
nc_attput(filename, nc_global, 'geographic_location', '44.2155N	12.4766E');
nc_attput(filename, nc_global, 'Depth', '10m');
nc_attput(filename, nc_global, 'conventions', 'CF-1.4');
nc_attput(filename, nc_global, 'terms_for_use', 'These data are property of ARPA-SIMC 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);

SWHVariable = struct(...
    'Name', 'SWH', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Significan_Wave_height', 'Value', ' metre') ...
    );
nc_addvar(filename, SWHVariable);

MWDVariable = struct(...
    'Name', 'MWD', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Mean_Wave_Direction_(Nautical_Convention)', 'Value', 'degrees') ...
    );
nc_addvar(filename, MWDVariable);

TmVariable = struct(...
    'Name', 'Tm', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Mean_Wave_Period', 'Value', 'seconds') ...
    );
nc_addvar(filename, TmVariable);

TpVariable = struct(...
    'Name', 'Tp', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Peak_Period', 'Value', 'seconds') ...
    );
nc_addvar(filename, TpVariable);

TempVariable = struct(...
    'Name', 'Temp', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Sea_Surface_Temperature', 'Value', 'degrees Celcius') ...
    );
nc_addvar(filename, TempVariable);

% Store Variable
nc_varput(filename, 'DATE', date);
nc_varput(filename, 'SWH', swh);
nc_varput(filename, 'MWD', mwd);
nc_varput(filename, 'Tm', tm);
nc_varput(filename, 'Tp', tp);
nc_varput(filename, 'Temp', temp);

%End file
nc_dump(filename)
