HWRF  trunk@4391
Classes | Functions | Variables
hwrf.namelist Namespace Reference

This module provides two different ways to generate Fortran namelist files from HWRFConfig sections: More...

Detailed Description

This module provides two different ways to generate Fortran namelist files from HWRFConfig sections:

NamelistInserter - given a file, or a multi-line string, finds angle bracket (<vartype:stuff>) strings with values from an HWRFConfig section. This is the suggested method for namelists with very few configurable items.

Conf2Namelist - given one or more Config sections, find values of the form nlsec.nlkey=value. The nlsec specifies the namelist &nlsec, the nlkey specifies the namelist variable name, and the value is converted to a Python value and then a Fortran namelist value. The Conf2Namelist provides ways to append multiple Conf2Namelist objects' data to form Fortran one-dimensional arrays. This is to support WRF namelists, where one specifies each domain's data in a 1D array.

In addition, this module provides two functions to_fortnml and from_fortnml to convert between in-memory Python objects and strings suitable for pasting in a Fortran namelist to achieve the same value in Fortran.

Classes

class  Conf2Namelist
 Generates a Fortran namelist entirely from config files. More...
 
class  NamelistInserter
 Insert config file data into a Fortran namelist file. More...
 
class  NamelistRecursion
 used to indicate namelist recursion More...
 

Functions

def to_fortnml
 converts a Python object to fortran namelist format More...
 
def from_fortnml (py)
 Converts a fortran namelist value to a Python object. More...
 

Variables

list __all__ = ['to_fortnml','from_fortnml','Conf2Namelist','NamelistInserter']
 Symbols exported by "from hwrf.namelist import *".
 
tuple emptydict = dict()
 used for efficiency in a few places that need an empty dict()
 
tuple fortnml_parse = re.compile()
 a regular expression for tokenizing a fortran namelist scalar value
 

Function Documentation

def hwrf.namelist.from_fortnml (   py)

Converts a fortran namelist value to a Python object.

This is the inverse function for to_fortnml. Given a string from a Fortran namelist value, returns an equivalent python object. Will throw NamelistValueError if an unrecognized object is present, or NamelistRecursion if you send a nested container (such as a list of lists).

Parameters
pythe string to convert
Returns
the Python object

Definition at line 121 of file namelist.py.

def hwrf.namelist.to_fortnml (   py,
  exc_hint = '' 
)

converts a Python object to fortran namelist format

Converts the given python data structure to a string suitable for representing in a Fortran namelist. It can handle bool, int, float, string, datetime, fraction and a list (or tuple) of those. A datetime is converted via strftime('"%Y-%m-%d_%H:%M:%S"'). The optional exc_hint is used when raising exceptions to give an idea of what file and line number, or config option name, generated the exception.

Conversions:

  • True ==> "T"
  • False ==> "F"
  • int ==> str(py)
  • float ==> str(py)
  • fractions.Fraction ==> str(float(py))
  • basestring ==> double-quoted string
  • datetime.datetime ==> WPS/WRF format date stamp: Y-m-D_H:M:S
  • list or tuple ==> comma-separated list of converted values

Anything else will raise an exception.

Returns
the converted value as a string
Parameters
pythe object to convert
exc_hintprepended to exception messages when exceptions are raised. Should contain file and line information.

Definition at line 44 of file namelist.py.

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