function fnv_create_xbeach_grid(varargin)
%CREATE_XBEACH_GRID  One line description goes here.
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = create_xbeach_grid(varargin)
%
%   Input: For <keyword,value> pairs call create_xbeach_grid() without arguments.
%   varargin  =
%
%   Output:
%   varargout =
%
%   Example
%   create_xbeach_grid
%
%   See also

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2014 Deltares
%       Joost den Bieman
%
%       joost.denbieman@deltares.nl
%
%       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 General Public License as published by
%   the Free Software Foundation, either version 3 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 General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this library.  If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://www.OpenEarth.eu">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: 16 Sep 2014
% Created with Matlab version: 8.2.0.701 (R2013b)

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

%% Settings
OPT = struct(   ...
    'SaveDir',      'd:\checkouts\openearthmodels\deltares\future_nourishment_volumes\models', ...
    'hrDataDir',    'd:\checkouts\openearthmodels\deltares\profile_fluctuation_dune_erosion\data', ...
    'JarkusID',     [], ...
    'years',        [], ...
    'x',            [], ...
    'y',            [], ...
    'z',            []  ...
);

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

%% Representative profile
% x, y and z are matrices with cross-shore profile coordinates in the
% columns, can have multiple rows (== multiple profiles)

if isempty(OPT.y)
    xCross  = -fliplr(OPT.x{1});
else
    xCross  = cumsum(sqrt(diff(OPT.x.^2)+diff(OPT.y.^2)));
end

z(1,:) = fliplr(OPT.z{1});

if numel(OPT.x) > 1
    for iX = 2:numel(OPT.x)
       z(iX,:) = interp1(-OPT.x{iX},OPT.z{iX},xCross);
    end
end

zMax    = max(z,[],1);

%% XBeach settings
runTime = 5*3600;
dxmin   = 2;

%% Normative boundary conditions
HR  = get_hr_boundaryconditions('id',OPT.JarkusID,'datadir',OPT.hrDataDir);

%% Create model grid
[xGrid, ~]  = xb_grid_xgrid(xCross,zMax,'dxmin',dxmin,'Tm',HR.Tp,'wl',HR.Rp);
% [xGrid, ~, zGrid]  = xb_grid_optimize('x',xCross,'z',zMax,'xgrid',{'dxmin',dxmin,'Tm',HR.Tp,'wl',HR.Rp},'finalise', {'n',4,'zmin', min(min(OPT.z),-20), 'z0', HR.Rp,'actions',{'seaward_flatten' 'seaward_extend'}});

%% Create XBeach input
for iProfile = 1:size(z,1)
    modelDir    = fullfile(OPT.SaveDir,[num2str(OPT.JarkusID) '_' num2str(OPT.years(iProfile))]);
%     zGrid       = interp1(xCross, OPT.z(iProfile,:), xGrid);
    [x, ~, zGrid]  = xb_grid_optimize('x',xCross,'z',z(iProfile,:),'xgrid',{'dxmin',dxmin,'Tm',HR.Tp,'wl',HR.Rp},'finalise', {'n',4,'zmin', min(min(min(z)),-20), 'z0', HR.Rp,'actions',{'seaward_extend' 'seaward_flatten'}});
    
    if ~isdir(modelDir)
        mkdir(modelDir)
    end
%     'finalise', {'n',4,'zmin', min(min(OPT.z(iProfile,:)),-20), 'z0', HR.Rp,'actions',{'seaward_flatten' 'seaward_extend'}}
    xbm = xb_generate_model('bathy',{'x', x,'y',zeros(size(xCross)), 'z', zGrid, 'optimize',false, 'world_coordinates',false}, ...
        'tide', {'front', [HR.Rp HR.Rp], 'time', [0 runTime]}, ...
        'waves', {'Hm0', HR.Hs, 'Tp', HR.Tp, 'mainang', 270, 'duration', runTime+500}, ...
        'settings', {'thetamin', -90, 'thetamax', 90, 'dtheta', 180, 'thetanaut', 0, ...
        'outputformat', 'netcdf', ...
        'tstop', runTime, 'tintg', 60, 'tintm', runTime, 'tintp', 1, 'tstart', 0, ...
        'globalvar',{'H', 'zb', 'zs', 'hh','u', 'v', 'R', 'D', 'thetamean', 'Fx', 'Fy', 'wetz'}, ...
        'meanvar',{'H', 'zs', 'hh', 'u', 'v', 'thetamean'}}, ...
        'write', true, 'path', modelDir);
end