|
| 1 | +import numpy as np |
| 2 | +import hpgeom as hpg |
| 3 | + |
| 4 | +from astropy.table import Table |
| 5 | +import astropy.units as units |
| 6 | +from astropy.time import Time |
| 7 | +from astropy.coordinates import EarthLocation |
| 8 | + |
| 9 | +from . import decasu_globals |
| 10 | +from .utils import compute_visit_iqr_and_optics_scale |
| 11 | + |
| 12 | +try: |
| 13 | + import lsst.obs.lsst |
| 14 | + import lsst.sphgeom |
| 15 | + import psycopg |
| 16 | + lsst_imported = True |
| 17 | +except ImportError: |
| 18 | + lsst_imported = False |
| 19 | + |
| 20 | + |
| 21 | +class LsstWcsConsDbBuilder: |
| 22 | + """ |
| 23 | + Build a WCS table from the LSST Consolidated Database and get intersecting |
| 24 | + pixels. |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + config : `Configuration` |
| 29 | + decasu configuration object. |
| 30 | + dbfile : `str` |
| 31 | + Input database file. |
| 32 | + bands : `list` |
| 33 | + Bands to run. Empty list means use all. |
| 34 | + compute_pixels : `bool`, optional |
| 35 | + Compute pixels when rendering WCS? |
| 36 | + """ |
| 37 | + def __init__(self, config, dbstring, bands, compute_pixels=True): |
| 38 | + if not lsst_imported: |
| 39 | + raise RuntimeError("Cannot use LsstWcsConsDbBuilder without Rubin Science Pipelines.") |
| 40 | + |
| 41 | + self.config = config |
| 42 | + self.compute_pixels = compute_pixels |
| 43 | + |
| 44 | + query_string = ( |
| 45 | + "SELECT cvq.eff_time, cvq.psf_sigma, " |
| 46 | + "cvq.sky_bg, cvq.sky_noise, cvq.zero_point, " |
| 47 | + "cv.detector, cv.visit_id, cv.s_region, " |
| 48 | + "v.band, v.exp_time, v.exp_midpt_mjd, v.sky_rotation " |
| 49 | + "FROM cdb_LSSTCam.ccdvisit1_quicklook as cvq, cdb_LSSTCam.ccdvisit1 as cv, " |
| 50 | + "cdb_LSSTCam.visit1 as v " |
| 51 | + ) |
| 52 | + where_string = ( |
| 53 | + "WHERE cvq.ccdvisit_id=cv.ccdvisit_id and " |
| 54 | + "cv.visit_id=v.visit_id and " |
| 55 | + "detector<189 and cvq.zero_point is not null " |
| 56 | + ) |
| 57 | + |
| 58 | + if len(self.config.lsst_db_additional_selection) > 0: |
| 59 | + where_string = where_string + " and " + self.config.lsst_db_additional_selection |
| 60 | + |
| 61 | + if len(bands) > 0: |
| 62 | + where_string = where_string + " and v.band in (" + ",".join([f"'{band}'" for band in bands]) + ")" |
| 63 | + |
| 64 | + where_string = where_string + f" and v.exp_midpt_mjd >= {self.config.mjd_min}" |
| 65 | + where_string = where_string + f" and v.exp_midpt_mjd <= {self.config.mjd_max}" |
| 66 | + |
| 67 | + query_string = query_string + where_string + ";" |
| 68 | + |
| 69 | + with psycopg.Connection.connect(dbstring) as conn: |
| 70 | + cur = conn.execute(query_string) |
| 71 | + rows = cur.fetchall() |
| 72 | + |
| 73 | + db_table = Table( |
| 74 | + np.asarray( |
| 75 | + rows, |
| 76 | + dtype=[ |
| 77 | + ("eff_time", "f4"), |
| 78 | + ("psf_sigma", "f4"), |
| 79 | + ("sky_bg", "f4"), |
| 80 | + ("sky_noise", "f4"), |
| 81 | + ("zero_point", "f4"), |
| 82 | + ("detector", "i4"), |
| 83 | + ("visit_id", "i8"), |
| 84 | + ("s_region", "U200"), |
| 85 | + ("band", "U2"), |
| 86 | + ("exptime", "f4"), |
| 87 | + ("mjd", "f8"), |
| 88 | + ("sky_rotation", "f4"), |
| 89 | + ], |
| 90 | + ), |
| 91 | + ) |
| 92 | + |
| 93 | + if len(bands) == 0: |
| 94 | + self.bands = np.unique(db_table["band"]) |
| 95 | + else: |
| 96 | + self.bands = bands |
| 97 | + |
| 98 | + print(f"Found {len(db_table)} detector visits for {len(self.bands)} bands.") |
| 99 | + |
| 100 | + # Add extra columns. |
| 101 | + # Units of degrees. |
| 102 | + db_table["decasu_lst"] = np.zeros(len(db_table)) |
| 103 | + # Units of electrons. |
| 104 | + db_table["skyvar"] = db_table["sky_noise"]**2. |
| 105 | + # Units of arcsec. |
| 106 | + db_table[config.fwhm_field] = 2.355*config.arcsec_per_pix*db_table["psf_sigma"] |
| 107 | + |
| 108 | + print("Computing local sidereal time...") |
| 109 | + loc = EarthLocation(lat=config.latitude*units.degree, |
| 110 | + lon=config.longitude*units.degree, |
| 111 | + height=config.elevation*units.m) |
| 112 | + |
| 113 | + t = Time(db_table[config.mjd_field], format="mjd", location=loc) |
| 114 | + lst = t.sidereal_time("apparent") |
| 115 | + db_table["decasu_lst"][:] = lst.to_value(units.degree) |
| 116 | + |
| 117 | + # Compute a couple of additional psf quantities. |
| 118 | + db_table[f"{config.fwhm_field}_iqr"] = np.zeros(len(db_table)) |
| 119 | + db_table[f"{config.fwhm_field}_optics_scale"] = np.zeros(len(db_table)) |
| 120 | + |
| 121 | + print('Computing fwhm scaled properties...') |
| 122 | + compute_visit_iqr_and_optics_scale(self.config, db_table) |
| 123 | + |
| 124 | + instrument = lsst.obs.lsst.LsstCam() |
| 125 | + camera = instrument.getCamera() |
| 126 | + |
| 127 | + decasu_globals.table = db_table |
| 128 | + decasu_globals.lsst_camera = camera |
| 129 | + |
| 130 | + def __call__(self, row): |
| 131 | + """ |
| 132 | + Compute intersecting pixels for onw row. |
| 133 | +
|
| 134 | + Parameters |
| 135 | + ---------- |
| 136 | + row : `int` |
| 137 | + Row to compute intersecting pixels. |
| 138 | +
|
| 139 | + Returns |
| 140 | + ------- |
| 141 | + wcs : `int` |
| 142 | + Placeholder. |
| 143 | + pixels : `list` |
| 144 | + List of nside = `config.nside_run` intersecting pixels. |
| 145 | + Returned if compute_pixels is True in initialization. |
| 146 | + centers : `tuple` [`float`]] |
| 147 | + """ |
| 148 | + if (row % 10000) == 0: |
| 149 | + print("Working on WCS index %d" % (row)) |
| 150 | + |
| 151 | + # Link to global table. |
| 152 | + self.table = decasu_globals.table |
| 153 | + |
| 154 | + region_str = self.table["s_region"][row] |
| 155 | + |
| 156 | + region = lsst.sphgeom.Region.from_ivoa_pos("".join(region_str.split("ICRS")).upper()) |
| 157 | + centroid = lsst.sphgeom.LonLat(region.getCentroid()) |
| 158 | + center = [centroid.getLon().asDegrees(), centroid.getLat().asDegrees()] |
| 159 | + |
| 160 | + if self.compute_pixels: |
| 161 | + vertices = np.asarray([[v.x(), v.y(), v.z()] for v in region.getVertices()]) |
| 162 | + pixels = hpg.query_polygon_vec(self.config.nside_run, vertices, inclusive=True, fact=16) |
| 163 | + return 0, pixels, center |
| 164 | + else: |
| 165 | + return 0, center |
0 commit comments