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).

  1. shall use struct.unpack or numpy.fromfile or array module?
  2. do have read first vector , afterwards reshape it? (have seen option numpy-way)
  3. how define 2d array numpy , how define dtype read big-endian byte ordering?
  4. is there issue array ordering (column or row wise) take account?

short answers per sub-question:

  1. i don't think array module has way specify endianness. between struct module , numpy think numpy easier use, fortran-like ordered arrays.
  2. 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.
  3. 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.
  4. 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 argument reshape , 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

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -