function varargout = deltaflumeDE2006_2netcdf(varargin)
%DELTAFLUMEDE2006_2NETCDF  write profile data of Deltaflume dune erosion tests (2006) to netcdf
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = deltaflumeDE2006_2netcdf(varargin)
%
%   Input:
%   varargin  =
%
%   Output:
%   varargout =
%
%   Example
%   deltaflumeDE2006_2netcdf
%
%   See also

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2009 Delft University of Technology
%       C.(Kees) den Heijer
%
%       C.denHeijer@TUDelft.nl
%
%       Faculty of Civil Engineering and Geosciences
%       P.O. Box 5048
%       2600 GA Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or
%   modify it under the terms of the GNU Lesser General Public
%   License as published by the Free Software Foundation, either
%   version 2.1 of the License, or (at your option) any later version.
%
%   This library is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%   Lesser General Public License for more details.
%
%   You should have received a copy of the GNU Lesser General Public
%   License along with this library. If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tools is part of <a href="http://OpenEarth.Deltares.nl">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 08 Oct 2009
% Created with Matlab version: 7.4.0.287 (R2007a)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

%%
OPT = struct(...
    'rawdir', strrep(fileparts(which(mfilename)), 'scripts', 'raw'),...
    'processeddir', 'P:\mcdata\opendap\deltares\deltaflume2006_dune_erosion\');

OPT = setProperty(OPT, varargin{:});

%% additional metadata

xWHM = [36.7 41 43.85];
xPS = [41 70 100 130 150 170 190 200 205 209];
zPS = [3 3 3.4 3.4 3.4 3.4 3.95 4.15 4.3 4.25];
xFV = reshape(repmat(xPS([1 8 9]), 2,1), 1, 6);

%%
contents = dir(fullfile(OPT.rawdir, 'FlumeWall'));
tests = {contents.name};
for i = 1:length(contents)
    %% profile measurements
    tekalfiles = dir(fullfile(OPT.rawdir, 'FlumeWall', tests{i}, '*.tek'));
    for j = 1:length(tekalfiles)
        FileName = fullfile(OPT.rawdir, 'FlumeWall', tests{i}, tekalfiles(j).name);
        FileInfo = tekal('read', FileName);
        if j == 1
            netcdffilename = fullfile(OPT.processeddir, [tests{i} '.nc']);
            % make sure that path exist
            mkpath(OPT.processeddir)
            % create empty netcdf file
            nc_create_empty(netcdffilename)
            % add global atributes
            nc_attput(netcdffilename, nc_global, 'title', 'Deltaflume dune erosion test data');
            nc_attput(netcdffilename, nc_global, 'institution', 'Deltares');
            nc_attput(netcdffilename, nc_global, 'source', 'mechanical (amphibious) bed profile follower, Pressure sensors, Flow velocity sensors')
            nc_attput(netcdffilename, nc_global, 'history', ['Deltares measurement data (' strrep('$HeadURL$', ['scripts/' mfilename '.m'], ['raw/FlumeWall/', tests{i} '/']) '), converted to netCDF on ' date ' with script with $Id$']);
            nc_attput(netcdffilename, nc_global, 'references', 'van Gent, M. R. A., van Thiel de Vries, J. S. M., Coeveld, E. M., de Vroeg, J. H., and van de Graaff, J. (2008). Large-scale dune erosion tests to study the influence of wave periods. Coastal Engineering, 55(12):1041-1051. DOI: 10.1016/j.coastaleng.2008.04.003');
            nc_attput(netcdffilename, nc_global, 'terms_for_use', 'These data can be used freely for research purposes provided that the following source is acknowledged: Deltares (van Gent et al., 2008).');
            nc_attput(netcdffilename, nc_global, 'disclaimer', 'This data is made available in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.');
            
            %% add dimensions
            % cross-shore profile measurement
            nc_add_dimension(netcdffilename, 'cross_shore_profile', FileInfo.Field.Size(1))
            % time profile measurement
            nc_add_dimension(netcdffilename, 'time_profile', length(tekalfiles))
            %% add variables
            s = struct(...
                'Name', {...
                'cross_shore_profile'...
                'time_profile'...
                'altitude'},...
                ...
                'Nctype', {...
                nc_double...
                nc_int...
                nc_double},...
                ...
                'Dimension', {...
                {'cross_shore_profile'}...
                {'time_profile'}...
                {'time_profile' 'cross_shore_profile'}},...
                ...
                'Attribute', {...
                struct(...
                'Name', {'long_name', 'units', 'comment'}, ...
                'Value', {'cross-shore coordinate', 'm', 'cross-shore coordinate with respect to the wave board'})...
                struct(...
                'Name', {'standard_name', 'units', 'comment', 'axis'},...
                'Value', {'time', 's', 'time from beginning of test', 'T'})...
                struct(...
                'Name', {'standard_name', 'units', 'comment', 'coordinates'}, ...
                'Value', {'surface_altitude', 'm', 'altitude above flume bottom', 'cross-shore'})...
                });
            for k = 1:length(s)
                nc_addvar(netcdffilename, s(k));
            end
            % read x coordinates
            x = FileInfo.Field.Data(:,1);
            % pre-allocate z
            z = NaN(FileInfo.Field.Size(1), length(tekalfiles));
        end
        % read z coordinates
        z(:,j) = FileInfo.Field.Data(:,2);
        t(j) = str2double(strrep(FileInfo.Field.Comments{2}, '*Profile at time [hours]: ', ''))*3600;
        
        if j == length(tekalfiles)
            % store values
            nc_varput(netcdffilename, 'cross_shore_profile', x)
            nc_varput(netcdffilename, 'altitude', z')
            nc_varput(netcdffilename, 'time_profile', t)
        end
    end
    
    %% Wave height, pressure and flow velocity measurements
    asciifiles = dir(fullfile(OPT.rawdir, 'FlumeWall', tests{i}, '*.ASC'));
    for j = 1:length(asciifiles)
        data = load(fullfile(OPT.rawdir, 'FlumeWall', tests{i}, asciifiles(j).name));
        if j == 1
            % cross-shore wave height meters
            nc_add_dimension(netcdffilename, 'cross_shore_WHM', length(xWHM))
            % cross-shore pressure sensors
            nc_add_dimension(netcdffilename, 'cross_shore_PS', size(xPS,2))
            % cross-shore flow velocity meters
            nc_add_dimension(netcdffilename, 'cross_shore_FV', length(xFV))
            %% add variables
            s = struct(...
                'Name', {...
                'cross_shore_WHM'...
                'cross_shore_PS'...
                'altitude_PS'...
                'cross_shore_FV'},...
                ...
                'Nctype', {...
                nc_double...
                nc_double...
                nc_double...
                nc_double},...
                ...
                'Dimension', {...
                {'cross_shore_WHM'}...
                {'cross_shore_PS'}...
                {'cross_shore_PS'}...
                {'cross_shore_FV'}},...
                ...
                'Attribute', {...
                struct(...
                'Name', {'long_name', 'units', 'comment'}, ...
                'Value', {'cross-shore position of wave height meters', 'm', 'cross-shore position of wave height meters with respect to the wave board'})...
                struct(...
                'Name', {'long_name', 'units', 'comment'}, ...
                'Value', {'cross-shore position of pressure sensors', 'm', 'cross-shore position of pressure sensors with respect to the wave board'})...
                struct(...
                'Name', {'long_name', 'units', 'comment'}, ...
                'Value', {'vertical position of pressure sensors', 'm', 'vertical position of pressure sensors with respect to the flume bottom'})...
                struct(...
                'Name', {'long_name', 'units', 'comment'}, ...
                'Value', {'cross-shore position of flow velocity sensors', 'm', 'cross-shore position of flow velocity sensors with respect to the wave board'})...
                });
            for k = 1:length(s)
                nc_addvar(netcdffilename, s(k));
            end
            %% store values
            nc_varput(netcdffilename, 'cross_shore_WHM', xWHM)
            nc_varput(netcdffilename, 'cross_shore_PS', xPS)
            nc_varput(netcdffilename, 'altitude_PS', zPS)
            nc_varput(netcdffilename, 'cross_shore_FV', xFV)
        end
        
        time_varname = ['time_' char(j+64)];
        % time wave height, velocity and concentration measurement
        nc_add_dimension(netcdffilename, time_varname, size(data,1))
        %% add variables
        s = struct(...
            'Name', {...
            time_varname},...
            ...
            'Nctype', {...
            nc_double},...
            ...
            'Dimension', {...
            {time_varname}},...
            ...
            'Attribute', {...
            struct(...
            'Name', {'standard_name', 'units', 'comment', 'axis'},...
            'Value', {'time', 's', ['time from beginning of experiment period ' char(j+64)], 'T'})...
            });
        for k = 1:length(s)
            nc_addvar(netcdffilename, s(k));
        end
        %% store values
        nc_varput(netcdffilename, time_varname, data(:,1))
        %% add variables
        vars = {...
            ['water_level_elevation_' char(j+64)]...
            ['pressure_' char(j+64)]...
            ['flow_velocity_' char(j+64)]};
        s = struct(...
            'Name', vars,...
            ...
            'Nctype', {...
            nc_double...
            nc_double...
            nc_double},...
            ...
            'Dimension', {...
            {time_varname 'cross_shore_WHM'}...
            {time_varname 'cross_shore_PS'}...
            {time_varname 'cross_shore_FV'}},...
            ...
            'Attribute', {...
            struct(...
            'Name', {'long_name', 'units', 'comment'}, ...
            'Value', {'water level elevation', 'm', 'water level elevation with respect to still water level'})...
            struct(...
            'Name', {'long_name', 'units'},...
            'Value', {'pressure', 'N/m^2'})...
            struct(...
            'Name', {'long_name', 'units', 'comment'}, ...
            'Value', {'flow velocity', 'm/s', 'flow velocity'})...
            });
        for k = 1:length(s)
            nc_addvar(netcdffilename, s(k));
        end
        
        %% store values
        nc_varput(netcdffilename, vars{1}, data(:,2:4))
        nc_varput(netcdffilename, vars{2}, data(:,5:14))
        nc_varput(netcdffilename, vars{3}, data(:,15:end))
    end
end