% define the directory where the raw files are stored
datadir=fullfile('..','raw');
% check which files are in the data directory
files=dir(fullfile(datadir, '07_2009_tide.txt'));
dataset=load(fullfile(datadir, files(1).name));

% the file contains 5 columns
data_h_tide=dataset(:,1);
tide_h=dataset(:,2);
data_f_tide=dataset(:,3);
tide_f=dataset(:,4);
data_h_temp=dataset(:,5);
temp_h=dataset(:,6);
data_f_temp=dataset(:,7);
temp_f=dataset(:,8);

% Convert the rmd +gm to a date
dates=datenum(num2str(data_h_tide), 'yyyymmddHHMMSS');
referencedate = datenum('1970/01/01');
data_h_tide_ok = dates-referencedate;

dates=datenum(num2str(data_f_tide), 'yyyymmddHHMMSS');
referencedate = datenum('1970/01/01');
data_f_tide_ok = dates-referencedate;

dates=datenum(num2str(data_h_temp), 'yyyymmddHHMMSS');
referencedate = datenum('1970/01/01');
data_h_temp_ok = dates-referencedate;

dates=datenum(num2str(data_f_temp), 'yyyymmddHHMMSS');
referencedate = datenum('1970/01/01');
data_f_temp_ok = dates-referencedate;

% The size of the dimension depends on how much data is read...
ntime = size(dataset,1);


% this is the file we're saving results to 
filename=('07_2009_tide.nc');
nc_create_empty(filename)

% Now we use the number of timesteps
nc_add_dimension(filename,'time',ntime)

varstruct.Name='data_h_tide';
varstruct.Nctype=nc_double;
varstruct.Dimension={'time'};
nc_addvar(filename,varstruct)
nc_attput(filename,'data_h_tide','units','data_harbour_tide days since 1970-01-01 00:00')
nc_attput(filename, 'data_h_tide', 'standard_name', 'time')

varstruct.Name='tide_h';
varstruct.Nctype=nc_double;
varstruct.Dimension={'time'};
nc_addvar(filename,varstruct)
nc_attput(filename,'tide_h','units','m')
nc_attput(filename, 'tide_h', 'standard_name', 'tide_harbour')

varstruct.Name='data_f_tide';
varstruct.Nctype=nc_double;
varstruct.Dimension={'time'};
nc_addvar(filename,varstruct)
nc_attput(filename,'data_f_tide','units','data_falochron_tide days since 1970-01-01 00:00')
nc_attput(filename, 'data_f_tide', 'standard_name', 'time')

varstruct.Name='tide_f';
varstruct.Nctype=nc_double;
varstruct.Dimension={'time'};
nc_addvar(filename,varstruct)
nc_attput(filename,'tide_f','units','m')
nc_attput(filename, 'tide_f', 'standard_name', 'tide_falochron')

varstruct.Name='data_h_temp';
varstruct.Nctype=nc_double;
varstruct.Dimension={'time'};
nc_addvar(filename,varstruct)
nc_attput(filename,'data_h_temp','units','data_harbour_temp days since 1970-01-01 00:00')
nc_attput(filename, 'data_h_temp', 'standard_name', 'time')

varstruct.Name='temp_h';
varstruct.Nctype=nc_double;
varstruct.Dimension={'time'};
nc_addvar(filename,varstruct)
nc_attput(filename,'temp_h','units',' degree C')
nc_attput(filename, 'temp_h', 'standard_name', 'temp_harbour')

varstruct.Name='data_f_temp';
varstruct.Nctype=nc_double;
varstruct.Dimension={'time'};
nc_addvar(filename,varstruct)
nc_attput(filename,'data_f_temp','units','data_falochron_temp days since 1970-01-01 00:00')
nc_attput(filename, 'data_f_temp', 'standard_name', 'time')

varstruct.Name='temp_f';
varstruct.Nctype=nc_double;
varstruct.Dimension={'time'};
nc_addvar(filename,varstruct)
nc_attput(filename,'temp_f','units',' degree C')
nc_attput(filename, 'temp_f', 'standard_name', 'temp_harbour')

nc_attput(filename, nc_global, 'title','Tide (07-2009)')
nc_attput(filename, nc_global, 'history', 'This file was created using script: 07_2009_tide.m ');
nc_attput(filename, nc_global, 'description', 'NetCDf File with data of Tide (07-2009) from harbour and falochron');

nc_varput(filename, 'data_h_tide', data_h_tide_ok)
nc_varput(filename, 'tide_h', tide_h)
nc_varput(filename, 'data_f_tide', data_f_tide_ok)
nc_varput(filename, 'tide_f', tide_f)
nc_varput(filename, 'data_h_temp', data_h_temp_ok)
nc_varput(filename, 'temp_h', temp_h)
nc_varput(filename, 'data_f_temp', data_f_temp_ok)
nc_varput(filename, 'temp_f', temp_f)

nc_dump(filename)


