mfouesneau / pyphot

suite to deal with passband photometry
https://mfouesneau.github.io/pyphot/
MIT License
57 stars 18 forks source link

Error when calling load_all_filters #37

Closed sundarjhu closed 2 years ago

sundarjhu commented 2 years ago

The following code results in an AttributeError:

from pyphot import sandbox as pyphot
a = pyphot.get_Library()
f = a.load_all_filters()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/pyphot/sandbox.py", line 1480, in load_all_filters
    filters = [s._load_filter(fname, interp=interp, lamb=lamb)
  File "/usr/local/lib/python3.9/site-packages/pyphot/sandbox.py", line 1480, in <listcomp>
    filters = [s._load_filter(fname, interp=interp, lamb=lamb)
  File "/usr/local/lib/python3.9/site-packages/pyphot/sandbox.py", line 1433, in _load_filter
    fnode = ftab.get_node('/filters/' + fname)
AttributeError: 'NoneType' object has no attribute 'get_node'

The reason seems to be that the hdf attribute is set to NoneType when instantiating UnitHDF_Library:

type(a.hdf)
<class 'NoneType'>

As a workaround, I've used the following successfully:

f = a.load_all_filters(a.get_library_content())
f = a.load_all_filters(a.content())

I'm probably missing something when instantiating the UnitHDF_Library, please let me know. Thanks!

mfouesneau commented 2 years ago

a = pyphot.UnitHDF_Library() creates an empty library in memory. Hence no filters there. (One could make a better error message) Is this what you want to do?

sundarjhu commented 2 years ago

Sorry about that, I pasted the wrong command into the first line, it should be a = pyphot.get_library(). I've fixed that in the original post.

mfouesneau commented 2 years ago

I find a weird bug that I did not imagine but makes sense

with lib:
    print(lib.hdf)    # works fine
    for fname in lib.content:
          print(lib.hdf)     # returns None

Because lib.content calls again the context of the library, it also closes the file, which is then not reopened.

mfouesneau commented 2 years ago

9a1a88f should fix this. Could you test it on your side?

sundarjhu commented 2 years ago

Thank you very much! I'm using Python 3.9.0, and get_library works with the latest sandbox.py commit.

mfouesneau commented 2 years ago

Closing then