%Load and read the data
datadir=fullfile('..','raw\S4');
files=dir(fullfile(datadir, '*.xls'));
xls_file = 's4_april09_inlet.xls';
[dataset,txt]=xlsread(fullfile(datadir, xls_file),'S4_April09');
dates = datenum(txt(3:end,2),'mm/dd/yyyy');
times = dates+dataset(:,3); %Add hours to dates
depth = dataset(:,5); %Mean high water above sensor
hsig = dataset(:,7); %Significant wave height
tz = dataset(:,11); %zero-upcrossing wave period (Tz)
wdir = dataset(:,15); %Wave direction (in direction wave is going to)
Vnorth = dataset(:,19);%Current speed north
Vnorth(isnan(Vnorth))=999; %Set Nans to 999
Veast = dataset(:,21);%Current speed east
Veast(isnan(Veast))=999; %Set Nans to 999
Dir = dataset(:,24);%Current direction
Dir(isnan(Dir))=999; %Set Nans to 999

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

% Add global atributes
nc_attput(filename, nc_global, 'title', 'Bevano river mouth S4 measurements');
nc_attput(filename, nc_global, 'institution', 'Università di Ferrara');
nc_attput(filename, nc_global, 'source', 'S4 measurements')
nc_attput(filename, nc_global, 'comment', 'Located at mouth of Bevano River')

%Add dimensions
nc_add_dimension(filename,'points',length(times))
%nc_add_dimension(filename,'elev',length(elev))

%Add variable attributes
varstruct.Name='t';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'t','units','days')
nc_attput(filename,'t','long_name','time')
nc_attput(filename, 't', 'standard_name', 'time')
nc_attput(filename, 't', 'comment', 'days since 1 January 0 AD (standard MATLAB date format)')

varstruct.Name='D';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'D','units','m')
nc_attput(filename,'D','long_name','depth')
nc_attput(filename, 'D', 'standard_name', 'depth')
nc_attput(filename, 'D', 'comment', 'Mean high water above sensor')

varstruct.Name='hs';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'hs','units','m')
nc_attput(filename,'hs','long_name','significant_wave_height')
nc_attput(filename, 'hs', 'standard_name', 'sea_surface_wave_significant_height')

varstruct.Name='tz';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'tz','units','s')
nc_attput(filename,'tz','long_name','wave_period')
nc_attput(filename, 'tz', 'standard_name', 'sea_surface_wave_zero_upcrossing_period')

varstruct.Name='wdir';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'wdir','units','degrees from north')
nc_attput(filename,'wdir','long_name','wave direction')
nc_attput(filename, 'wdir', 'standard_name', 'sea_surface_wave_to_direction')

varstruct.Name='u';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'u','units','m/s')
nc_attput(filename,'u','long_name','eastward_velocity')
nc_attput(filename, 'u', 'standard_name', 'eastward_sea_water_velocity')
nc_attput(filename, 'u', 'comment', 'NaNs set to 999')

varstruct.Name='v';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'v','units','m/s')
nc_attput(filename,'v','long_name','northward_velocity')
nc_attput(filename, 'v', 'standard_name', 'northward_sea_water_velocity')
nc_attput(filename, 'v', 'comment', 'NaNs set to 999')

varstruct.Name='dir';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'dir','units','degrees from north')
nc_attput(filename,'dir','long_name','current_direction')
nc_attput(filename, 'dir', 'standard_name', 'direction_of_sea_water_velocity')
nc_attput(filename, 'dir', 'comment', 'NaNs set to 999')

%Add variable data
nc_varput(filename, 't', times)
nc_varput(filename, 'D', depth)
nc_varput(filename, 'hs', hsig)
nc_varput(filename, 'tz', tz)
nc_varput(filename, 'wdir', wdir)
nc_varput(filename, 'u', Veast)
nc_varput(filename, 'v', Vnorth)
nc_varput(filename, 'dir', Dir)

%End file
nc_dump(filename)