function bOK = ADCP2netcdf(strOutPath, strOutFile, mValues);
% function bOK = ADCP2netcdf(strInPath, strInFile, strOutPath, strOutFile, nLines);
%
% Converts ASCII files with ADCP 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 FTO
% 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);
vHmOVariable = struct(...
    'Name', 'SignificantWaveHeight', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'metre') ...
    );
nc_addvar(strPathOut, vHmOVariable);
vPeakDirVariable = struct(...
    'Name', 'PeakDirection', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'deg') ...
    );
nc_addvar(strPathOut, vPeakDirVariable);
vTPEVariable = struct(...
    'Name', 'PeakPeriod', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'sec') ...
    );
nc_addvar(strPathOut, vTPEVariable);
vHmaxVariable = struct(...
    'Name', 'MaxWaveHeight', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'metre') ...
    );
nc_addvar(strPathOut, vHmaxVariable);
vmeanVariable = struct(...
    'Name', 'MeanWavePeriod', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'sec') ...
    );
nc_addvar(strPathOut, vmeanVariable);
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);
vDepthVariable = struct(...
    'Name', 'depth', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'm') ...
    );
nc_addvar(strPathOut, vDepthVariable);
vWLVariable = struct(...
    'Name', 'WaterLevelGauge', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'm') ...
    );
nc_addvar(strPathOut, vWLVariable);
vWLeqVariable = struct(...
    'Name', 'WaterLevelEquipment', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'm') ...
    );
nc_addvar(strPathOut, vWLeqVariable);

% Put variables
nc_varput(strPathOut, 'Time', vDate);
nc_varput(strPathOut, 'SignificantWaveHeight', mValues(:,3));
nc_varput(strPathOut, 'PeakDirection', mValues(:,4));
nc_varput(strPathOut, 'PeakPeriod', mValues(:,5));
nc_varput(strPathOut, 'MaxWaveHeight', mValues(:,6));
nc_varput(strPathOut, 'MeanWavePeriod', mValues(:,7));
nc_varput(strPathOut, 'VelMag', mValues(:,8));
nc_varput(strPathOut, 'VelDir', mValues(:,9));
nc_varput(strPathOut, 'depth', mValues(:,10));
nc_varput(strPathOut, 'WaterLevelGauge', mValues(:,11));
nc_varput(strPathOut, 'WaterLevelEquipment', mValues(:,12));

nc_dump(strPathOut);
bOK = 1;
