Skip to content

Commit 5240817

Browse files
committed
missions implementation
1 parent b43c5bb commit 5240817

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

astroquery/mast/missions.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,19 @@ def filter_products(self, products, *, extension=None, **filters):
503503
warnings.warn(f"Column '{colname}' not found in product table.", InputWarning)
504504
continue
505505

506-
vals = [vals] if isinstance(vals, str) else vals
507-
col_mask = np.isin(products[colname], vals)
506+
col_data = products[colname]
507+
# If the column is an integer or float, treat differently
508+
if col_data.dtype.kind in 'if' and isinstance(vals, str):
509+
try:
510+
col_mask = utils.parse_numeric_product_filter(vals)(col_data)
511+
except ValueError:
512+
warnings.warn(f"Could not parse numeric filter '{vals}' for column '{colname}'.", InputWarning)
513+
continue
514+
else:
515+
if isinstance(vals, str):
516+
vals = [vals]
517+
col_mask = np.isin(col_data, vals)
518+
508519
filter_mask &= col_mask
509520

510521
# Return filtered products

astroquery/mast/utils.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
Miscellaneous functions used throughout the MAST module.
77
"""
88

9+
import re
910
import warnings
10-
import numpy as np
1111

12+
import numpy as np
1213
import requests
1314
import platform
14-
1515
from astropy.coordinates import SkyCoord
1616
from astropy.table import Table
1717
from astropy import units as u
@@ -345,3 +345,36 @@ def remove_duplicate_products(data_products, uri_key):
345345
f"Only returning {len(unique_products)} unique product(s).")
346346

347347
return unique_products
348+
349+
350+
def parse_numeric_product_filter(val):
351+
"""
352+
Parses a numeric product filter value and returns a function that can be used to filter
353+
a column of a product table.
354+
355+
Parameters
356+
----------
357+
val : str
358+
The filter value as a string. It can be a single number, a range in the form of "start..end",
359+
or a comparison operator followed by a number (e.g., ">=10", "<5", ">100.5", etc.).
360+
361+
Returns
362+
-------
363+
response : function
364+
A function that takes a column of a product table and returns a boolean mask indicating
365+
which rows satisfy the filter condition.
366+
"""
367+
range_pattern = re.compile(r'[+-]?(\d+(\.\d*)?|\.\d+)\.\.[+-]?(\d+(\.\d*)?|\.\d+)')
368+
if val.startswith('>='):
369+
return lambda col: col >= float(val[2:])
370+
elif val.startswith('<='):
371+
return lambda col: col <= float(val[2:])
372+
elif val.startswith('>'):
373+
return lambda col: col > float(val[1:])
374+
elif val.startswith('<'):
375+
return lambda col: col < float(val[1:])
376+
elif range_pattern.fullmatch(val):
377+
start, end = map(float, val.split('..'))
378+
return lambda col: (col >= start) & (col <= end)
379+
else:
380+
return lambda col: col == float(val)

0 commit comments

Comments
 (0)