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

Last change on this file since 226 was 226, checked in by pinsard, 17 years ago

corrections of some misspellings in some *.pro

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