From b8c3b1e8246e86e823a40f8275527271da13077a Mon Sep 17 00:00:00 2001 From: havok2063 Date: Sun, 8 Dec 2024 16:58:47 -0800 Subject: [PATCH] adding release filter for queries using the mjd cutoffs --- python/valis/db/queries.py | 24 +++++++++++++++++++++++- python/valis/routes/query.py | 6 +++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/python/valis/db/queries.py b/python/valis/db/queries.py index 1209a33..c76bc68 100644 --- a/python/valis/db/queries.py +++ b/python/valis/db/queries.py @@ -11,6 +11,7 @@ import astropy.units as u import deepmerge import peewee +from peewee import Case from astropy.coordinates import SkyCoord from sdssdb.peewee.sdss5db import apogee_drpdb as apo from sdssdb.peewee.sdss5db import boss_drp as boss @@ -26,7 +27,7 @@ def append_pipes(query: peewee.ModelSelect, table: str = 'stacked', - observed: bool = True) -> peewee.ModelSelect: + observed: bool = True, release: str = None) -> peewee.ModelSelect: """ Joins a query to the SDSSidToPipes table Joines an existing query to the SDSSidToPipes table and returns @@ -72,6 +73,27 @@ def append_pipes(query: peewee.ModelSelect, table: str = 'stacked', if observed: qq = qq.where(vizdb.SDSSidToPipes.has_been_observed == observed) + if release: + # get the release + rel = vizdb.Releases.select().where(vizdb.Releases.release==release).first() + + # if a release has no cutoff info, then force the cutoff to 0, query will return nothing + # to fix this we want mjd cutoffs by survey for all older releases + if not rel.mjd_cutoff_apo and not rel.mjd_cutoff_lco: + rel.mjd_cutoff_apo = 0 + rel.mjd_cutoff_lco = 0 + + # create the mjd cutoff condition + qq = qq.where(vizdb.SDSSidToPipes.mjd <= Case( + vizdb.SDSSidToPipes.obs, + ( + ('apo', rel.mjd_cutoff_apo), + ('lco', rel.mjd_cutoff_lco) + ), + None + ) + ) + return qq diff --git a/python/valis/routes/query.py b/python/valis/routes/query.py index 5a7d25c..476c049 100644 --- a/python/valis/routes/query.py +++ b/python/valis/routes/query.py @@ -107,7 +107,7 @@ async def main_search(self, body: SearchModel): query=query) # append query to pipes if query: - query = append_pipes(query, observed=body.observed) + query = append_pipes(query, observed=body.observed, release=self.release) # query iterator res = query.dicts().iterator() if query else [] @@ -125,7 +125,7 @@ async def cone_search(self, """ Perform a cone search """ res = cone_search(ra, dec, radius, units=units) - r = append_pipes(res, observed=observed) + r = append_pipes(res, observed=observed, release=self.release) # return sorted by distance # doing this here due to the append_pipes distinct return sorted(r.dicts().iterator(), key=lambda x: x['distance']) @@ -208,7 +208,7 @@ async def carton_program(self, with database.atomic(): database.execute_sql('SET LOCAL enable_seqscan=false;') query = carton_program_search(name, name_type) - query = append_pipes(query, observed=observed) + query = append_pipes(query, observed=observed, release=self.release) return query.dicts().iterator() @router.get('/obs', summary='Return targets with spectrum at observatory',