A Python module and command line tool for parsing, writing, and modifying Fortran 90 namelist files
npx @tessl/cli install tessl/pypi-f90nml@1.4.0A comprehensive Python module and command line tool for parsing, writing, and modifying Fortran 90 namelist files. f90nml converts Fortran namelist files into Python dictionary-like objects, enabling seamless integration with Python workflows for scientific computing and configuration management.
pip install f90nmlimport f90nmlFor advanced configuration:
from f90nml import Parser, Namelistimport f90nml
# Read a namelist file
nml = f90nml.read('input.nml')
# Access values like a dictionary
print(nml['config_nml']['steps'])
print(nml['config_nml']['use_biharmonic'])
# Modify values
nml['config_nml']['steps'] = 1000
nml['config_nml']['visc'] = 1.0e-5
# Write back to file
nml.write('output.nml')
# Parse namelist string directly
nml_str = '&data_nml x=1 y=2 /'
nml = f90nml.reads(nml_str)
# Create patch to modify existing file while preserving formatting
patch = {'config_nml': {'steps': 500, 'visc': 2.0e-4}}
f90nml.patch('input.nml', patch, 'patched.nml')f90nml uses a layered architecture designed for flexibility and precision:
This design enables f90nml to handle complex Fortran namelist features like derived types, multidimensional arrays, start indices, and comment preservation while providing a Pythonic interface.
High-level functions for reading, writing, and modifying Fortran namelist files with automatic type conversion and format detection.
def read(nml_path):
"""
Parse a Fortran namelist file and return its contents.
Args:
nml_path: str or file-like object - Path to namelist file or file object
Returns:
Namelist: Dictionary-like object containing namelist data
"""
def reads(nml_string):
"""
Parse a Fortran namelist string and return its contents.
Args:
nml_string: str - String containing Fortran namelist data
Returns:
Namelist: Dictionary-like object containing namelist data
"""
def write(nml, nml_path, force=False, sort=False):
"""
Save a namelist to disk using file path or file object.
Args:
nml: Namelist or dict - Namelist data to write
nml_path: str or file-like object - Output file path or file object
force: bool - Overwrite existing files (default: False)
sort: bool - Sort namelist keys alphabetically (default: False)
"""
def patch(nml_path, nml_patch, out_path=None):
"""
Create a new namelist based on input namelist and reference dict.
Args:
nml_path: str - Path to original namelist file
nml_patch: dict - Dictionary of values to patch
out_path: str, optional - Output file path (default: adds ~ suffix)
Returns:
Namelist: Patched namelist object
"""Dictionary-like container objects with extensive formatting controls for Fortran-compliant output generation.
class Namelist(OrderedDict):
"""Representation of Fortran namelist in Python environment."""
def __init__(self, *args, default_start_index=None, **kwds): ...
def write(self, nml_path, force=False, sort=False): ...
def patch(self, nml_patch): ...
def todict(self, complex_tuple=False): ...
# Formatting properties
column_width: int
default_start_index: int
end_comma: bool
indent: str
uppercase: bool
logical_repr: dictNamelist Objects and Formatting
Configurable parsing engine for handling Fortran syntax variations, custom comment tokens, and complex array indexing.
class Parser:
"""Fortran namelist parser with configurable options."""
def __init__(self): ...
def read(self, nml_fname, nml_patch_in=None, patch_fname=None): ...
def reads(self, nml_string): ...
# Configuration properties
comment_tokens: str
default_start_index: int
global_start_index: int
row_major: bool
sparse_arrays: bool
strict_logical: boolLow-level utility functions for type conversion, array indexing, and tokenization that provide the foundation for f90nml's robust processing capabilities.
# Type conversion functions
def pyfloat(v_str): ...
def pycomplex(v_str): ...
def pybool(v_str, strict_logical=True): ...
def pystr(v_str): ...
# Multidimensional array indexing
class FIndex:
def __init__(self, bounds, first=None): ...
def __next__(self): ...
# Low-level tokenization
class Tokenizer:
def __init__(self): ...
def parse(self, line): ...Utility Functions and Advanced Components
Shell-based tool for namelist manipulation, format conversion, and integration with scientific computing workflows.
f90nml input.nml -g group_name -v variable=value
f90nml input.nml -f json > output.json
f90nml input.nml -p patch_file.nml -o output.nmlclass Namelist(OrderedDict):
"""Dictionary-like container for namelist groups and variables."""
def __init__(self, *args, default_start_index=None, **kwds): ...
def write(self, nml_path, force=False, sort=False): ...
def patch(self, nml_patch): ...
def todict(self, complex_tuple=False): dict
class Parser:
"""Configurable Fortran namelist parser."""
def __init__(self): ...
def read(self, nml_fname, nml_patch_in=None, patch_fname=None): Namelist
def reads(self, nml_string): Namelist
class Cogroup(list):
"""List of Namelist groups which share a common key."""
def __init__(self, nml, key, *args, **kwds): ...
def update(self, args): ...
keys: list
class NmlKey(str):
"""String containing internal key for duplicate key handling."""
def __new__(cls, value='', *args, **kwargs): ...
class FIndex:
"""Column-major multidimensional index iterator."""
def __init__(self, bounds, first=None): ...
def __next__(self): tuple
class Tokenizer:
"""Fortran namelist tokenizer for lexical analysis."""
def __init__(self): ...
def parse(self, line): list
# Type conversion functions
pyfloat(v_str: str) -> float
pycomplex(v_str: str) -> complex
pybool(v_str: str, strict_logical: bool = True) -> bool
pystr(v_str: str) -> str