Re: reading unformatted binary data (WRITEU) with FORTRAN routine [message #2698 is a reply to message #2617] |
Mon, 15 August 1994 08:19  |
grunes
Messages: 68 Registered: September 1993
|
Member |
|
|
In article <11AUG199413273078@stars.gsfc.nasa.gov> kucera@stars.gsfc.nasa.gov (Terry Kucera) writes:
> Well, this is sort of a FORTRAN question, but it has an IDL angle,
> so maybe someone here can help me.
> I've stored an array of floating points in an unformated binary data file
> using WRITEU in IDL. Can anyone tell me how to read the data back using
> FORTRAN? Terry Kucera
> kucera@stars.gsfc.nasa.gov
This is actually a very complex question. Fortran does not standardize
file formats. This is (IM-not-HO) the primary reason that C and C++
have largely replaced Fortran in most of the software market, although
C-Fanatics would disagree (and C has some other minor advantages).
In Sun Fortran, I use the following
open(1,file=...,form='unformatted',access='direct',recl=1,
& status='old')
irec=1
...
read(1,rec=irec)array
irec=irec+ <# of bytes in array>
...
(if you are using a large array, and your compiler gives you a compile
or run-time error about reading too many bytes, just read smaller parts
of the array.)
The same steps will work with SGI Fortran, if you compile everything with
the -bytereclen switch. I would guess that, that would also work on
most Unix work stations, but I may be wrong.
With Microsoft Fortran 4.01 (on PCs--very old) I think the open statement
looks like
open(1,file=...,form='binary',access='direct',recl=1,
& status='old')
With Lahey Fortran (for the PC) I think it looks like
open(1,file=...,form='transparent',access='direct',recl=1,
& status='old')
I have assumed that the binary representation was the same on the machines
doing input and output. If not (e.g., when transfering data between
PC and Sun), you may be able to compensate for byte order by using the
byteorder,array
statement in IDL, before you WRITEU the array.
I very much wish "they" would make raw file format I/O a part of the
Fortran standard. (I complained on the Fortran news group, and
it was suggested I should join the standards groups. What a waste
of time.)
There is another solution: call a C language routine to read the data.
Unfortunately, there is no standard on how Fortran and C talk to each
other, either. But that is often the way people do it.
Mitchell R Grunes (grunes@imsy1.nrl.navy.mil)
Allied-Signal Technical Services
c/o Code 7230 Naval Research Lab
|
|
|