Skip to content

BUG: fix Heasarch.locate_data returning empty rows #3275

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

Merged
merged 4 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ heasarc
^^^^^^^

- Add support for astropy.table.Row in Heasarc.download_data and Heasarc.locate_data. [#3270]
- Heasarc.locate_data returns empty rows with an error in the error_message column if there are
no data associated with that row rather than filtering it out. [#3275]


Infrastructure, Utility and Other Changes and Additions
Expand Down
13 changes: 11 additions & 2 deletions astroquery/heasarc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,13 @@
session=self._session
)
dl_result = query.execute().to_table()
dl_result = dl_result[dl_result['content_type'] == 'directory']
dl_result = dl_result[['ID', 'access_url', 'content_length']]
# include rows that have directory links (i.e. data) and those
# that report errors (usually means there are no data products)
dl_result = dl_result[np.ma.mask_or(

Check warning on line 529 in astroquery/heasarc/core.py

View check run for this annotation

Codecov / codecov/patch

astroquery/heasarc/core.py#L529

Added line #L529 was not covered by tests
dl_result['content_type'] == 'directory',
dl_result['error_message'] != ''
)]
dl_result = dl_result[['ID', 'access_url', 'content_length', 'error_message']]

Check warning on line 533 in astroquery/heasarc/core.py

View check run for this annotation

Codecov / codecov/patch

astroquery/heasarc/core.py#L533

Added line #L533 was not covered by tests

# add sciserver and s3 columns
newcol = [
Expand Down Expand Up @@ -622,6 +627,10 @@
'`~locate_data` first'
)

# remove rows that dont have data, if any
if 'error_message' in links.colnames:
links = links[links['error_message'] == '']

if host == 'heasarc':

log.info('Downloading data from the HEASARC ...')
Expand Down
22 changes: 22 additions & 0 deletions astroquery/heasarc/tests/test_heasarc.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,28 @@ def test_download_data__table_row():
assert os.path.exists(f'{downloaddir}/data/file.txt')


def test_download_data__exclude_rows_with_errors():
with tempfile.TemporaryDirectory() as tmpdir:
datadir = f'{tmpdir}/data'
downloaddir = f'{tmpdir}/download'
os.makedirs(datadir, exist_ok=True)
with open(f'{datadir}/file.txt', 'w') as fp:
fp.write('data')
# include both a file and a directory
tab = Table({
'sciserver': [f'{tmpdir}/data/file.txt', f'{tmpdir}/data'],
'error_message': ['', 'Error']
})
# The patch is to avoid the test that we are on sciserver
with patch('os.path.exists') as exists:
exists.return_value = True
Heasarc.download_data(tab, host="sciserver", location=downloaddir)
assert os.path.exists(f'{downloaddir}/file.txt')
# data/ should be excluded because it has an error
assert not os.path.exists(f'{downloaddir}/data')
assert not os.path.exists(f'{downloaddir}/data/file.txt')


# S3 mock tests
s3_bucket = "nasa-heasarc"
s3_key1 = "some/location/file1.txt"
Expand Down
Loading