AttributeError: 'HeasarcClass' object has no attribute '_last_catalog_name'

I roughly was following instructions from NuSTAR tutorial notebook to download some obsids in XMM and I am running into the following problem. Let me know if there was a better place to ask this python heasarc related question

from astroquery.heasarc import Heasarc

xmm_table = heasarc.query_region(catalog=‘xmmmaster’, spatial=‘all-sky’)

mask = np.where(xmm_table[‘obsid’]==‘0900280101’)
data = xmm_table[mask] ## table of length 1

links = Heasarc.locate_data(data)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[36], line 1
----> 1 links = Heasarc.locate_data(data)

File /opt/envs/sas/lib/python3.12/site-packages/astroquery/heasarc/core.py:511, in HeasarcClass.locate_data(self, query_result, catalog_name)
    506     raise ValueError('No __row column found in query_result. '
    507                      'query_result needs to be the output of '
    508                      'query_region or a subset.')
    510 if catalog_name is None:
--> 511     catalog_name = self._last_catalog_name
    512 if not (
    513     isinstance(catalog_name, str)
    514     and catalog_name in self.tap.tables.keys()
    515 ):
    516     raise ValueError(f'Unknown catalog name: {catalog_name}')

AttributeError: 'HeasarcClass' object has no attribute '_last_catalog_name'

The code you posted should work:

from astroquery.heasarc import Heasarc
xmm_table = Heasarc.query_region(catalog='xmmmaster', spatial='all-sky')
data = xmm_table[xmm_table['obsid']=='0900280101'] 
links = Heasarc.locate_data(data)

We recently made some updates that make the above done in a more efficient way. The updates are in the pre-released version of astroquery ((0.4.12)), which you can install by doing: !pip install astroquery --upgrade --pre in you notebook). You can then do:

from astroquery.heasarc import Heasarc
xmm_table = Heasarc.query_region(catalog='xmmmaster', column_filters={'obsid': '0900280101'})
links = Heasarc.locate_data(xmm_table)

# or with a list
xmm_table = Heasarc.query_region(catalog='xmmmaster', column_filters={'obsid': ['0606321501', '0606321601']})

Today after refreshing the server this works, but I have seen this error appear a few times already when I was doing the above call in a notebook with lots of other work. I will try to provide more details next time it appears if it does.

Thanks for reporting this - its quite an odd problem.

So you re-tried it this morning, without changing any versions of Astroquery, and it worked this time?

Are you using your own custom environment, or one of the built in ones?

This was all in sas environment but before I encountered it in the default one whatever it was. It’s usually as I said once I’ve been doing a lot of things and not starting “fresh”.

I will try to pin it down more, though I might not be needing to download things for a while.

The error can happen if multiple queries are made to different tables, or if locate_data is called before query_region. In that case, you may need to explicitly pass the catalog name to locate_data. So in the above case, it is:

links = Heasarc.locate_data(data, catalog_name='xmmmaster')

In the latest version of astroquery (0.4.12; not released yet), the error is clearer and tells you to pass catalog_name if it cannot figure it out.

Ok, I think this likely was the case!