-
Notifications
You must be signed in to change notification settings - Fork 51
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
Zero-indexed array indices are ignored #31
Comments
Further bugs downstream in
Amazing that this was never tested before... |
There seem to be more fundamental problems related to the starting index. Nearly everything in the current implementation assumes a one-indexed array. For example:
will create this array:
and save the output as
which could break if the Fortran array was not originally 1-indexed. We need to include explicit array bounds, and track the original indices of data, when reading indexable arrays. |
As of d1df701 this now appears to be fixed. |
Does this relate to #24 also? If I read a namelist like: a%b%c(2) = 2,
a%b%c(3) = 3, and then write it again, what will it print? |
It seems like this change breaks the ability to access the variables with the indices that are in the namelist? So, for my example above: import f90nml
n = f90nml.read('test.nml')
print(n)
print(n['nml_group']['a']['b']['c'][1])
print(n['nml_group']['a']['b']['c'][2]) The previous version would print
Where as the current version gives:
Additionally, if we now write to another format like JSON, we loose the indices: print(json.dumps(n,indent=4)) gives: {
"nml_group": {
"a": {
"b": {
"c": [
2,
3
]
}
}
}
} whereas the previous version would give: {
"nml_group": {
"a": {
"b": {
"c": [
null,
2,
3
]
}
}
}
} In my case, the indices in the file are significant, and I want to preserve them. Is there a way to have the old behavior be an option? |
I think a
whereas now it produces this:
which are both equivalent under Fortran -- assuming 1-indexing! If You're right that the Python output has changed, since I feel like the newer one is more in line with the Fortran representation, and ultimately produces safer output, e.g. But I just tested out a change to the default start index (using 1 instead of the first cited index of the vector) which restores the old behaviour, and I can add a property to the |
BTW I would probably use something like this:
to prevent index errors relative to negative indexing (e.g. However for Also, I would probably want to turn this sort of behaviour off by default! |
Re: #24 There's some deeper problems with entanglement between arrays, derived types, and lists of derived types, and too many little checks to accomodate all cases (even when they should not happen). Unfortuantely I think it will take a big refactoring to get #24 working well. |
reopening this ticket since it is causing issues with some use cases (see JSON output issue above). |
I've added a property
|
Thanks! I think that will work for me. I'll test it soon. |
Looks like I forgot to push this commit to github, tuturu... Anyway I hope it works, I will probably do a PyPI update once the documentation has been updated. |
Terrific! It works for me. Thanks! >>> import json
>>> from f90nml import Parser
>>> p = Parser()
>>> p.global_start_index = 1
>>> n = p.read('test.nml')
>>> print(json.dumps(n,indent=4))
{
"nml_group": {
"a": {
"b": {
"c": [
null,
2,
3
]
}
}
}
}
>>> print(n['nml_group']['a']['b']['c'][1])
2
>>> print(n['nml_group']['a']['b']['c'][2])
3 |
Finally documented, closing this! |
I'm encountering a similar issue, which I assume is related to this ticket. In [21]: %cat tmp/input.F90
&MYLIST
myarr(-1,-1:3) = 0 1 2 3
myarr(0,-1:3) = 4 5 6 7
myarr(1,-1:3) = 8 9 10 11
/
In [22]: nml = f90nml.read("tmp/input.F90")
In [23]: nml
Out[23]:
Namelist([('mylist',
Namelist([('myarr',
[[0, 4, 8, 6, 7],
[None, None, 9],
[None, None, 10],
[None, None, 11]])]))]) |
Thanks for reporting this. I can replicate it:
The starting index is correctly identified as (-1,-1) for all records, but it's incorrectly placing them. Something like this is happening at
and then [8, 9, 10, 11] is oddly placed correctly:
In other words, the index order is swapped for the first two record, but is correct for third record. I've confirmed at least this much, and it's happening in |
I have a somewhat simpler (and more damning) example. The following namelists:
returns very different outputs:
If the starting index is below 1, then the contents are somehow reversed, thus producing the odd output from the original example. This could be related to the |
The issue was - once again - an irresponsible use of ( There were in fact two completely unrelated problems:
I have pushed a fix which resolves these issues. I will do a PyPI after I have provided sufficient testing (perhaps your example case). |
Many thanks for the prompt investigation! |
If we have a zero-indexed array, e.g.
then it incorrectly interprets 0 as
None
(due to a bug inFIndex
) and uses the default value of 1. The above case fails because there are more than 3 values in the array and the iterator runs out of indices.The text was updated successfully, but these errors were encountered: