specprodDB API
specprodDB
Tools for loading DESI data into databases.
In particular, this code loads the spectroscopic production database.
It does not include:
Pipeline processing status database.
The full imaging and targeting databases, but only the imaging and targeting associated with a spectroscopic production.
specprodDB.batch
Generate batch scripts for loading the database.
- specprodDB.batch.get_options()[source]
Parse command-line options.
- Returns:
The parsed options.
- Return type:
- specprodDB.batch.main()[source]
Entry point for command-line script.
- Returns:
An integer suitable for passing to
sys.exit().- Return type:
- specprodDB.batch.prepare_template(options)[source]
Convert command-line options to template inputs and format.
- Parameters:
options (
argparse.Namespace) – The parsed options.- Returns:
A dictionary mapping file name to contents.
- Return type:
specprodDB.coeff
Patch redrock template coefficients. This is meant to fix bad values in the
zpix and ztile tables in the coeff_0 … coeff_9 columns.
This affected fuji, guadalupe and iron, but not loa.
Details of the patch application SQL commands can be found in the file
data/coeff_patch.sql.
Operational Steps
Create patch FITS files for all affected specprod:
fuji,guadalupe,iron.Load all tables. As each table is loaded, move it to
coeff_patch. This isn’t especially efficient, but it avoids code complexity elsewhere. See thedata/coeff_patch.sqlfor details on how this is done.Apply the patch to guadalupe, be prepared to restore that schema if something goes wrong.
Apply the patch to all tables.
Make a dump and tape backup of
coeff_patch.Within
coeff_patchrename tables to match Data Lab names:desi_edr,desi_dr1.Make another dump and tape backup.
Copy to Data Lab and apply patches there.
- class specprodDB.coeff.ZpixPatch(*args: Any, **kwargs: Any)[source]
Table for patching Zpix table.
- classmethod convert(data, survey=None, program=None, row_index=None)[source]
Convert data into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.survey (
str, optional) – Survey name. If not set, it will be obtained from data.program (
str, optional) – Program name. If not set, it will be obtained from data.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.
- Returns:
A list of ORM objects.
- Return type:
- class specprodDB.coeff.ZtilePatch(*args: Any, **kwargs: Any)[source]
Table for patching Ztile table.
- classmethod convert(data, tileid=None, night=None, row_index=None)[source]
Convert data into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.tileid (
int, optional) – Tile ID number. If not set, it will be obtained from data.night (
int, optional) – Night number. This is loaded into thefirstnightcolumn. If not set, it will be obtained from data.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.
- Returns:
A list of ORM objects.
- Return type:
- specprodDB.coeff.copy_columns(catalog, rowfilter, catalog_type)[source]
Copy columns from catalog into a new table.
- specprodDB.coeff.get_options(description='Extract coefficient columns to create a coeff patch table.')[source]
Parse command-line options.
- Parameters:
description (
str, optional) – Override the description in the command-line help.- Returns:
The parsed options.
- Return type:
- specprodDB.coeff.main()[source]
Entry point for command-line script.
- Returns:
An integer suitable for passing to
sys.exit().- Return type:
specprodDB.load
Code for loading a spectroscopic production database. This includes both targeting and redshift data.
- class specprodDB.load.Base(*args: Any, **kwargs: Any)[source]
SQLAlchemy 2.0 replacement for
Base = declarative_base().
- class specprodDB.load.Exposure(*args: Any, **kwargs: Any)[source]
Representation of the EXPOSURES HDU in the exposures file.
- classmethod convert(data, row_index=None)[source]
Convert data into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.
- Returns:
A list of ORM objects.
- Return type:
- class specprodDB.load.Fiberassign(*args: Any, **kwargs: Any)[source]
Representation of the FIBERASSIGN table in a fiberassign file.
Notes
Targets are assigned to a
location. Alocationhappens to correspond to afiber, but this correspondence could change over time, and therefore should not be assumed to be a rigid 1:1 mapping.PLATE_RA,PLATE_DECare sometimes missing. These can be copies ofTARGET_RA,TARGET_DEC, but in principle they could be different if chromatic offsets in targeting positions were ever implemented.
- classmethod convert(data, tileid=None, row_index=None)[source]
Convert data into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.tileid (
int, optional) – Tile ID number. If not set, it will be obtained from data.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.
- Returns:
A list of ORM objects.
- Return type:
- Raises:
KeyError – If tileid is not set and could not be obtained from data.
- class specprodDB.load.Frame(*args: Any, **kwargs: Any)[source]
Representation of the FRAMES HDU in the exposures file.
Notes
The column
frameidis a combination ofexpidand the camera name:frameid = 100*expid + cameraid(camera)
where
cameraid()isspecprodDB.util.cameraid().- classmethod convert(data, row_index=None)[source]
Convert data into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.
- Returns:
A list of ORM objects.
- Return type:
- class specprodDB.load.Photometry(*args: Any, **kwargs: Any)[source]
Contains only photometric quantities associated with a
TARGETID.This table is deliberately designed so that
TARGETIDcan serve as a primary key. Any quantities created or modified by desitarget are defined in theTargetclass.However we avoid the use of the term “tractor” for this table, because not every target will have tractor photometry,
Notes
The various
LC(light curve) columns, which are vector-valued, are not yet implemented.- classmethod convert(data, row_index=None)[source]
Convert data into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.
- Returns:
A list of ORM objects.
- Return type:
- class specprodDB.load.Potential(*args: Any, **kwargs: Any)[source]
Representation of the POTENTIAL_ASSIGNMENTS table in a fiberassign file.
- classmethod convert(data, tileid=None, row_index=None)[source]
Convert data into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.tileid (
int, optional) – Tile ID number. If not set, it will be obtained from data.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.
- Returns:
A list of ORM objects.
- Return type:
- Raises:
KeyError – If tileid is not set and could not be obtained from data.
- class specprodDB.load.SchemaMixin
Mixin class to allow schema name to be changed at runtime. Also automatically sets the table name.
- class specprodDB.load.Target(*args: Any, **kwargs: Any)[source]
Representation of the pure-desitarget quantities in the
TARGETPHOTtable in the targetphot files.- classmethod convert(data, survey=None, tileid=None, row_index=None)[source]
Convert data into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.survey (
str, optional) – Survey name. If not set, it will be obtained from data.tileid (
int, optional) – Tile ID number. If not set, it will be obtained from data.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.
- Returns:
A list of ORM objects.
- Return type:
- Raises:
KeyError – If survey or tileid are not set and could not be obtained from data.
- class specprodDB.load.Tile(*args: Any, **kwargs: Any)[source]
Representation of the tiles file.
Notes
Most of the data that are currently in the tiles file are derivable from the exposures table with much greater precision:
CREATE VIEW f5.tile AS SELECT tileid, -- SURVEY, FAPRGRM, FAFLAVOR? COUNT(*) AS nexp, SUM(exptime) AS exptime, MIN(tilera) AS tilera, MIN(tiledec) AS tiledec, SUM(efftime_etc) AS efftime_etc, SUM(efftime_spec) AS efftime_spec, SUM(efftime_gfa) AS efftime_gfa, MIN(goaltime) AS goaltime, -- OBSSTATUS? SUM(lrg_efftime_dark) AS lrg_efftime_dark, SUM(elg_efftime_dark) AS elg_efftime_dark, SUM(bgs_efftime_bright) AS bgs_efftime_bright, SUM(lya_efftime_dark) AS lya_efftime_dark, -- GOALTYPE? MIN(mintfrac) AS mintfrac, MAX(night) AS lastnight FROM f5.exposure GROUP BY tileid;However because of some unresolved discrepancies, we’ll just load the full tiles file for now.
- classmethod convert(data, row_index=None)[source]
Convert data into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.
- Returns:
A list of ORM objects.
- Return type:
- class specprodDB.load.Version(*args: Any, **kwargs: Any)[source]
Store package version metadata.
- classmethod convert(data, row_index=None)[source]
Convert the inputs into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.
- Returns:
A list of ORM objects.
- Return type:
- class specprodDB.load.Zpix(*args: Any, **kwargs: Any)[source]
Representation of the
ZCATALOGtable in zpix files.- classmethod convert(data, survey=None, program=None, row_index=None)[source]
Convert data into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.survey (
str, optional) – Survey name. If not set, it will be obtained from data.program (
str, optional) – Program name. If not set, it will be obtained from data.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.
- Returns:
A list of ORM objects.
- Return type:
- Raises:
KeyError – If survey or program are not set and the equivalent data are not available in data.
- class specprodDB.load.Ztile(*args: Any, **kwargs: Any)[source]
Representation of the
ZCATALOGtable in ztile files.- classmethod convert(data, survey=None, program=None, tileid=None, night=None, row_index=None, spgrp='cumulative')[source]
Convert data into ORM objects ready for loading.
- Parameters:
data (
Table) – Data table to convert.survey (
str, optional) – Survey name. If not set, it will be obtained from data.program (
str, optional) – Program name. If not set, it will be obtained from data.tileid (
int, optional) – Tile ID number. If not set, it will be obtained from data.night (
int, optional) – Night number. This is loaded into thefirstnightcolumn. If not set, it will be obtained from data.row_index (
numpy.ndarray, optional) – Only convert the rows indexed by row_index. If not specified, convert all rows.spgrp (
str, optional) – Normally this will be set to the default value: ‘cumulative’.
- Returns:
A list of ORM objects.
- Return type:
- Raises:
KeyError – If survey, program, tileid or night are not set and the equivalent data are not available in data.
Notes
If tileid is set, this method assumes data comes from one and only one tile.
The above has a secondary assumption that, at least for cumulative tile-based spectra, the first night is the same for all spectra.
night becomes
firstnight, whilespgrpvalis equivalent to “lastnight” for cumulative tile-based spectra.
- specprodDB.load.deduplicate_targetid(data)[source]
Find targetphot rows that are not already loaded into the Photometry table and resolve any duplicate TARGETID.
- Parameters:
data (
astropy.table.Table) – The initial data read from the file.- Returns:
An array of rows that are safe to load.
- Return type:
- specprodDB.load.finitize(data, replacement_value=-9999.0)[source]
Convert
NaNand other non-finite floating point values.
- specprodDB.load.get_options(description='Load redshift data into a specprod database.')[source]
Parse command-line options.
- Parameters:
description (
str, optional) – Override the description in the command-line help.- Returns:
The parsed options.
- Return type:
- specprodDB.load.load_file(filepaths, tcls, hdu=1, row_filter=None, q3c=None, chunksize=50000, alternate_load=False)[source]
Load data file into the database, assuming that column names map to database column names with no surprises.
- Parameters:
filepaths (
strorlist) – Full path to the data file or set of data files.tcls (
sqlalchemy.ext.declarative.api.DeclarativeMeta) – The table to load, represented by its class.hdu (
intorstr, optional) – Read a data table from this HDU (default 1).row_filter (callable, optional) – If set, apply this filter to the rows to be loaded. The function should return an array of indexes of “good” rows.
q3c (
str, optional) – If set, create q3c index on the table, using the RA column named q3c.chunksize (
int, optional) – If set, load database chunksize rows at a time (default 50000).alternate_load (
bool, optional) – IfTrueuse an alternate loading scheme that may reduce memory use.
- Returns:
The grand total of rows loaded.
- Return type:
- specprodDB.load.load_versions(photometry, redshift, release, specprod, tiles)[source]
Load version metadata.
The inputs to this function are normally specified in the specprod configuration file. Other necessary metadata are obtained at import time.
- specprodDB.load.main()[source]
Entry point for command-line script.
- Returns:
An integer suitable for passing to
sys.exit().- Return type:
- specprodDB.load.setup_db(dbfile='specprod.db', hostname=None, username='desi_admin', schema=None, overwrite=False, public=False, verbose=False)[source]
Initialize the database connection.
- Parameters:
dbfile (
str, optional) – Name of a SQLite file for output (defaultspecprod.db). If no path is specified in the file name, the current working directory will be used.hostname (
str, optional) – Name of a PostgreSQL server for output.username (
str, optional) – Username on a PostgreSQL server for database connection.schema (
str, optional) – Name of database schema that will contain output tables.overwrite (
bool, optional) – IfTrue, overwrite any existing schema or table.public (
bool, optional) – IfTrue, allow public access to the database or schema.verbose (
bool, optional) – IfTrue, Print extra debugging information for SQL queries.
- Returns:
Trueif the configured database is a PostgreSQL database.- Return type:
- Raises:
RuntimeError – If database connection details could not be found.
- specprodDB.load.upsert(rows, do_nothing=False)[source]
Convert a list of ORM objects into an
INSERT ... ON CONFLICTstatement.
specprodDB.patch
Patch top-level exposures and tiles summary files. This is meant to be applied
to the daily specprod, but others could be patched, in principle.
- specprodDB.patch.back_patch_inconsistent_values(patched)[source]
When the primary round of patching is done, copy some values back into the exposures and frames files.
- specprodDB.patch.get_data(options)[source]
Read in source and destination data.
:param
argparse.Namespace: The parsed command-line options.
- specprodDB.patch.get_options()[source]
Parse command-line options.
- Returns:
The parsed options.
- Return type:
- specprodDB.patch.main()[source]
Entry point for command-line script.
- Returns:
An integer suitable for passing to
sys.exit().- Return type:
- specprodDB.patch.match_rows(left, right)[source]
Match rows in left to rows in right.
- Parameters:
src (array-like) – The column to be matched. This could be “artificial”.
dst (array-like) – The column to be matched. This could be “artificial”.
- Returns:
The row indexes of left and right that match.
- Return type:
- specprodDB.patch.patch_exposures(src_exposures, dst_exposures, first_night=None)[source]
Patch exposures data in dst_exposures with the data in src_exposures.
- Parameters:
- Returns:
A copy of dst_exposures with data replaced from src_exposures.
- Return type:
- specprodDB.patch.patch_exposures_efftime_spec(src_exposures, dst_exposures, dst_tiles)[source]
Patch exposures that have
EFFTIME_SPEC == 0where the corresponding tile hasEFFTIME_SPEC > 0.
- specprodDB.patch.patch_frames(src_frames, dst_frames)[source]
Patch frames data in dst_frames with the data in src_frames.
- specprodDB.patch.patch_missing_frames_mjd(exposures, frames)[source]
Update MJD values in frames after exposures has been patched.
- specprodDB.patch.patch_tiles(src_tiles, dst_tiles, timestamp)[source]
Patch frames data in dst_tiles with the data in src_tiles.
- Parameters:
src_tiles (
Table) – Source of tiles data.dst_tiles (
Table) – Data to be patched.timestamp (
datetime.datetime) – Fill value for theUPDATEDcolumn.
- Returns:
A copy of dst_tiles with data replaced from src_tiles.
- Return type:
specprodDB.tile
Code for loading one or more separate tiles into the spectroscopic production database.
Notes
Some of this code may be combined or otherwise refactored with specprodDB.load
in the future.
- specprodDB.tile.get_options(description='Load data for one tile into a specprod database.')[source]
Parse command-line options.
- Parameters:
description (
str, optional) – Override the description in the command-line help.- Returns:
The parsed options.
- Return type:
- specprodDB.tile.load_fiberassign(tile)[source]
Load the fiber assignments and potential assignments for tile.
- Parameters:
tile (
Tile) – The tile with fiber assignments to load.- Returns:
A tuple containing the lists of
FiberassignandPotentialobjects loaded.- Return type:
- specprodDB.tile.load_photometry(photometry)[source]
Insert the data in photometry into the database.
- Parameters:
photometry (
Table) – A Table containing the photometry data.- Returns:
A list of
Photometryobjects loaded.- Return type:
- specprodDB.tile.load_redshift(tile, spgrp='cumulative')[source]
Load redshift data associated with tile.
- Parameters:
- Returns:
A list of
Ztileobjects loaded.- Return type:
- Raises:
ValueError – If the value of spgrp is not supported.
- specprodDB.tile.load_targetphot(targetphot, loaded_photometry)[source]
Load the photometry, such as it is, for objects that do not have Tractor photometry.
- Parameters:
targetphot (
Table) – A Table containing the targeting data.loaded_photometry (
list) – A list ofPhotometryobjects already loaded.
- Returns:
A list of
Photometryobjects loaded.- Return type:
- specprodDB.tile.main()[source]
Entry point for command-line script.
- Returns:
An integer suitable for passing to
sys.exit().- Return type:
- specprodDB.tile.potential_photometry(tile, targets)[source]
Assemble a Table of targets that will be used to find photometric data.
targets is assumed to come from one tile that has not already been loaded. Any existing photometry already loaded will be excluded from the returned list.
- specprodDB.tile.potential_targets(tileid)[source]
Find potential targets associated with tileid.
Sky targets are not returned.
- specprodDB.tile.targetphot(catalog)[source]
Find the target data associated with the targets in catalog.
- specprodDB.tile.tractorphot(catalog)[source]
Find the photometry data associated with the targets in catalog.
- specprodDB.tile.update_primary()[source]
Update the primary classification after some number of tiles has been loaded.
- specprodDB.tile.update_q3c()[source]
Update the q3c indexes after some number of tiles has been loaded.
specprodDB.util
Classes and functions for use by all database code.
- specprodDB.util.cameraid(camera)[source]
Converts camera (e.g. ‘b0’) to an integer in a simple but ultimately arbitrary way.
- specprodDB.util.checkgzip(filename)[source]
Check for existence of filename, with or without a
.gzextension.- Parameters:
filename (
str) – Filename to check.- Returns:
Path of existing file with or without
.gz.- Return type:
- Raises:
FileNotFoundError – If neither file type exists.
- specprodDB.util.common_options(description)[source]
Define a set of common command-line options.
Individual command-line scripts will add additional options.
- Parameters:
description (
str) – Define the description in the command-line help.- Returns:
An argument parser to which further arguments may be added.
- Return type:
- specprodDB.util.convert_dateobs(timestamp, tzinfo=None)[source]
Convert a string timestamp into a
datetime.datetimeobject.- Parameters:
timestamp (
str) – Timestamp in string format.tzinfo (
datetime.tzinfo, optional) – If set, add time zone to the timestamp.
- Returns:
The converted timestamp.
- Return type:
- specprodDB.util.fiberassignid(targetid, tileid, location)[source]
Convert inputs into an arbitrary large integer.
- specprodDB.util.frameid(expid, camera)[source]
Converts the pair expid, camera into an arbitrary integer suitable for use as a primary key.
- specprodDB.util.no_sky(catalog)[source]
Identify objects in catalog that are not sky targets.
- Parameters:
catalog (
Table) – Any Table containing aTARGETIDcolumn.- Returns:
The indexes of rows that are not sky targets.
- Return type:
- specprodDB.util.parse_pgpass(hostname='specprod-db.desi.lbl.gov', username='desi_admin')[source]
Read a
~/.pgpassfile.
- specprodDB.util.programid(program)[source]
Converts program (e.g. ‘bright’) to an integer in a simple but ultimately arbitrary way.
- specprodDB.util.spgrpid(spgrp)[source]
Converts spgrp (e.g. ‘cumulative’) to an integer in a simple but ultimately arbitrary way.
- specprodDB.util.surveyid(survey)[source]
Converts survey (e.g. ‘main’) to an integer in a simple but ultimately arbitrary way.
- specprodDB.util.targetphotid(targetid, tileid, survey)[source]
Convert inputs into an arbitrary large integer.
- specprodDB.util.zpixid(targetid, survey, program)[source]
Convert inputs into an arbitrary large integer.