function [ok,info,varargout] = plotChart(sec,trans,varargin)
%PLOTCHART  plot the coastlines for the given section, transect combination.
%
%   It is possible to plot a chart of the MKL, BKL and TKL coastlines(kustlijnen)
%   for a given section and transect number combination. When no such
%   combination is found the function returns some info.
%   When the section is not found you will see a list of possible sections.
%   When the transect is not found in the given section an overview of
%   possible transects is given.
%
%   Syntax:
%  [ok,info] = plotChart(sec,trans,saveAsPlot)
%
%   Input:
%     sec        = the section to be selected
%                  1 - a number
%                  2 - a section name
%     trans      = the number of a transection
%     varargin   = keyword/value pairs
%                  'storage' - the directory where jpg files are stored
%                  'jpg'     - yes/no create the jpg file for a chart.
%                              'no'is default
%   Also: 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        = when true the chart could be created, otherwise it is false
%     info      = one line with text when ok is false
%     varargout = possible transsection numbers when the one given is not
%                 found. It is best to use this option because you can select
%                 a transect from this output again.
%
%   Example
%   plotChart
%
%   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: 03 Mar 2010
% Created with Matlab version: 7.8.0.347 (R2009a)

% $Id: plotChart.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/plotChart.m $
% $Keywords: $

%%

OPT.storage = '';
OPT.jpg     = 'no';
OPT         = setProperty(OPT,varargin{:});
if isempty(OPT.storage)
    storeAt = '';
else
    storeAt = [OPT.storage filesep];
end

if ~strcmpi(OPT.jpg,'yes')
    saveAsPlot = false;
else
    saveAsPlot = true;
end

ok   = true;
info = '';
if nargout > 2
    varargout{1} = [];
end

%%
% load the data that is produced with function jarkus_GEChart
load CoastLineInfo

% get the correct section number
if isnumeric(sec)
    % check for the given number
    info = 'No correct section number given';
    nrSec = cell2mat(sections.number(find(cell2mat(sections.number(:)) == sec)));
else
    %check for the given name
    info = 'No correct section name given';
    for n = 1 : length(sections.name)
        nrSec = [];
        if strcmpi(sections.name{n},sec)
            nrSec = sections.number{n};
            break
        end
    end
end
if isempty(nrSec)
    %no correct number or name given
    ok   = false;
    disp '=======================================';
    disp '= possible section names and numbers: =';
    disp '=======================================';
    if nargout > 2
        gather = {};
    end
    for n = 1 : length(sections.name)
        %s = ['name=' sections.name{n} '; number =' num2str(sections.number{n})]
        disp (['name=' sections.name{n} '; number =' num2str(sections.number{n})]);
        if nargout > 2
            gather{n,1} = sections.name{n};
            gather{n,2} = num2str(sections.number{n});
        end
    end
    if nargout > 2
        varargout = gather;
    end
    return
else
    info = '';
    indSec = find(cell2mat(sections.number) == nrSec);
end

combi = nrSec * 1e6 + trans;
nsecTrans = find(secTrans == combi);
if isempty(nsecTrans)
    ok = false;
    info = 'No correct transsections found for the section';
    disp '===============================';
    disp '= possible transsect numbers: =';
    disp '===============================';
    if nargout > 2
        gather = [];
    end
    
    m = 0;
    for n = 1 : length(secTrans)
        secNr   = floor(secTrans(n) / 1.e6);
        if secNr == nrSec
           transNr = secTrans(n) - secNr * 1.e6;
           disp (num2str(transNr))
           m = m + 1;
           if nargout > 2
               gather(m) = transNr;
           end
        end
    end
    if m == 0
        disp 'No transects found for the given section';
    end
    if nargout > 2
        varargout{1} = gather';
    end
    return
else
    if nargout > 2
        gather = [];
        m = 0;
        for n = 1 : length(secTrans)
            secNr   = floor(secTrans(n) / 1.e6);
            if secNr == nrSec
               transNr = secTrans(n) - secNr * 1.e6;
               m = m + 1;
               gather(m) = transNr;
            end
        end
        varargout{1} = gather';
    end
end

% found the section/transection combination; possible to plot the chart
figure;
plot(years,charts.MKL(nsecTrans,:),'bx-');
hold on
plot(years,charts.BKL(nsecTrans,:),'ro-');
plot(years,charts.TKL(nsecTrans,:),'g.-');
grid on

s = ['Section=' num2str(nrSec) ' (' sections.name{indSec} '); transect=' num2str(trans)];
title(s);
legend('MKL','BKL','TKL','location','best');
xlabel('year');
ylabel('distance from RSP (m)');

if saveAsPlot
    s=[storeAt num2str(nrSec) '_' num2str(trans)];
     print(gcf,'-dpng','-r100', [s '.png']);
%    print(gcf,'-djpeg','-zbuffer','-r300', [s '.jpg']);
end
close(gcf)

