|
Re: need help with ascii I/O second try. Ignor first!! [message #18522 is a reply to message #18521] |
Tue, 04 January 2000 00:00  |
Liam E. Gumley
Messages: 378 Registered: January 2000
|
Senior Member |
|
|
peter brooker wrote:
> One of our measurement tools generate an ascii output file
> that looks like the following.
>
> DEVICE LAYER SLOT DATE TIME SITE MEAS
> x19y n53 B2 8/24/99 8:28:48 PM 1 0.5663
> x19y n53 B2 8/24/99 8:29:20 PM 2 0.5692
> x19y n53 B2 8/24/99 8:30:01 PM 3 0.5762
> x19y n53 B2 8/24/99 8:32:43 PM 4 0.5666
>
> The data I care about for each group are in the last two
> columns. I do not care about the first line. The spacing
> between the columns varies.
>
> What is the best way to read in the lines and then store
> the information from the last two columns into real arrays?
Try this:
;---cut here---
PRO TEST, DATA
;- Open the file
openr, lun, 'test.dat', /get_lun
;- Skip the first line
blank = ''
readf, lun, blank, format='(a1)'
;- Read all records
data = fltarr(2, 100000)
nrec = 0
while not eof(lun) do begin
;- Read current record
record = ''
readf, lun, record
;- Separate the string into columns
record = strcompress(record)
record = str_sep(record, ' ')
;- Get the last two columns
ncols = n_elements(record)
var1 = record[ncols - 2]
var2 = record[ncols - 1]
;- Store in data array
data[0, nrec] = float(var1)
data[1, nrec] = float(var2)
;- Increment record counter
nrec = nrec + 1
endwhile
;- Close the file
free_lun, lun
;- Trim data array
data = data[*, 0:nrec-1]
END
;---cut here---
Assuming your data file is named test.dat:
IDL> .compile test
% Compiled module: TEST.
IDL> test, data
% Compiled module: STR_SEP.
IDL> help, data
DATA FLOAT = Array[2, 4]
IDL> print, data
1.00000 0.566300
2.00000 0.569200
3.00000 0.576200
4.00000 0.566600
Cheers,
Liam.
--
Liam E. Gumley
Space Science and Engineering Center, UW-Madison
http://cimss.ssec.wisc.edu/~gumley
|
|
|