function bOK = RCM92netcdf(strOutPath, strOutFile, mValues);
% function bOK = RCM92netcdf(strInPath, strInFile, strOutPath, strOutFile, nLines);
%
% Converts ASCII files with RCM9 data into netCDF
% format. The ASCII files with raw data format contains information in the 
% Belgiam coast. The data has been prepared for the MICORE 
% project: www.micore.eu
%
% #INPUTS:  -strInPath: string with the dictory path where the raw data is
%            saved. Relative paths are allowed, e.g. '../raw/19931115/'.
%            Include the / sign at the end of the string.
%           -strInFile: string with the name of the input ASCII file
%           -strOutPath: string with the dictory path where the output file
%            will be saved. Relative paths are allowed, e.g. '../raw/19931115/'.
%           -strOutFile: string with the name of the output file in format
%            netCDF
%           -nLines: total number of lines with metadata in the file, not taking into
%            account the comment lines starting with *.
%           -nCols: total number of columns in the file, taking into
%            account the first column with the date.
%
% #OUTPUTS: -bOK: Boolean with the result of the conversion from ASCII to netCDF
%
% #REMARKS: * The format of the raw data ASCII file is described at the
% begining of the file
%           * This function calls functions from the OpenEarth Matlab
%             library
%

% International Marine and Dredging Consultants, IMDC
% www.imdc.be
% Antwerp, Belgium
%
% Written by: Fernando Toro, Piet Haerens
%             fto@imdc.be
% Date: November 2009
% Modified: April 2011
% Date:

yyyy = floor(mValues(:,1)/10000);
mm = floor((mValues(:,1) - (yyyy*10000))/100);
dd = mValues(:,1) - (yyyy*10000) - (mm*100);
hh = floor(mValues(:,2)/10000);
min = floor((mValues(:,2) - (hh*10000))/100);
ss = mValues(:,2) - (hh*10000) - (min*100);
vDate = datenum(yyyy, mm, dd, hh, min, ss);

% Create output netCDF file
strPathOut = fullfile(strOutPath, strOutFile);
nc_create_empty(strPathOut);
nc_add_dimension(strPathOut, 'time', length(vDate));

% Add variables
timeVariable = struct(...
    'Name', 'Time', ...
    'Nctype', 'double', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'days') ...
    );
nc_addvar(strPathOut, timeVariable);
vVelmagVariable = struct(...
    'Name', 'VelMag', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'm/s') ...
    );
nc_addvar(strPathOut, vVelmagVariable);
vVeldirVariable = struct(...
    'Name', 'VelDir', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'deg') ...
    );
nc_addvar(strPathOut, vVeldirVariable);
vTempVariable = struct(...
    'Name', 'temperature', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'degC') ...
    );
nc_addvar(strPathOut, vTempVariable);
vCondVariable = struct(...
    'Name', 'conductivity', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'mS/cm') ...
    );
nc_addvar(strPathOut, vCondVariable);
vWPressVariable = struct(...
    'Name', 'WaterPressure', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'dbar') ...
    );
nc_addvar(strPathOut, vWPressVariable);
vTurbidityVariable = struct(...
    'Name', 'turbidity', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'NTU') ...
    );
nc_addvar(strPathOut, vTurbidityVariable);
vTiltVariable = struct(...
    'Name', 'VerticalTilting', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'deg') ...
    );
nc_addvar(strPathOut, vTiltVariable);
vSalVariable = struct(...
    'Name', 'salinity', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'ppt') ...
    );
nc_addvar(strPathOut, vSalVariable);
vSSCVariable = struct(...
    'Name', 'SuspendedSedimentConcentration', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'mg/l') ...
    );
nc_addvar(strPathOut, vSSCVariable);
vWLVariable = struct(...
    'Name', 'WaterLevel', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'm') ...
    );
nc_addvar(strPathOut, vWLVariable);

% Put variables
nc_varput(strPathOut, 'Time', vDate);
nc_varput(strPathOut, 'VelMag', mValues(:,3));
nc_varput(strPathOut, 'VelDir', mValues(:,4));
nc_varput(strPathOut, 'temperature', mValues(:,5));
nc_varput(strPathOut, 'conductivity', mValues(:,6));
nc_varput(strPathOut, 'WaterPressure', mValues(:,7));
nc_varput(strPathOut, 'turbidity', mValues(:,8));
nc_varput(strPathOut, 'VerticalTilting', mValues(:,9));
nc_varput(strPathOut, 'salinity', mValues(:,10));
nc_varput(strPathOut, 'SuspendedSedimentConcentration', mValues(:,11));
nc_varput(strPathOut, 'WaterLevel', mValues(:,12));

nc_dump(strPathOut);
bOK = 1;
