stateset (version 0.1.1, 12 Jan 2005)
index
stateset.py

Define class StateSet for model State objects.
 
The StateSet class is a container that is a collection of State
class objects.

 
Modules
       
numarray.ma.MA
numarray.linear_algebra.mlab
numarray
copy
os
user

 
Classes
       
__builtin__.object
StateSet

 
class StateSet(__builtin__.object)
    Collection of State objects at different ids.
 
The StateSet class is a container that is a collection of State
class objects.  The StateSet keys for mapping the State objects 
are the id attributes in the State objects; the keys in a State-
Set object are unique, and thus each StateSet object can have 
only as many State objects as there are unique ids.
 
Public Instance Attributes:
* delt:  Timestep (i.e. the difference between the time for States
  at "tp1" and "t0"), in units time_units (of the State variables).
  Default is None.
* id:  Name of the object.  The name contains information about the 
  set of states.  There are no standardized set of values that this
  attribute has to conform to.  String.  Default is None.
 
All the instance attributes can be set directly or via keywords
passed in the instantiation parameter list.  See the __init__ func-
tion docstring for details.
 
Example:
>>> from field import Field
>>> from state import State
>>> data1 = N.array([1.1, 3.0, -4.8, 2.9, 2.1])
>>> data2 = N.array([31.1, 0.4, 4.0, -4.9, 9.1])
>>> data3 = N.array([3.1, 2.4, 4.9, 8.1, -9.2])
>>> f1a = Field(data1, id='u', units='m/s', axis=['Latitude'])
>>> f2a = Field(data2, id='v', units='m/s', axis=['Latitude'])
>>> f3a = Field(data3, id='w', units='m/s', axis=['Latitude'])
>>> f1b = Field(data1*0.1, id='u', units='m/s', axis=['Latitude'])
>>> f2b = Field(data2*0.2, id='v', units='m/s', axis=['Latitude'])
>>> s1 = State(f1a, f2a, f3a, long_name='winds', id='t0')
>>> s2 = State(f1b, f2b, long_name='horiz_winds', id='tm1')
>>> set = StateSet(s1, s2, id='wind_collection')
 
>>> print set.id
wind_collection
>>> print set.delt
None
>>> print set.keys()
['tm1', 't0']
 
>>> print type(set['t0'])
<class 'state.State'>
>>> print set['tm1'].keys()
['u', 'v']
>>> print set['tm1']['v'].units
m/s
 
  Methods defined here:
__contains__(self, item)
__delitem__(self, key)
__getitem__(self, key)
__init__(self, *args, **kwds)
Initialize class object.
 
Positional Arguments:
* All arguments of this initialization function are assumed to
  be State objects which are put into the container's data
  structure, private attribute _data.  If any of the arguments 
  have the same id attribute, only the last State object in
  the argument list with that id will be stored; the rest will 
  be ignored.
 
Keyword Arguments:
* All keywords are attributes of the instance.  Any other key-
  words in the calling line are ignored.  See the class doc-
  string for details.
 
The private variable _data is the data structure that holds
all the States.  The keys to _data (accessible via the public
function keys) are the State.id attributes and the values of
_data (accessible via the public function values) are State
objects.
__iter__(self)
Returns an iterkeys equivalent iterator over StateSet data.
__len__(self)
__setitem__(self, key, value)
Method to add/rebind items.
 
Raises a KeyError exception if key is not equal to value.id.
add(self, *args)
Add/overwrite State objects given in args to StateSet object.
 
Positional Arguments:
* All arguments are State objects and are added to the StateSet
  object self in the order they are given in the argument list.
  If any of the arguments have id attributes equal to an exist-
  ing key in self, the key is detached from its previous value 
  and associated with the new value given in the argument.
clear(self)
Clears all State objects in the StateSet object.
copy(self)
Returns deepcopy of StateSet object.
copymeta(self)
Returns deepcopy of StateSet object metadata.
 
The entire StateSet object is copied, except for the States
held in the StateSet container.  Thus, the returned StateSet
object contains no data or State metadata, but rather only
StateSet metadata.
del_earliest(self)
Removes the earliest State object from StateSet container.
 
Earliest is defined in terms of relative timestep.  Only non-
"tendency" State ids (i.e. those with prefix "tm", "tp", or 
value of "t0") in the StateSet object are considered.  All
other State ids, valid or not (e.g. "tendency" State ids like 
"tend1") are ignored.
 
Examples of the Idea of How the Method Is Used and Works:
   set = StateSet(s1, s2, s3)
   set.keys() -> ['tp1', 't0', 'tm2']
   set.del_earliest() 
   set.keys() -> ['tp1', 't0']
 
   set = StateSet(s1, s2, s3)
   set.keys() -> ['tend1', 't0', 'tm2']
   set.del_earliest() 
   set.keys() -> ['tend1', 't0']
del_latest(self)
Removes the latest State object from StateSet container.
 
