A Python module and command line tool for parsing, writing, and modifying Fortran 90 namelist files
—
Shell-based tool for namelist manipulation, format conversion, and integration with scientific computing workflows. The f90nml command line interface provides direct access to namelist functionality from shell scripts and batch processing environments.
The f90nml command line tool provides a unified interface for namelist operations.
f90nml [OPTIONS] [INPUT_FILE] [OUTPUT_FILE]
# Core options:
-g, --group GROUP # Specify namelist group to modify
-v, --variable VAR=VALUE # Specify variable to add or modify
-p, --patch # Modify existing namelist as patch
-f, --format FORMAT # Specify output format (json, yaml, nml)
--version # Show version informationUsage Examples:
# Display help
f90nml --help
# Show version
f90nml --version
# Simple file reading (outputs to stdout)
f90nml input.nmlModify individual variables within namelist groups directly from the command line.
f90nml INPUT_FILE -g GROUP_NAME -v VARIABLE=VALUE [OUTPUT_FILE]
# Multiple variables:
f90nml INPUT_FILE -g GROUP_NAME -v VAR1=VALUE1 -v VAR2=VALUE2Usage Examples:
# Modify single variable
f90nml config.nml -g run_params -v timesteps=1000
# Modify multiple variables in same group
f90nml config.nml -g run_params -v timesteps=1000 -v dt=0.01
# Save to specific output file
f90nml input.nml -g physics -v gravity=9.81 modified.nml
# Modify different groups (requires multiple calls)
f90nml temp.nml -g group1 -v param1=100 temp2.nml
f90nml temp2.nml -g group2 -v param2=200 final.nmlConvert between namelist and other configuration formats for interoperability.
# Output formats:
f90nml INPUT_FILE -f json # Convert to JSON
f90nml INPUT_FILE -f yaml # Convert to YAML
f90nml INPUT_FILE -f nml # Keep as namelist (default)
# Input format auto-detected from extension:
# .json -> JSON input
# .yaml -> YAML input
# .nml -> Namelist input (default)Usage Examples:
# Convert namelist to JSON
f90nml config.nml -f json > config.json
# Convert namelist to YAML
f90nml config.nml -f yaml > config.yaml
# Convert JSON back to namelist
f90nml config.json config.nml
# Convert with modification
f90nml input.nml -g params -v value=42 -f json > modified.json
# Pipeline processing
f90nml input.nml -f json | jq '.params.timesteps = 2000' | f90nml -f nml > output.nmlApply modifications while preserving original file structure, comments, and formatting.
f90nml INPUT_FILE -p -g GROUP -v VARIABLE=VALUE [OUTPUT_FILE]
# Patch mode preserves:
# - Original formatting and indentation
# - Comments and whitespace
# - Variable ordering
# - Only modifies specified valuesUsage Examples:
# Patch single value (creates input.nml~)
f90nml config.nml -p -g run_config -v timesteps=2000
# Patch with specific output
f90nml config.nml -p -g run_config -v timesteps=2000 patched.nml
# Patch multiple variables
f90nml model.nml -p -g physics -v gravity=9.81 -v viscosity=1e-5 updated.nml
# Patch with format conversion
f90nml input.nml -p -g params -v debug=.true. -f json > patched.jsonComplex operations combining multiple features for workflow automation.
Parameter Sweeps:
# Script for parameter sweep
for timesteps in 100 500 1000 2000; do
f90nml base.nml -g run_config -v timesteps=$timesteps run_${timesteps}.nml
doneConfiguration Management:
# Create environment-specific configs
f90nml base.nml -g io -v output_dir="/scratch/prod" production.nml
f90nml base.nml -g io -v output_dir="/tmp/test" -g debug -v verbose=.true. test.nmlFormat Pipeline:
# Convert, modify, convert back
f90nml config.nml -f json \
| jq '.run_params.timesteps = 5000' \
| f90nml -f nml > modified.nmlBatch Processing:
# Process multiple files
for file in configs/*.nml; do
basename=$(basename "$file" .nml)
f90nml "$file" -g params -v threads=8 "modified_${basename}.nml"
doneCommon error scenarios and their command line behavior:
# File not found
f90nml nonexistent.nml
# Output: f90nml: error: Cannot read file 'nonexistent.nml'
# Exit code: 1
# Invalid group name
f90nml config.nml -g nonexistent_group -v param=value
# Output: Adds new group 'nonexistent_group' with param=value
# Invalid variable syntax
f90nml config.nml -g params -v "invalid syntax"
# Output: f90nml: error: Variable must be in format 'NAME=VALUE'
# Exit code: 1
# Unsupported format
f90nml config.nml -f xml
# Output: f90nml: error: format must be one of: json, yaml, nml
# Exit code: 1
# Write permission error
f90nml config.nml -g params -v test=1 /read-only/output.nml
# Output: f90nml: error: Permission denied: '/read-only/output.nml'
# Exit code: 1Real-world examples of command line integration with scientific workflows.
HPC Job Submission:
#!/bin/bash
#SBATCH --job-name=simulation
#SBATCH --ntasks=64
# Modify namelist for this job
f90nml base_config.nml \
-g parallel -v nprocs=64 \
-g io -v output_dir="$SCRATCH/run_$SLURM_JOB_ID" \
job_config.nml
# Run simulation
mpirun -n 64 ./simulation job_config.nmlDocker Configuration:
FROM scientific/base
COPY base_config.nml /app/
WORKDIR /app
# Configure at runtime
ENTRYPOINT ["sh", "-c", "f90nml base_config.nml -g runtime -v threads=$THREADS -g io -v data_dir=$DATA_DIR config.nml && ./simulation config.nml"]CI/CD Pipeline:
# .github/workflows/test.yml
steps:
- name: Generate test configurations
run: |
f90nml base.nml -g test -v mode=fast -g io -v output_dir=./test_output fast_test.nml
f90nml base.nml -g test -v mode=thorough -g io -v output_dir=./full_test thorough_test.nml
- name: Run tests
run: |
./run_tests.sh fast_test.nml
./run_tests.sh thorough_test.nmlConfiguration Templating:
# generate_config.sh
#!/bin/bash
TEMPLATE="template.nml"
OUTPUT="$1"
ENVIRONMENT="$2"
case "$ENVIRONMENT" in
"production")
f90nml "$TEMPLATE" \
-g runtime -v debug=.false. \
-g io -v output_dir="/data/production" \
-g performance -v optimization=high \
"$OUTPUT"
;;
"development")
f90nml "$TEMPLATE" \
-g runtime -v debug=.true. \
-g io -v output_dir="/tmp/dev" \
-g performance -v optimization=none \
"$OUTPUT"
;;
esacThe command line tool uses standard exit codes for automation:
YAML format support requires the PyYAML library:
# Install with YAML support
pip install f90nml[yaml]
# Use YAML format
f90nml config.nml -f yaml > config.yaml
f90nml config.yaml -f nml > config.nmlWithout PyYAML installed:
f90nml config.nml -f yaml
# Output: f90nml: error: YAML support requires PyYAML library
# Exit code: 1Install with Tessl CLI
npx tessl i tessl/pypi-f90nml