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

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

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

Constructor & Destructor Documentation

def flextest.flexread.read_grid.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 773 of file read_grid.py.

774  def __init__(self, filename, mode="r", order="fortran"):
775  """Open the `filename` for write/read binary files.
776 
777  The `mode` can be 'r', 'w' or 'a' for reading (default),
778  writing or appending. The file will be created if it doesn't
779  exist when opened for writing or appending; it will be
780  truncated when opened for writing. Add a '+' to the mode to
781  allow simultaneous reading and writing.
782 
783  `order` specifies whether the file is is written in 'fortran'
784  or 'c' order.
785  """
786  self.mode = mode + "b"
787  self.file = open(filename, mode=self.mode, buffering=1)
788  """The file handler."""
789  if order not in ['fortran', 'c']:
790  raise ValueError, "order should be either 'fortran' or 'c'."
791  self.order = order
792  """The order for file ('c' or 'fortran')."""
793 
794 
795 

Member Function Documentation

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

Definition at line 904 of file read_grid.py.

905  def close(self):
906  "End access to file."
907  self.file.close()
908 
def flextest.flexread.read_grid.BinaryFile.flush (   self)

Definition at line 899 of file read_grid.py.

900  def flush(self):
901  "Flush buffers to file."
902  self.file.flush()
903 
def flextest.flexread.read_grid.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 796 of file read_grid.py.

797  def read(self, dtype, shape=(1,)):
798  """Read an array of `dtype` and `shape` from current position.
799 
800  `shape` must be any tuple made of integers or even () for scalars.
801 
802  The current position will be updated to point to the end of
803  read data.
804  """
805  if not isinstance(dtype, np.dtype):
806  dtype = np.dtype(dtype)
807  if type(shape) is int:
808  shape = (shape,)
809  if type(shape) is not tuple:
810  raise ValueError, "shape must be a tuple"
811  length = dtype.itemsize
812  rank = len(shape)
813  if rank == 1:
814  length *= shape[0]
815  elif rank > 1:
816  length *= np.array(shape).prod()
817 
818  # Correct the shape in case dtype is multi-dimensional
819  if shape != (1,):
820  shape = shape + dtype.shape
821  else:
822  shape = dtype.shape
823  rank = len(shape)
824 
825  if shape in (1, (1,)):
826  order = "c"
827  else:
828  order = self.order
829 
830  # Read the data from file
831  data = self.file.read(length)
832  if len(data) < length:
833  raise EOFError, "Asking for more data than available in file."
834 
835 
836  # Convert read string into a regular array, or scalar
837  dts = dtype.base.str[1:]
838  if rank == 0:
839  if dts[1] == "S":
840  data = str(data)
841  elif dts in self.structtypes:
842  data = struct.unpack(self.structtypes[dts], data)[0]
843  else:
844  data = np.ndarray(shape=shape, buffer=data, dtype=dtype.base)
845  if rank == 0:
846  # Retrieve the scalar out of the 0-dim array
847  data = data[()]
848 
849  if rank > 1:
850  # If original data file is in fortran mode, reverse the
851  # shape first
852  if order == "fortran":
853  shape = [i for i in shape[::-1]]
854  data = data.reshape(shape)
855  # If original data file is in fortran mode, do a transpose.
856  # As the shape was reversed previously, we get the original
857  # shape again.
858  if self.order == "fortran":
859  data = data.transpose().copy()
860  # Do an additional copy just in case the array is not
861  # well-behaved (i.e., it is not aligned or not contiguous).
862  elif not data.flags.behaved:
863  data = data.copy()
864  return data
865 
def flextest.flexread.read_grid.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 879 of file read_grid.py.

880  def seek(self, offset, whence=0):
881  """Move to new file position.
882 
883  Argument offset is a byte count. Optional argument whence
884  defaults to 0 (offset from start of file, offset should be >=
885  0); other values are 1 (move relative to current position,
886  positive or negative), and 2 (move relative to end of file,
887  usually negative, although many platforms allow seeking beyond
888  the end of a file). If the file is opened in text mode, only
889  offsets returned by tell() are legal. Use of other offsets
890  causes undefined behavior.
891  """
892  self.file.seek(offset, whence)
893 
def flextest.flexread.read_grid.BinaryFile.tell (   self)

Definition at line 894 of file read_grid.py.

895  def tell(self):
896  "Returns current file position, an integer (may be a long integer)."
897  return self.file.tell()
898 
def flextest.flexread.read_grid.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 866 of file read_grid.py.

867  def write(self, arr):
868  """Write an `arr` to current position.
869 
870  The current position will be updated to point to the end of
871  written data.
872  """
873  # Transpose data if case we need to
874  if (self.order == "fortran") != (arr.flags.fortran):
875  arr = arr.transpose().copy()
876  # Write the data to file
877  self.file.write(arr.data)
878 

Member Data Documentation

flextest.flexread.read_grid.BinaryFile.file

Definition at line 786 of file read_grid.py.

flextest.flexread.read_grid.BinaryFile.mode

Definition at line 785 of file read_grid.py.

flextest.flexread.read_grid.BinaryFile.order

Definition at line 790 of file read_grid.py.

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

Definition at line 768 of file read_grid.py.


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