Latest is defined in terms of relative timestep.  Only non-
"tendency" State ids (i.e. those with prefix "tm", "tp", or 
value of "t0") in the StateSet object are considered.  All
other State ids, valid or not (e.g. "tendency" State ids like 
"tend1") are ignored.
 
Examples of the Idea of How the Method Is Used and Works:
   set = StateSet(s1, s2, s3)
   set.keys() -> ['tp1', 't0', 'tm2']
   set.del_latest() 
   set.keys() -> ['tm2', 't0']
 
   set = StateSet(s1, s2, s3)
   set.keys() -> ['tend1', 't0', 'tm2']
   set.del_latest() 
   set.keys() -> ['tend1', 'tm2']
has_key(self, key)
Returns True if State object of id key is in StateSet object.
items(self)
Returns copy of list of all State items in StateSet object.
 
The copy is a shallow copy (I think) and thus references to
other objects are not copied recursively.
keys(self)
Returns copy of list of all State object ids in StateSet object.
 
The copy is a shallow copy (I think) and thus references to
other objects are not copied recursively.
meta_ok_to_interface(self, IMobj, check_long_name=False)
Returns True if metadata passes checks for interfaces.
 
Tests whether the Field objects metadata in the State objects
that make up this StateSet object pass consistency checks in 
accordance with the description given in InterfaceMeta object 
IMobj.  If all the Field objects pass, this method returns 
True.  See the docstring for the Field class for details as to 
what the consistency checks entail.
 
Positional Input Argument:
* IMobj:  InterfaceMeta object that defines the accepted meta-
  data that you wish to check this State variable's Field var-
  iable's metadata against.  For details see:
 
     http://geosci.uchicago.edu/csc/modelutil/doc/imeta.html
 
Keyword Input Arguments:
* check_long_name:  If set to True, the method not only checks 
  units but also long_name.  Default is False.
refdata(self)
Returns reference to data of StateSet object.
 
This returns a reference to the private attribute _data, which
is a dictionary.
setdata(self, data)
Reference StateSet private _data attribute to data.
 
This makes a reference of data to the private attribute _data, 
which is a dictionary.  Thus, argument data must be a diction-
ary.
shift_time_stateid(self, nstep)
Shift State objects nstep timesteps; delete "tendencies".
 
The State objects in the StateSet container are all shifted
forward or backward the number of time steps given in nstep.
For example, if nstep=-1:  the State object with id "t0" has
its id changed to "tm1" and the value of its time attribute 
is decreased by delt; the State object with id "tm1" has its
id changed to "tm2", etc.  "Tendency" State variables (e.g. 
id with prefix "tend") are deleted from the StateSet, as are 
State objects with None as key.
 
If either self.delt is None or the time attribute of a State
variable is None, the time attribute of that State variable
after the shift is set to None (if the time attribute does
not exist for that State variable pre-shift, it remains
undefined).
 
Positional Input Parameter:
* nstep:  Number of timesteps to move all State objects for-
  ward or backward.  Integer.  Required input, with no default 
  value.
 
Since shifting timesteps requires that all the timesteps in
the StateSet are consistent with one another, this method also
executes the time_ok() method on the post-shift States and 
throws an exception if it returns False.
 
Example:
>>> from field import Field
>>> from state import State
>>> data1 = N.array([1.1, 3.0, -4.8, 2.9, 2.1])
>>> data2 = N.array([31.1, 0.4, 4.0, -4.9, 9.1])
>>> data3 = N.array([3.1, 2.4, 4.9, 8.1, -9.2])
>>> f1a = Field(data1, id='u', units='m/s')
>>> f2a = Field(data2, id='v', units='m/s')
>>> f3a = Field(data3, id='w', units='m/s')
>>> s1 = State(f3a, id='tend1')
>>> s2 = State(f1a, f2a, id='t0')
>>> s3 = State(f2a, id='tm1')
>>> s = StateSet(s1, s2, s3, id='velocities')
>>> s.keys()
['tm1', 'tend1', 't0']
>>> s.shift_time_stateid(-1)
>>> s.keys()
['tm1', 'tm2']
sort_stateid(self)
Returns a list of the State ids in time-ascending order.
 
Only non-"tendency" State ids (i.e. those with prefix "tm",
"tp", or value of "t0") in the StateSet object are sorted and 
returned.  All other State ids, valid or not (e.g. "tendency" 
State ids like "tend1") are ignored.
 
Examples of the Idea of How the Method Is Used and Works:
   set = StateSet(s1, s2, s3)
   set.keys() -> ['tp1', 't0', 'tm2']
   set.sort_stateid() -> ['tm2', 't0', 'tp1']
 
   set = StateSet(s1, s2, s3, s4)
   set.keys() -> ['tp1', 't0', 'tm2', 'tend1']
   set.sort_stateid() -> ['tm2', 't0', 'tp1']
state_list(self)
Returns summary list of State objects in StateSet object.
 
