Skip to content

Commit 4edd9ee

Browse files
authored
Merge pull request #3270 from zoghbi-a/heasarc-fix-3269
fix-3269: add support for rows in heasarc.download_data
2 parents 469aea4 + a4357f6 commit 4edd9ee

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ API changes
1414
Service fixes and enhancements
1515
------------------------------
1616

17+
heasarc
18+
^^^^^^^
19+
20+
- Add support for astropy.table.Row in Heasarc.download_data and Heasarc.locate_data. [#3270]
1721

1822

1923
Infrastructure, Utility and Other Changes and Additions

astroquery/heasarc/core.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import tarfile
77
import warnings
88
import numpy as np
9-
from astropy.table import Table
9+
from astropy.table import Table, Row
1010
from astropy import coordinates
1111
from astropy import units as u
1212
from astropy.utils.decorators import deprecated, deprecated_renamed_argument
@@ -470,7 +470,7 @@ def locate_data(self, query_result=None, catalog_name=None):
470470
471471
Parameters
472472
----------
473-
query_result : `astropy.table.Table`, optional
473+
query_result : `astropy.table.Table` or `astropy.table.Row`, optional
474474
A table that contain the search results. Typically as
475475
returned by query_region. If None, use the table from the
476476
most recent query_region call.
@@ -492,8 +492,14 @@ def locate_data(self, query_result=None, catalog_name=None):
492492
else:
493493
query_result = self._last_result
494494

495-
if not isinstance(query_result, Table):
496-
raise TypeError('query_result need to be an astropy.table.Table')
495+
if isinstance(query_result, Row):
496+
query_result = query_result.table[[query_result.index]]
497+
498+
elif not isinstance(query_result, Table):
499+
raise TypeError(
500+
'query_result need to be an astropy.table.Table or '
501+
'astropy.table.Row'
502+
)
497503

498504
# make sure we have a column __row
499505
if '__row' not in query_result.colnames:
@@ -582,7 +588,7 @@ def download_data(self, links, host='heasarc', location='.'):
582588
583589
Parameters
584590
----------
585-
links : `astropy.table.Table`
591+
links : `astropy.table.Table` or `astropy.table.Row`
586592
The result from locate_data
587593
host : str
588594
The data host. The options are: heasarc (default), sciserver, aws.
@@ -603,6 +609,9 @@ def download_data(self, links, host='heasarc', location='.'):
603609
if len(links) == 0:
604610
raise ValueError('Input links table is empty')
605611

612+
if isinstance(links, Row):
613+
links = links.table[[links.index]]
614+
606615
if host not in ['heasarc', 'sciserver', 'aws']:
607616
raise ValueError('host has to be one of heasarc, sciserver, aws')
608617

astroquery/heasarc/tests/test_heasarc.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,28 @@ def test_locate_data():
274274
Heasarc.locate_data()
275275

276276
with pytest.raises(
277-
TypeError, match="query_result need to be an astropy.table.Table"
277+
TypeError, match=(
278+
"query_result need to be an astropy.table.Table or astropy.table.Row"
279+
)
278280
):
279281
Heasarc.locate_data([1, 2])
280282

281283
with pytest.raises(ValueError, match="No __row column found"):
282284
Heasarc.locate_data(Table({"id": [1, 2, 3.0]}), catalog_name="xray")
283285

284286

287+
def test_locate_data_row():
288+
table = Table({"id": [1, 2, 3.0]})
289+
290+
# we cannot do full call as this is not remote,
291+
# but if we check that we error on __row not input type
292+
with pytest.raises(ValueError, match="No __row column found"):
293+
Heasarc.locate_data(table[0], catalog_name="xray")
294+
295+
with pytest.raises(ValueError, match="No __row column found"):
296+
Heasarc.locate_data(table[0:2], catalog_name="xray")
297+
298+
285299
def test_download_data__empty():
286300
with pytest.raises(ValueError, match="Input links table is empty"):
287301
Heasarc.download_data(Table())
@@ -332,6 +346,25 @@ def test_download_data__outside_sciserver():
332346
)
333347

334348

349+
def test_download_data__table_row():
350+
with tempfile.TemporaryDirectory() as tmpdir:
351+
datadir = f'{tmpdir}/data'
352+
downloaddir = f'{tmpdir}/download'
353+
os.makedirs(datadir, exist_ok=True)
354+
with open(f'{datadir}/file.txt', 'w') as fp:
355+
fp.write('data')
356+
# include both a file and a directory
357+
tab = Table({'sciserver': [f'{tmpdir}/data/file.txt', f'{tmpdir}/data']})
358+
# The patch is to avoid the test that we are on sciserver
359+
with patch('os.path.exists') as exists:
360+
exists.return_value = True
361+
Heasarc.download_data(tab[0], host="sciserver", location=downloaddir)
362+
Heasarc.download_data(tab[1], host="sciserver", location=downloaddir)
363+
assert os.path.exists(f'{downloaddir}/file.txt')
364+
assert os.path.exists(f'{downloaddir}/data')
365+
assert os.path.exists(f'{downloaddir}/data/file.txt')
366+
367+
335368
# S3 mock tests
336369
s3_bucket = "nasa-heasarc"
337370
s3_key1 = "some/location/file1.txt"

docs/heasarc/heasarc.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ The first gives the url to the data from the main heasarc server. The second giv
201201
the local path to the data on Sciserver. The last gives the S3 URI to the data in the cloud.
202202
You can specify where the data are to be downloaded using the ``location`` parameter.
203203

204-
To download the data, you can pass ``links`` table to `~astroquery.heasarc.HeasarcClass.download_data`,
204+
To download the data, you can pass ``links`` table (or row) to `~astroquery.heasarc.HeasarcClass.download_data`,
205205
specifying from where you want the data to be fetched by specifying the ``host`` parameter. By default,
206206
the data is fetched from the main HEASARC servers.
207207
The recommendation is to use different hosts depending on where your code is running:

0 commit comments

Comments
 (0)