function [XB Precision] = CreateEmptyXBeachVar(varargin)
%CREATEEMPTYXBEACHVAR  creates communication variable for XBeach in- and output
%
% The variable contains three main fields:
%   - settings :    In which calculation settings as well as output
%               settings 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.
%
%   Example
%   createEmptyXBeachVar
%
%   See also XB_run XBeach_Write_Inp XB_Read_Results

%   --------------------------------------------------------------------
%   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
XB = struct(...
    'settings', [],...
    'Input', [],...
    'Output', []);

%% settings
XB.settings = struct(...
    'Info', [],...
    'Grid', [],...
    'Waves', [],...
    'Flow', [],...
    'SedInput', []);

%% info settings
XB.settings.Info = struct(...
    'WaveBoundType', '',...
    'n_d', 1,...
    'n_w', 1,...
    'S', 1,...
    'nVarOutDef', 20);

XB.settings.Info = applyInput(XB.settings.Info, varargin{:});

%% Grid settings
XB.settings.Grid = struct(...
    'nx', [],...
    'ny', [],...
    'vardx', 0,...
    'depfile', {''},...
    'xfile', '',...
    'yfile', '',...
    'dx', [],...
    'dy', [],...
    'xori', 0,...
    'yori', 0,...
    'alfa', 0,...
    'posdwn', -1);

XB.settings.Grid = applyInput(XB.settings.Grid, varargin{:});

%% Wave settings
XB.settings.Waves = struct(...
    'break', 1,...
    'Hrms', [],...
    'Tm01', [],...
    'dir0', 270,...
    'hmin', .05,...
    'gamma', [],...
    'alpha', 1,...
    'delta', .0,...
    'n', [],...
    'rho', 1000,...
    'g', 9.81,...
    'thetamin', -180.0,...
    'thetamax', 180.0,...
    'dtheta', 360.0,...
    'wci', 0,...
    'instat', 6,...
    'bcfile', {''},...
    'rt', 2261,...
    'dthc', .2,...
    'nuh', .1,...
    'nuhfac', 1,...
    'roller', 1,...
    'beta', .1,...
    'random', 1,...
    'back', 1);

XB.settings.Waves = applyInput(XB.settings.Waves, varargin{:});

%% Flow settings
XB.settings.Flow = struct(...
    'nonh', 0,...
    'tideloc', [],...
    'zs0file', {''},...
    'tidelen', {''},... % no specification (length(XB.settings.Flow.zs0)
    'paulrevere', [],...
    'zs0', [],...
    'C', 65.,...
    'eps', .001,...
    'umin', .0,...
    'tstart', 100,...
    'tint', 1,...
    'tstop', 2261,...
    'CFL', 0.9');

XB.settings.Flow = applyInput(XB.settings.Flow, varargin{:});

%% Sediment settings
XB.settings.SedInput = struct(...
    'A', .002,...
    'dico', 1.0,...
    'D50', .0002,...
    'D90', .0003,...
    'rhos', 2650,...
    'morfac', 10,...
    'morstart', 100,...
    'por', .4,...
    'dryslp', 1.0,...
    'wetslp', .15,...
    'hswitch', .1);

XB.settings.SedInput = applyInput(XB.settings.SedInput, varargin{:});

%% Output option settings
XB.settings.OutputOptions = struct(...
    'nglobalvar', [],...
    'OutVars', []);
XB.settings.OutputOptions.OutVars = {'cc' 'dims' 'D' 'E' 'Fx' 'Fy' 'Gd' 'hh' 'H' 'R' 'Su' 'Sv' 'u' 'ue' 'urms' 'v' 've' 'zb' 'zs'};

XB.settings.OutputOptions = applyInput(XB.settings.OutputOptions, varargin{:});

XB.settings.OutputOptions.nglobalvar = length(XB.settings.OutputOptions.OutVars);

%% input
XB.Input = struct(...
    'xInitial', [],...
    'yInitial', [],...
    'zInitial', []);

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

%% specification of precision to write in the input files
Precision = {...
    'nx', '%g';...
    'ny', '%g';...
    'vardx', '%g';...
    'dx', '%g';...
    'dy', '%g';...
    'posdwn', '%g';...
    'break', '%g';...
    'n', '%g';...
    'rho', '%g';...
    'wci', '%g';...
    'instat', '%g';...
    'rt', '%g';...
    'nuhfac', '%g';...
    'roller', '%g';...
    'random', '%g';...
    'back', '%g';...
    'nonh', '%g';...
    'tideloc', '%g';...
    'tstart', '%g';...
    'tint', '%g';...
    'zs0file', '%s';...
    'tstop', '%g';...
    'rhos', '%g';...
    'morfac', '%g';...
    'morstart', '%g';...
    'bcfile', '%s';...
    'depfile', '%s';...
    'nglobalvar', '%f'};

%% apply predefined input
function varargout = applyInput(OPT, varargin)
% subfunction to assign values which are specified in varargin to the
% related fields of the XB 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})};