function varargout = plotTextFiles(dirName,varargin)
%PLOTTEXTFILES  Plot the series in selected txt files
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = plotTextFiles(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;
for n=1:length(f)
    if ~f(n).isdir
        series=loadTextFile([setBackslash(dirName,'set') f(n).name],'header',OPT.header);
        series(series == -999.9) = NaN;
        m = m + 1;
        if f(n).name(1:1) == 's'
            specs='s-g';
        elseif f(n).name(1:1) == 'g'
            specs='x-r' ;
        elseif f(n).name(1:1) == 'd'
            specs='.-b';
        end
        if m == 1
            plot(series(:,1),series(:,2),specs);
            hold on
            grid on
        else
            plot(series(:,1),series(:,2),specs);
        end 
        if f(n).name(1:1) == 's'
            if ~springtij
                lh{m} = 'Springtij';
                springtij=true;
            else
                lh{m} = '';
            end
        elseif f(n).name(1:1) == 'g'
            if ~gemtij
                lh{m} = 'Gemiddeld tij';
                gemtij=true;
            else
                lh{m} = '';
            end
        elseif f(n).name(1:1) == 'd'
            if ~doodtij
                lh{m} = 'Doodtij';
                doodtij=true;
            else
                lh{m} = '';
            end
        end
            
    end
end

legend(lh,'location','east');
xlabel('Jaar');
ylabel('Waterstand [m tov NAP]');

title(OPT.title);
    
print(gcf,'-dpng','-r300',[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
