function writeIngrepen(OPT,ingrepen)
%WRITEINGREPEN  Write all ingrepen series to the file
%
%   The ingrepen are written to the file. There is a column for each series
%   type, preceded with the time string.
%
%   Syntax:
%   writeIngrepen(file,ingrepen)
%
%   Input:
%   file     = the name of the file where the series values are written, it is
%              a text file with columns seperated by spaces.
%   ingrepen = the structured variable with the ingrepen series values,
%              created in Westerschelde_ingrepen.m with a call to 
%              computeIngrepen.m
%
%   Output:
%
%   Example
%          
%   See also Westerschelde_ingrepen.m, computeIngrepen.m

%% 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: 25 Oct 2010
% Created with Matlab version: 7.8.0.347 (R2009a)

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

%% Open file

file = OPT.file;

if ~isempty(OPT.cd)
    current = pwd;
    cd(OPT.cd);
end

if iscell(file)
    fid = fopen(file{1},'w'); %the file is created or overwritten
else
    fid = fopen(file,'w'); %the file is created or overwritten
end

% write the series names to the file

c = fprintf(fid,'%s \n','** date');
for n =  1 : length(ingrepen.names)
    c = fprintf(fid,'%s%s \n','** ',ingrepen.names{n});
end
c = fprintf(fid,'%s \n','==');

% write the series values for each date moment

spaces = '              ';
s      = [];
for n = 1 : length(ingrepen.date)
%     c = fprintf(fid,'%s %E %E %E %E %E %E\n',ingrepen.date{n}, ingrepen.series(1).data(n)); %,...

    for m = 1 : 6
        if isnan(ingrepen.series(m).data(n))
            s{m} = 'NaN';
        else
            s{m} = sprintf('%E', ingrepen.series(m).data(n));
        end
        l1 = length(s{m});
        if l1 < 14
            s{m} = [spaces(1:14-l1) s{m}];
        end
    end
    c = sprintf('%s %s %s %s %s %s %s',ingrepen.date{n}, s{1:6});
    ok = fprintf(fid,'%s \n',c);
end

if ~isempty(OPT.cd)
    cd(current);
end

end

