Re: Mixed ASCII/Binary Files [message #9434 is a reply to message #9301] |
Thu, 26 June 1997 00:00   |
David Foster
Messages: 341 Registered: January 1996
|
Senior Member |
|
|
DALY wrote:
>
> Hi,
>
> I'm running IDL 4.0.1 under Windows 95 and am having a heck of
> a time doing anything with mixed ASCII/binary files (header/data)
> files. I've tried all kinds of tricks to read the header and then
> the data, but IDL's file pointer keeps getting lost. Does anybody
> know of any fixes/patches? I know I can separate the header and
> data with another package and then read into IDL, but that kind of defeats
> the purpose of having a high-power package like IDL. Any
> comments/suggestions will be greatly appreciated.
Chaz -
I'm not sure what kind of data you're trying to read, but I'll
include a code snippet from a program I have that will extract
individual 2D images out of a single file that contains a 3D array.
The file can have a header, and you can either specify the size of
the header to "skip" (and return as argument) or specify a specific
string that signifies the end of the flag. Here's the code, it may
serve as an example. Let me know if you'd be interested in the entire
program.
; Skip over header if HEADER_SIZE or HEADER_FLAG specified
if ( keyword_set( HEADER_SIZE ) ) then begin
header_array = bytarr( header_size )
readu, unit, header_array
endif else if ( keyword_set( HEADER_FLAG ) ) then begin
; Search file for flag signaling end of header
line = ''
temp = ''
while ( line ne header_flag and not eof( unit ) ) do begin
finfo = fstat( unit ) ; Get file position before read
readf, unit, line
if ( keyword_set( HEADER_ARRAY ) ) then $
temp = [ temp, line ]
endwhile
if ( keyword_set( HEADER_ARRAY ) ) then begin
header_array = temp( 1 : n_elements( temp ) - 1 )
temp = 0
endif
; Ensure that file pointer is just AFTER the header flag!
; (READF may have grabbed too many bytes)
point_lun, unit, finfo.cur_ptr + strlen( header_flag )
if ( line ne header_flag ) then begin
status = 1
message, 'Header flag ' + header_flag + $
' not found in file ' + fname, /continue
endif
Hope this helps.
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 2200
La Jolla, CA 92037
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
"I have this theory that if we're told we're bad,
then that's the only idea we'll ever have.
But maybe if we are surrounded in beauty,
someday we will become what we see." - Jewel Kilcher
|
|
|