[SciPy-user] byteswapping IO problem

Jeffery D. Collins jcollins_boulder at earthlink.net
Fri Jun 11 09:22:50 CDT 2004


Hello,

I am having a problem getting scipy.io.fopen to properly read a big 
endian complex array (typecode='F') on a little endian machine.  When 
the bs=1 switch is enabled in the fread method, the entire complex 
number is swapped (real and imaginary parts reversed).  Here is an example:

On the Sun (big endian), create and store:
[ 0. +5.j, 1. +6.j, 2. +7.j, 3. +8.j, 4. +9.j, 5.+10.j, 6.+11.j, 
7.+12.j, 8.+13.j, 9.+14.j,]

On Linux (little endian), perform a byteswapped read:
fh = io.fopen('jnk.iq','rb')
fh.read(10,'F', bs=1)

[  5.+0.j,  6.+1.j,  7.+2.j,  8.+3.j,  9.+4.j, 10.+5.j, 11.+6.j, 
12.+7.j, 13.+8.j, 14.+9.j,]

The entire complex number gets swapped, not just the individual floating 
point components.

Read again using fromstring:
fh = file('jnk.iq','rb')
s = fh.read()
fromstring(s, typecode='F', count=10).byteswapped()

[ 0. +5.j, 1. +6.j, 2. +7.j, 3. +8.j, 4. +9.j, 5.+10.j, 6.+11.j, 
7.+12.j, 8.+13.j, 9.+14.j,]
 
After a quick look at the code, it would seem that changes would be 
needed in numpyio_fromfile with respect to its call to rbo(), the 
byteswapping routine, to treat complex numbers differently.  Something 
like this seems to work:

  if (dobyteswap) {
    if (read_type == 68 || read_type == 70) /* complex typecodes: F or D */
        rbo(ibuff,myelsize/2,nread*2);
    else
        rbo(ibuff,myelsize,nread);
  }
 
A similar treatment is necessary for writing byteswapped files.  If this 
approach seems reasonable, let me know and I'll submit a patch.

Thanks!
--
Jeff





 



More information about the SciPy-user mailing list