function [nourishment, nourish_info_RWS] = csb_getNourishVol(varargin)
%CSB_GETNOURISHVOL  Retrieve nourishment information from Netcdf
%
%   Get nourishment volumes (shoreface, beach and dune) for the given
%   transects and years and store this in a structure. Last cell is the
%   information of single nourishments soleley, obtained from the RWS-excel
%   sheet.
%
%   Syntax:
%   varargout = csb_getNourishVol(varargin)
%
%   Input:
%   id       = transect id or coastal area
%   year     = year to evaluate
%   varargin =
% Output:
%   nourishment      = structure with the information of the nourishments (year,id)
%                      and a matrix per type of nourishment with the nourished volume per
%                      transect (m^3/m)
%   nourish_info_rws = information of the individual nourishment (RWS excel sheet) 
%  
%   Example
%   csb_getNourishVol
%
%   See also csb_collect_data csb_createDataMatrix csb_getIndicator

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2012 Deltares
%       Dirk Knipping
%
%       dirk.knipping@deltares.nl
%
%       P.O. Box 177
%       2600MH Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation, either version 3 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 General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this library.  If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://www.OpenEarth.eu">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: 21 Jun 2012
% Created with Matlab version: 7.13.0.564 (R2011b)

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

%% Settings
OPT = struct(...
    'nc_file','http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/suppleties/suppleties.nc',...
    'id',[],...
    'year',[]);

% overrule default settings by property pairs, given in varargin
OPT = setproperty(OPT, varargin{:});

% check input
if ischar(OPT.id) % id is areaname
    jarkus_areaname = nc_varget(jarkus_url,'areaname');
    jarkus_id       = nc_varget(jarkus_url,'id');
    OPT.id          = jarkus_id(strcmp(jarkus_areaname,{OPT.id}));
    if isempty(OPT.id)
        error('Areaname does not exist');
    end
end



%% Extract data from netcdf
nc.year         = str2num(datestr(nc_cf_time(OPT.nc_file,'n_time'),'yyyy'));
nc.id           = nc_varget(OPT.nc_file,'id');
nc.shore        = nc_varget(OPT.nc_file,'vol_shoreface');
nc.shore_time   = nc_varget(OPT.nc_file,'time_end_shoreface'); % end date of shoreface nourishment in case of multiple nourishments the maximum value holds
nc.beach        = nc_varget(OPT.nc_file,'vol_beach');
nc.beach_time   = nc_varget(OPT.nc_file,'time_end_beach'); 
nc.dune         = nc_varget(OPT.nc_file,'vol_dune');
nc.dune_time    = nc_varget(OPT.nc_file,'time_end_dune');

%% Find data of interest (years and ids)
indexyear = find(ismember(nc.year,OPT.year));
indexid   = find(ismember(nc.id,OPT.id));

nourishment = struct(...
            'shoreface',struct(...
                        'vol',nc.shore(indexid,indexyear),...
                        'year',nc.year(indexyear),...
                        'id',nc.id(indexid),...
                        'end_date',nc.shore_time(indexid,indexyear)),...
            'beach',struct(...
                        'vol',nc.beach(indexid,indexyear),...
                        'year',nc.year(indexyear),...
                        'id',nc.id(indexid),...
                        'end_date',nc.beach_time(indexid,indexyear)),...
            'dune',struct(...
                        'vol',nc.dune(indexid,indexyear),...
                        'year',nc.year(indexyear),...
                        'id',nc.id(indexid),...
                        'end_date',nc.dune_time(indexid,indexyear)));
                     
if ~isequal(nourishment.shoreface.year,OPT.year') && ~isequal(nourishment.beach.year,OPT.year') && ~isequal(nourishment.dune.year,OPT.year')
    error('inconsistency in years');
end

% if ~isequal(nourishment.id,OPT.id')
%     error('inconsistency in transect ids');
% end
%% Add information about single nourishments (RWS-database)
nourish_info_RWS = struct('vol', nc_varget(OPT.nc_file,'vol'),...
                'n_code',           nc_varget(OPT.nc_file,'n_code'),... % code used by rws
                'beg_stretch',      nc_varget(OPT.nc_file,'beg_stretch'),...
                'end_stretch',      nc_varget(OPT.nc_file,'end_stretch'),...
                'type',             nc_varget(OPT.nc_file,'type'),...
                'beg_date',         str2num(datestr(nc_cf_time(OPT.nc_file,'beg_date'),'yyyy')),...
                'end_date',         str2num(datestr(nc_cf_time(OPT.nc_file,'end_date'),'yyyy')),...
                'kustvak',          {cellstr(nc_varget(OPT.nc_file,'kustvak'))});
nourish_info_RWS.areacode = cell2mat(rws_kustvak(nourish_info_RWS.kustvak)');
nourish_info_RWS.length   = abs(nourish_info_RWS.beg_stretch-nourish_info_RWS.end_stretch);
nourish_info_RWS.transid  = nourish_info_RWS.areacode*10e5+nourish_info_RWS.beg_stretch;
nourish_info_RWS.rvol     = (nourish_info_RWS.vol./(nourish_info_RWS.length*10)); % relative volume [m3/m] same as in 
