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.read_header.BinaryFile Class Reference

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

Inheritance diagram for flextest.flexread.read_header.BinaryFile:
Inheritance graph
[legend]
Collaboration diagram for flextest.flexread.read_header.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 569 of file read_header.py.

Constructor & Destructor Documentation

def flextest.flexread.read_header.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 600 of file read_header.py.

601  def __init__(self, filename, mode="r", order="fortran"):
602  """Open the `filename` for write/read binary files.
603 
604  The `mode` can be 'r', 'w' or 'a' for reading (default),
605  writing or appending. The file will be created if it doesn't
606  exist when opened for writing or appending; it will be
607  truncated when opened for writing. Add a '+' to the mode to
608  allow simultaneous reading and writing.
609 
610  `order` specifies whether the file is is written in 'fortran'
611  or 'c' order.
612  """
613  self.mode = mode + "b"
614  self.file = open(filename, mode=self.mode, buffering=1)
615  """The file handler."""
616  if order not in ['fortran', 'c']:
617  raise ValueError, "order should be either 'fortran' or 'c'."
618  self.order = order
619  """The order for file ('c' or 'fortran')."""
620 

Member Function Documentation

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

Definition at line 728 of file read_header.py.

729  def close(self):
730  "End access to file."
731  self.file.close()
732 
def flextest.flexread.read_header.BinaryFile.flush (   self)

Definition at line 723 of file read_header.py.

724  def flush(self):
725  "Flush buffers to file."
726  self.file.flush()
727 
def flextest.flexread.read_header.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 621 of file read_header.py.

622  def read(self, dtype, shape=(1,)):
623  """Read an array of `dtype` and `shape` from current position.
624 
625  `shape` must be any tuple made of integers or even () for scalars.
626 
627  The current position will be updated to point to the end of
628  read data.
629  """
630  if not isinstance(dtype, np.dtype):
631  dtype = np.dtype(dtype)
632  if type(shape) is int:
633  shape = (shape,)
634  if type(shape) is not tuple:
635  raise ValueError, "shape must be a tuple"
636  length = dtype.itemsize
637  rank = len(shape)
638  if rank == 1:
639  length *= shape[0]
640  elif rank > 1:
641  length *= np.array(shape).prod()
642 
643  # Correct the shape in case dtype is multi-dimensional
644  if shape != (1,):
645  shape = shape + dtype.shape
646  else:
647  shape = dtype.shape
648  rank = len(shape)
649 
650  if shape in (1, (1,)):
651  order = "c"
652  else:
653  order = self.order
654 
655  # Read the data from file
656  data = self.file.read(length)
657  if len(data) < length:
658  raise EOFError, "Asking for more data than available in file."
659 
660  # Convert read string into a regular array, or scalar
661  dts = dtype.base.str[1:]
662  if rank == 0:
663  if dts[1] == "S":
664  data = str(data)
665  elif dts in self.structtypes:
666  data = struct.unpack(self.structtypes[dts], data)[0]
667  else:
668  data = np.ndarray(shape=shape, buffer=data, dtype=dtype.base)
669  if rank == 0:
670  # Retrieve the scalar out of the 0-dim array
671  data = data[()]
672 
673  if rank > 1:
674  # If original data file is in fortran mode, reverse the
675  # shape first
676  if order == "fortran":
677  shape = [i for i in shape[::-1]]
678  data = data.reshape(shape)
679  # If original data file is in fortran mode, do a transpose.
680  # As the shape was reversed previously, we get the original
681  # shape again.
682  if self.order == "fortran":
683  data = data.transpose().copy()
684  # Do an additional copy just in case the array is not
685  # well-behaved (i.e., it is not aligned or not contiguous).
686  elif not data.flags.behaved:
687  data = data.copy()
688  return data
689 
def flextest.flexread.read_header.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 703 of file read_header.py.

704  def seek(self, offset, whence=0):
705  """Move to new file position.
706 
707  Argument offset is a byte count. Optional argument whence
708  defaults to 0 (offset from start of file, offset should be >=
709  0); other values are 1 (move relative to current position,
710  positive or negative), and 2 (move relative to end of file,
711  usually negative, although many platforms allow seeking beyond
712  the end of a file). If the file is opened in text mode, only
713  offsets returned by tell() are legal. Use of other offsets
714  causes undefined behavior.
715  """
716  self.file.seek(offset, whence)
717 
def flextest.flexread.read_header.BinaryFile.tell (   self)

Definition at line 718 of file read_header.py.

719  def tell(self):
720  "Returns current file position, an integer (may be a long integer)."
721  return self.file.tell()
722 
def flextest.flexread.read_header.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 690 of file read_header.py.

691  def write(self, arr):
692  """Write an `arr` to current position.
693 
694  The current position will be updated to point to the end of
695  written data.
696  """
697  # Transpose data if case we need to
698  if (self.order == "fortran") != (arr.flags.fortran):
699  arr = arr.transpose().copy()
700  # Write the data to file
701  self.file.write(arr.data)
702 

Member Data Documentation

flextest.flexread.read_header.BinaryFile.file

Definition at line 613 of file read_header.py.

flextest.flexread.read_header.BinaryFile.mode

Definition at line 612 of file read_header.py.

flextest.flexread.read_header.BinaryFile.order

Definition at line 617 of file read_header.py.

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

Definition at line 595 of file read_header.py.


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