Skip to main content

Fortran 90 namelist parser

Project description

A Python module for parsing Fortran namelist files

About f90nml

The f90nml module takes a Fortran 90 namelist file and parses it into a Python dict of namelist groups, each containing a dict of its variables. Fortran data types are converted to equivalent Python types.

Usage

To read a Fortran namelist file as a dict, use the read() method:

nml = f90nml.read(nml_filename)

To save a dict as a Fortran namelist file, use the write() method:

nml.write(output_filename)

The write() method will abort if the output file already exists.

To patch an existing file against a dict, use the patch() method:

nml.patch(input_filename, nml_patch, output_filename)

Namelist fields in input_filename will be checked against nml_patch and updated with new values. Fields in nml_patch which do not exist in input_filename will be appended to the namelist groups. The function returns the updated namelist dict and saves the patched namelist file to output_filename.

The patch() method currently does not allow patching of vectors or user types (%), but may be included in the future.

Additional Features

Derived types

Additional NmlDicts are created to traverse user-defined types. For example, if you want to access z in the following namelist:

&dtype_nml
   x%y%z = 1
/

then z can be accessed in the equivalent namelist nml by typing

z = nml['dtype_nml']['x']['y']['z']

Overwriting an existing file

To overwrite an existing file when using the write method, use the force flag:

nml.write(nml_filename, force=True)

Notes

The read method produces an NmlDict, which behaves as a dict with case-insensitive keys, due to the case insensitivity of Fortran. This implementation is currently not a true case-insensitive dict, and is only intended to accommodate individual references and assignments.

In a Fortran executable, the data types of values in the namelist files are set by the corresponding variables within the program, and cannot in general be determined from the namelist file alone. Therefore, f90nml only makes an approximate guess about its data type.

The following namelist features are currently not supported:

  • Multidimensional vector assignment (v(:,:) = 1, 2, 3, 4)

  • Upcast vector elements if components differ (x(i) = 1, x(j) = 2.0)

  • stdin/stdout support (?, ?=)

Licensing

f90nml is distributed under the Apache 2.0 License.

Contact

Marshall Ward <python@marshallward.org>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

f90nml-0.10.1.tar.gz (19.7 kB view details)

Uploaded Source

File details

Details for the file f90nml-0.10.1.tar.gz.

File metadata

  • Download URL: f90nml-0.10.1.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for f90nml-0.10.1.tar.gz
Algorithm Hash digest
SHA256 32ef98d15b20d41f6b2b16c2e1b9ade52a1313c8037ae0d679115d85f0e6f5af
MD5 1f2c5adec1172bfee34f1c9135fa9f0f
BLAKE2b-256 eb303a941102596e9d8be2508b3a05eb21c57c78b7d6cf452e1ce4804da8a48c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page