function [volumes,ok] = computeVolumes(file,sheets,wanted,endDate)
%COMPUTEVOLUMES  for the given sheets compute the combined volumes
%
%   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:
%   [volumes,ok] = computeVolumes(file,sheets,wanted)
%
%   Input:
%   file   = the name of the excell file
%   sheets = the names of the sheets to be used for the computation
%   wanted = the type of volumes to be computed. When 'all' is found all
%            available volume types are computed. Available now:
%               'total'
%               'diep'
%               'ondiep'
%               'plaat'
%               'tussen'
%               'ingrepen' (per jaar, dus niet cumulatief)
%            the volumes are cumulative volumes.
%
%   Output:
%   volumes = 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
%   computeVolumes
%
%   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: 07 Sep 2010
% Created with Matlab version: 7.8.0.347 (R2009a)

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

%% Extra information for software design

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

% Total volume:   2
% Diep volume:   12
% Ondiep volume: 13

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

% record range: 4 - 57

% 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;

%get the column numbers for the various volume types
nr = setColumn({'total','diep','ondiep','ingreep'}); 

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

if isempty(record.end)
    [dummy1,dummy2,sheet] = xlsread(file,sheets{1});
    for n = 1 : length(sheet)
        s = sheet(n,1);
        if strcmpi(s,endDate)
            record.end = n;
            break
        end
    end
end


%compute total volume
if found('all',wanted) || found('total',wanted)
    ok = true;
    for nrSheet = 1: length(sheets)
        [dummy1,dummy2,sheet] = xlsread(file,sheets{nrSheet});
        if stored == -1
            %fill the date 
            stored = 0;
            volumes.date = sheet(record.begin:record.end,1);
        end
        if nrSheet == 1
            stored = stored + 1;
        end 
        volumes.names{stored} = 'total';
        y = getcolumn(sheet,nr(1));
        a = y(record.begin:record.end,1);
        [a,ok0] = cell2num(a);
        if nrSheet == 1
            x = a;
        else
            x = x + a;
        end
    end
    %compute the cumulative volume
    volumes.series(stored).data{1} = 0;
    for n = 2:length(x)
        volumes.series(stored).data{n} = volumes.series(stored).data{n-1} - (x(n) - x(n-1));
    end
end

%compute volume beneden -5m NAP
if found('all',wanted) || found('diep',wanted)
    ok = true;
    for nrSheet = 1: length(sheets)
        [dummy1,dummy2,sheet] = xlsread(file,sheets{nrSheet});
        if stored == -1
            %fill the date 
            stored = 0;
            volumes.date = sheet(record.begin:record.end,1);
        end
        if nrSheet == 1
            stored = stored + 1;
        end 
        volumes.names{stored} = 'diep';
        y = getcolumn(sheet,nr(2));
        a = y(record.begin:record.end,1);
        [a,ok0] = cell2num(a);
        if nrSheet == 1
            x = a;
        else
            x = x + a;
        end
    end
    %compute the cumulative volume
    volumes.series(stored).data{1} = 0;
    for n = 2:length(x)
        volumes.series(stored).data{n} = volumes.series(stored).data{n-1} - (x(n) - x(n-1));
    end
end

%compute volume beneden -2m NAP
if found('all',wanted) || found('ondiep',wanted)
    ok = true;
    for nrSheet = 1: length(sheets)
        [dummy1,dummy2,sheet] = xlsread(file,sheets{nrSheet});
        if stored == -1
            %fill the date 
            stored = 0;
            volumes.date = sheet(record.begin:record.end,1);
        end
        if nrSheet == 1
            stored = stored + 1;
        end 
        volumes.names{stored} = 'ondiep';
        y = getcolumn(sheet,nr(3));
        a = y(record.begin:record.end,1);
        [a,ok0] = cell2num(a);
        if nrSheet == 1
            x = a;
        else
            x = x + a;
        end
    end
    %compute the cumulative volume
    volumes.series(stored).data{1} = 0;
    for n = 2:length(x)
        volumes.series(stored).data{n} = volumes.series(stored).data{n-1} - (x(n) - x(n-1));
    end
