%Load and read the data
datadir=fullfile('..','raw');
files=dir(fullfile(datadir, '*.xls'));
xls_file = 'Bathy_Micore_Sept_09.xls';
[dataset,txt]=xlsread(fullfile(datadir, xls_file));
x=dataset(:,1);
y=dataset(:,2);
elev=dataset(:,3);
lat=dataset(:,4);
lon=dataset(:,5);
dates=char(txt(2:end,1));
times=char(txt(2:end,2));
dd=str2num(dates(:,1:2));
mm=str2num(dates(:,4:5));
yyyy=str2num(dates(:,7:10));
hh=str2num(times(:,1:2));
mins=str2num(times(:,4:5));
sec=str2num(times(:,7:11));
survey_times=datenum(yyyy,mm,dd,hh,mins,sec);

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

% Add global atributes
nc_attput(filename, nc_global, 'title', 'Lido di Dante/Lido di Classe bathymetry september 2009');
nc_attput(filename, nc_global, 'institution', 'Università di Ferrara');
nc_attput(filename, nc_global, 'source', 'Ecosounder field measurements')
nc_attput(filename, nc_global, 'comment', 'Surveys undertaken between 20/09/2009 and 22/09/2010')

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

%Add variable attributes
varstruct.Name='x';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'x','units','m')
nc_attput(filename,'x','long_name','x_coordinate')
nc_attput(filename, 'x', 'standard_name', 'projection_x_coordinate')
nc_attput(filename, 'x', 'comment', 'eastings based on WGS84 UTM Zone 32 projection')

varstruct.Name='y';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'y','units','m')
nc_attput(filename,'y','long_name','y_coordinate')
nc_attput(filename, 'y', 'standard_name', 'projection_y_coordinate')
nc_attput(filename, 'y', 'comment', 'northings based on WGS84 UTM Zone 32 projection')

varstruct.Name='elevation';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'elevation','units','m')
nc_attput(filename,'elevation','comment','reference level is mean sea level in Genova')

varstruct.Name='lat';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'lat','units','degrees_north')
nc_attput(filename,'lat','long_name','latitude')
nc_attput(filename, 'lat', 'standard_name', 'latitude')
nc_attput(filename, 'lat', 'comment', 'WGS84')

varstruct.Name='lon';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'lon','units','degrees_east')
nc_attput(filename,'lon','long_name','longitude')
nc_attput(filename, 'lon', 'standard_name', 'longitude')
nc_attput(filename, 'lon', 'comment', 'WGS84')

varstruct.Name='survey_time';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'survey_time','units','days')
nc_attput(filename,'survey_time','long_name','survey_time')
%nc_attput(filename, 'survey_time', 'standard_name', 'longitude')
nc_attput(filename, 'survey_time', 'comment', 'GMT + 2 hours, matlab date format')

%Add variable data
nc_varput(filename, 'x', x)
nc_varput(filename, 'y', y)
nc_varput(filename, 'elevation', elev)
nc_varput(filename, 'lat', lat)
nc_varput(filename, 'lon', lon)
nc_varput(filename, 'survey_time', survey_times)


%End file
nc_dump(filename)