function d = vakloding_readGridDataNetcdf(varargin)
%VAKLODING_READGRIDDATANETCDF   read RWS grid bathymetyry from an OPeNDAP/netCDF file
%
%   D = vakloding_readGridDataNetcdf(opendap_file/netcdf_file, year, soundingID)
%
%   reads grid data from opendap_url/netcdf_file, where D is a vakloding struct 
%   as generated by VAKLODING_CREATEGRIDSTRUCT where field Z contains
%   all available times as the 3rd dimension
%
%   Example:
%
%    directory = 'http://dtvirt5.deltares.nl:8080/thredds/dodsC/opendap/'; % either remote
%    directory = 'P:\mcdata\opendap\'                                      % or local
%
%    vakloding_readGridDataNetcdf([directory,'/rijkswaterstaat/vaklodingen/vaklodingenKB129_1312.nc'])
%
%   See also:  VAKLODING_CREATEGRIDSTRUCT, VAKLODING_READGRIDDATANETCDFFILL

%   --------------------------------------------------------------------
%   Copyright (C) 2009 <COMPANY>
%       
%
%       <EMAIL>	
%
%       <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, write to the Free Software
%   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
%   USA
%   or http://www.gnu.org/licenses/licenses.html, http://www.gnu.org/, http://www.fsf.org/
%   --------------------------------------------------------------------

% Created: 22 Mar 2009
% Created with Matlab version: 7.5.0.338 (R2007b)

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

%%
% datatype -> number or string
% (http://dtvirt5.deltares.nl:8080/thredds/catalog/opendap/rijkswaterstaat/
% vaklodingen/catalog.html)
% name -> 
% year 
% soundingID

fname      = [];
year       = NaN;
soundingID = NaN;
switch nargin
    case 0
        error('at least base_url/base_path required as argument')
        return
    case 1
        [fname] = deal(varargin{:});
        % 1 argument, get all grids? This won't work.
    case 2
        % year is actually not enough because there are more than 1 samples
        % /year
        [fname, year] = deal(varargin{:});
    case 3
        % not sure what the sounding id is?
        [fname, year, soundingID] = deal(varargin{:});
end

d          = vakloding_creategridstruct();
x          = nc_varget (fname, 'x');
y          = nc_varget (fname, 'y');
dates      = nc_cf_time(fname);
datematrix = cell2mat(arrayfun(@datevec, dates, 'UniformOutput', false));

if (~isnan(year))
    date_indices = find(sum(datematrix(:,1) == year,2));
else
    date_indices = 1:length(dates);
end
d.Z = nan(length(x),length(y),length(date_indices));
for i=1:length(date_indices)
    % assume ordering by date
    disp(['Getting ' datestr(dates(date_indices(i)))])
    newZ             = nc_varget(fname, 'z', [date_indices(i)-1, 0, 0], [1, length(y), length(x)]);
    newZ(newZ==-9999)= nan;
    d.Z(:,:,i)       = squeeze(newZ)';
    d.t(i,:)         = dates(i);
end
d.X         = repmat(x, [1, length(y)]);
d.Y         = repmat(y, [1, length(x)])';
d.contour   = [min(x), min(y); min(x), max(y); max(x), max(y); max(x), min(y); min(x),min(y)];
d.xllcorner = min(x);
d.yllcorner = min(y);
d.year      = datematrix(date_indices,1);
d.cellsize  = x(2) - x(1);

end
