[44] | 1 | ;+ |
---|
| 2 | ; READ_FTP |
---|
| 3 | ; |
---|
| 4 | ; Syntax: |
---|
| 5 | ; READ_FTP, remote_host [, files] [, directory] [,/FILE] [,DATA=variable] |
---|
| 6 | ; [,USER=string] [,PASS=string] [,/PTR] |
---|
| 7 | ; |
---|
| 8 | ; Arguments |
---|
| 9 | ; remote_host - Name of the remote host (ftp server) that you want |
---|
| 10 | ; to connect to, or a complete ftp location such as for example: |
---|
| 11 | ; ftp://ftp.rsinc.com/pub/gzip/README.GZIP |
---|
| 12 | ; |
---|
| 13 | ; directory - Remote directory where the files reside on the ftp |
---|
| 14 | ; server |
---|
| 15 | ; |
---|
| 16 | ; files - A single filename or an array of filenames to be |
---|
| 17 | ; retrieved. |
---|
| 18 | ; |
---|
| 19 | ; Keywords |
---|
| 20 | ; FILE - Set this keyword to make a local copy of the file to be |
---|
| 21 | ; transferred. The local file will have the same name as the |
---|
| 22 | ; remote file and will be placed in the current working |
---|
| 23 | ; directory. |
---|
| 24 | ; |
---|
| 25 | ; DATA - Set this to a named variable that will contain either a |
---|
| 26 | ; byte array or an array of pointers to byte arrays with the |
---|
| 27 | ; transferred data. If there is more than one file, an array |
---|
| 28 | ; of pointers is returned, one for each file. |
---|
| 29 | ; Note that when downloading large files using /FILE |
---|
| 30 | ; instead will require much less memory since the entire file |
---|
| 31 | ; is not stored in a variable in that case. |
---|
| 32 | ; |
---|
| 33 | ; PTR - Set this keyword to return an array of pointers |
---|
| 34 | ; even when there is only one file. |
---|
| 35 | ; |
---|
| 36 | ; USER - Specify user name to connect to server with. Default is: |
---|
| 37 | ; anonymous. |
---|
| 38 | ; |
---|
| 39 | ; PASS - Specify password to use when connecting. Default is: |
---|
| 40 | ; test@test.com. |
---|
| 41 | ; |
---|
| 42 | ; Examples of use. |
---|
| 43 | ; 1) Retrieve and print the contents of ftp://ftp.rsinc.com/pub/gzip/README.GZIP: |
---|
| 44 | ; IDL> READ_FTP, 'ftp://ftp.rsinc.com/pub/gzip/README.GZIP', DATA=data |
---|
| 45 | ; IDL> help, data |
---|
| 46 | ; DATA BYTE = Array[2134] |
---|
| 47 | ; IDL> print, string(data) |
---|
| 48 | ; ------------------------------------------------------------------------------ |
---|
| 49 | ; README file: Research Systems Anonymous FTP site (ftp.rsinc.com) |
---|
| 50 | ; pub directory |
---|
| 51 | ; gzip directory |
---|
| 52 | ; ------------------------------------------------------------------------------ |
---|
| 53 | ; ... |
---|
| 54 | ; |
---|
| 55 | ; 2) Retrieve some files from podaac.jpl.nasa.gov and store the files |
---|
| 56 | ; in the current working directory: |
---|
| 57 | ; |
---|
| 58 | ; IDL> files = string(lindgen(10)+50,format='(%"MGB370.%3.3d.gz")') |
---|
| 59 | ; IDL> READ_FTP, 'podaac.jpl.nasa.gov', files, $ |
---|
| 60 | ; IDL> 'pub/sea_surface_height/topex_poseidon/mgdrb/data/MGB_370', /FILE |
---|
| 61 | ; IDL> spawn,'dir MGB*',/log_output |
---|
| 62 | ; Volume in drive C is Local Disk |
---|
| 63 | ; Volume Serial Number is 34CE-24DF |
---|
| 64 | ; |
---|
| 65 | ; Directory of C:\test\test0307 |
---|
| 66 | ; |
---|
| 67 | ; 07/28/2003 11:58a 362,167 MGB370.050.gz |
---|
| 68 | ; 07/28/2003 11:58a 333,005 MGB370.051.gz |
---|
| 69 | ; 07/28/2003 11:58a 310,287 MGB370.052.gz |
---|
| 70 | ; 07/28/2003 11:58a 358,771 MGB370.053.gz |
---|
| 71 | ; 07/28/2003 11:59a 387,282 MGB370.054.gz |
---|
| 72 | ; 07/28/2003 11:59a 361,633 MGB370.055.gz |
---|
| 73 | ; 07/28/2003 11:59a 383,075 MGB370.056.gz |
---|
| 74 | ; 07/28/2003 11:59a 365,844 MGB370.057.gz |
---|
| 75 | ; 07/28/2003 11:59a 383,918 MGB370.058.gz |
---|
| 76 | ; 07/28/2003 12:00p 372,712 MGB370.059.gz |
---|
| 77 | ; 10 File(s) 3,618,694 bytes |
---|
| 78 | ; |
---|
| 79 | ; These compressed files can cosequently be opened with OPENR and the |
---|
| 80 | ; /COMPRESSED keyword. |
---|
| 81 | ; |
---|
| 82 | ;- |
---|
| 83 | pro ftp_post, u, cmd, res, out=out, count=count |
---|
| 84 | compile_opt idl2 |
---|
| 85 | if (cmd ne '') then begin |
---|
| 86 | printf, u, cmd, format='(a)' |
---|
| 87 | ; |
---|
| 88 | ; comment out the following line to disable debug info |
---|
| 89 | print, '>'+cmd |
---|
| 90 | endif |
---|
| 91 | if (size(out,/type) eq 0) then out='2?? *' |
---|
| 92 | catch, err |
---|
| 93 | if (err ne 0) then return |
---|
| 94 | line='' |
---|
| 95 | count=0 |
---|
| 96 | while arg_present(res) do begin |
---|
| 97 | readf, u, line |
---|
| 98 | if count eq 0 then res=line else res=[res,line] |
---|
| 99 | count=count+1 |
---|
| 100 | ; |
---|
| 101 | ; comment out the following line to disable debug info |
---|
| 102 | print, '<'+line |
---|
| 103 | if strmatch(line,out) then break |
---|
| 104 | endwhile |
---|
| 105 | end |
---|
| 106 | |
---|
| 107 | pro ftp_parse_pasv, text, host, port |
---|
| 108 | t=strtrim(text,2) |
---|
| 109 | ind=where(strcmp(t,'227',3)) |
---|
| 110 | i=ind[0] |
---|
| 111 | if (i ne -1) then begin |
---|
| 112 | sub=stregex(t[i],'\([0-9,]*\)',/extract) |
---|
| 113 | p=strsplit(strmid(sub,1,strlen(sub)-2),',',/extract) |
---|
| 114 | p=strtrim(p,2) |
---|
| 115 | host=p[0]+'.'+p[1]+'.'+p[2]+'.'+p[3] |
---|
| 116 | port=256*long(p[4])+long(p[5]) |
---|
| 117 | endif |
---|
| 118 | end |
---|
| 119 | |
---|
| 120 | pro read_ftp, site, files, dir, port, data=data, file=file, user=user, $ |
---|
| 121 | pass=pass, ptr=ptr |
---|
| 122 | compile_opt idl2 |
---|
| 123 | if n_elements(port) eq 0 then port='ftp' |
---|
| 124 | if n_elements(files) eq 0 then begin |
---|
| 125 | if strcmp(site,'ftp://',6) then host=strmid(site,6) else host=site |
---|
| 126 | pos=strpos(host,'/') |
---|
| 127 | dir=strmid(host,pos) |
---|
| 128 | host=strmid(host,0,pos) |
---|
| 129 | pos=strpos(dir,'/',/reverse_search) |
---|
| 130 | files=strmid(dir,pos+1) |
---|
| 131 | dir=strmid(dir,0,pos) |
---|
| 132 | endif else host=site |
---|
| 133 | if (size(user,/type) eq 0) then user='anonymous' |
---|
| 134 | if (size(pass,/type) eq 0) then pass='test@test.com' |
---|
| 135 | ; |
---|
| 136 | socket, u, host, port, connect_timeout=5, read_timeout=5, /get_lun |
---|
| 137 | ftp_post, u, '', res |
---|
| 138 | ftp_post, u, 'USER '+user, res, out='3?? *' |
---|
| 139 | ftp_post, u, 'PASS '+pass, res |
---|
| 140 | ftp_post, u, 'TYPE I', res |
---|
| 141 | if (size(dir,/type) ne 0) then ftp_post, u, 'CWD '+dir, res |
---|
| 142 | if keyword_set(file) or arg_present(data) then begin |
---|
| 143 | bufsize=512 |
---|
| 144 | buffer=bytarr(bufsize) |
---|
| 145 | n=n_elements(files) |
---|
| 146 | if arg_present(data) then dat=ptrarr(n) |
---|
| 147 | for i=0, n-1 do begin |
---|
| 148 | ftp_post, u, 'SIZE '+files[i], res, out='213 *' |
---|
| 149 | sz=long64(strmid(res[n_elements(res)-1],4)) |
---|
| 150 | if arg_present(data) then dat[i]=ptr_new(bytarr(sz)) |
---|
| 151 | ftp_post, u, 'PASV', res |
---|
| 152 | ftp_parse_pasv, res, host, port |
---|
| 153 | ftp_post, u, 'RETR '+files[i], res, out='1?? *' |
---|
| 154 | socket, v, host, port, connect_timeout=5, read_timeout=5, $ |
---|
| 155 | /get_lun, /rawio |
---|
| 156 | tc=0ll |
---|
| 157 | if keyword_set(file) then openw,w,files[i],/get_lun |
---|
| 158 | while (tc lt sz) do begin |
---|
| 159 | if (sz-tc lt bufsize) then begin |
---|
| 160 | bufsize=sz-tc |
---|
| 161 | buffer=bytarr(bufsize) |
---|
| 162 | endif |
---|
| 163 | readu, v, buffer, transfer_count=dtc |
---|
| 164 | if arg_present(data) then $ |
---|
| 165 | (*dat[i])[tc]=(dtc eq bufsize)?buffer:buffer[0:dtc-1] |
---|
| 166 | if keyword_set(file) then $ |
---|
| 167 | writeu,w,(dtc eq bufsize)?buffer:buffer[0:dtc-1] |
---|
| 168 | tc=tc+dtc |
---|
| 169 | endwhile |
---|
| 170 | free_lun, v |
---|
| 171 | if keyword_set(file) then free_lun, w |
---|
| 172 | ftp_post, u, '', res |
---|
| 173 | endfor |
---|
| 174 | if arg_present(data) then begin |
---|
| 175 | if (n gt 1 or keyword_set(ptr)) then data=dat $ |
---|
| 176 | else data=temporary(*dat[0]) |
---|
| 177 | endif |
---|
| 178 | endif |
---|
| 179 | ftp_post, u, 'QUIT', res |
---|
| 180 | free_lun, u |
---|
| 181 | end |
---|