| name | fortran-namelist |
| description | Reading and modifying Fortran namelist configuration files for scientific models |
Fortran Namelist Skill
Overview
Fortran namelists are human-readable configuration files used by many scientific models. They use §ion syntax and key=value pairs. Parsing requires careful handling of Fortran syntax quirks.
File Format
§ion_name
parameter1 = value1
parameter2 = value2, value3, value4
array_param = 1, 2, 3, 4, 5
/
Key features:
- Sections enclosed with
&name and /
- Each section ends with
/
- Values can be scalars or comma-separated arrays
- Comments may appear (starting with
!)
- Spaces and newlines are flexible
Reading Namelists
Using f90nml Library (Recommended)
import f90nml
nml = f90nml.read('/path/to/config.nml')
kw = nml['light']['Kw']
coef_mix = nml['mixing']['coef_mix_hyp']
nml['light']['Kw'] = 0.35
nml['mixing']['wind_factor'] = 1.1
nml.write('/path/to/config.nml', force=True)
Installation
pip install f90nml
Manual Parsing with Regex
For simple changes without additional dependencies:
import re
def read_nml_parameter(nml_file, section, param):
"""Extract single parameter value from namelist"""
with open(nml_file, 'r') as f:
content = f.read()
pattern = (r'&' + section + r'.*?' +
r'(\s+' + param + r'\s*=\s*)' +
r'([^,\n/]+)')
match = re.search(pattern, content, re.DOTALL)
if match:
value_str = match.group(2).strip()
try:
return float(value_str)
except ValueError:
return value_str
return None
def update_nml_parameter(nml_file, section, param, value):
"""Update a parameter in namelist"""
with open(nml_file, 'r') as f:
content = f.read()
pattern = (r'(&' + section + r'.*?)' +
r'(\s+' + param + r'\s*=\s*)' +
r'([^,\n/]+)')
replacement = r'\g<1>\g<2>' + str(value)
new_content = re.sub(pattern, replacement, content,
count=1, flags=re.DOTALL)
(nml_file, ) f:
f.write(new_content)
Working with Arrays
Fortran namelists support array values:
&init_profiles
the_depths = 0, 1, 2, 3, 4, 5
the_temps = 5.1, 5.1, 5.0, 4.9, 4.8, 4.7
/
Reading Arrays
import f90nml
nml = f90nml.read('glm3.nml')
depths = nml['init_profiles']['the_depths']
temps = nml['init_profiles']['the_temps']
print(depths[0])
print(temps[5])
Modifying Arrays
Arrays should not be modified for this task, but if needed:
nml['init_profiles']['the_depths'] = [0, 1, 2, 3, 4, 5, 6]
nml['init_profiles']['the_temps'] = [5.1, 5.1, 5.0, 4.9, 4.8, 4.7, 4.6]
Best Practices
- Always use f90nml when available—it handles quirks properly
- Make backup before modifying:
cp glm3.nml glm3.nml.bak
- Validate changes by reading back after write
- Whitespace preservation: f90nml maintains formatting
- Section case: Fortran is case-insensitive for sections/params
- Comments: f90nml preserves comments during read/write
Validation Example
import f90nml
nml = f90nml.read('glm3.nml')
print(f"Original Kw: {nml['light']['Kw']}")
nml['light']['Kw'] = 0.35
nml.write('glm3.nml', force=True)
nml2 = f90nml.read('glm3.nml')
assert nml2['light']['Kw'] == 0.35
print("Verification passed!")
Common Issues
- Array syntax: Use
[...] in Python, Fortran has = with commas
- Type mismatch: Ensure values match expected types (int vs float)
- Spacing: f90nml handles this; manual regex may fail on unexpected spacing
- Case sensitivity: Parameter names are case-insensitive in Fortran