Skip to content

Improved f90nml parsing error messaging  #146

@emsellem

Description

@emsellem

Hi
I am trying to load a namefile from the simulation code Ramses (name = namelist.txt). It works most of the time but I have a run when this fails and gives the following message (see below).
This may be just that my namefile is wrongly formatted. Is there a way for me to easily read the namelist and see where the formatting may be wrong?

Apologies for the naive question.

Thanks for any tips that may help.

Eric

======================================
[within ipython]

nm = "namelist.txt"
f90nml.read(nm)


AssertionError                            Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 f90nml.read(nm)

File ~/.local/lib/python3.9/site-packages/f90nml/__init__.py:31, in read(nml_path)
     13 """Parse a Fortran namelist file and return its contents.
     14 
     15 File object usage:
   (...)
     28 >>> nml = parser.read(nml_file)
     29 """
     30 parser = Parser()
---> 31 return parser.read(nml_path)

File ~/.local/lib/python3.9/site-packages/f90nml/parser.py:281, in Parser.read(self, nml_fname, nml_patch_in, patch_fname)
    279 nml_file = open(nml_fname, 'r') if nml_is_path else nml_fname
    280 try:
--> 281     return self._readstream(nml_file, nml_patch)
    282 except StopIteration:
    283     raise ValueError('End-of-file reached before end of namelist.')

File ~/.local/lib/python3.9/site-packages/f90nml/parser.py:388, in Parser._readstream(self, nml_file, nml_patch_in)
    385 # Set the next active variable
    386 if self.token in ('=', '(', '%'):
--> 388     v_name, v_values = self._parse_variable(
    389         g_vars,
    390         patch_nml=grp_patch
    391     )
    393     if v_name in g_vars:
    394         v_prior_values = g_vars[v_name]

File ~/.local/lib/python3.9/site-packages/f90nml/parser.py:554, in Parser._parse_variable(self, parent, patch_nml)
    551 self._update_tokens()
    552 self._update_tokens()
--> 554 v_att, v_att_vals = self._parse_variable(
    555     v_parent,
    556     patch_nml=v_patch_nml
    557 )
    559 next_value = Namelist()
    560 next_value[v_att] = v_att_vals

File ~/.local/lib/python3.9/site-packages/f90nml/parser.py:566, in Parser._parse_variable(self, parent, patch_nml)
    561     self._append_value(v_values, next_value, v_idx)
    563 else:
    564     # Construct the variable array
--> 566     assert self.token == '='
    567     n_vals = None
    569     self._update_tokens()

AssertionError: 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions