|
6 | 6 | Miscellaneous functions used throughout the MAST module.
|
7 | 7 | """
|
8 | 8 |
|
| 9 | +import re |
9 | 10 | import warnings
|
10 |
| -import numpy as np |
11 | 11 |
|
| 12 | +import numpy as np |
12 | 13 | import requests
|
13 | 14 | import platform
|
14 |
| - |
15 | 15 | from astropy.coordinates import SkyCoord
|
16 | 16 | from astropy.table import Table
|
17 | 17 | from astropy import units as u
|
@@ -345,3 +345,36 @@ def remove_duplicate_products(data_products, uri_key):
|
345 | 345 | f"Only returning {len(unique_products)} unique product(s).")
|
346 | 346 |
|
347 | 347 | 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