Skip to content

Commit 16572a1

Browse files
committed
query the NASA exoplanet archive using their API
1 parent 3a21e51 commit 16572a1

File tree

3 files changed

+92
-61
lines changed

3 files changed

+92
-61
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,23 @@ _build
2424
docs/api
2525
docs/_build
2626

27+
# Environments
28+
.env
29+
.venv
30+
env/
31+
venv/
32+
ENV/
33+
env.bak/
34+
venv.bak/
35+
2736
# Eclipse editor project files
2837
.project
2938
.pydevproject
3039
.settings
3140

41+
# VScode editor project files
42+
.vscode/*
43+
3244
# Pycharm editor project files
3345
.idea
3446

astroquery/nasa_exoplanet_archive/nasa_exoplanet_archive.py

+80-33
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,33 @@
88
from astropy.table import QTable
99
from astropy.coordinates import SkyCoord
1010
import astropy.units as u
11+
from urllib.parse import quote
1112

1213
__all__ = ['NasaExoplanetArchive']
1314

14-
EXOPLANETS_CSV_URL = ('http://exoplanetarchive.ipac.caltech.edu/cgi-bin/'
15-
'nstedAPI/nph-nstedAPI?table=exoplanets')
15+
EXOPLANETS_CSV_URL = ('http://exoplanetarchive.ipac.caltech.edu/cgi-bin/nstedAPI/nph-nstedAPI?table=exoplanets')
1616
TIME_ATTRS = {'rowupdate': 'iso'}
1717
BOOL_ATTRS = ('pl_kepflag', 'pl_ttvflag', 'pl_k2flag', 'st_massblend',
1818
'st_optmagblend', 'st_radblend', 'st_teffblend')
1919

2020

2121
class NasaExoplanetArchiveClass(object):
2222
"""
23-
Exoplanet Archive querying object. Use the ``get_confirmed_planets_table``
24-
or ``query_planet`` methods to get information about exoplanets via the NASA
25-
Exoplanet Archive.
23+
Querying object for `NExScI Exoplanet Archive Confirmed Planets archive
24+
<http://exoplanetarchive.ipac.caltech.edu/index.html>`_
25+
26+
The Exoplanet Archive table returns lots of columns of data. A full
27+
description of the columns can be found `here
28+
<https://exoplanetarchive.ipac.caltech.edu/docs/API_exoplanet_columns.html>`_
29+
30+
Use the ``query_planet`` or ``query_star`` methods to get information
31+
about exoplanets via the Archive.
2632
"""
2733
def __init__(self):
2834
self._param_units = None
2935
self._table = None
36+
self._cache_planet = None
37+
self._cache_star = None
3038

3139
@property
3240
def param_units(self):
@@ -38,18 +46,16 @@ def param_units(self):
3846

3947
return self._param_units
4048

41-
def get_confirmed_planets_table(self, cache=True, show_progress=True,
42-
table_path=None, all_columns=False):
49+
def query_planet(self, planet_name, cache=True, show_progress=True,
50+
table_path=None, all_columns=False):
4351
"""
44-
Download (and optionally cache) the `NExScI Exoplanet Archive Confirmed
45-
Planets table <http://exoplanetarchive.ipac.caltech.edu/index.html>`_.
46-
47-
The Exoplanet Archive table returns lots of columns of data. A full
48-
description of the columns can be found `here
49-
<https://exoplanetarchive.ipac.caltech.edu/docs/API_exoplanet_columns.html>`_
52+
Download (and optionally cache) the NASA Exoplanet
53+
table for a certain planet.
5054
5155
Parameters
5256
----------
57+
planet_name : str
58+
Name of planet
5359
cache : bool (optional)
5460
Cache exoplanet table to local astropy cache? Default is `True`.
5561
show_progress : bool (optional)
@@ -65,27 +71,26 @@ def get_confirmed_planets_table(self, cache=True, show_progress=True,
6571
Returns
6672
-------
6773
table : `~astropy.table.QTable`
68-
Table of exoplanet properties.
74+
Table of one exoplanet's properties.
6975
"""
70-
if self._table is None or not cache:
76+
77+
planet_name = '\'' + planet_name + '\''
78+
EXOPLANET_URL_PLANET = EXOPLANETS_CSV_URL + '&where=pl_name=' + quote(planet_name)
79+
80+
if self._table is not None and self._cache_planet is planet_name and cache:
81+
pass
82+
83+
else:
7184
if table_path is None:
72-
exoplanets_url = EXOPLANETS_CSV_URL
85+
exoplanets_url = EXOPLANET_URL_PLANET
7386
if all_columns:
74-
exoplanets_url = EXOPLANETS_CSV_URL + '&select=*'
87+
exoplanets_url = EXOPLANET_URL_PLANET + '&select=*'
7588

7689
table_path = download_file(exoplanets_url, cache=cache,
7790
show_progress=show_progress,
7891
timeout=120)
7992
exoplanets_table = ascii.read(table_path)
8093

81-
# Store column of lowercase names for indexing:
82-
lowercase_names = [host_name.lower().replace(' ', '') + letter
83-
for host_name, letter in
84-
zip(exoplanets_table['pl_hostname'].data,
85-
exoplanets_table['pl_letter'].data)]
86-
exoplanets_table['NAME_LOWERCASE'] = lowercase_names
87-
exoplanets_table.add_index('NAME_LOWERCASE')
88-
8994
# Create sky coordinate mixin column
9095
exoplanets_table['sky_coord'] = SkyCoord(ra=exoplanets_table['ra'] * u.deg,
9196
dec=exoplanets_table['dec'] * u.deg)
@@ -98,28 +103,70 @@ def get_confirmed_planets_table(self, cache=True, show_progress=True,
98103
exoplanets_table[col].unit = u.Unit(self.param_units[col])
99104

100105
self._table = QTable(exoplanets_table)
106+
self._cache_planet = planet_name
101107

102108
return self._table
103109

104-
def query_planet(self, planet_name, **kwargs):
110+
def query_star(self, host_name, cache=True, show_progress=True,
111+
table_path=None, all_columns=False):
105112
"""
106-
Get table of exoplanet properties.
113+
Download (and optionally cache) the NASA Exoplanet
114+
table for a certain planet.
107115
108116
Parameters
109117
----------
110-
planet_name : str
111-
Name of planet
112-
kwargs : dict (optional)
113-
Extra keyword arguments passed to ``get_confirmed_planets_table``.
118+
host_name : str
119+
Name of stellar system
120+
cache : bool (optional)
121+
Cache exoplanet table to local astropy cache? Default is `True`.
122+
show_progress : bool (optional)
123+
Show progress of exoplanet table download (if no cached copy is
124+
available). Default is `True`.
125+
table_path : str (optional)
126+
Path to a local table file. Default `None` will trigger a
127+
download of the table from the internet.
128+
all_columns : bool (optional)
129+
Return all available columns. The default returns only the
130+
columns in the default category at the link above.
114131
115132
Returns
116133
-------
117134
table : `~astropy.table.QTable`
118135
Table of one exoplanet's properties.
119136
"""
120137

121-
exoplanet_table = self.get_confirmed_planets_table(**kwargs)
122-
return exoplanet_table.loc[planet_name.strip().lower().replace(' ', '')]
138+
host_name = '\'' + host_name + '\''
139+
EXOPLANET_URL_PLANET = EXOPLANETS_CSV_URL + '&where=pl_hostname=' + quote(host_name)
140+
141+
if self._table is not None and self._cache_star is host_name and cache:
142+
pass
143+
144+
else:
145+
if table_path is None:
146+
exoplanets_url = EXOPLANET_URL_PLANET
147+
if all_columns:
148+
exoplanets_url = EXOPLANET_URL_PLANET + '&select=*'
149+
150+
table_path = download_file(exoplanets_url, cache=cache,
151+
show_progress=show_progress,
152+
timeout=120)
153+
exoplanets_table = ascii.read(table_path)
154+
155+
# Create sky coordinate mixin column
156+
exoplanets_table['sky_coord'] = SkyCoord(ra=exoplanets_table['ra'] * u.deg,
157+
dec=exoplanets_table['dec'] * u.deg)
158+
159+
# Assign units to columns where possible
160+
for col in exoplanets_table.colnames:
161+
if col in self.param_units:
162+
# Check that unit is implemented in this version of astropy
163+
if hasattr(u, self.param_units[col]):
164+
exoplanets_table[col].unit = u.Unit(self.param_units[col])
165+
166+
self._table = QTable(exoplanets_table)
167+
self._cache_star = host_name
168+
169+
return self._table
123170

124171

125172
NasaExoplanetArchive = NasaExoplanetArchiveClass()

astroquery/nasa_exoplanet_archive/tests/test_nasa_exoplanet_archive_remote.py

-28
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@
1313
'data', 'nasa_exoplanet_archive.csv')
1414

1515

16-
@remote_data
17-
def test_exoplanet_archive_table():
18-
table = NasaExoplanetArchive.get_confirmed_planets_table(cache=False)
19-
20-
# Check some planets are in the table
21-
expected_planets = ['HD 189733 b', 'Kepler-186 f', 'TRAPPIST-1 b',
22-
'HD 209458 b', 'Kepler-62 f', 'GJ 1214 b']
23-
24-
for planet in expected_planets:
25-
assert planet.lower().replace(" ", "") in table['NAME_LOWERCASE']
26-
27-
assert 'pl_trandep' not in table.colnames
28-
29-
3016
@remote_data
3117
def test_hd209458b_exoplanet_archive():
3218
# Testing intentionally un-stripped string, no spaced:
@@ -121,17 +107,3 @@ def test_hd209458b_exoplanet_archive_coords():
121107
sep = params['sky_coord'].separation(simbad_coords)
122108

123109
assert abs(sep) < 5 * u.arcsec
124-
125-
126-
@remote_data
127-
def test_exoplanet_archive_table_all_columns():
128-
table = NasaExoplanetArchive.get_confirmed_planets_table(cache=False, all_columns=True)
129-
130-
# Check some planets are in the table
131-
expected_planets = ['HD 189733 b', 'Kepler-186 f', 'TRAPPIST-1 b',
132-
'HD 209458 b', 'Kepler-62 f', 'GJ 1214 b']
133-
134-
for planet in expected_planets:
135-
assert planet.lower().replace(" ", "") in table['NAME_LOWERCASE']
136-
137-
assert 'pl_trandep' in table.colnames

0 commit comments

Comments
 (0)