Search code examples
pythonnumpynumpy-memmap

Is there a way with numpy to memmap a big-endian complex number?


I'm trying to load data from very large files that are formatted to be complex64 but in big-endian. Because of their size I want to use memmap. I can load the array as big-endian floats like such:

arr = np.memmap(fn, dtype='>f4', shape=(M,N,2))

but if I try to get a view of it as a complex number it reverts the byteswapping ie

arr[m,n,0]+1j*arr[m,n,1] != arr[m,n].view(dtype=np.complex64)

but

arr[m,n,0]+1j*arr[m,n,1] == arr[m,n].view(dtype=np.complex64).byteswap()

But this either creates a copy of the data or modifies the files. Is there a way to make this work without making a copy of the data or modifying the existing files?


Solution

  • It appears that the .byteswap() method doesn't modify or copy the data. So

    narr = arr[m,n].view(dtype=np.complex64).byteswap()

    Is the way to go. Hopefully that helps someone else.