Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read/write a Fortran namelist with Python?

I would like to know how to easily read and write values from a Fortran namelist file in Python.

like image 334
froggy Avatar asked Jun 06 '13 09:06

froggy


2 Answers

There is a module called f90nml which reads/writes Fortran namelists. With this module you can read a namelist into a nested Python dictionary:

import f90nml
nml = f90nml.read('sample.nml')

The values can be edited and written back to disk.

nml['config_nml']['steps'] = 432
nml.write('new_sample.nml')

The package can be installed with pip:

pip install f90nml

Source code is at https://github.com/marshallward/f90nml

like image 55
teekarna Avatar answered Oct 23 '22 18:10

teekarna


I wrote a python module to read/write Fortran namelist files because I couldn't find anything that quite worked for me: https://github.com/leifdenby/namelist_python

It:

  • Parses ints, floats, booleans, escaped strings and complex numbers.
  • Parses arrays in both index notation and inlined.
  • Can write namelist format files.
  • Has tab-completion and variable assignment in interactive console

I've written quite a few tests too, if there are any namelist files that don't parse correctly let me know and I'll have a look.

like image 28
leifdenby Avatar answered Oct 23 '22 17:10

leifdenby