comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » OPENR F77 SWAP_IF_LITTLE_ENDIAN
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: OPENR F77 SWAP_IF_LITTLE_ENDIAN [message #90727 is a reply to message #90725] Mon, 06 April 2015 13:00 Go to previous messageGo to previous message
Paul Van Delst[1] is currently offline  Paul Van Delst[1]
Messages: 1157
Registered: April 2002
Senior Member
On 04/06/15 15:43, fawltylanguage@gmail.com wrote:
> On Monday, April 6, 2015 at 8:04:05 PM UTC+2, Edward Hyer wrote:
>> ; open and write a file using F77_UNFORMATTED
>> IDL> openw,lun,/get_lun,/f77,'/tmp/test_f77'
>> IDL> writeu,lun,long(2015032518),float(6),long(0)
>> IDL> free_lun,lun
>> ; read the file back in
>> IDL> openr,lun,/get_lun,/f77,'/tmp/test_conc'
>> IDL> qq1=long(0) & qq2=float(0) & qq3=long(0)
>> IDL> readu,lun,qq1,qq2,qq3
>> IDL> print,qq1,qq2,qq3
>> 2013052306 6.00000 0
>> IDL> free_lun,lun
>> ; now try reading it with /SWAP_IF_LITTLE_ENDIAN
>> IDL> openr,lun,/get_lun,/f77,'/tmp/test_conc',/swap_if_little_end ian
>> IDL> readu,lun,qq1,qq2,qq3
>> % READU: Corrupted f77 unformatted file detected. Unit: 100
>> File: /tmp/test_conc
>> % Execution halted at: $MAIN$
>>
>>
>> Has anyone encountered this behavior?
>>
>> --Edward H.
>
> F77 unformatted files contain record length data. Byte swapping them
> will result in corrupted records.

Oh, it didn't even occur to me that the OP didn't know about the
(ubiquitous but non-standard) format of Fortran unformatted sequential
files.

For the OP (apolgoies if you do know this):

Fortran unformatted sequential files have the following structure:

[next record length]
[record]
[previous record length]

repeated for each record. That is, each record is bookended with a
record marker indicating the length of the record. Having the record
length at the end of the record as well is to allow code to BACKSPACE
records like one would have had to do on magnetic tapes back in old day.

At any rate, when you byte swap the data via the
"/swap_if_little_endian" keyword, you are also byteswapping the record
markers. And that leads to potentially screwed up reads.

Doing an octal dump of the original *little-endian* file:

$ od -t d4 tmp.test_f77
0000000 12 2015032518 1086324736 0
0000020 12
0000024

Note the "12" at the beginning and end. The record is 12 bytes long.

Now lets do the same, but for a purposefully written big-endian file:

IDL> openw,lun,/get_lun,/f77,'be.tmp.test_f77',/swap_endian
IDL> writeu,lun,long(2015032518),float(6),long(0)
IDL> free_lun,lun

$ od -t d4 be.tmp.test_f77
0000000 201326592 -957080968 49216 0
0000020 201326592
0000024

Because of the byteswapping, this is the equivalent of the little-endian
file when the "/swap_if_little_endian" keyword is used. When this file
is read IDL thinks the record should be 201326592 bytes long. Which is
isn't.

Thus, error.

cheers,

paulv
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: cgBlendImage, more than 2 images?
Next Topic: 5th & 95th Percentiles

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 13:39:21 PDT 2025

Total time taken to generate the page: 0.00357 seconds