function bOK = waterlevel2netcdf(strInPath, strInFile, strOutPath, strOutFile, strZRef);
% function bOK = waterlevel2netcdf(strInPath, strInFile, strOutPath, strOutFile, strZRef);
%
% Converts ASCII files with water level 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
%           -strZRef: Reference system used for the water levels (i.e. NAP
%            (used in The Netherlands) or TAW (used in Belgium).
%            TAW = 2.303 + NAP
%
% #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
%             - First column date: YYYYMMDD.HHmmSS
%             - Second column: water level in meters with respect to NAP or
%               TAW. 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, vLevel] = readwaterlevels(strInPath, strInFile, strZRef);

% Create output netCDF file
strPathOut = fullfile(strOutPath, strOutFile);
nc_create_empty(strPathOut);
nc_add_dimension(strPathOut, 'time', length(vDate));
timeVariable = struct(...
    'Name', 'Time', ...
    'Nctype', 'double', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'days') ...
    );
nc_addvar(strPathOut, timeVariable);
waterlevelsVariable = struct(...
    'Name', 'waterlevel', ...
    'Nctype', 'float', ...
    'Dimension', {{'time'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'metre') ...
    );
nc_addvar(strPathOut, waterlevelsVariable);
nc_varput(strPathOut, 'Time', vDate);
nc_varput(strPathOut, 'waterlevel', vLevel);
nc_dump(strPathOut);
bOK = 1;
