diff --git a/python/valis/db/queries.py b/python/valis/db/queries.py index 0590682..2275d06 100644 --- a/python/valis/db/queries.py +++ b/python/valis/db/queries.py @@ -13,6 +13,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 @@ -28,7 +29,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 @@ -77,6 +78,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 866430c..cdb5171 100644 --- a/python/valis/routes/query.py +++ b/python/valis/routes/query.py @@ -115,7 +115,7 @@ async def main_search(self, body: SearchModel): # 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 [] @@ -133,7 +133,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'])