Contributed by: Doug Rickman, Global Hydrology and Climate Center, MSFC, NASA
This is aimed at intermediate REXX users.
The code assumes the ftp library is already loaded.
Automated recursive down load of the massive USGS data failed everyway we tried. This was created to get it by shear brute force. It illustrates the use of the ftp library rxFtp.
/*****************************************************************************/
/*************** FTP operation examples ***************/
/* note the "" in the ftppwd command and the stripping of the returned string*/
/* note you must create the directory to written into. ftpget will not */
/* create directories as needed. */
/* conect to the USGS server */
host='edcftp.cr.usgs.gov'
userid='anonymous'
password='your_email_address'
rc = FtpSetUser(host,userid,password)
/* for each letter of the alphabet get the data directories */
do i = 1 to 26
NewDirectory='/pub/data/DLG/100K/'
rc=ftpchdir(NewDirectory)
rc=ftpdir('*',Directory.)
do j=1 to Directory.0
parse var Directory.j permissions number owner 26 group 29 size 43 month 47 day 51 year 56 name
select /* we are looking for directories other than "." and ".." */
when right(Directory.j,1)=':' then SubDirectory=substr(Directory.j,1,length(Directory.j)-1)
when left(permissions,1)='d' & name='.' then nop
when left(permissions,1)='d' & name='..' then nop
when left(permissions,1)='d' then queue Letter subdirectory name day month year
otherwise nop
end /* select */
end j /* do */
drop(Directory.)
end i
do while queued()<>0
Data=linein("QUEUE:")
parse var Data Subdirectory Name Day Month Year .
NewDirectory='/pub/data/DLG/100K/'||Subdirectory||'/'||name
rc=ftpchdir(NewDirectory)
rc=ftppwd("CurrentD")
say 'CurrentD='substr(CurrentD,2,length(CurrentD)-23)
end
/*****************************************************************************/
/*************** FTP error handling ***************/
rc=ftpget(LocalFile,RemoteFile)
if rc=-1 then call ErrorHandler FTPERRNO 'FTPGet'
rc = FtpSetUser(host,userid,password)
if rc=0 then say 'FTPSetUser error! The input string(s) is not valid. '
rc = FTPSetBinary('BINARY')
if rc=0 then say 'FTPSetBinary error! The input string is not valid. '
rc=FTPPing(host,length)
if Datatype(rc,'N')<>1 then call ErrorHandler rc FTPPing
/* --------------------------------------------------------------------------*/
/* --- begin subroutine - ErrorHandler: -------------*/
/* Pass the arguments FTPERRNO and ErrorSource */
/* FTPPing, FtpSetUser and FtpSetBinary errors are found by examining the */
/* returned values. FTPPing should be a number. If not it is an error. */
/* FtpSetUser and FtpSetBinary return either 1 or 0. Zero means the string */
/* is not valid. For FTPPing the returned value should be set to FTPERRNO */
/* and ErrorHandler called. */
ErrorHandler: procedure
parse arg FTPERRNO ErrorSource
if translate(ErrorSource)='FTPPING' then call PingError
select
when FTPERRNO="FTPSERVICE" then say 'Error: "unknown service" occurred',
||'while processing 'ErrorSource
when FTPERRNO="FTPHOST" then say 'Error: "unknown host" occurred',
||' while processing 'ErrorSource
when FTPERRNO="FTPSOCKET" then say 'Error: "unable to obtain socket" ',
||'occurred while processing 'ErrorSource
when FTPERRNO="FTPCONNECT" then say 'Error: "unable to connect to server" ',
||'occurred while processing 'ErrorSource
when FTPERRNO="FTPLOGIN" then say 'Error: "login failed" occurred',
||' while processing 'ErrorSource
when FTPERRNO="FTPABORT" then say 'Error: "transfer stopped" occurred',
||' while processing 'ErrorSource
when FTPERRNO="FTPLOCALFILE" then say 'Error: "problem opening local file"',
||' occurred while processing 'ErrorSource
when FTPERRNO="FTPDATACONN" then say 'Error: "problem initializing data ',
||'connection" occurred while processing 'ErrorSource
when FTPERRNO="FTPCOMMAND" then say 'Error: "command failed" occurred',
||' while processing 'ErrorSource
when FTPERRNO="FTPPROXYTHIRD" then say 'Error: "proxy server does not support ',
||'third party transfers" occurred while processing 'ErrorSource
when FTPERRNO="FTPNOPRIMARY" then say 'Error: "no primary connection for ',
||'proxy transfer" occurred while processing 'ErrorSource
otherwise say 'FTPERRNO='FTPERRNO '(No text available for this error number) ',
||'occurred while processing 'ErrorSource
end /* select */
exit
PingError:
select
when FTPERRNO="PINGREPLY" then say 'Host does not reply '
when FTPERRNO="PINGSOCKET" then say 'Unable to obtain socket '
when FTPERRNO="PINGPROTO" then say 'Unknown protocol ICMP '
when FTPERRNO="PINGSEND" then say 'Send failed '
when FTPERRNO="PINGRECV" then say 'Receive failed '
when FTPERRNO="PINGHOST" then say 'Unknown host '
exit
/* --- end soubroutine - ErrorHandler: -------------*/
/* --------------------------------------------------------------------------*/