function [D Precision] = CreateDurostaVar(varargin)
%CREATEDUROSTAVAR  creates communication variable for DUROSTA in- and output
%
% The variable contains three main fields:
%   - settings :    In which calculation settings as well as output
%               settings can be specified. This has fields Params, in which
%               the exact input variables needed in the DUROSTA input file
%               are defined and Output, in which the desired output
%               locations and time frames can be specified.
%   - Input :       This contains input like the initial profile, Hs, Tp,
%               grid specification, start and stop times etc.
%   - Output :      In which calculation results can be stored.
%   By use of PropertyName-PropertyValue pairs, the various elements of
%   settings and Input can be set.
%
%   Syntax:
%   [D Precision] = CreateDurostaVar(varargin)
%
%   Input:
%   varargin  = 'PropertyName' propertyvalue pairs
%
%   Output:
%   D         = Durosta communication structure
%   Precision = cell array containing info about the precision to write the
%               variables in the input file
%
%   Example
%   CreateDurostaVar
%
%   See also

%   --------------------------------------------------------------------
%   Copyright (C) 2009 Deltares
%       Pieter van Geer / Kees den Heijer
%
%       Pieter.vanGeer@Deltares.nl	
%
%       Deltares
%       P.O. Box 177
%       2600 MH 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, write to the Free Software
%   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
%   USA
%   or http://www.gnu.org/licenses/licenses.html, http://www.gnu.org/, http://www.fsf.org/
%   --------------------------------------------------------------------

% Created: 04 Feb 2009
% Created with Matlab version: 7.4.0.287 (R2007a)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$

%% main structure
D = struct(...
    'settings', [],...
    'Input', [],...
    'Output', []);

%% settings
D.settings = struct(...
    'Params', [],...
    'Output', []);

% params settings
D.settings.Params = struct(... %see input file specifications for details
    'ALFA', [],...
    'ALFAD', [],...
    'AROH2', [],...
    'DIF', [],...
    'DSED', [],...
    'DTMAX', [],...
    'DZMAX', [],...
    'FW', [],...
    'FZS', [],...
    'GAMMA', [],...
    'IFROUT', [],...
    'KALFA', [],...
    'KCO', [],...
    'KCOR', [],...
    'KEPS0', [],...
    'KFLUX', [],...
    'KHEL', [],...
    'KMU', [],...
    'KSW', [],...
    'RDISED', [],...
    'RK', [],...
    'VALK', [],...
    'WSED', [],...
    'XK', [],...
    'ZDRY', [],...
    'END', 0);

D.settings.Params = applyInput(D.settings.Params, varargin{:});

% output settings
D.settings.Output = struct(...
    'OutputSteps', [],... % desired output time frames 
    'VertProfLoc', [],... % Location(s) of vertical profile output(s)
    'VertProfRes', []);   % resulution of vertical output profiles.

D.settings.Output = applyInput(D.settings.Output, varargin{:});

%% input
D.Input = struct(...
    'Grid', [],...       % x-coordinates of grid points (array)
    'dx', [],...         % dx for fixed cell size computation cells (only used if Grid=[])
    'xInitial', [],...   % x-coordinates of initial profile
    'zInitial', [],...   % z-coordinates of initial profile
    'Hs', [],...         % Hs (either fixed in time - one value -, or variable - vector, see input file specs -)
    'Tp', [],...         % Same for Tp
    'WL', [],...         % Same for WL
    'Tstart', 0,...      % Start time
    'Tstop', 0,...       % End time
    'Revetment', [],...  % revetment specification (see input file specification and DUROSTA_Inp_File)
    'WaveAngle', [],...  % Same for wave angle
    'LongVel', [],...    % Longshore velocity
    'LongVelGrad', []);  % Longshore velocity gradient

D.Input = applyInput(D.Input, varargin{:});

%% output
D.Output = struct(...
    'FinalProfileX', [],...
    'InitialProfileZ', [],...
    'FinalProfileZ', [],...
    'Volumes', struct('Columns', [], 'Data', []),...
    'VertProfZ', [],...
    'VertProfH', [],...
    'VertProfUx', [],...
    'VertProfConc', [],...
    'VertProfSsusx', []);

%% precision
Precision = {...
    'ALFA', '%7.6f';...
    'ALFAD', '%7.6f';...
    'AROH2', '%7.6f';...
    'DIF', '%7.6f';...
    'DSED', '%7.6f';...
    'DTMAX', '%7.6f';...
    'DZMAX', '%7.6f';...
    'FW', '%7.6f';...
    'FZS', '%7.6f';...
    'GAMMA', '%7.6f';...
    'IFROUT', '%1.0f';...
    'KALFA', '%7.6f';...
    'KCO', '%7.6f';...
    'KCOR', '%7.6f';...
    'KEPS0', '%7.6f';...
    'KFLUX', '%7.6f';...
    'KHEL', '%7.6f';...
    'KMU', '%7.6f';...
    'KSW', '%7.6f';...
    'RDISED', '%7.6f';...
    'RK', '%7.6f';...
    'VALK', '%7.6f';...
    'WSED', '%7.6f';...
    'XK', '%7.6f';...
    'ZDRY', '%7.6f';...
    'END', '%7.6f';...
    'Grid', '%.3f';...       % x-coordinates of grid points (array)
    'dx', '%.3f';...         % dx for fixed cell size computation cells (only used if Grid=[])
    'xInitial', '%.3f';...   % x-coordinates of initial profile
    'zInitial', '%.3f';...   % z-coordinates of initial profile
    'Hs', '%.3f';...         % Hs (either fixed in time - on value -, or variable - vector, see input file specs -)
    'Tp', '%.3f';...         % Same for Tp
    'WL', '%.3f';...         % Same for WL
    'Tstart', '%4.2f';...    % Start time
%     'Tstop', 0;...           % End time
    'Revetment', '%.3f';...  % revetment specification (see input file specification and DUROSTA_Inp_File)
    'WaveAngle', '%.3f';...  % Same for wave angle
    'LongVel', '%.3f';...    % Longshore velocity
    'LongVelGrad', '%.3f' ...% Longshore velocity gradient
    };

%% apply predefined input
function varargout = applyInput(OPT, varargin)
% subfunction to assign values which are specified in varargin to the
% related fields of the Durosta structure

id = false(size(varargin));
for i = 1:2:length(varargin)
    [id(i) id(i+1)] = deal(any(strcmpi(varargin{i}, fieldnames(OPT))));
end

varargout = {setproperty(OPT, varargin{id})};