function [ok,info,varargout] = plotSectionCharts(section,varargin)
%PLOTSECTIONCHARTS  Plot the charts with MKL, TKL and BKL coastlines for the given section
%
%   The charts for the section and its jarkus transects are plotted 
%   as jpg files. The charts are for the years where possibly MKL, BKL and
%   TKL coastline information was found
%
%   Syntax:
%   plotSectionCharts(section)
%
%   Input: 
%     section  = the section number to plot the charts for
%     varargin = when given:
%                "plotAllCharts.m" called from this function
%                otherwise it is assumed that it did run by a call to the
%                function itself
%     load CoastLineInfo
%       a save must have been done of various data in "CoastLineInfo"
%         charts  : the coast line values for the transects and the years
%         years   : the years with possible data for the coast lines
%         secTrans: the id for the section and the transect = section*10^6 +
%                   transect
%         LonLat  : the Lon/lat coordinates for the given transects in secTrans
%         sections: the section numbers and the related section names
%
%   Output:
%      ok        = 0 no correct computation, the function stopped
%                 1 no problems
%      info      = info message
%      varargout = when ok==0, possible section numbers
%
%   Example
%   plotSectionCharts
%
%   See also 

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2010 <COMPANY>
%       Lou Verhage
%
%       <lou.verhage@deltares.nl>	
%
%       <ADDRESS>
%
%   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: 22 Mar 2010
% Created with Matlab version: 7.8.0.347 (R2009a)

% $Id: plotSectionCharts.m 4926 2011-04-18 13:00:40Z verhage $
% $Date: 2011-04-18 15:00:40 +0200 (Mon, 18 Apr 2011) $
% $Author: verhage $
% $Revision: 4926 $
% $HeadURL: https://svn.oss.deltares.nl/repos/openearthrawdata/trunk/rijkswaterstaat/kustlijnkaartenboek/scripts/plotSectionCharts.m $
% $Keywords: $

%% initialize

OPT.storage  = '';
OPT.mfile    = '';
OPT.waittext = '';

OPT = setProperty(OPT,varargin);
if strcmpi(OPT.mfile,'plotAllCharts')
    called = true;
else
    called = false;
end

load CoastLineInfo;
% get the transects for the given section
clear LonLat charts years

% get the correct section number

if isnumeric(section)
    % check for the given number
    info = 'No correct section number given';
    nrSec = cell2mat(sections.number(find(cell2mat(sections.number(:)) == section)));
else
    %check for the given name
    info = 'No correct section name given';
    for n = 1 : length(sections.name)
        nrSec = [];
        if strcmpi(sections.name{n},section)
            nrSec = sections.number{n};
            break
        end
    end
end
if nargout > 2
    varargout{1} =[];
end
if ~isempty(nrSec)
    ok = false;
    if called
        handle = waitbar(0, [mfilename ' ' OPT.waittext]);
        m = 0;
        for n = 1 : length(secTrans)
            secNr = floor(secTrans(n) / 1.e6);
            if secNr == section
                m = m + 1;
            end
        end
    end
    telop = 0;
    for n = 1 : length(secTrans)
        secNr = floor(secTrans(n) / 1.e6);
        if secNr == section
            transNr = secTrans(n) - secNr * 1.e6;
            telop = telop + 1;
            if called
                waitbar(telop/m);
            end
            if nargout > 2
                [ok,info,check] = plotChart(section,transNr,'storage',OPT.storage,'jpg','yes');
            else
                [ok,info] = plotChart(section,transNr,'storage',OPT.storage,'jpg','yes');
            end
            if ~ok
                if called
                    waitbar(1, handle, 'Done!');
                    close(handle)
                end
                if nargout > 2
                    varargout{1} = check;
                end
                return
            end
        end
    end
    if called
        waitbar(1, handle, 'Done!');
        close(handle)
    end
else
    ok = false;
    info = ['Section number "' num2str(section) '" is not found'];
    if nargout > 2
        gather = {};
        for n = 1 : length(sections.name)
            gather{n,1} = sections.name{n};
            gather{n,2} = num2str(sections.number{n});
        end
        varargout{1} = gather;
    end
end

