function varargout = plotWSHistoryFiles(dirName,varargin)
%plotWSHistoryFiles  Plot the series in selected txt files
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = plotWSHistoryFiles(dirName,varargin)
%
%   Input:
%   dirName   = the directory name where the text files are found
%   varargin  = keyword/value combinations
%               'files'  : the name of the file(s) to be used, wildcards
%                          are allowed. 
%               'header' : the number of header lines to be skipped
%                          default=0
%               'x'      : the column of the x-base, default = 1
%               'y'      : the column of the series values, default = 2
%               'plot'   : the name of the jpg file
%
%   Output:
%   varargout = 1 : success
%               0 : no success
%
%   Example
%   plotTextFiles('dirname','header',5,'plot','try')
%
%   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: 01 Sep 2010
% Created with Matlab version: 7.8.0.347 (R2009a)

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

%% initialize

OPT.files  = '*.txt';
OPT.header = 0;
OPT.x      = 1;
OPT.y      = 2;
OPT.plot   = 'test';
OPT.title  = 'test';
OPT = setproperty(OPT,varargin{:});

% get the files for the given combination of dir and files
f=dir([setBackslash(dirName,'set') OPT.files]);

m = 0;
l = 0;
lh ='';
springtij = false;
gemtij    = false;
doodtij   = false;
series_s = [];
series_g = [];
series_d = [];
for n=1:length(f)
    if ~f(n).isdir && ~strcmpi(f(n).name,'temp.txt')
        series=loadTextFile([setBackslash(dirName,'set') f(n).name],'header',OPT.header);
        series(series == -999.9) = NaN;
        series(:,2) = series(:,2)/100.00;
        if f(n).name(1:1) == 's'
            if ~springtij
                springtij = true;
                series_s = series;
            else
                series_s(end+1,:) = [NaN,NaN];
                series_s = [series_s;series];
            end
        elseif f(n).name(1:1) == 'g'
            if ~gemtij
                gemtij = true;
                series_g = series;
            else
                series_g(end+1,:) = [NaN,NaN];
                series_g = [series_g;series];
            end
        elseif f(n).name(1:1) == 'd'
            if ~doodtij
                doodtij = true;
                series_d = series;
            else
                series_d(end+1,:) = [NaN,NaN];
                series_d = [series_d;series];
            end
        end
    end
end


if ~isempty(series_s)
    specs='s-g';
    plot(series_s(:,1),series_s(:,2),specs);
    if m == 0
        hold on
        grid on
    end
    m = m + 1;
    lh{m} = 'Spring tij';
end

if ~isempty(series_g)
    specs='x-r';
    plot(series_g(:,1),series_g(:,2),specs);
    if m == 0
        hold on
        grid on
    end
    m = m + 1;
    lh{m} = 'Gemiddeld tij';
end

if ~isempty(series_d)
    specs='.-b';
    plot(series_d(:,1),series_d(:,2),specs);
    if m == 0
        hold on
        grid on
    end
    m = m + 1;
    lh{m} = 'Dood tij';
end


legend(lh,'location','east');
xlabel('Jaar');
ylabel('Waterstand [m tov NAP]');
title(OPT.title);
% datetick('x','yyyy','keepticks')    
print(gcf,'-dpng','-r100',[setBackslash(dirName,'set') OPT.plot '.png']);
%% print(gcf,'-dpdf','-painters',[setBackslash(dirName,'set') OPT.plot '.pdf']);
close(gcf);
if nargout ==1 
    varargout{1} = 1;
end
