source: trunk/SRC/ToBeReviewed/LECTURE/read_ftp.pro @ 378

Last change on this file since 378 was 371, checked in by pinsard, 16 years ago

improvements of headers (alignments of IDL prompt in examples)

  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1;+
2;
3; @file_comments
4;
5; @categories
6;
7; @param U
8;
9; @param CMD
10;
11; @param RES
12;
13; @keyword OUT
14;
15; @keyword COUNT
16; Upon return, the number of elements in the result set.
17; This is only important when the result set is the empty
18; set, in which case COUNT is set to zero.
19;
20; @restrictions
21;
22; @examples
23;
24; @history
25;
26; @version
27; $Id$
28;
29;-
30PRO ftp_post, u, cmd, res, out=out, count=count
31;
32  compile_opt idl2, strictarrsubs
33;
34  if (cmd ne '') then begin
35    printf, u, cmd, format='(a)'
36;
37; comment out the following line to disable debug info
38    print, '>'+cmd
39  endif
40  if (size(out,/type) eq 0) then out='2?? *'
41  catch, err
42  if (err ne 0) then return
43  line=''
44  count=0
45  while arg_present(res) do begin
46    readf, u, line
47    if count eq 0 then res=line else res=[res,line]
48    count=count+1
49;
50; comment out the following line to disable debug info
51    print, '<'+line
52    if strmatch(line,out) then break
53  endwhile
54end
55;
56;+
57;
58; @file_comments
59;
60; @categories
61;
62; @param TEXT
63; ASCII text string containing the message.
64;
65; @param HOST
66;
67; @param PORT
68;
69; @restrictions
70;
71; @examples
72;
73; @history
74;
75; @version
76; $Id$
77;
78;-
79PRO ftp_parse_pasv, text, host, port
80  t=strtrim(text,2)
81  ind=where(strcmp(t,'227',3))
82  i=ind[0]
83  if (i ne -1) then begin
84    sub=stregex(t[i],'\([0-9,]*\)',/extract)
85    p=strsplit(strmid(sub,1,strlen(sub)-2),',',/extract)
86    p=strtrim(p,2)
87    host=p[0]+'.'+p[1]+'.'+p[2]+'.'+p[3]
88    port=256*long(p[4])+long(p[5])
89  endif
90end
91;
92;+
93;
94; @file_comments
95;   READ_FTP, remote_host [, files] [, directory] [,/FILE] [,DATA=variable]
96;              [,USER=string] [,PASS=string] [,/PTR]
97;
98; @param DIR
99; Remote directory where the files reside on the ftp server
100;
101; @param FILES
102; A single filename or an array of filenames to be retrieved.
103;
104; @keyword FILE
105; Set this keyword to make a local copy of the file to be
106; transferred.  The local file will have the same name as the
107; remote file and will be placed in the current working
108; directory.
109;
110; @keyword DATA
111; Set this to a named variable that will contain either a
112; byte array or an array of pointers to byte arrays with the
113; transferred data.  If there is more than one file, an array
114; of pointers is returned, one for each file.
115; Note that when downloading large files using /FILE
116; instead will require much less memory since the entire file
117; is not stored in a variable in that case.
118;
119; @keyword PTR
120; Set this keyword to return an array of pointers
121; even when there is only one file.
122;
123; @keyword USER {default=anonymous}
124; Specify user name to connect to server with.
125;
126; @keyword PASS {default=test\@test.com}
127; Specify password to use when connecting.
128;
129; @examples
130; 1) Retrieve and print the contents of ftp://ftp.rsinc.com/pub/gzip/README.GZIP:
131;   IDL> READ_FTP, 'ftp://ftp.rsinc.com/pub/gzip/README.GZIP', DATA=data
132;   IDL> help, data
133;          DATA            BYTE      = Array[2134]
134;   IDL> print, string(data)
135;     ------------------------------------------------------------------------------
136;     README file: Research Systems Anonymous FTP site (ftp.rsinc.com)
137;                   pub directory
138;                   gzip directory
139;   ------------------------------------------------------------------------------
140;   ...
141;
142; 2) Retrieve some files from podaac.jpl.nasa.gov and store the files
143;    in the current working directory:
144;
145;   IDL> files = string(lindgen(10)+50,format='(%"MGB370.%3.3d.gz")')
146;   IDL> READ_FTP, 'podaac.jpl.nasa.gov', files,  $
147;   IDL>       'pub/sea_surface_height/topex_poseidon/mgdrb/data/MGB_370', /FILE
148;   IDL> spawn,'dir MGB*',/log_output
149;     Volume in drive C is Local Disk
150;     Volume Serial Number is 34CE-24DF
151;
152;     Directory of C:\test\test0307
153;
154;    07/28/2003  11:58a             362,167 MGB370.050.gz
155;    07/28/2003  11:58a             333,005 MGB370.051.gz
156;    07/28/2003  11:58a             310,287 MGB370.052.gz
157;    07/28/2003  11:58a             358,771 MGB370.053.gz
158;    07/28/2003  11:59a             387,282 MGB370.054.gz
159;    07/28/2003  11:59a             361,633 MGB370.055.gz
160;    07/28/2003  11:59a             383,075 MGB370.056.gz
161;    07/28/2003  11:59a             365,844 MGB370.057.gz
162;    07/28/2003  11:59a             383,918 MGB370.058.gz
163;    07/28/2003  12:00p             372,712 MGB370.059.gz
164;                  10 File(s)      3,618,694 bytes
165;
166;  These compressed files can consequently be opened with OPENR and the
167;   /COMPRESSED keyword.
168;
169; @history
170;
171; @version
172; $Id$
173;
174; @todo
175; seb: que fait-on de "syntax" au debut du header?
176; give examples with date in year 0 (should not exists but may happen)
177;
178;-
179PRO read_ftp, site, files, dir, port, data=data, file=file, user=user, $
180              pass=pass, ptr=ptr
181;
182  compile_opt idl2
183;
184  if n_elements(port) eq 0 then port='ftp'
185  if n_elements(files) eq 0 then begin
186    if strcmp(site,'ftp://',6) then host=strmid(site,6) else host=site
187    pos=strpos(host,'/')
188    dir=strmid(host,pos)
189    host=strmid(host,0,pos)
190    pos=strpos(dir,'/',/reverse_search)
191    files=strmid(dir,pos+1)
192    dir=strmid(dir,0,pos)
193  endif else host=site
194  if (size(user,/type) eq 0) then user='anonymous'
195  if (size(pass,/type) eq 0) then pass='test@test.com'
196                                ;
197  socket, u, host, port, connect_timeout=5, read_timeout=5, /get_lun
198  ftp_post, u, '', res
199  ftp_post, u, 'USER '+user, res, out='3?? *'
200  ftp_post, u, 'PASS '+pass, res
201  ftp_post, u, 'TYPE I', res
202  if (size(dir,/type) ne 0) then ftp_post, u, 'CWD '+dir, res
203  if keyword_set(file) or arg_present(data) then begin
204    bufsize=512
205    buffer=bytarr(bufsize)
206    n=n_elements(files)
207    if arg_present(data) then dat=ptrarr(n)
208    for i=0, n-1 do begin
209      ftp_post, u, 'SIZE '+files[i], res, out='213 *'
210      sz=long64(strmid(res[n_elements(res)-1],4))
211      if arg_present(data) then dat[i]=ptr_new(bytarr(sz))
212      ftp_post, u, 'PASV', res
213      ftp_parse_pasv, res, host, port
214      ftp_post, u, 'RETR '+files[i], res, out='1?? *'
215      socket, v, host, port, connect_timeout=5, read_timeout=5, $
216         /get_lun, /rawio
217      tc=0ll
218      if keyword_set(file) then openw,w,files[i],/get_lun
219      while (tc lt sz) do begin
220        if (sz-tc lt bufsize) then begin
221          bufsize=sz-tc
222          buffer=bytarr(bufsize)
223        endif
224        readu, v, buffer, transfer_count=dtc
225        if arg_present(data) then $
226           (*dat[i])[tc]=(dtc eq bufsize)?buffer:buffer[0:dtc-1]
227        if keyword_set(file) then $
228           writeu,w,(dtc eq bufsize)?buffer:buffer[0:dtc-1]
229        tc=tc+dtc
230      endwhile
231      free_lun, v
232      if keyword_set(file) then free_lun, w
233      ftp_post, u, '', res
234    endfor
235    if arg_present(data) then begin
236       if (n gt 1 or keyword_set(ptr)) then data=dat $
237       else data=temporary(*dat[0])
238     endif
239  endif
240  ftp_post, u, 'QUIT', res
241  free_lun, u
242end
Note: See TracBrowser for help on using the repository browser.