get_BADC_data.m 2.06 KB
Newer Older
1
function tfiles = get_BADC_data(site, filepath, files, varargin)
2 3 4 5 6 7 8 9 10 11
% 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
12
%   credentials - optional cell array of {'username', 'password'}.
13 14 15 16 17 18 19
% 
% 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
20
%   'metum').
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

global ftbverbose

assert(iscell(files), 'Provide a cell array of files to download')

tdir = fullfile(tempdir, 'metum');

if exist(tdir, 'dir') ~= 7
    mkdir(tdir)
end

nf = length(files);

tfiles = cell(nf, 1);

for j = 1:nf
    % Open a remote connection to the FTP site. I found that the timeout on
    % the FTP connection to ftp.ceda.ac.uk is pretty low, so it's best to
    % explicitly re-open it each time we want a bunch of new files.
40 41 42 43 44
    if nargin == 3
        remote = ftp(site);
    elseif nargin == 4
        remote = ftp(site, credentials(1), credentials(2));
    end
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    S = whos('remote');
    assert(strcmpi(S.class, 'ftp'), 'remote is not an FTP class. See HELP FTP.')
    clear S
    
    try
        cd(remote, filepath);
    catch
        warning('No such path %s (file %s)', filepath, files{j})
        continue
    end

    try
        if ftbverbose
            fprintf('Downloading %s to %s\n', fullfile(site, filepath, files{j}), tdir)
        end
        tfiles{j} = mget(remote, files{j}, tdir);
    catch
        tfiles{j} = nan;
        warning('Failed to fetch data from %s', fullfile(site, filepath, files{j}))
    end
    
    % Close the connection to the FTP server.
    close(remote)
end