FortranNamelist

Package for parsing Fortran namelist, modified from OMFT namelist.


License
MIT
Install
pip install FortranNamelist==0.1.2

Documentation

FortranNamelist

FortranNamelist is a standalone python package/class for parsing Fortran/IDL namelist. It is modified from OMFIT namelist.

Features:

  • Support Fortran indexing
  • Support multi-dimensional arrays
  • Retain comments

NOTE: I haven't tested/cleaned all the functions. Please use at your own warranty.

Install

You can install the package using pip.

pip install FortranNamelist

Or from the GitHub

pip install git+https://github.com/zhucaoxiang/namelist

You can also install it by cloning the source code.

git clone https://github.com/zhucaoxiang/namelist.git
cd namelist
python setup.py install

Use

Here is some simple examples of using the package. There are more functions and you can have a view in the source code.

Parse Fortran namelist

By default, it will use Fortran indexing convention. There is not special meaning for negative index.

from FortranNamelist import *
ranged_nl = NamelistFile(input_string='''
                             &test
                                 entry(-2, -2) = -10
                                 entry(1,1) = 1
                                 entry(2,1:2) = 1, 2
                                 entry(3,1) = 1
                                 entry(3,2) = 2
                                 entry(3,3) = 3
                                 entry(4,1) = 4
                                 entry(5,1:2) = 1, 2
                                 entry(6:7,1) = 6, 7
                             /
                             ''')
print(ranged_nl['test']['entry'][3,1]==1)
print(ranged_nl['test']['entry'][-2,-2]==-10)

You can also read namelist from file.

ranged_nl = NamelistFile('path-to-file')

Edit namelist variables

The namelist variable can be directly accessed by using Fortran indexing style, like

ranged_nl['test']['entry'][-2,-2] = 1

Or they can be accessed by using dict keys, like

for key in ranged_nl['test']['entry'].data.keys():
    ranged_nl['test']['entry'].data[key] *= -1

Unfortunately, multi-dimensional arrays are not supported with array operations.

Write namelist to file

You can write the namelist to a file by using

ranged_nl.save() # default saved to ranged_nl.filename

or using

ranged_nl.saveas('path-to-new-file')

Use python indexing

You can also switch to python indexing convention

with python_environment(ranged_nl) :
    print(ranged_nl['test']['entry'][0,0])
    print(ranged_nl['test']['entry'][-1,-1])

Contact

You can report bugs on GitHub issues and propose improvements using pull request. For more information, please contact Dr. Caoxiang ZHU (caoxiangzhu[at]gmail.com).