1

I have a IDL procedure reading a binary file and I try to translate it into a Python routine. The IDL code look like :

a = uint(0)
b = float(0)
c = float(0)
d = float(0)
e = float(0)
x=dblarr(nptx)
y=dblarr(npty)
z=dblarr(nptz)
openr,11,name_file_data,/f77_unformatted
readu,11,a
readu,11,b,c,d,e
readu,11,x
readu,11,y
readu,11,z

it works perfectly. So I'm writing the same thing in python but I can't find the same results (even the value of 'a' is different). Here is my code :

x=np.zeros(nptx,float)
y=np.zeros(npty,float)
z=np.zeros(nptz,float)
with open(name_file_data, "rb") as fb:
    a, = struct.unpack("I", fb.read(4))
    b,c,d,e = struct.unpack("ffff", fb.read(16))
    x[:] = struct.unpack(str(nptx)+"d", fb.read(nptx*8))[:]
    y[:] = struct.unpack(str(npty)+"d", fb.read(npty*8))[:]
    z[:] = struct.unpack(str(nptz)+"d", fb.read(nptz*8))[:]

Hope it will help anyone to answer me.

Update : As suggested in the answers, I'm now trying the module "FortranFile", but I'm not sure I understood everything about its use.

from scipy.io import FortranFile
f=FortranFile(name_file_data, 'r')
a=f.read_record('H')
b=f.read_record('f','f','f','f')

However, instead of having an integer for 'a', I got : array([0, 0], dtype=uint16).

And I had this following error for 'b': Size obtained (1107201884) is not a multiple of the dtypes given (16)

dodo27
  • 11
  • 2

1 Answers1

1

According to a table of IDL data types, UINT(0) creates a 16 bit integer (i.e. two bytes). In the Python struct module, the I format character denotes a 4 byte integer, and H denotes an unsigned 16 bit integer.

Try changing the line that unpacks a to

    a, = struct.unpack("H", fb.read(2))

Unfortunately, this probably won't fix the problem. You use the option /f77_unformatted with openr, which means the file contains more than just the raw bytes of the variables. (See the documentation of the OPENR command for more information about /f77_unformatted.)

You could try to use scipy.io.FortranFile to read the file, but there are no gaurantees that it will work. The binary layout of an unformatted Fortran file is compiler dependent.

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
  • I tried but all the values are always different. idl: a=0; b=10000.000 c=31.820000 etc python: a=4, b=0.0. c=3.67e-40 – dodo27 Sep 18 '18 at 16:12