end

%compute volume boven -2m NAP
if found('all',wanted) || found('plaat',wanted)
    ok = true;
    for nrSheet = 1: length(sheets)
        [dummy1,dummy2,sheet] = xlsread(file,sheets{nrSheet});
        if stored == -1
            %fill the date 
            stored = 0;
            volumes.date = sheet(record.begin:record.end,1);
        end
        if nrSheet == 1
            stored = stored + 1;
        end 
        volumes.names{stored} = 'plaat';
        y1 = getcolumn(sheet,nr(1));
        y2 = getcolumn(sheet,nr(3));
        a1 = y1(record.begin:record.end,1);
        a2 = y2(record.begin:record.end,1);
        [a1,ok0] = cell2num(a1);
        [a2,ok0] = cell2num(a2);
        if nrSheet == 1
            x = a1-a2;
        else
            x = x + (a1-a2);
        end
    end
    %compute the cumulative volume
    volumes.series(stored).data{1} = 0;
    for n = 2:length(x)
        volumes.series(stored).data{n} = volumes.series(stored).data{n-1} - (x(n) - x(n-1));
    end
end

%compute volume tussen -5m NAP en -2m NAP
if found('all',wanted) || found('tussen',wanted)
    ok = true;
    for nrSheet = 1: length(sheets)
        [dummy1,dummy2,sheet] = xlsread(file,sheets{nrSheet});
        if stored == -1
            %fill the date 
            stored = 0;
            volumes.date = sheet(record.begin:record.end,1);
        end
        if nrSheet == 1
            stored = stored + 1;
        end 
        volumes.names{stored} = 'tussen';
        y1 = getcolumn(sheet,nr(2));
        y2 = getcolumn(sheet,nr(3));
        a1 = y1(record.begin:record.end,1);
        a2 = y2(record.begin:record.end,1);
        [a1,ok0] = cell2num(a1);
        [a2,ok0] = cell2num(a2);
% ++ oude situatie
%         if nrSheet == 1
%             x = a1-a2;
%         else
%             x = x + (a1-a2);
%         end
% ++ nieuwe situatie
        if nrSheet == 1
            x = a2-a1;
        else
            x = x + (a2-a1);
        end
    end
    %compute the cumulative volume
    volumes.series(stored).data{1} = 0;
    for n = 2:length(x)
        volumes.series(stored).data{n} = volumes.series(stored).data{n-1} - (x(n) - x(n-1));
    end
end

%get the volume of the total ingrepen (per jaar)
if found('all',wanted) || found('ingreep',wanted)
    ok = true;
    for nrSheet = 1: length(sheets)
        [dummy1,dummy2,sheet] = xlsread(file,sheets{nrSheet});
        if stored == -1
            %fill the date 
            stored = 0;
            volumes.date = sheet(record.begin:record.end,1);
        end
        if nrSheet == 1
            stored = stored + 1;
        end 
        volumes.names{stored} = 'ingreep';
        y1 = getcolumn(sheet,nr(4));
        a1 = y1(record.begin:record.end,1);
        [a1,ok0] = cell2num(a1);
        if nrSheet == 1
            x = a1;
        else
            x = x + a1;
        end
    end
    %assign to volumes
    x(x==0)=NaN;
    volumes.series(stored).data{1} = 0;
    for n = 1:length(x)
        volumes.series(stored).data{n} = x(n);
    end
    
    stored = stored + 1;
    volumes.names{stored} = 'cum. ingreep';

    volumes.series(stored).data{1} = 0;
    start = 0.0;
    for n = 1:length(x)
        if ~isnan(x(n))
            start = start + x(n);
            volumes.series(stored).data{n} = start;
        else
            volumes.series(stored).data{n} = NaN;
        end
    end
    
end
