Re: Reading F77 on Windows platforms [message #30431 is a reply to message #30416] |
Wed, 24 April 2002 06:14  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Sverre Solberg wrote:
>
> I have problems reading F77-unformatted files when running idl on a
> Windows 2000
> platform. Opening the file (with ,/f77) works, but as soon as I try to
> read something, an error message claiming 'corrupted f77 file' shows
> up. The program works fine though on a Unix station, so the f77-file
> should be ok. According to the manual (for idl 5.5) the F77 should
> work both on Unix and Windows.
>
> In Fortran the file is created like this:
> open (<unit>,file=<name>, form='unformatted')
> write (<unit>) var1, var2, ...
>
> If opening the file in idl without the '/f77' keyword I dont get any
> complaints about corrupt file, but I then get problems retrieving the
> data. On Unix I am able to trace the file content in 4-bytes words
> (word by word) and it looks all fine. On Windows, however, I dont get
> the same results. Why??
Play around with the "swap endian" keywords on the IDL open statement. Moving from a big-endian
(most unix) to little-endian (PC) also swaps the record length markers at the beginning and end
of each record that is written in the Fortran code. So, by opening with /f77_unformatted and no
byte swapping keyword, your first record of length, say for e.g., 16 bytes, will end up being
interpreted as 238573485837 bytes. (or something like that....or maybe even negative!).
If you must use Fortran unformatted sequential access files across platforms then my advice -
in your fortran code, the first thing you should write is a 4-byte unique "magic" number (I use
123456789). In IDL when you open the file, open it *without* the /f77_unformatted keyword. Read
the first 8-bytes, toss the first four (the record length of the first record), and check that
the value of the remaining 4-bytes is 123456789. If it is, set a variable swap = 0. If not,
check if the value is SWAP_ENDIAN( 123456789 ). If this is true, set the variable swap = 1.
Close the file and then open it again using /F77_unformatted, SWAP_ENDIAN = swap. If the magic
number value is neither 123456789 or SWAP_ENDIAN( 123456789 ) then you have a file that
requires your personal intervention to read. :o)
Alternatively, you could adopt the convention of always writing in big-endian format and use
the SWAP_IF_LITTLE_ENDIAN keyword on the IDL open.
The best method (I think) - output in netCDF format. Then your read and write functions are the
same across platforms. A bit more work up front, but you'll be laughing later on.
paulv
--
Paul van Delst Religious and cultural
CIMSS @ NOAA/NCEP purity is a fundamentalist
Ph: (301)763-8000 x7274 fantasy
Fax:(301)763-8545 V.S.Naipaul
|
|
|