FLEXPART Testing Environment CTBTO WO8
 All Classes Namespaces Files Functions Variables Pages
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
flextest.flexread.pflexible.BinaryFile Class Reference

HELPER FUNCTIONS ##########. More...

Inheritance diagram for flextest.flexread.pflexible.BinaryFile:
Inheritance graph
[legend]
Collaboration diagram for flextest.flexread.pflexible.BinaryFile:
Collaboration graph
[legend]

Public Member Functions

def __init__
 
def read
 
def write
 
def seek
 
def tell
 
def flush
 
def close
 

Public Attributes

 mode
 
 file
 
 order
 

Static Public Attributes

dictionary structtypes
 

Detailed Description

HELPER FUNCTIONS ##########.

BinaryFile: A class for accessing data to/from large binary files
   

The data is meant to be read/write sequentially from/to a binary file.
One can request to read a piece of data with a specific type and shape
from it.  Also, it supports the notion of Fortran and C ordered data,
so that the returned data is always well-behaved (C-contiguous and
aligned).

This class is seeking capable.

:Author:   Francesc Alted
:Contact:  faltet@pytables.org
:Created:  2010-03-18
:Acknowledgment: Funding for the development of this code is provided
     through the Norwegian Research Council VAUUAV project #184724, 2010

Definition at line 4484 of file pflexible.py.

Constructor & Destructor Documentation

def flextest.flexread.pflexible.BinaryFile.__init__ (   self,
  filename,
  mode = "r",
  order = "fortran" 
)
Open the `filename` for write/read binary files.

The `mode` can be 'r', 'w' or 'a' for reading (default),
writing or appending.  The file will be created if it doesn't
exist when opened for writing or appending; it will be
truncated when opened for writing.  Add a '+' to the mode to
allow simultaneous reading and writing.

`order` specifies whether the file is is written in 'fortran'
or 'c' order.

Definition at line 4515 of file pflexible.py.

4516  def __init__(self, filename, mode="r", order="fortran"):
4517  """Open the `filename` for write/read binary files.
4518 
4519  The `mode` can be 'r', 'w' or 'a' for reading (default),
4520  writing or appending. The file will be created if it doesn't
4521  exist when opened for writing or appending; it will be
4522  truncated when opened for writing. Add a '+' to the mode to
4523  allow simultaneous reading and writing.
4524 
4525  `order` specifies whether the file is is written in 'fortran'
4526  or 'c' order.
4527  """
4528  self.mode = mode + "b"
4529  self.file = open(filename, mode=self.mode, buffering=1)
4530  """The file handler."""
4531  if order not in ['fortran', 'c']:
4532  raise ValueError, "order should be either 'fortran' or 'c'."
4533  self.order = order
4534  """The order for file ('c' or 'fortran')."""
4535 

Member Function Documentation

def flextest.flexread.pflexible.BinaryFile.close (   self)

Definition at line 4643 of file pflexible.py.

4644  def close(self):
4645  "End access to file."
4646  self.file.close()
4647 
def flextest.flexread.pflexible.BinaryFile.flush (   self)

Definition at line 4638 of file pflexible.py.

