fortran-namelist
Reading and modifying Fortran namelist configuration files for scientific models
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 93
- Forks
- 7
01 前言 Front Matter
- name
- fortran-namelist
- description
- Reading and modifying Fortran namelist configuration files for scientific models
03 SKILL.md 正文
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
&nameand/ - 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
# Read entire namelist
nml = f90nml.read('/path/to/config.nml')
# Access values
kw = nml['light']['Kw']
coef_mix = nml['mixing']['coef_mix_hyp']
# Modify values
nml['light']['Kw'] = 0.35
nml['mixing']['wind_factor'] = 1.1
# Write back
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: find section, then parameter
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 to find and replace parameter in section
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)
with open(nml_file, 'w') as 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'] # List
temps = nml['init_profiles']['the_temps'] # List
print(depths[0]) # 0
print(temps[5]) # 4.7
Modifying Arrays
Arrays should not be modified for this task, but if needed:
# Only modify if changing array values entirely
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
# Read original
nml = f90nml.read('glm3.nml')
print(f"Original Kw: {nml['light']['Kw']}")
# Modify
nml['light']['Kw'] = 0.35
nml.write('glm3.nml', force=True)
# Verify change persisted
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
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.