function varargout = joinldbs(ldbs,varargin)
%JOINLDBS  join a set of ldbs
%
%   this function loins a set of ldb's, where the set must have two ldb's
%   at least. The join is performed in the order of the ldb's in the set.
%   When at any moment no join is found the joining stops without given a
%   resulting join.
%
%   Syntax:
%   [msg,newldb] = joinldbs(ldbs,varargin)
%
%   Input:
%      ldbs     = the set of ldb's that must be joined
%      varargin = a set of key/value pairs
%              shiftldb1 used for testing reasons, shift the node
%                        numbering, default = 0
%              shiftldb2 used for testing reasons, shift the node
%                        numbering, default = 0
%             
%              distance  two points are supposed to coincide when the are
%                        within this distance from each other. Default = 0.
%              workdir   the work directory, when not given the current
%                        directory is used
%              file      the name of the file where to write the result to.
%                        Writing is done in the ldb format. The file is
%                        used as a file descriptor, so a path is allowed.
%                        You must give the extension yourself, it is not
%                        obliged. Default no file is plotted.
%              text      the header text used in the ldb file. When not
%                        given the name of the file is used.
%              plot      plot the given ldb's (default = no)
%              plotnew   plot the resulting ldb (default = from plot)
%
%   Output:
%      varargout = the next options are available
%                  1) not any output argument is asked for
%                  2) 1 argument, this is a message
%                  3) a second argument. This is the resulting ldb.
%   Example
%
%   See also 

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2010 Deltares
%       Lou Verhage
%
%       lou.verhage@deltares.nl	
%
%       Deltares
%       P.O. Box 177
%       2600MH Delft
%       Tel: +31 (0)88 335  8273
%       
%       Visiting adress:
%       Rotterdamseweg 185
%       2629 HD Delft
%       Tel: +31 (0)88 335  8276
%
%   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 tool is part of <a href="http://OpenEarth.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: 14 Dec 2010
% Created with Matlab version: 7.8.0.347 (R2009a)

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

%% handle input arguments

%              shiftldb1 used for testing reasons, shift the node
%                        numbering, default = 0
%              shiftldb2 used for testing reasons, shift the node
%                        numbering, default = 0
%             
%              distance  two points are supposed to coincide when the are
%                        within this distance from each other. Default = 0.
%              file      the name of the file where to write the result to.
%                        Writing is done in the ldb format. The file is
%                        used as a file descriptor, so a path is allowed.
%                        You must give the extension yourself, it is not
%                        obliged. Default no file is plotted.
%              text      the header text used in the ldb file. When not
%                        given the name of the file is used.
%              plot      plot the given ldb's (default = no)
%              plotnew   plot the resulting ldb (default = from plot)

OPT.shiftldb1 = 0;
OPT.shiftldb2 = 0;
OPT.distance  = 0;
OPT.workdir   = '';
OPT.file      = '';
OPT.text      = '';
OPT.plot      = 'no';
OPT.plotnew   = '';

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

if isempty(OPT.text) & ~isempty(OPT.file)
    [pathstr, name, ext, versn] = fileparts(OPT.file);
    OPT.text = name;
end

if isempty(OPT.plotnew)
    OPT.plotnew =  OPT.plot;
end

if OPT.workdir(end:end) ~= '\'
    OPT.workdir = [OPT.workdir '\'];
end
[pWorkdir, name, ext, versn] = fileparts(OPT.workdir);

if nargout > 0
    for n = 1: nargout;
        varargout{n} = '';
    end
end

if length(ldbs) < 2
    if nargout >= 1
        varargout{1} = 'Give at least two ldb file names';
        return
    end
end

%first join the first two ldb's
[pathstr, name, ext, versn] = fileparts(ldbs{1});
ldb1 = fullfile(pWorkdir,[name ext]);
if ~exist(ldb1)
    if nargout >= 1
        varargout{1} = ['File ' ldb1 ' does not exist.'];
    end
    return
end
ldb1 = readldb(ldb1);

[pathstr, name, ext, versn] = fileparts(ldbs{2});
ldb2 = fullfile(pWorkdir,[name ext]);
if ~exist(ldb2)
    if nargout >= 1
        varargout{1} = ['File ' ldb2 ' does not exist.'];
    end
    return
end
ldb2 = readldb(ldb2);

if strcmpi(OPT.plot,'yes')
    plot (ldb1.x,ldb1.y,'+-g');
    hold on
    plot(ldb2.x,ldb2.y,'-ob');
    axis equal
    grid on
end

msg = 'The given ldbs are combined.';
[newLdb,found] = combineldb(ldb1,ldb2,OPT);
if isempty(found)
    newLdb.x = [ldb1.x;NaN;ldb2.x];
    newLdb.y = [ldb1.y;NaN;ldb2(m).y];
    msg = 'The given ldb''s are not (all) combined'; 
end

%when there are more ldb's proceed joining
if length(ldbs) > 2
    for n = 3:length(ldbs);
        [pathstr, name, ext, versn] = fileparts(ldbs{n});
        ldb  = fullfile(pWorkdir,[name ext]);
        if ~exist(ldb)
            if nargout >= 1
                varargout{1} = ['File ' ldb ' does not exist.'];
            end
            return
        end
        ldb1 = readldb(ldb);
        if strcmpi(OPT.plot,'yes')
            plotldb (ldb,'color','-ok');
        end
        [newLdb1,found] = combineldb(newLdb,ldb1,OPT);
        if length(newLdb1) > 1
            newLdb = newLdb1(1);
            msg = 'The given ldb''s are not (all) combined'; 
            for m = 2:length(newLdb1)
                newLdb.x = [newLdb.x;NaN;newLdb1(m).x];
                newLdb.y = [newLdb.y;NaN;newLdb1(m).y];
            end
        else
            newLdb.x = newLdb1.x;%[newLdb1.x;NaN];
            newLdb.y = newLdb1.y;%[newLdb1.y;;NaN];
        end
    end
end
if strcmpi(OPT.plotnew,'yes') %&& ~isempty(found)
    plot(newLdb.x,newLdb.y,':xr');
    axis equal
end

if ~isempty(OPT.file)
    [pathstr, name, ext, versn] = fileparts(OPT.file);
    if isempty(ext)
        ext = '.ldb';
    end
    theFile = fullfile(pWorkdir,[name ext]);
    writeldb(theFile,OPT.text,newLdb);
end


