HWRF  trunk@4391
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
hwrf.namelist.Conf2Namelist Class Reference

Generates a Fortran namelist entirely from config files. More...

Detailed Description

Generates a Fortran namelist entirely from config files.

This class generates namelist information from any ConfigParser-like object, starting at a specified conf section. This differs from NamelistInserter in that no external file is read - only the ConfigParser is used. Also, an hwrf.config.HWRFConfig is not required; any ConfigParser-like object is sufficient.

When parsing the section, variables of the form nlsection.nlkey will be put in namelist section nlsection, with variable name nlkey. Variables that contain no "." but are valid Fortran variable names (except for the variable "namelist") are called "traits" and are accessible via trait_get, trait_set, and the other trait_* subroutines. The special variable "namelist" specifies a list of conf sections to recurse into. Variables in the local conf section will always override variables in included conf sections, and variables in later included conf sections will override variables in earlier included conf sections.

For example:

1  conf=RawConfigParser()
2  conf.readfp(StringIO('''
3  [sec1]
4  happiness_quotient=0.7
5  physics.mp_physics=85
6  physics.cu_physics=84
7  namelist=sec2,sec3
8  [sec2]
9  physics.cu_physics=4
10  physics.bl_pbl_physics=93
11  [sec3]
12  physics.bl_pbl_physics=3
13 ''')
14 str(Conf2Namelist(conf,'sec1'))

Will result in:

1 &physics
2  bl_pbl_physics=3
3  cu_physics=84
4  mp_physics=85
5 /

and a trait accessible via self.trait_get('happiness_quotient')

Definition at line 411 of file namelist.py.

Inheritance diagram for hwrf.namelist.Conf2Namelist:

Public Member Functions

def copy (self, other)
 Returns a copy of self. More...
 
def __init__
 Conf2Namelist constructor. More...
 
def nl_section (self, args)
 create namelists if they do not exist More...
 
def nl_set (self, section, var, data)
 Sets a variable in a namelist. More...
 
def nl_del (self, section, var)
 Removes a variable from a namelist. More...
 
def nl_del_sect (self, section)
 
def nl_have (self, section, var)
 does this namelist have this variable? More...
 
def nl_have_sect (self, section)
 
def nl_get
 get the value of a variable from a namelist More...
 
def nl_set_if_unset (self, section, var, data)
 Sets the value of a namelist variable if it has no value. More...
 
def nl_each (self, section)
 Iterates over variable,value tuples in the given namelist. More...
 
def trait_each (self)
 Iterates over variable,value tuples in the traits. More...
 
def trait_set (self, var, value)
 Sets a trait's value. More...
 
def trait_del (self, var)
 Deletes a trait. More...
 
def trait_get
 Returns a trait's value. More...
 
def trait_have (self, var)
 Returns True if the trait exists, and False otherwise. More...
 
def trait_set_if_unset (self, var, value)
 Sets the traits value if it does not have one. More...
 
def join (self, others)
 create array values by joining multiple Conf2Namelist objects More...
 
def copy
 duplicates this object More...
 
def remove_traits (self)
 Removes all traits. More...
 
def __str__ (self)
 synonym for make_namelist() More...
 
def make_namelist
 generates the namelist as a string More...
 
def namelist_sorter (self, section)
 return a sorting function for the variables in a namelist More...
 
def set_sorters (self, section_sorter, var_sorters)
 sets the sorting algorithms for namelists and variables More...
 

Public Attributes

 section_sorter
 The cmp function used to sort sections.
 
 var_sorters
 A dict mapping from section name to a cmp function used to sort namelist variables in that section.
 
 nl
 A dict of dicts used to store namelist information.
 

Static Public Attributes

string testnl
 a configuration string for testing More...
 
tuple nlentry = re.compile('\A(?:(?P<section>[a-zA-Z_][a-zA-Z0-9_]*)\.)?(?P<var>[a-zA-Z_][a-zA-Z0-9_%]*)\Z')
 A regular expression from re.compile. More...
 
tuple nlfalse = re.compile('\A(?i)(?:f.*|.false.)\Z')
 detects false Fortran logical constants
 
tuple nltrue = re.compile('\A(?i)(?:t.*|.true.)\Z')
 detects true Fortran logical constants
 
string TRAIT = '-trait-'
 special section name for traits
 

Constructor & Destructor Documentation

def hwrf.namelist.Conf2Namelist.__init__ (   self,
  conf = None,
  section = None,
  section_sorter = None,
  var_sorters = None,
  logger = None,
  nl = None,
  morevars = None 
)

Conf2Namelist constructor.

Creates a Conf2Namelist.

Parameters
confthe HWRFConfig object
sectionthe section to start searching from.
section_sorterthe cmp-like function to use to sort the sections when generating the output namelist
var_sortersa dict-like mapping from section name to a cmp-like function to use to sort variable names within each section.
loggera logging.Logger object to use to log messages
nla dict of dicts (or object that acts like that) to use to initialize the Conf2Namelist. Warning: this functionality is untested
morevarsa dict with additional variables to use when expanding strings This is simply passed to conf.items. See the HWRFConfig documentation for details.

Definition at line 517 of file namelist.py.

Referenced by hwrf.namelist.Conf2Namelist.copy().

Member Function Documentation

def hwrf.namelist.Conf2Namelist.__str__ (   self)

synonym for make_namelist()

Generates a Fortran namelist as a string. Equivalent to make_namelist().

Definition at line 786 of file namelist.py.

def hwrf.namelist.Conf2Namelist.copy (   self,
  other 
)
def hwrf.namelist.Conf2Namelist.copy (   self,
  section_subset = None,
  var_subset = None,
  other = None 
)

duplicates this object

Returns a copy of this object, or if other is specified, copies this object's contents into the target Conf2Namelist. When copying into a target Conf2Namelist, only values that are not already in that namelist will be copied. The copy has its own data structures, so modifying the copy will not modify the original. Optionally, you can copy only a subset of this object:

Parameters
section_subset= a callable object that returns True for each section to be kept
var_subset= a callable object that returns True for each variable to be kept.
otherif specified, this must be a Conf2Namelist. Instead of creating a new Conf2Namelist, data will be inserted into this one.

Definition at line 748 of file namelist.py.

Referenced by produtil.mpiprog.MPIRanksSPMD.__add__(), hwrf.storminfo.StormInfo.__add__(), produtil.mpiprog.MPIRank.__add__(), produtil.mpiprog.MPIRank.__getitem__(), produtil.prog.ImmutableRunner.runner(), hwrf.wrf.WRFSimulation.swcorner_dynamic(), and hwrf.wrf.WRFSimulation.swcorner_dynamic_multistorm().

def hwrf.namelist.Conf2Namelist.join (   self,
  others 
)

create array values by joining multiple Conf2Namelist objects

Generates a new Conf2Namelist by looping over all arguments in this namelist, and appending the other namelists' values in a list. If the other namelist does not have a given value, then this namelist's value, or the last namelist that had a value for that argument, will be appended. Variables or namelist sections that exist in the other namelists, but not this one, will be ignored.

Parameters
othersan iterable object (list, tuple) of Conf2Namelist

Definition at line 719 of file namelist.py.

Referenced by hwrf.namelist.Conf2Namelist.make_namelist().

def hwrf.namelist.Conf2Namelist.make_namelist (   self,
  section_sorter = None,
  var_sorters = None,
  morevars = None 
)

generates the namelist as a string

Returns the stringified namelist for this object, suitable for writing to a file for Fortran to read. Optionally, you can provide a sorting for the namelist entries:

Parameters
section_sorter= a cmp-like function for sorting sections by name If absent, cmp is used.
var_sorters= a dict-like mapping of section names to cmp-like objects for sorting variables within each section. If absent, self.namelist_sorter(sectionname) is used
morevarsa dict of additional variables which override values set in self.nl

Definition at line 793 of file namelist.py.

Referenced by hwrf.namelist.Conf2Namelist.__str__(), hwrf.wps.Geogrid.run(), hwrf.wps.Ungrib.run(), hwrf.wps.Metgrid.run(), and hwrf.tracker.TrackerTask.run().

def hwrf.namelist.Conf2Namelist.namelist_sorter (   self,
  section 
)

return a sorting function for the variables in a namelist

Returns a cmp function that orders namelist entries for the specified namelist section. See the argument "cmp" of the python built-in function sorted() for details.

Parameters
sectionthe namelist or Conf2Namelist.TRAIT
Returns
a cmp-like function sorter(a,b)

Definition at line 843 of file namelist.py.

Referenced by hwrf.namelist.Conf2Namelist.make_namelist().

def hwrf.namelist.Conf2Namelist.nl_del (   self,
  section,
  var 
)

Removes a variable from a namelist.

Removes a variable from a namelist

Parameters
sectionthe namelist
varthe variable to delete

Definition at line 603 of file namelist.py.

Referenced by hwrf.namelist.Conf2Namelist.trait_del().

def hwrf.namelist.Conf2Namelist.nl_del_sect (   self,
  section 
)
Removes a namelist section from the namelist

Definition at line 613 of file namelist.py.

def hwrf.namelist.Conf2Namelist.nl_each (   self,
  section 
)

Iterates over variable,value tuples in the given namelist.

Parameters
sectionthe namelist over which to iterate

Definition at line 666 of file namelist.py.

def hwrf.namelist.Conf2Namelist.nl_get (   self,
  section,
  var,
  default = None 
)

get the value of a variable from a namelist

Gets the value of a variable in a namelist. If the default is supplied and non-None, it will be returned if the variable does not exist. Raises NamelistKeyError if the variable does not exist a the default is not provided.

Parameters
sectionthe namelist name
varthe name of the variable in the namelist
defaultthe value to return if the namelist or variable do not exist

Definition at line 635 of file namelist.py.

Referenced by hwrf.namelist.Conf2Namelist.nl_set_if_unset(), and hwrf.namelist.Conf2Namelist.trait_get().

def hwrf.namelist.Conf2Namelist.nl_have (   self,
  section,
  var 
)

does this namelist have this variable?

Determines if the namelist exists and has the variable.

Returns
True if the namelist exists and it has the variable, or False otherwise
Parameters
sectionthe string name of the namelist
varthe string name of the variable

Definition at line 618 of file namelist.py.

Referenced by hwrf.namelist.Conf2Namelist.trait_have().

def hwrf.namelist.Conf2Namelist.nl_have_sect (   self,
  section 
)
Returns True if the namelist section exists in the namelist
and False otherwise

Definition at line 630 of file namelist.py.

def hwrf.namelist.Conf2Namelist.nl_section (   self,
  args 
)

create namelists if they do not exist

Ensures that the specified namelist exists. Returns self.

Parameters
argslist of namelist names

Definition at line 578 of file namelist.py.

def hwrf.namelist.Conf2Namelist.nl_set (   self,
  section,
  var,
  data 
)

Sets a variable in a namelist.

Sets the value of a namelist's variable.

Parameters
sectionthe namelist name
varthe name of the variable in the namelist
datathe value. This can be a string, datetime.datetime, int, float, fractions.Fraction or a list or tuple of such

Definition at line 586 of file namelist.py.

Referenced by hwrf.namelist.Conf2Namelist.nl_set_if_unset(), and hwrf.namelist.Conf2Namelist.trait_set().

def hwrf.namelist.Conf2Namelist.nl_set_if_unset (   self,
  section,
  var,
  data 
)

Sets the value of a namelist variable if it has no value.

If the namelist variable has a value, this function does nothing. Otherwise, it is the same as calling nl_set.

Parameters
sectionthe namelist name
varthe variable name
datathe value to set, passed to nl_set()

Definition at line 654 of file namelist.py.

Referenced by hwrf.namelist.Conf2Namelist.trait_set_if_unset().

def hwrf.namelist.Conf2Namelist.remove_traits (   self)

Removes all traits.

Deletes the special Conf2Namelist.TRAIT namelist

Returns
self

Definition at line 779 of file namelist.py.

def hwrf.namelist.Conf2Namelist.set_sorters (   self,
  section_sorter,
  var_sorters 
)

sets the sorting algorithms for namelists and variables

Sets the cmp-like functions for sorting sections and variables in each section. The section_sorter sorts sections. The var_sorters is a dict-like mapping from section name to a cmp-like function for sorting that section. If any sorter is unspecified, cmp will be used. See the "cmp" argument of the python built-in function sorted() for details.

Parameters
section_sortera cmp-like function for sorting namelist by namelist name. If section_sorters is None, cmp is used.
var_sortersa dict-like mapping from namelist name to a variable sorter function. Each variable sorter function must be a cmp-like function that compares variable names. If var_sorters is None, then cmp() will be used for all namelists.
Returns
self

Definition at line 856 of file namelist.py.

def hwrf.namelist.Conf2Namelist.trait_del (   self,
  var 
)

Deletes a trait.

Deletes a trait. This is the same as calling nl_del passing the Conf2Namelist.TRAIT constant as the section.

Parameters
varthe variable to delete.

Definition at line 688 of file namelist.py.

def hwrf.namelist.Conf2Namelist.trait_each (   self)

Iterates over variable,value tuples in the traits.

Definition at line 674 of file namelist.py.

def hwrf.namelist.Conf2Namelist.trait_get (   self,
  var,
  default = None 
)

Returns a trait's value.

Returns the value of a trait. If a default is given and non-None, returns the default if the trait does not exist. This is the same as calling nl_get() passing the Conf2Namelist.TRAIT as the section.

Parameters
varthe trait to get
defaultthe default value if the trait is unset

Definition at line 695 of file namelist.py.

def hwrf.namelist.Conf2Namelist.trait_have (   self,
  var 
)

Returns True if the trait exists, and False otherwise.

Determines if a trait is set. This is the same as passing Conf2Namelist.TRAIT as the section argument to nl_have()

Parameters
varthe trait to query

Definition at line 705 of file namelist.py.

def hwrf.namelist.Conf2Namelist.trait_set (   self,
  var,
  value 
)

Sets a trait's value.

This is the same as nl_set() but sets a trait. It simply passes the special constant Conf2Namelist.TRAIT as the namelist

Parameters
varthe name of the trait to set
valuethe value to set

Definition at line 680 of file namelist.py.

def hwrf.namelist.Conf2Namelist.trait_set_if_unset (   self,
  var,
  value 
)

Sets the traits value if it does not have one.

Sets the value of a trait if the trait does not already have a value. This is the same as calling nl_set_if_unset() passing the special value Conf2Namelist.TRAIT as the section.

Definition at line 712 of file namelist.py.

Member Data Documentation

hwrf.namelist.Conf2Namelist.nlentry = re.compile('\A(?:(?P<section>[a-zA-Z_][a-zA-Z0-9_]*)\.)?(?P<var>[a-zA-Z_][a-zA-Z0-9_%]*)\Z')
static

A regular expression from re.compile.

This is used to scan config section option names to detect if they are namelist variable names (format: section.variable).

Definition at line 479 of file namelist.py.

hwrf.namelist.Conf2Namelist.testnl
static
Initial value:
1 = '''[sec1]
2 happiness_quotient=0.7
3 physics.mp_physics=85
4 physics.cu_physics=84
5 namelist=sec2,sec3
6 [sec2]
7 physics.cu_physics=4
8 physics.bl_pbl_physics=93
9 domains.something=32
10 [sec3]
11 physics.bl_pbl_physics=3'''

a configuration string for testing

Definition at line 462 of file namelist.py.


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