* Minor updates.

master
wirawan 15 years ago
parent 40ac890d6f
commit c87ba9dbfa
  1. 25
      iofmt/fortbin.py

@ -1,4 +1,4 @@
# $Id: fortbin.py,v 1.2 2010-02-19 18:39:17 wirawan Exp $ # $Id: fortbin.py,v 1.3 2010-05-28 18:43:14 wirawan Exp $
# #
# wpylib.iofmt.fortbin module # wpylib.iofmt.fortbin module
# Created: 20100208 # Created: 20100208
@ -15,7 +15,7 @@ import sys
from wpylib.sugar import ifelse from wpylib.sugar import ifelse
class fortran_bin_file(object): class fortran_bin_file(object):
"""A tool for reading Fortran binary files. """A tool for reading and writing Fortran binary files.
Caveat: On 64-bit systems, typical Fortran implementations still have int==int32 Caveat: On 64-bit systems, typical Fortran implementations still have int==int32
(i.e. the LP64 programming model), unless "-i8" kind of option is enabled. (i.e. the LP64 programming model), unless "-i8" kind of option is enabled.
@ -34,10 +34,18 @@ class fortran_bin_file(object):
def open(self, filename, mode="r"): def open(self, filename, mode="r"):
self.F = open(filename, mode+"b") self.F = open(filename, mode+"b")
def close(self):
if getattr(self, "F", None):
self.F.close()
self.F = None
def read(self, *fields, **opts): def read(self, *fields, **opts):
"""Reads a Fortran record. """Reads a Fortran record.
The description of the fields are given as The description of the fields are given as
(name, dtype, length) tuples.""" either (name, dtype) or (name, dtype, length) tuples.
If length is not specified, then a scalar value is read.
Length is a scalar for 1-D array, or a tuple or list for multidimensional
array."""
from numpy import fromfile as rd from numpy import fromfile as rd
if self.debug or opts.get("debug"): if self.debug or opts.get("debug"):
dbg = lambda msg : sys.stderr.write(msg) dbg = lambda msg : sys.stderr.write(msg)
@ -62,9 +70,7 @@ class fortran_bin_file(object):
"Attempting to read %d bytes from a record of length %d bytes" \ "Attempting to read %d bytes from a record of length %d bytes" \
% (expected_len, reclen) % (expected_len, reclen)
if "out" in opts: if "dest" in opts:
rslt = opts["out"]
elif "dest" in opts:
rslt = opts["dest"] rslt = opts["dest"]
else: else:
rslt = {} rslt = {}
@ -108,7 +114,7 @@ class fortran_bin_file(object):
return rslt return rslt
def writevals(self, *vals, **opts): def write_vals(self, *vals, **opts):
"""Writes a Fortran record. """Writes a Fortran record.
Only values need to be given, because the types are known. Only values need to be given, because the types are known.
This is a direct converse of read subroutine.""" This is a direct converse of read subroutine."""
@ -159,7 +165,7 @@ class fortran_bin_file(object):
reclen.tofile(self.F) reclen.tofile(self.F)
def writefields(self, src, *fields, **opts): def write_fields(self, src, *fields, **opts):
if (issubclass(src.__class__, dict) and issubclass(dict, src.__class__)) \ if (issubclass(src.__class__, dict) and issubclass(dict, src.__class__)) \
or "__getitem__" in dir(src): or "__getitem__" in dir(src):
def getval(d, k): def getval(d, k):
@ -189,6 +195,9 @@ class fortran_bin_file(object):
else: else:
raise ValueError, "Invalid field type: %s" % str(type(f)) raise ValueError, "Invalid field type: %s" % str(type(f))
self.write_vals(*vals, **opts)
def array_major_dim(arr): def array_major_dim(arr):
"""Tests whether a numpy array is column or row major. """Tests whether a numpy array is column or row major.
It will return the following: It will return the following:

Loading…
Cancel
Save