


Child function to do the actual downloading from the BADC site via FTP.
If the remote file doesn't exist, the function continues to the next file
warning that the file couldn't be found. You may end up with gaps if the
Met Office don't have those data (e.g. the 31st July 2010 doesn't have
any model results, for some reason).
INPUTS:
site - FTP server name (e.g. ftp.ceda.ac.uk')
filepath - path to the files to download
files - cell array of a file or files to download
credentials - optional cell array of {'username', 'password'}.
OUTPUTS:
tfiles = cell array of files downloaded. NaN = failed to download.
WARNING:
This function will indiscriminately overwrite files in the destination
directory (which is the system temporary directory appended with
'metum').

0001 function tfiles = get_BADC_data(site, filepath, files, varargin) 0002 % Child function to do the actual downloading from the BADC site via FTP. 0003 % If the remote file doesn't exist, the function continues to the next file 0004 % warning that the file couldn't be found. You may end up with gaps if the 0005 % Met Office don't have those data (e.g. the 31st July 2010 doesn't have 0006 % any model results, for some reason). 0007 % 0008 % INPUTS: 0009 % site - FTP server name (e.g. ftp.ceda.ac.uk') 0010 % filepath - path to the files to download 0011 % files - cell array of a file or files to download 0012 % credentials - optional cell array of {'username', 'password'}. 0013 % 0014 % OUTPUTS: 0015 % tfiles = cell array of files downloaded. NaN = failed to download. 0016 % 0017 % WARNING: 0018 % This function will indiscriminately overwrite files in the destination 0019 % directory (which is the system temporary directory appended with 0020 % 'metum'). 0021 0022 global ftbverbose 0023 0024 assert(iscell(files), 'Provide a cell array of files to download') 0025 0026 tdir = fullfile(tempdir, 'metum'); 0027 0028 if exist(tdir, 'dir') ~= 7 0029 mkdir(tdir) 0030 end 0031 0032 nf = length(files); 0033 0034 tfiles = cell(nf, 1); 0035 0036 for j = 1:nf 0037 % Open a remote connection to the FTP site. I found that the timeout on 0038 % the FTP connection to ftp.ceda.ac.uk is pretty low, so it's best to 0039 % explicitly re-open it each time we want a bunch of new files. 0040 if nargin == 3 0041 remote = ftp(site); 0042 elseif nargin == 4 0043 remote = ftp(site, credentials(1), credentials(2)); 0044 end 0045 S = whos('remote'); 0046 assert(strcmpi(S.class, 'ftp'), 'remote is not an FTP class. See HELP FTP.') 0047 clear S 0048 0049 try 0050 cd(remote, filepath); 0051 catch 0052 warning('No such path %s (file %s)', filepath, files{j}) 0053 continue 0054 end 0055 0056 try 0057 if ftbverbose 0058 fprintf('Downloading %s to %s\n', fullfile(site, filepath, files{j}), tdir) 0059 end 0060 tfiles{j} = mget(remote, files{j}, tdir); 0061 catch 0062 tfiles{j} = nan; 0063 warning('Failed to fetch data from %s', fullfile(site, filepath, files{j})) 0064 end 0065 0066 % Close the connection to the FTP server. 0067 close(remote) 0068 end 0069