HWRF
trunk@4391
|
Generates a Fortran namelist entirely from config files. More...
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:
Will result in:
and a trait accessible via self.trait_get('happiness_quotient')
Definition at line 411 of file namelist.py.
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 | |
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.
conf | the HWRFConfig object |
section | the section to start searching from. |
section_sorter | the cmp-like function to use to sort the sections when generating the output namelist |
var_sorters | a dict-like mapping from section name to a cmp-like function to use to sort variable names within each section. |
logger | a logging.Logger object to use to log messages |
nl | a dict of dicts (or object that acts like that) to use to initialize the Conf2Namelist. Warning: this functionality is untested |
morevars | a 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().
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 | |||
) |
Returns a copy of self.
Creates a shallow copy of self
other | unused; may be removed in the future |
Definition at line 496 of file namelist.py.
Referenced by produtil.mpiprog.MPIRanksSPMD.__add__(), hwrf.storminfo.StormInfo.__add__(), produtil.mpiprog.MPIRank.__add__(), produtil.mpiprog.MPIRank.__getitem__(), hwrf.namelist.Conf2Namelist.copy(), hwrf.namelist.Conf2Namelist.join(), produtil.prog.ImmutableRunner.runner(), hwrf.wrf.WRFSimulation.swcorner_dynamic(), and hwrf.wrf.WRFSimulation.swcorner_dynamic_multistorm().
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:
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. |
other | if 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.
others | an 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:
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 |
morevars | a 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.
section | the namelist or Conf2Namelist.TRAIT |
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
section | the namelist |
var | the 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.
section | the 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.
section | the namelist name |
var | the name of the variable in the namelist |
default | the 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.
section | the string name of the namelist |
var | the 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.
args | list 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.
section | the namelist name |
var | the name of the variable in the namelist |
data | the 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.
section | the namelist name |
var | the variable name |
data | the 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
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.
section_sorter | a cmp-like function for sorting namelist by namelist name. If section_sorters is None, cmp is used. |
var_sorters | a 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. |
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.
var | the 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.
var | the trait to get |
default | the 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()
var | the 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
var | the name of the trait to set |
value | the 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.
|
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.
|
static |