
% define the directory where the raw files are stored
datadir=fullfile('..','raw');
% check which files are in the data directory
files=dir(fullfile(datadir, '*.txt'));
% we're just doing the first one (renamed from filename because this was
% used as output)
%dataset=xlsread(fullfile(datadir, files(1).name));
dataset=load(fullfile(datadir, files(1).name));
% the file contains 3 columns
x92=dataset(:,1);
y92=dataset(:,2);
z92=dataset(:,3);
x84=dataset(:,4);
y84=dataset(:,5);
z84=dataset(:,6);
% check how many columns we have
npoints = 1373;
%npoints = length(lat);
% we assume that all 3 columns have the same length, better check it...
%assert(length(x) == length(y))
%assert(length(x) == length(elev))

% this is the file we're saving results to 
filename=('Bathy_micore.nc');
nc_create_empty(filename)
%by using the command length(lat_variable) it shows an error!
nc_add_dimension(filename,'points',npoints)
varstruct.Name='x92';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'x92','units','mters')
nc_attput(filename, 'x92', 'standard_name', 'projection_x_coordinate92')

varstruct.Name='y92';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'y92','units','meters')
nc_attput(filename, 'y92', 'standard_name', 'projection_y_coordinate92')

varstruct.Name='z92';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'z92','units','meters')
nc_attput(filename, 'z92', 'standard_name', 'projection_z_coordinate92')

varstruct.Name='x84';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'x84','units','meters')
nc_attput(filename, 'x84', 'standard_name', 'projection_x_coordinate84')

varstruct.Name='y84';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'y84','units','meters')
nc_attput(filename, 'y84', 'standard_name', 'projection_y_coordinate84')

varstruct.Name='z84';
varstruct.Nctype=nc_double;
varstruct.Dimension={'points'};
nc_addvar(filename,varstruct)
nc_attput(filename,'z84','units','meters')
nc_attput(filename, 'z84', 'standard_name', 'projection_z_coordinate84')

%nc_add_dimension(filename,'z92',length(elev))
%varstruct.Name='elevation';
%varstruct.Nctype=nc_double;
%varstruct.Dimension={'points'};
%nc_addvar(filename,varstruct)
%nc_attput(filename,'elevation','units','m')

% there is an error here too!nc_attput(filename,'title','Converting bathymetry data to NETcdf file')
%nc_varput(filename, 'elevation', elev)
nc_varput(filename, 'x92', x92)
nc_varput(filename, 'y92', y92)
nc_varput(filename, 'z92', z92)
nc_varput(filename, 'x84', x84)
nc_varput(filename, 'y84', y84)
nc_varput(filename, 'z84', z84)

nc_dump(filename)





