%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read the wave data simulated by the ARPA-SIMC Sea State operational 
% forecasting system extracted on a point located in front of Cesenatico
% town and write data into a NetCDF
% 
% 20110426 Andrea Valentini avalentini@arpa.emr.it
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

null=find(dati==-9999.000);
dati(null)=NaN;

aa=dati(:,3);       %year
MM=dati(:,2);       %month
dd=dati(:,1);       %day
scad=dati(:,5);     %scad
swh=dati(:,6);      %significant wave height in m
mwd=dati(:,7);      %mean wave direction (°N)
tp=dati(:,8);       %peak period (s)
tm=dati(:,9);       %mean period (s)

date=[aa MM dd scad aa*00 aa*00];
date=datevec(datenum(date)); %trasformo la data di emissione con scadenza in data con orari
aa=date(:,1);       %year
MM=date(:,2);       %month
dd=date(:,3);       %day
hh=date(:,4);       %hour

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

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

% Add global atributes
nc_attput(filename, nc_global, 'title', 'SWAN model wave data, ARPA-SIMC, Italy');
nc_attput(filename, nc_global, 'institution', 'ARPA-SIMC Emilia Romagna, Italy');
nc_attput(filename, nc_global, 'source', 'MEDITARE ARPA-SIMC Sea State Operational System');
nc_attput(filename, nc_global, 'geographic_location', '44.217N  12.475E');
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);

% 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);

%End file
nc_dump(filename)
