Ed Bueler <[email protected]> writes:

> Barry --
>
> I am reading the resulting file successfully using
>
> import struct
> import sys
> f = open('timesteps','r')
> while True:
>     try:
>         bytes = f.read(8)
>     except:
>         print "f.read() failed"
>         sys.exit(1)

It seems odd to catch the exception and convert to sys.exit(1).  This
just loses the stack trace (which might have more detailed information)
and makes the debugger catch the wrong location.

>     if len(bytes) > 0:
>         print struct.unpack('>d',bytes)[0]
>     else:
>         break
> f.close()
>
> However, was there a more elegant intended method?  

I would use numpy.fromfile, which is one line.

> I am disturbed by the apparent need to specify big-endian-ness (=
> '>d') in the struct.unpack() call.

You need it because PETSc binary files are big-endian.  There is no
place to encode the byte order in these raw binary files, so some
convention is required for the file to portable.

HDF5 includes this information, though it is an annoying dependency.
Numpy files (*.npy) are a simpler alternative that PETSc could decide to
support.

https://docs.scipy.org/doc/numpy/neps/npy-format.html

Attachment: signature.asc
Description: PGP signature

Reply via email to