Skip to content

Commit d4266ea

Browse files
authored
Merge pull request #3275 from zoghbi-a/fix-3271
BUG: fix Heasarch.locate_data returning empty rows
2 parents f3f4426 + af2d6f4 commit d4266ea

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

Diff for: CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ heasarc
1818
^^^^^^^
1919

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

2224

2325
Infrastructure, Utility and Other Changes and Additions

Diff for: astroquery/heasarc/core.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,13 @@ def locate_data(self, query_result=None, catalog_name=None):
524524
session=self._session
525525
)
526526
dl_result = query.execute().to_table()
527-
dl_result = dl_result[dl_result['content_type'] == 'directory']
528-
dl_result = dl_result[['ID', 'access_url', 'content_length']]
527+
# include rows that have directory links (i.e. data) and those
528+
# that report errors (usually means there are no data products)
529+
dl_result = dl_result[np.ma.mask_or(
530+
dl_result['content_type'] == 'directory',
531+
dl_result['error_message'] != ''
532+
)]
533+
dl_result = dl_result[['ID', 'access_url', 'content_length', 'error_message']]
529534

530535
# add sciserver and s3 columns
531536
newcol = [
@@ -622,6 +627,10 @@ def download_data(self, links, host='heasarc', location='.'):
622627
'`~locate_data` first'
623628
)
624629

630+
# remove rows that dont have data, if any
631+
if 'error_message' in links.colnames:
632+
links = links[links['error_message'] == '']
633+
625634
if host == 'heasarc':
626635

627636
log.info('Downloading data from the HEASARC ...')

Diff for: astroquery/heasarc/tests/test_heasarc.py

+22
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,28 @@ def test_download_data__table_row():
365365
assert os.path.exists(f'{downloaddir}/data/file.txt')
366366

367367

368+
def test_download_data__exclude_rows_with_errors():
369+
with tempfile.TemporaryDirectory() as tmpdir:
370+
datadir = f'{tmpdir}/data'
371+
downloaddir = f'{tmpdir}/download'
372+
os.makedirs(datadir, exist_ok=True)
373+
with open(f'{datadir}/file.txt', 'w') as fp:
374+
fp.write('data')
375+
# include both a file and a directory
376+
tab = Table({
377+
'sciserver': [f'{tmpdir}/data/file.txt', f'{tmpdir}/data'],
378+
'error_message': ['', 'Error']
379+
})
380+
# The patch is to avoid the test that we are on sciserver
381+
with patch('os.path.exists') as exists:
382+
exists.return_value = True
383+
Heasarc.download_data(tab, host="sciserver", location=downloaddir)
384+
assert os.path.exists(f'{downloaddir}/file.txt')
385+
# data/ should be excluded because it has an error
386+
assert not os.path.exists(f'{downloaddir}/data')
387+
assert not os.path.exists(f'{downloaddir}/data/file.txt')
388+
389+
368390
# S3 mock tests
369391
s3_bucket = "nasa-heasarc"
370392
s3_key1 = "some/location/file1.txt"

0 commit comments

Comments
 (0)