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

Last change on this file since 157 was 157, checked in by navarro, 18 years ago

header improvements + xxx doc

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