Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved f90nml parsing error messaging #146

Open
emsellem opened this issue Oct 10, 2022 · 3 comments
Open

Improved f90nml parsing error messaging #146

emsellem opened this issue Oct 10, 2022 · 3 comments

Comments

@emsellem
Copy link

emsellem commented Oct 10, 2022

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: 
@marshallward
Copy link
Owner

marshallward commented Oct 10, 2022

Can you attach a copy of the namelist? Or an example which has the same issue?

Reporting a namelist error is a harder proposal, since the parser is not very mature and doesn't have great awareness of its state. (I think compiler people would call it a "hand parser"). We might be able to do something like report the problematic line, though.

@emsellem
Copy link
Author

Thanks for this: actually I think that the namelist is just not well formatted and has been wrongly written by the f90 programme. So it is actually probably not a f90nml problem. Before going further and wasting your time, I'll have a deeper look myself. Thanks again and apologies for the trouble!

@marshallward
Copy link
Owner

No trouble at all, and I agree with you and @cphyc (#148) that these assertions might be better replaced with proper exceptions.

@marshallward marshallward changed the title Problem when loading a namefile Improved f90nml parsing error messaging Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants