Skip to content

Commit a5da0d0

Browse files
authored
Merge pull request #616 from keflavich/issue616
Astroquery.splatalogue query not respecting "exclude" keyword(?)
2 parents 7f28fd0 + 122ed1f commit a5da0d0

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

astroquery/splatalogue/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
:Originally contributed by:
99
10-
Magnus Vilehlm Persson ([email protected])
10+
Magnus Vilhelm Persson ([email protected])
1111
"""
1212
from astropy import config as _config
1313

astroquery/splatalogue/core.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import warnings
99
from astropy.io import ascii
1010
from astropy import units as u
11+
from astropy import log
1112
from ..query import BaseQuery
1213
from ..utils import async_to_sync
1314
from ..utils.docstr_chompers import prepend_docstr_noreturns
@@ -129,7 +130,8 @@ def _parse_kwargs(self, min_frequency=None, max_frequency=None,
129130
show_upper_degeneracy=None, show_molecule_tag=None,
130131
show_qn_code=None, show_lovas_labref=None,
131132
show_lovas_obsref=None, show_orderedfreq_only=None,
132-
show_nrao_recommended=None):
133+
show_nrao_recommended=None,
134+
parse_chemistry_locally=False):
133135
"""
134136
The Splatalogue service returns lines with rest frequencies in the
135137
range [min_frequency, max_frequency].
@@ -164,7 +166,9 @@ def _parse_kwargs(self, min_frequency=None, max_frequency=None,
164166
165167
``' H2CO '`` - Just 1 species, H2CO. The spaces prevent including
166168
others.
167-
169+
parse_chemistry_locally : bool
170+
Attempt to determine the species ID #'s locally before sending the
171+
query? This will prevent queries that have no matching species.
168172
chem_re_flags : int
169173
See the `re` module
170174
energy_min : `None` or float
@@ -185,7 +189,9 @@ def _parse_kwargs(self, min_frequency=None, max_frequency=None,
185189
exclude : list
186190
Types of lines to exclude. Default is:
187191
(``'potential'``, ``'atmospheric'``, ``'probable'``)
188-
Can also exclude ``'known'``
192+
Can also exclude ``'known'``.
193+
To exclude nothing, use 'none', not the python object None, since
194+
the latter is meant to indicate 'leave as default'
189195
only_NRAO_recommended : bool
190196
Show only NRAO recommended species?
191197
line_lists : list
@@ -264,10 +270,13 @@ def _parse_kwargs(self, min_frequency=None, max_frequency=None,
264270
# include all
265271
payload['sid[]'] = []
266272
elif chemical_name is not None:
267-
species_ids = self.get_species_ids(chemical_name, chem_re_flags)
268-
if len(species_ids) == 0:
269-
raise ValueError("No matching chemical species found.")
270-
payload['sid[]'] = list(species_ids.values())
273+
if parse_chemistry_locally:
274+
species_ids = self.get_species_ids(chemical_name, chem_re_flags)
275+
if len(species_ids) == 0:
276+
raise ValueError("No matching chemical species found.")
277+
payload['sid[]'] = list(species_ids.values())
278+
else:
279+
payload['chemical_name'] = chemical_name
271280

272281
if energy_min is not None:
273282
payload['energy_range_from'] = float(energy_min)
@@ -291,7 +300,12 @@ def _parse_kwargs(self, min_frequency=None, max_frequency=None,
291300
raise ValueError("Invalid version specified. Allowed versions "
292301
"are {vers}".format(vers=str(self.versions)))
293302

294-
if exclude is not None:
303+
if exclude == 'none':
304+
for e in ('potential', 'atmospheric', 'probable', 'known'):
305+
# Setting a keyword value to 'None' removes it (see query_lines_async)
306+
log.debug("Setting no_{0} to None".format(e))
307+
payload['no_' + e] = None
308+
elif exclude is not None:
295309
for e in exclude:
296310
payload['no_' + e] = 'no_' + e
297311

@@ -383,6 +397,9 @@ def query_lines_async(self, min_frequency=None, max_frequency=None,
383397
max_frequency=max_frequency,
384398
**kwargs))
385399

400+
# Add an extra step: sometimes, need to REMOVE keywords
401+
data_payload = {k:v for k,v in data_payload.items() if v is not None}
402+
386403
if get_query_payload:
387404
return data_payload
388405

astroquery/splatalogue/tests/test_splatalogue.py

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from ...utils.testing_tools import MockResponse
44
from astropy import units as u
55
from astropy.tests.helper import pytest, remote_data
6+
from astropy.extern.six.moves import urllib_parse
67
import requests
78
import os
89

@@ -106,13 +107,53 @@ def test_band_crashorno():
106107
assert exc.value.args[0] == "Invalid frequency band."
107108

108109

109-
# regression test : version selection should work
110-
# Unfortunately, it looks like version1 = version2 on the web page now, so this
111-
# may no longer be a valid test
110+
#Upstream changed: there is no distinction between versions for this molecule
111+
# # regression test : version selection should work
112+
# # Unfortunately, it looks like version1 = version2 on the web page now, so this
113+
# # may no longer be a valid test
114+
# @remote_data
115+
# def test_version_selection():
116+
# results = splatalogue.Splatalogue.query_lines(
117+
# min_frequency= 703*u.GHz,
118+
# max_frequency=706*u.GHz,
119+
# chemical_name='Acetaldehyde',
120+
# version='v1.0'
121+
# )
122+
# assert len(results)==1
123+
124+
def test_exclude(patch_post):
125+
# regression test for issue 616
126+
d = splatalogue.Splatalogue.query_lines_async(114 * u.GHz, 116 * u.GHz,
127+
chemical_name=' CO ',
128+
exclude=None,
129+
get_query_payload=True)
130+
131+
exclusions = {'no_atmospheric': 'no_atmospheric',
132+
'no_potential': 'no_potential',
133+
'no_probable': 'no_probable',}
134+
135+
for k,v in exclusions.items():
136+
assert d[k] == v
137+
138+
d = splatalogue.Splatalogue.query_lines_async(114 * u.GHz, 116 * u.GHz,
139+
chemical_name=' CO ',
140+
exclude='none',
141+
get_query_payload=True)
142+
143+
for k,v in exclusions.items():
144+
assert k not in d
145+
146+
for k in d:
147+
assert k[:3] != 'no_'
148+
112149
@remote_data
113-
def test_version_selection():
150+
def test_exclude_remote():
151+
# regression test for issue 616
152+
# only entry should be D213CO Formaldehyde 351.96064 3.9e-06 -- -- 6(2,4)-5(2,3) -2.0065 ... 0.0 -2.94058 -- 44.097 63.44616 55.83714 80.33772 CDMS
114153
results = splatalogue.Splatalogue.query_lines(
115-
min_frequency=703 * u.GHz, max_frequency=706 * u.GHz,
116-
chemical_name='Acetaldehyde', version='v1.0')
117-
118-
assert len(results) == 133
154+
min_frequency=351.9*u.GHz,
155+
max_frequency=352.*u.GHz,
156+
chemical_name='Formaldehyde',
157+
exclude='none',
158+
)
159+
assert len(results) >= 1

0 commit comments

Comments
 (0)