function httpopendap2cache(URL,pathhttp,pathlocal)
%HTTPOPENDAP2CACHE  Create .bat from OpenDAP HTTP server .xml to cache .nc
%
%   Script to convert an OpenDAP XML catalog on an HTTP Server to a .BAT  
%   file that can be executed in Windows PowerShell in order to download  
%   all NC variables that are defined.
%   
%   - Needs the function xml2struct (Faltena, 2010) to run.
%   - Allocated local folders need to be created in advance.
%   - It could be that large files (2+ GB) have problems downloading in Bitsadmin.
%
%   Example:
%       URL = 'http://opendap.deltares.nl/thredds/catalog/opendap/rijkswaterstaat/kustlidar/catalog.xml';
%       pathhttp = 'http://opendap.deltares.nl/thredds/fileServer/opendap/rijkswaterstaat/kustlidar/';  
%       pathlocal = 'C:\opendap\kustlidar\nc\';
%         httpopendap2cache(URL,pathhttp,pathlocal)


%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2015 Van Oord
%       Arjen-Pieter Troost
%
%       <a.p.troost@gmail.com>
%
%       Van Oord
%       Schaardijk 211
%       3063 NH Rotterdam
%       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.

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

%% Process input and write to Matlab working directory
[~,name,ext] = fileparts(URL);
urlwrite(URL, [pathlocal,name,ext]);
struct = xml2struct([pathlocal,name,ext]);

%% Convert dataset attributes to a single cell
variable = struct.catalog.dataset.Attributes.name;
dataset = struct.catalog.dataset.dataset;

numvars = numel(dataset);
if numvars == 1
    b = dataset.Attributes.name; 
    vars{1,2} = b;
else
    vars = cell([1 numvars]);
    for n=1:numvars
        vars{n,1} = n;
        vars{n,2} = dataset{1,n}.Attributes.name;
    end
end

%% Create .BAT file
fid = fopen([pathlocal,name,'.bat'], 'wt');
 if fid < 0
     error('File creation failed');
 end
textA = 'bitsadmin /transfer Job';
textB = ' /download /priority normal ';
 for n=1:numvars
    X = [textA,num2str(n, '%03i'),textB,pathhttp,vars{n,2},' ',pathlocal,vars{n,2}];
    fwrite(fid, evalc('[disp(X)]'));
 end
fclose(fid);