Python - read 2d array from binary data -
i try read 2d array floats binary file python. files have been written big endian fortran program (it intermediate file of weather research , forecast model). know dimension sizes of array read (nx & ny) fortran , idl programer lost, how manage in python. (later on want visualize array).
- shall use
struct.unpack
ornumpy.fromfile
orarray module
? - do have read first vector , afterwards reshape it? (have seen option numpy-way)
- how define 2d array numpy , how define dtype read big-endian byte ordering?
- is there issue array ordering (column or row wise) take account?
short answers per sub-question:
- i don't think
array
module has way specify endianness. betweenstruct
module , numpy think numpy easier use, fortran-like ordered arrays. - all data inherently 1-dimensional far hardware (disk, ram, etc) concerned, yes reshaping 2d representation necessary.
numpy.fromfile
reshape must happen explicitly afterwards,numpy.memmap
provides way reshape more implicitly. - the easiest way specify endianness numpy use short type string, similar approach needed
struct
module. in numpy>f
,>f4
specify single precision ,>d
,>f8
double precision big-endian floating point. - your binary file walk array along rows (c-like) or along columns (fortran-like). whichever of two, has taken account represent data properly. numpy makes easy
order
keyword argumentreshape
,memmap
(among others).
all in all, code example:
import numpy np filename = 'somethingsomething' open(filename, 'rb') f: nx, ny = ... # parse; advance file-pointer data segment data = np.fromfile(f, dtype='>f8', count=nx*ny) array = np.reshape(data, [nx, ny], order='f')
Comments
Post a Comment