Skip to content

Commit 23b19c6

Browse files
committed
revised lookup strategy: try non-regex first
bugfix; test is WIP
1 parent 0419d34 commit 23b19c6

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

astroquery/linelists/cdms/core.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
from astroquery.utils import async_to_sync
1111
# import configurable items declared in __init__.py
1212
from astroquery.linelists.cdms import conf
13-
from astroquery.jplspec import lookup_table
1413
from astroquery.exceptions import InvalidQueryError, EmptyResponseError
1514

15+
import re
1616

1717
__all__ = ['CDMS', 'CDMSClass']
1818

@@ -129,11 +129,11 @@ def query_lines_async(self, min_frequency, max_frequency, *,
129129
# escape parentheses in molecule names if needed
130130
# (assumes _no_ escapes done; if you give 'XY\(ZG)', i.e.,
131131
# escape one and not the other, this won't work)
132-
if re.search("[()]", molecule):
133-
if len(re.findall(r'\(', molecule)) != len(re.findall(r'(', molecule)):
134-
molecule = re.sub(r'(', r'\(', molecule)
135-
if len(re.findall(r'\)', molecule)) != len(re.findall(r')', molecule))
136-
molecule = re.sub(r')', r'\)', molecule)
132+
# if re.search("[()]", molecule):
133+
# if len(re.findall(r'\(', molecule)) != len(re.findall(r'(', molecule)):
134+
# molecule = re.sub(r'(', r'\(', molecule)
135+
# if len(re.findall(r'\)', molecule)) != len(re.findall(r')', molecule))
136+
# molecule = re.sub(r')', r'\)', molecule)
137137
if parse_name_locally:
138138
self.lookup_ids = build_lookup()
139139
luts = self.lookup_ids.find(molecule, flags)
@@ -227,7 +227,7 @@ def _parse_result(self, response, verbose=False):
227227
'ELO': 38,
228228
'GUP': 48,
229229
'TAG': 51,
230-
'QNFMT': 57,
230+
'QNFMT': 58,
231231
'Ju': 61,
232232
'Ku': 63,
233233
'vu': 65,
@@ -314,12 +314,48 @@ def tryfloat(x):
314314
CDMS = CDMSClass()
315315

316316

317+
class Lookuptable(dict):
318+
319+
def find(self, st, flags):
320+
"""
321+
Search dictionary keys for a regex match to string s
322+
323+
Parameters
324+
----------
325+
s : str
326+
String to compile as a regular expression
327+
Can be entered non-specific for broader results
328+
('H2O' yields 'H2O' but will also yield 'HCCCH2OD')
329+
or as the specific desired regular expression for
330+
catered results, for example: ('H20$' yields only 'H2O')
331+
332+
flags : int
333+
Regular expression flags.
334+
335+
Returns
336+
-------
337+
The list of values corresponding to the matches
338+
339+
"""
340+
341+
R = re.compile(st, flags)
342+
343+
out = {}
344+
345+
for kk, vv in self.items():
346+
match = (st in kk) or R.search(str(kk))
347+
if match:
348+
out[kk] = vv
349+
350+
return out
351+
352+
317353
def build_lookup():
318354

319355
result = CDMS.get_species_table()
320356
keys = list(result[1][:]) # convert NAME column to list
321357
values = list(result[0][:]) # convert TAG column to list
322358
dictionary = dict(zip(keys, values)) # make k,v dictionary
323-
lookuptable = lookup_table.Lookuptable(dictionary) # apply the class above
359+
lookuptable = Lookuptable(dictionary) # apply the class above
324360

325361
return lookuptable

astroquery/linelists/cdms/tests/test_cdms_remote.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ def test_remote_regex():
5353
'TAG', 'QNFMT', 'Ju', 'Jl', "vu", "vl", "Ku", "Kl", "F", "name"])
5454

5555
assert set(tbl['name']) == {'H2CN', 'HC-13-N, v=0'}
56+
57+
58+
@pytest.mark.remote_data
59+
def test_2375():
60+
pass

0 commit comments

Comments
 (0)