Re: reading ASCII Data [message #14993] |
Tue, 13 April 1999 00:00  |
David Foster
Messages: 341 Registered: January 1996
|
Senior Member |
|
|
Mirko wrote:
>
> Hello
>
> We have ASCII files (floats) with a different number of lines and
> different number of data in each line. The data was generated by a mass
> spectrometer software under Win 3.11. Each measurement line ends with
> '0D'xb instead of '0D'xb and '0A'xb. Is there a way to find the END OF
> LINE with an IDL function (like EOF) because each line starts with some
> "header information" which has to be skipped before the measured data
> starts.
> How can you handle a variable number of floats?
>
> Cheers
>
> Mirko
A good start would be to use FILE_STRARR.PRO to read the file into
an array of strings. This could be parsed fairly easily. I thought
it'd be trivial using IDL's STR_SEP(), but you can't get rid of
the extra space characters easily (it gets confused when two
delimiting characters are next to each-other...dreadful programming!).
Anyways, FILE_STRARR.PRO and FILE_STRARR.DOC are included below.
Dave
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
David S. Foster Univ. of California, San Diego
Programmer/Analyst Brain Image Analysis Laboratory
foster@bial1.ucsd.edu Department of Psychiatry
(619) 622-5892 8950 Via La Jolla Drive, Suite 2240
La Jolla, CA 92037
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
; FILE_STRARR.PRO 5-27-94
;
; Routine to read text file and return a STRARR containing
; the lines of text. Returns a STRARR(2) containing the null
; string '' and the value of !err_string if an I/O error is encountered
;
; This code adapted from XDISPLAYFILE
FUNCTION file_strarr, fname
ON_IOERROR, IO_ERROR
openr, unit, fname, /get_lun, error=err
if (err ne 0) then begin
return, ['ERROR', !err_string]
endif else begin
max_lines = 1000
a = strarr(max_lines)
i = 0
c = ''
while (not eof(unit)) do begin
readf, unit, c
a[i] = c
i = i + 1
if (i eq max_lines - 2) then begin
a = [a, strarr(max_lines)]
max_lines = max_lines + max_lines
endif
endwhile
a = a[0:i-1]
free_lun, unit
return, a
endelse
IO_ERROR:
message, 'Error reading file: ' + fname, /continue
print, !err_string
return, ['ERROR', !err_string]
END
FILE_STRARR
Use this routine to read a text file and return
a STRARR variable containing the lines of text.
This is useful when displaying text-files in
text widgets. If an I/O error such as file-not-
found occurs then returns a STRARR(2) containing
the string 'ERROR' and the value of the IDL
system variable !ERR_STRING.
Calling Sequence
Text = FILE_STRARR(Filename)
Arguments
Filename
The name of the file containing the text
you wish to return as a STRARR.
Outputs
Returns the lines of text from the file as
a STRARR variable. If an I/O error occurs
then this will contain two elements: the
string 'ERROR' and the value of the IDL
system variable !ERR_STRING when the error
occured.
Example
; Locate an IDL program file
Ret = FIND_PROCEDURE('mrsegreg.pro',Filename)
; Put this file into a STRARR
Text = FILE_STRARR(Filename)
if (Text(0) eq 'ERROR' and $
n_elements(Text) eq 2) then
message, 'Error reading file ' + Filename
|
|
|