4639  def flush(self):
4640  "Flush buffers to file."
4641  self.file.flush()
4642 
def flextest.flexread.pflexible.BinaryFile.read (   self,
  dtype,
  shape = (1, 
)
Read an array of `dtype` and `shape` from current position.

`shape` must be any tuple made of integers or even () for scalars.

The current position will be updated to point to the end of
read data.

Definition at line 4536 of file pflexible.py.

4537  def read(self, dtype, shape=(1,)):
4538  """Read an array of `dtype` and `shape` from current position.
4539 
4540  `shape` must be any tuple made of integers or even () for scalars.
4541 
4542  The current position will be updated to point to the end of
4543  read data.
4544  """
4545  if not isinstance(dtype, np.dtype):
4546  dtype = np.dtype(dtype)
4547  if type(shape) is int:
4548  shape = (shape,)
4549  if type(shape) is not tuple:
4550  raise ValueError, "shape must be a tuple"
4551  length = dtype.itemsize
4552  rank = len(shape)
4553  if rank == 1:
4554  length *= shape[0]
4555  elif rank > 1:
4556  length *= np.array(shape).prod()
4557 
4558  # Correct the shape in case dtype is multi-dimensional
4559  if shape != (1,):
4560  shape = shape + dtype.shape
4561  else:
4562  shape = dtype.shape
4563  rank = len(shape)
4564 
4565  if shape in (1, (1,)):
4566  order = "c"
4567  else:
4568  order = self.order
4569 
4570  # Read the data from file
4571  data = self.file.read(length)
4572  if len(data) < length:
4573  raise EOFError, "Asking for more data than available in file."
4574 
4575  # Convert read string into a regular array, or scalar
4576  dts = dtype.base.str[1:]
4577  if rank == 0:
4578  if dts[1] == "S":
4579  data = str(data)
4580  elif dts in self.structtypes:
4581  data = struct.unpack(self.structtypes[dts], data)[0]
4582  else:
4583  data = np.ndarray(shape=shape, buffer=data, dtype=dtype.base)
4584  if rank == 0:
4585  # Retrieve the scalar out of the 0-dim array
4586  data = data[()]
4587 
4588  if rank > 1:
4589  # If original data file is in fortran mode, reverse the
4590  # shape first
4591  if order == "fortran":
4592  shape = [i for i in shape[::-1]]
4593  data = data.reshape(shape)
4594  # If original data file is in fortran mode, do a transpose.
4595  # As the shape was reversed previously, we get the original
4596  # shape again.
4597  if self.order == "fortran":
4598  data = data.transpose().copy()
4599  # Do an additional copy just in case the array is not
4600  # well-behaved (i.e., it is not aligned or not contiguous).
4601  elif not data.flags.behaved:
4602  data = data.copy()
4603  return data
4604 
def flextest.flexread.pflexible.BinaryFile.seek (   self,
  offset,
  whence = 0 
)
Move to new file position.

Argument offset is a byte count.  Optional argument whence
defaults to 0 (offset from start of file, offset should be >=
0); other values are 1 (move relative to current position,
positive or negative), and 2 (move relative to end of file,
usually negative, although many platforms allow seeking beyond
the end of a file).  If the file is opened in text mode, only
offsets returned by tell() are legal.  Use of other offsets
causes undefined behavior.

Definition at line 4618 of file pflexible.py.

4619  def seek(self, offset, whence=0):
4620  """Move to new file position.
4621 
4622  Argument offset is a byte count. Optional argument whence
4623  defaults to 0 (offset from start of file, offset should be >=
4624  0); other values are 1 (move relative to current position,
4625  positive or negative), and 2 (move relative to end of file,
4626  usually negative, although many platforms allow seeking beyond
4627  the end of a file). If the file is opened in text mode, only
4628  offsets returned by tell() are legal. Use of other offsets
4629  causes undefined behavior.
4630  """
4631  self.file.seek(offset, whence)
4632 
def flextest.flexread.pflexible.BinaryFile.tell (   self)

Definition at line 4633 of file pflexible.py.

4634  def tell(self):
4635  "Returns current file position, an integer (may be a long integer)."
4636  return self.file.tell()
4637 
def flextest.flexread.pflexible.BinaryFile.write (   self,
  arr 
)
Write an `arr` to current position.

The current position will be updated to point to the end of
written data.

Definition at line 4605 of file pflexible.py.

4606  def write(self, arr):
4607  """Write an `arr` to current position.
4608 
4609  The current position will be updated to point to the end of
4610  written data.
4611  """
4612  # Transpose data if case we need to
4613  if (self.order == "fortran") != (arr.flags.fortran):
4614  arr = arr.transpose().copy()
4615  # Write the data to file
4616  self.file.write(arr.data)
4617 

Member Data Documentation

flextest.flexread.pflexible.BinaryFile.file

Definition at line 4528 of file pflexible.py.

flextest.flexread.pflexible.BinaryFile.mode

Definition at line 4527 of file pflexible.py.

flextest.flexread.pflexible.BinaryFile.order

Definition at line 4532 of file pflexible.py.

dictionary flextest.flexread.pflexible.BinaryFile.structtypes
static
Initial value:
1 = {
2  'i1': 'b', 'i2': 'h', 'i4': 'i',
3  'f4': 'f', 'f8': 'd',
4  }

Definition at line 4510 of file pflexible.py.


The documentation for this class was generated from the following file: