2.3.11. vacumm.misc.namelist – Fortran namelists

2.3.11.2. Content

Load / save fortran namelist string / file.

class Namelist[source]

Bases: dict

Handles the fortran namelist format as below:

! header comment

&namelist1 b=.true. i=0,f=0.1, s="ab" , ss="abcd" /    ! inline comment

! standalone comment
# another "comment"

&namelist2
integer=1500, float=1.5e3    ! inline comment
str1="A : B"
str2="/path/to/file.txt"
array(0)=0
array(1)=1
/

Empty lines, spaces and comments (from “:”, “!” or “#” characters to the end of line) are allowed.

Namelists start tag is the “&” character and end tag is “/”, both on their own line or not.

Variables are delimited with space(s) or comma.

Namelists (sections) are stored and made accessible in a dictionnary fashion as this class inherit from dict.

  • Keys are the namelists names, values are the variables of the corresponding namelist.
  • Namelist variables are also stored in dicts.
  • If the variable is an array like variable, values are also stored in a dict.
Variables types are automatically handled, available types are:
  • boolean: stored as bool python variable
  • integer: as int
  • real/scientific: as float
  • array: as dict

Note

  • Variables cannot be inserted directly into the Namelist, you must access/setup the namelist level before (see examples) that’s because there is no global variable in a namelist file !
Examples:
>>> n = Namelist()
>>> # The following is not correct and would results in a ValueError:
>>> n['myvar'] = 0
... ValueError: Cannot set a global variable in namelist, value must be a dict
>>> # That's the good way to do things:
>>> n['mynamelist'] = {}
>>> n['mynamelist']['myvar'] = 0
>>> n = Namelist()
>>> n['namelist1'] = dict(integer=1, real=1.0, string='hello', array=dict(a=0,b=1))
>>> print n.save_string()
... &namelist1
...   real = 1.0
...   integer = 1
...   array(a) = 0
...   array(b) = 1
...   string = "hello"
...   /
>>> n = Namelist.from_file('mynamelist.txt')
classmethod from_file(filepath, *a, **k)[source]

Factory method populating the created namelist with a file.

classmethod from_string(string, *a, **k)[source]

Factory method populating the created namelist with a string.

load_file(filepath)[source]

Load namelists from a text file

load_string(string)[source]

Load namelists from a string.

static remove_comments(s, strip=True)[source]

Internal method for comments filtering. Return s without fortran comments. If strip is True, also eliminate empty lines and leading and trailing spaces

save_file(filename, append=False)[source]

Save the (fortran) formatted namelists to a file

save_string()[source]

Return the (fortran) formated namelists