function bOK = wave2netcdf(strInPath, strInFile, strOutPath, strOutFile, nCols);
% function bOK = wave2netcdf(strInPath, strInFile, strOutPath, strOutFile, nCols);
%
% Converts ASCII files with wave time series data into netCDF
% format. The ASCII files with raw data format contains significant 
% storms 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
%           -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:
%             - Lines of comments starting with % sign
%             - 1st column date: YYYYMMDD.HHmmSS
%             - 2nd to 9th column: magnitudes. Number of columns may vary. 
%               Order of columns is not the same. Nodata values are -999.00
%           * 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: July 2009
% Modified:
% Date:

% Read raw data input file
[vDate, vH33, vGTZ, vHmO, vTPE, vTmO2] = readwaves(strInPath, strInFile, nCols);

% 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);
vH33Variable = struct(...
    'Name', 'h33', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'metre') ...
    );
nc_addvar(strPathOut, vH33Variable);
vGTZVariable = struct(...
    'Name', 'gtz', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'sec') ...
    );
nc_addvar(strPathOut, vGTZVariable);
vHmOVariable = struct(...
    'Name', 'hmo', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'metre') ...
    );
nc_addvar(strPathOut, vHmOVariable);
vTPEVariable = struct(...
    'Name', 'tpe', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'sec') ...
    );
nc_addvar(strPathOut, vTPEVariable);
vTmO2Variable = struct(...
    'Name', 'tmo2', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'sec') ...
    );
nc_addvar(strPathOut, vTmO2Variable);

% Put variables
nc_varput(strPathOut, 'Time', vDate);
nc_varput(strPathOut, 'h33', vH33);
nc_varput(strPathOut, 'gtz', vGTZ);
nc_varput(strPathOut, 'hmo', vHmO);
nc_varput(strPathOut, 'tpe', vTPE);
nc_varput(strPathOut, 'tmo2', vTmO2);
nc_dump(strPathOut);
bOK = 1;
