function [mergedX, mergedZ, XRange] = fnv_merge_jarkus_model_data(varargin)
%FNV_MERGE_JARKUS_MODEL  One line description goes here.
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = fnv_merge_jarkus_model(varargin)
%
%   Input: For <keyword,value> pairs call fnv_merge_jarkus_model() without arguments.
%   varargin  =
%
%   Output:
%   varargout =
%
%   Example
%   fnv_merge_jarkus_model
%
%   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: 17 Sep 2014
% Created with Matlab version: 8.2.0.701 (R2013b)

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

%% Settings
OPT = struct(   ...
    'JarkusURL',    jarkus_url, ...
    'JarkusID',     []  ...
);

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

%% Get data
jrkTransect = jarkus_transects('id',OPT.JarkusID,'url',OPT.JarkusURL,'output',{'altitude','altitude_model','cross_shore'});
jrkX        = jrkTransect.cross_shore(~isnan(squeeze(jrkTransect.altitude(1,:,:))));
jrkZ        = squeeze(jrkTransect.altitude(1,:,~isnan(squeeze(jrkTransect.altitude(1,:,:)))));

modelX      = jrkTransect.cross_shore(~isnan(squeeze(jrkTransect.altitude_model(end,:,:))));
modelZ      = squeeze(jrkTransect.altitude_model(end,:,~isnan(squeeze(jrkTransect.altitude_model(end,:,:)))));

%% Plot data & define interpolation range
hFig1   = figure;
hAx1    = gca;
plot(hAx1, jrkX, jrkZ,'b.-','DisplayName','Jarkus profile')
hold(hAx1,'on')
plot(hAx1, modelX, modelZ,'r.-','DisplayName','Model bathymetry')
legend(hAx1,'show')
XRange  = ginput(2);
XRange  = XRange(:,1)';
close(hFig1);

%% Interpolate over XRange
jrkFilter       = jrkX < min(XRange);
modelFilter     = modelX > max(XRange);
interpFilter    = jrkX >= min(XRange) & jrkX <= max(XRange);
% interpFilterMod = modelX >= min(XRange) & modelX <= max(XRange);
interpX         = jrkX(interpFilter);
interpZ         = NaN(size(interpX,1),size(interpX,2));

jrkInterp       = jrkZ(interpFilter);
modelInterp     = interp1(modelX,modelZ,interpX);
% modelInterp     = modelZ(interpFilterMod);

for iX = 1:numel(interpX)
    jrkWeight   = 1-(interpX(iX)-min(interpX))/range(interpX);
    modelWeight = 1-jrkWeight;
    interpZ(iX) = jrkWeight*jrkInterp(iX) + modelWeight*modelInterp(iX);
end

mergedX = [jrkX(jrkFilter) interpX modelX(modelFilter)];
mergedZ = [jrkZ(jrkFilter)' interpZ modelZ(modelFilter)'];

%% Plot resulting merged profile
hFig2   = figure;
hAx2    = gca;
plot(hAx2, jrkX, jrkZ,'b.-','DisplayName','Jarkus profile')
hold(hAx2,'on')
plot(hAx2, modelX, modelZ,'r.-','DisplayName','Model bathymetry')
plot(hAx2, interpX, interpZ,'m.-','DisplayName','Interpolation','LineWidth',2)
hold(hAx2,'off')
legend(hAx2,'show')