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

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

improvements/corrections of some *.pro headers

  • 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;-
79;
80PRO ftp_parse_pasv, text, host, port
81  t=strtrim(text,2)
82  ind=where(strcmp(t,'227',3))
83  i=ind[0]
84  if (i ne -1) then begin
85    sub=stregex(t[i],'\([0-9,]*\)',/extract)
86    p=strsplit(strmid(sub,1,strlen(sub)-2),',',/extract)
87    p=strtrim(p,2)
88    host=p[0]+'.'+p[1]+'.'+p[2]+'.'+p[3]
89    port=256*long(p[4])+long(p[5])
90  endif
91end
92;
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;-
180;
181PRO read_ftp, site, files, dir, port, data=data, file=file, user=user, $
182              pass=pass, ptr=ptr
183;
184  compile_opt idl2
185;
186  if n_elements(port) eq 0 then port='ftp'
187  if n_elements(files) eq 0 then begin
188    if strcmp(site,'ftp://',6) then host=strmid(site,6) else host=site
189    pos=strpos(host,'/')
190    dir=strmid(host,pos)
191    host=strmid(host,0,pos)
192    pos=strpos(dir,'/',/reverse_search)
193    files=strmid(dir,pos+1)
194    dir=strmid(dir,0,pos)
195  endif else host=site
196  if (size(user,/type) eq 0) then user='anonymous'
197  if (size(pass,/type) eq 0) then pass='test@test.com'
198                                ;
199  socket, u, host, port, connect_timeout=5, read_timeout=5, /get_lun
200  ftp_post, u, '', res
201  ftp_post, u, 'USER '+user, res, out='3?? *'
202  ftp_post, u, 'PASS '+pass, res
203  ftp_post, u, 'TYPE I', res
204  if (size(dir,/type) ne 0) then ftp_post, u, 'CWD '+dir, res
205  if keyword_set(file) or arg_present(data) then begin
206    bufsize=512
207    buffer=bytarr(bufsize)
208    n=n_elements(files)
209    if arg_present(data) then dat=ptrarr(n)
210    for i=0, n-1 do begin
211      ftp_post, u, 'SIZE '+files[i], res, out='213 *'
212      sz=long64(strmid(res[n_elements(res)-1],4))
213      if arg_present(data) then dat[i]=ptr_new(bytarr(sz))
214      ftp_post, u, 'PASV', res
215      ftp_parse_pasv, res, host, port
216      ftp_post, u, 'RETR '+files[i], res, out='1?? *'
217      socket, v, host, port, connect_timeout=5, read_timeout=5, $
218         /get_lun, /rawio
219      tc=0ll
220      if keyword_set(file) then openw,w,files[i],/get_lun
221      while (tc lt sz) do begin
222        if (sz-tc lt bufsize) then begin
223          bufsize=sz-tc
224          buffer=bytarr(bufsize)
225        endif
226        readu, v, buffer, transfer_count=dtc
227        if arg_present(data) then $
228           (*dat[i])[tc]=(dtc eq bufsize)?buffer:buffer[0:dtc-1]
229        if keyword_set(file) then $
230           writeu,w,(dtc eq bufsize)?buffer:buffer[0:dtc-1]
231        tc=tc+dtc
232      endwhile
233      free_lun, v
234      if keyword_set(file) then free_lun, w
235      ftp_post, u, '', res
236    endfor
237    if arg_present(data) then begin
238       if (n gt 1 or keyword_set(ptr)) then data=dat $
239       else data=temporary(*dat[0])
240     endif
241  endif
242  ftp_post, u, 'QUIT', res
243  free_lun, u
244end
Note: See TracBrowser for help on using the repository browser.