[Numpy-discussion] byteswap questions

Russell E. Owen rowen at cesmail.net
Mon Oct 31 16:56:21 CST 2005


I've got a module that can send array data to ds9. It screws up on 
byteswapped data and I'm trying to fix it.

The code sends the data to ds9 via a pipe using arr.tofile(). To send 
byteswapped data I think I have two choices (given that I don't want to 
modify the input array):

1) Figure out the byte order and specify a suitable flag to ds9 (it 
accepts "bigendian" or "littleendian" flags).

This avoids copying the data, so it sounds attractive, but can I do it 
without relying on "internals"? Numarrays have an "isbyteswapped" 
method, but all this says is "not native machine order"; it doesn't 
actually tell me the order.

I need to know if the order is bigendian or littleendian, and I can't 
find a documented way to get that, just an undocumented attribute 
_byteorder.


2) If the array is not in native byte order, make a native byte ordered 
copy before sending it.

The following seems to work:
if arr.isbyteswapped():
  arr = arr.copy()

but is this the recommended way to get native-order copy?

- Is "copy" guaranteed to return a copy in native byte order? The 
documentation doesn't say.
- I was tempted to use num.array(arr) to make the copy, but the 
documentation doesn't say what "array" does if the input is a already a 
numarray array.

As an aside, I tried to use the byteswapped method, but it has a totally 
different effect:

>>> d
array([[-9012., -9012., -9012., ..., -9012., -9012., -9012.],
...type=Float32)
>>> d.isbyteswapped()
1
>>> dcopy = d.copy()
>>> d
array([[-9012., -9012., -9012., ..., -9012., -9012., -9012.],
 ...type=Float32)
>>> dcopy.isbyteswapped()
0
>>> dswap = d.byteswapped()
>>> dswap
array([[  1.91063654e-38,   1.91063654e-38,   1.91063654e-38, ...,
  ...type=Float32)

which I guess means I'd have to byteswap the byteswapped data. Aargh.

-- Russell





More information about the Numpy-discussion mailing list