function [ingrepen,ok] = computeIngrepen(file,sheets,endDate)
%COMPUTEINGREPEN  for the given sheets gather the ingrepen
%
%   All given sheets are used to compute the combined volumes. Volumes to
%   be computed:
%        Total volume
%        Volume for diep water
%        Volume for ondiep water
%
%   Syntax:
%   [ingrepen,ok] = computeIngrepen(file,sheets,endDate)
%
%   Input:
%   file   = the name of the excell file
%   sheets = the names of the sheets to be used for the computation
%
%   Output:
%!?   ingrepen = the resulting volume series the date/time series (index = 1)
%!?              the parameter is a structure with the following division:
%!?             volumes.names : the names of the series (= columns)
%!?              volumes.date  : the dates of the series values
%!?              volumes.series: the date series of the volumes in the same
%!?                             order as given with the names.
%             
%   ok      = true when successfull
%
%   Example
%   computeIngrepen
%
%   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: 14 Oct 2010
% Created with Matlab version: 7.8.0.347 (R2009a)

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

%% Extra information for software design

% Each type of ingreep uses a specific column in the sheets. When more
% sheets are used, the data in the columns is summed up for each ingreep
% type. The software gives the column number for each type.

% baggeren:            4 (D)
% storten:             5 (E)
% zandwinning:         6 (F)
% wrak:                7 (G)

% Extra information
% total ingreep:       8 (H) = (E-F+G-D)/1.1
% cumulative ingreep:  9 (I) = SUM(H) each year
% Vtot:                2 (B)
% Vtot cumulative:     3 (C) = sum(B) each year
% nat cumulative:     10 (J) = C - I

% The number of date records wil change over time, in the future. The
% software gives the record range to be used. The ingrepen are computed
% for each date record.

% record range: 4 - 70

% A check is done whether the records hold the same dates

%% Computation

%set record range
record.begin = 4;
if isempty(endDate)
    record.end   = 70;
else
    record.end = [];
end

% initialize the volume parameter

% % volumes.names = [];
% % volumes.date  = [];
    
stored = -1;

% set the column numbers for the various ingreep types (see extra information)


ingrepen.names = {'baggeren','storten','zandwinning','wrak','totaal','cumulatief'};
nr   = {4,5,6,7};
sign = {-1.0,1.0,-1.0,1.0}; 

% no volumes computed yet
ok = false;
if isempty(sheets)
    return
end
ok = true;

%compute totals of ingrepen

for nrSheet = 1: length(sheets)
    ii=nrSheet
    s=sheets(nrSheet)
    [dummy1,dummy2,sheet] = xlsread(file,sheets{nrSheet});
    if stored == -1
        %fill the date 
        stored = 0;
        if isempty(record.end)
            for n = 1 : length(sheet)
                s = sheet(n,1);
                if strcmpi(s,endDate)
                    record.end = n;
                    break
                end
            end
        end
        l = record.end - record.begin + 1;
        for n = 1 : length(nr)
            ingrepen.series(n).data = ones(l,1)*NaN;
        end
        ingrepen.date = sheet(record.begin:record.end,1);
    end
    
    for n = 1 : 4
        y = getcolumn(sheet,nr{n});
        a = y(record.begin:record.end,1);
        [a,ok0] = cell2num(a);
        a(a == 0) = NaN;
        ingrepen.series(n).data = operation('+',ingrepen.series(n).data,a);
    end
    
end

for n = 1 : 4
    ingrepen.series(n).data = sign{n} * ingrepen.series(n).data / (1.1);
end

% compute total ingreep
ingrepen.series(5).data = operation('+',ingrepen.series(2).data,ingrepen.series(3).data);
ingrepen.series(5).data = operation('+',ingrepen.series(5).data,ingrepen.series(4).data);
ingrepen.series(5).data = operation('+',ingrepen.series(5).data,ingrepen.series(1).data);
% ingrepen.series(5).data = ingrepen.series(5).data/(1.1);

%compute the cumulative ingreep
ingrepen.series(6).data    = zeros(l,1);
if isnan(ingrepen.series(5).data(1))
    ingrepen.series(6).data(1) = 0;
else
    ingrepen.series(6).data(1) = ingrepen.series(5).data(1);
end
    
for n = 2:l
    ingrepen.series(6).data(n) = operation('+',ingrepen.series(6).data(n-1),ingrepen.series(5).data(n));
end
ingrepen.series(6).data = ingrepen.series(6).data - ingrepen.series(6).data(1);

end