A list of tuples is returned, each tuple describing one of the
State objects the StateSet object is a container for.  Each
tuple holds the string values of the following State attributes,
in this form:  (id, long_name).  If either the id or long_name
attributes are empty, an empty string is returned.
 
Example of the Idea of How the Method Is Used and Works:
   s1 = State(f1a, f2a, f3a, long_name='3-D_winds', id='t0')
   s2 = State(f1b, f2b, long_name='2-D_winds')
   st = StateSet(s1, s2, id='velocities')
   st.state_list() -> [(None, '2-D_winds'), ('t0', '3-D_winds')]
stateid2int(self)
Returns list of non-"tendency" State ids as integers.
 
The source list of State ids is the keys in the present State-
Set object.  In the returned list, "tm" ids are negative and 
"tp" ids are positive.  "t0" is 0.  Any "tend*" terms in the 
State id list are ignored.  The returned list does not share
memory with the original keys list, but the order of the list
is the same as the input keys.
 
Example of the Idea of How the Method Is Used and Works:
   set = StateSet(s1, s2, s3)
   set.keys() -> ['tp1', 't0', 'tm2']
   set.stateid2int() -> [1, 0, -2]
time_ok(self)
Returns True if passes consistency checks for time parameters.
 
Examines the time parameters for the StateSet container as well
as the constituent State objects to see that they are consis-
tent with each other.  The StateSet container can only hold
non-"tendency" State objects (e.g. the presence of State ob-
jects with id prefixed by "tend" will throw an exception). 
 
Method makes the following consistency checks:
- Each of the State variables have the same time_units (or un-
  defined or equal to None).
- Values of time, delt, and id are consistent with each other.
  If the "t0" State id does not exist, the "t0" object does 
  not have the time attribute (or it equals None), or the delt 
  attribute does not exist or equals None, this check is igno-
  red and True is returned.
 
In the consistency checks, if an attribute is undefined or set
to None, the value is assumed to match any value.  If no checks 
are done, default of the method is to return True.
values(self)
Returns copy of list of all State objects in StateSet object.
 
The copy is a shallow copy (I think) and thus references to
other objects are not copied recursively.
wrap(self)
Connect the MPI parallel subdomains of all State variables.
 
Runs through all State variables and executes the wrap method
for the State variable.

Data and other attributes defined here:
__dict__ = <dictproxy object>
dictionary for instance variables (if defined)
__weakref__ = <attribute '__weakref__' of 'StateSet' objects>
list of weak references to the object (if defined)

 
Functions
       
int2stateid(inlist)
Returns list of integers as list of non-"tendency" State ids.
 
In the returned list, negative integers become "tm" State ids and 
positive integers become "tp" ids.  0 becomes "t0".  The numeral(s)
after the "tm" or "tp" prefix is converted from the magnitude of 
the integer.  The returned list does not share memory with inlist, 
but the order of the list is the same as the input list.
 
Positional Input Parameter:
* inlist:  List of integers.  Function does not test that this is
  the case, but use of different values will likely give an unin-
  telligible result or an exception.
 
Examples:
>>> a = [1, 0, -2]
>>> print int2stateid(a)
['tp1', 't0', 'tm2']
>>> a = [1, 0, -2, None]
>>> print int2stateid(a)
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs()
stateid2int(inlist)
Returns non-"tendency" State ids list inlist as list of integers.
 
In the returned list, "tm*" ids become negative integers and "tp*" 
ids become positive.  "t0" becomes 0.  The magnitude of the integer 
is equal to the numeral(s) after the "tm" or "tp" prefix.  There 
can be no "tend*" terms or any other named values in inlist or an 
exception is thrown.  The returned list does not share memory with 
inlist, but the order of the list is the same as the input list.
 
Positional Input Parameter:
* inlist:  List of State ids.  Usually this list is the keys of a 
  StateSet object.  List of strings.
 
Examples:
>>> a = ['tp1', 't0', 'tm2']
>>> print stateid2int(a)
[1, 0, -2]
>>> a = ['tp1', 't0', 'tm2', 'tend1']
>>> print stateid2int(a)
Traceback (most recent call last):
    ...
ValueError: Bad keys

 
Data
        __author__ = 'Johnny Lin <http://www.johnny-lin.com/>'
__credits__ = 'Thanks to the CSC group at the University of Chicago.'
__date__ = '12 Jan 2005'
__test__ = {'Additional Examples and Tests': "\n#- Initialize a StateSet object:\n\n>>> from fiel...ist()\n[(None, '2-D_winds'), ('t0', '3-D_winds')]\n"}
__version__ = '0.1.1'
gemath_num_package = 'numarray'
gemathrc = '/home/jlin/.gemathrc.py'
home_dir = '/home/jlin'
look_up_gemathrc = True

 
Author
        Johnny Lin <http://www.johnny-lin.com/>

 
Credits
        Thanks to the CSC group at the University of Chicago.