def readOpsim(opsimfile, constraint=None, dbcols=None): # Read opsim database. opsdb = OpsimDatabase(opsimfile) if dbcols is None: dbcols = [] colmap = getColMap(opsdb) reqcols = [ colmap['mjd'], colmap['ra'], colmap['dec'], colmap['filter'], colmap['exptime'], colmap['seeingEff'], colmap['seeingGeom'], colmap['fiveSigmaDepth'] ] degreesIn = colmap['raDecDeg'] for col in reqcols: if col not in dbcols: dbcols.append(col) simdata = opsdb.fetchMetricData(dbcols, sqlconstraint=constraint) opsdb.close() print("Queried data from opsim %s, fetched %d visits." % (opsimfile, len(simdata))) simdata = fixObsData(simdata, degreesIn=degreesIn) return simdata, colmap
def readOpsim(opsimfile, constraint=None, footprint='camera', dbcols=None): """Read the opsim database. Parameters ---------- opsimfile: str Name (& path) of the opsim database file. constraint: str, opt Optional SQL constraint (minus 'where') on the opsim data to read from db. Default is None. footprint: str, opt Footprint option for the final matching of object against OpSim FOV. Default 'camera' means that 'rotSkyPos' must be fetched from the db. Any other value will not require rotSkyPos. dbcols: list of str, opt List of additional columns to query from the db and add to the output observations. Default None. Returns ------- np.ndarray, dictionary The OpSim data read from the database, and the dictionary mapping the column names to the data. """ # Read opsim database. opsdb = OpsimDatabase(opsimfile) colmap = getColMap(opsdb) if 'rotSkyPos' not in colmap: colmap['rotSkyPos'] = 'rotSkyPos' # Set the minimum required columns. min_cols = [ colmap['mjd'], colmap['night'], colmap['ra'], colmap['dec'], colmap['filter'], colmap['exptime'], colmap['seeingGeom'], colmap['fiveSigmaDepth'] ] if footprint == 'camera': min_cols.append(colmap['rotSkyPos']) if dbcols is not None: min_cols += dbcols min_cols = list(set(min_cols)) # Check if these minimum required columns are in the database. simdata = opsdb.query_columns(tablename=opsdb.defaultTable, colnames=min_cols, sqlconstraint=constraint, numLimit=1) # If that was successful, there are some additional columns that can be useful: more_cols = [colmap['rotSkyPos'], colmap['seeingEff'], 'solarElong'] failed_cols = [] for col in more_cols: try: simdata = opsdb.query_columns(tablename=opsdb.defaultTable, colnames=[col], sqlconstraint=constraint, numLimit=1) except ValueError: failed_cols.append(col) for col in failed_cols: more_cols.remove(col) cols = min_cols + more_cols cols = list(set(cols)) logging.info('Querying for columns:\n %s' % (cols)) # Go ahead and query for all of the observations. simdata = opsdb.fetchMetricData(cols, sqlconstraint=constraint) opsdb.close() logging.info("Queried data from opsim %s, fetched %d visits." % (opsimfile, len(simdata))) return simdata, colmap