nmlfile: Interface to Fortran namelist

This module provides a Python interface to Fortran namelist files. Fortran namelists are not quite a fully standardized format, but this module and its NmlFile class do the best they can.

class cape.nmlfile.NmlFile(*args, **kw)
apply_dict(opts: dict)

Set values from a Python dictionary

Call:
>>> nml.apply_dict(opts)
Inputs:
nml: NmlFile

Namelist index

opts: dict

Dictionary of namelist sections and their settings

get_opt(sec: str, opt: str, j=None, vdef=None, k=0)

Get value of one variable in one section

Call:
>>> val = nml.set_opt(sec, opt, j=None, vdef=None, k=0)
Inputs:
nml: NmlFile

Namelist index

sec: str

Name of section

name: str

Name of option

j: {None} | int | slice | tuple

Index or indices of val to return nml[sec][opt]

vdef: {None} | object

Default value if opt not present in nml[sec]

k: {0} | int

Read the kth section named sec

Outputs:
val: object

Value to set to nml[sec][opt]

read_nmlfile(fname: str)

Read a namelist file

Call:
>>> nml.read_nmlfile(fname)
Inputs:
nml: NmlFile

Namelist index

fname: str

Name of file

set_opt(sec: str, opt: str, val, j=None, k=0)

Set value of one variable in one section

This can be a partial setting of one entry in a 1-D array. Multidimensional arrays cannot be safely set using this function.

Call:
>>> nml.set_opt(sec, opt, val, j=None, k=0)
Inputs:
nml: NmlFile

Namelist index

sec: str

Name of section

name: str

Name of option

val: object

Value to set to nml[sec][opt]

j: {None} | int | slice

Index or indices of where to save val in nml[sec][opt]

k: {0} | int

Modify the kth section named sec

set_sec(sec: str, secopts: dict, k=0)

Set values for one namelist section

Call:
>>> nml.set_sec(sec, secopts, k=0)
Inputs:
nml: NmlFile

Namelist index

sec: str

Name of section

secopts: dict

Options dictionary for section sec

k: {0} | int

Modify the kth section named sec

write(fname=None)

Write namelist to file

Call:
>>> nml.write(fname=None)
Inputs:
nml: NmlFile

Namelist index

fname: {nml.fname} | str

Name of file

write_sec(fp, secname: str, sec: dict)

Write one section to file

Call:
>>> nml.write_sec(fp, secname, sec)
Inputs:
nml: NmlFile

Namelist index

fp: io.IO Base

File handle open for writing

secname: str

Name of variable to write

sec: dict

Values of settings for that section

write_var(fp, name: str, val: object)

Write a single variable to file

Call:
>>> nml.write_var(fp, name, val)
Inputs:
nml: NmlFile

Namelist index

fp: io.IO Base

File handle open for writing

name: str

Name of variable to write

val: object

Any value to convert to namelist text

cape.nmlfile.parse_index_str(txt: str)

Parse index string

Call:
>>> nval, inds = _read_indices(txt)
Inputs:
txt: str

Text of an LHS index, e.g. "(:, 2:5, 3)"

Outputs:
nval: int

Size of slice, -1 for slices like :

inds: list[ind]

List of indices read, [] if no indices used

ind: None | int | tuple

Individual index, : -> None; 3:6 -> (3, 6)

Versions:
  • 2023-06-08 @ddalle: v1.0