API

fastspecfit

Tools for fast stellar continuum, emission-line, and broadband photometric modeling.

fastspecfit.continuum

Methods and tools for continuum-fitting.

class fastspecfit.continuum.ContinuumTools(data, templates, phot, igm, tauv_guess=0.1, vdisp_guess=250.0, tauv_bounds=(0.0, 2.0), vdisp_bounds=(75.0, 500.0), vdisp_nbin=5, fluxnorm=1e+17, massnorm=10000000000.0, fastphot=False, constrain_age=False)[source]

Tools for dealing with spectral continua.

_stellar_objective(params, templateflux, dust_emission, fit_vdisp, conv_pre, objflam, objflamistd, specflux, specistd, synthphot, synthspec)[source]

Objective function for fitting a stellar continuum.

static _younger_than_universe(age, tuniv, agepad=0.5)[source]

Return the indices of the templates younger than the age of the universe (plus an agepadding amount) at the given redshift. age in yr, agepad and tuniv in Gyr

static attenuate(M, A, zfactors, wave, dustflux)

Compute attenuated version of a model spectrum, including contribution of dust emission.

static attenuate_nodust(M, A, zfactors)

Compute attenuated version of a model spectrum M, without dust emission.

build_stellar_continuum(templateflux, templatecoeff, tauv, vdisp=None, conv_pre=None, dust_emission=True)[source]

Build a stellar continuum model.

Parameters:
  • templateflux (numpy.ndarray [ntemplates, npix]) – Rest-frame, native-resolution template spectra corresponding to templatewave.

  • templatecoeff (numpy.ndarray [ntemplates]) – Column vector of positive coefficients corresponding to each template.

  • tauv (float) – V-band optical depth, tau(V).

  • vdisp (float) – Velocity dispersion in km/s. If None, do not convolve to the specified velocity dispersion (usually because templateflux has already been smoothed to some nominal value).

  • conv_pre (tuple or None) – Optional preprocessing data to accelerate template convolution with vdisp (may be present only if vdisp is not None).

  • dust_emission (bool) – Model impact of infrared dust emission spectrum. Energy-balance is used to compute the normalization of this spectrum.

Returns:

contmodel – Full-wavelength, native-resolution, observed-frame model spectrum.

Return type:

numpy.ndarray [npix]

static cfluxes_keys()[source]

Simple method which defines the observed-frame continuum flux keys and wavelengths.

continuum_fluxes(continuum, uniqueid=0, width1=50.0, width2=100.0, debug_plots=False)[source]

Compute rest-frame luminosities and observed-frame continuum fluxes.

continuum_to_photometry(contmodel, filters=None, phottable=False, get_abmag=False)[source]

Synthesize photometry from a continuum model.

Parameters:
  • contmodel (numpy.ndarray [npix]) – Full-wavelength, native-resolution, observed-frame model spectrum.

  • filters (list or speclite.filters.FilterSequence [nfilt]) – Optionally override the filter curves stored in the filters attribute of the global Photometry object.

  • phottable (bool) – Return an astropy.table.Table with additional bandpass information. Otherwise, return synthesized photometry in f_lambda (10^17 erg/s/cm2/A) units, which are used in fitting Only true for QA.

  • get_abmag (bool) – Add AB magnitudes to the synthesized photometry table (only applies if phottable=True. Only used for QA.

Returns:

modelphot – Synthesized model photometry as an array or a table, depending on phottable.

Return type:

numpy.ndarray or astropy.table.Table [nfilt]

continuum_to_spectroscopy(contmodel, interp=False)[source]

Synthesize spectroscopy from a continuum model.

Parameters:
  • contmodel (numpy.ndarray [npix]) – Full-wavelength, native-resolution, observed-frame model spectrum.

  • interp (bool) – For cosmetic (plotting) purposes, interpolate over masked pixels.

Returns:

modelflux – Observed-frame model spectrum at the instrumental resolution and wavelength sampling given by specres and specwave.

Return type:

numpy.ndarray [nwave]

fit_stellar_continuum(templateflux, fit_vdisp=False, conv_pre=None, vdisp_guess=None, tauv_guess=None, vdisp_bounds=None, tauv_bounds=None, coeff_guess=None, dust_emission=True, objflam=None, objflamistd=None, specflux=None, specistd=None, synthphot=False, synthspec=False)[source]

Fit a stellar continuum using bounded non-linear least-squares.

Parameters:
  • templateflux (numpy.ndarray [ntemplate, npix]) – Grid of input (model) spectra.

  • fit_vdisp (bool) – If True, solve for the velocity dispersion; if False, use a nominal dispersion.

  • conv_pre (tuple of None) – If not None, preprocessing data for convolving templateflux with vdisp values. (Occurs only if fit_vdisp is True.)

  • vdisp_guess (float) – Guess for scalar value of the velocity dispersion if fitting.

  • tauv_guess (float) – Guess scalar value of the dust optical depth.

  • coeff_guess (numpy.ndarray [ntemplates]) – Guess of the template coefficients.

  • vdisp_bounds (tuple) – Two-element list of minimum and maximum allowable values of the velocity dispersion; only used if fit_vdisp=True.

  • tauv_bounds (tuple) – Two-element list of minimum and maximum allowable values of the V-band optical depth, tau(V).

  • dust_emission (bool) – Model impact of infrared dust emission spectrum. Energy-balance is used to compute the normalization of this spectrum.

  • objflam – Measured object photometry (used if fitting photometry).

  • objflamistd – Sqrt of inverse variance of objflam (used if fitting photometry).

  • specflux (numpy.ndarray [nwave]) – Observed-frame spectrum in 10**-17 erg/s/cm2/A corresponding to specwave (used if fitting spectroscopy).

  • specfluxistd (numpy.ndarray [nwave]) – Sqrt of inverse variance of the observed-frame spectrum (used if fitting spectroscopy).

  • synthphot (bool) – True iff fitting objective includes object photometry.

  • synthspec (bool) – True iff fitting objective includes observed spectrum.

Returns:

  • tauv (float) – Maximum-likelihood V-band optical depth.

  • vdisp (float) – Maximum-likelihood velocity dispersion in km/s, or nominal velocity if it was not fitted.

  • templatecoeff (numpy.ndarray [ntemplate]) – Column vector of maximum-likelihood template coefficients.

  • resid (numpy.ndarray) – Vector of residuals at final parameter values.

Notes

This method supports several different fitting ‘modes’, depending on the whether vdisp is fitted or left nominal, and whether the caller requests fitting a model against spectroscopy, photometry, or both.

In all cases, we solve for dust attenuation via the tau(V) parameter and we also include IGM attenuation.

get_zfactors(igm, ztemplatewave, redshift, dluminosity)[source]

Convenience method to cache the factors that depend on redshift and the redshifted wavelength array, including the attenuation due to the IGM.

ztemplatewavenumpy.ndarray [npix]

Redshifted wavelength array.

redshiftfloat

Object redshift.

dluminosityfloat

Luminosity distance corresponding to redshift.

static lums_keys()[source]

Simple method which defines the rest-frame luminosity keys and wavelengths.

static smooth_continuum(wave, flux, ivar, linemask, camerapix, uniqueid=0, smooth_window=75, smooth_step=125, clip_sigma=2.0, nminpix=15, nmaskpix=9, debug_plots=False)[source]

Build a smooth, nonparametric continuum spectrum.

Parameters:
  • wave (numpy.ndarray [npix]) – Observed-frame wavelength array.

  • flux (numpy.ndarray [npix]) – Spectrum corresponding to wave.

  • ivar (numpy.ndarray [npix]) – Inverse variance spectrum corresponding to flux.

  • linemask (numpy.ndarray of type bool, optional, defaults to None) – Boolean mask with the same number of pixels as wave where True means a pixel is (possibly) affected by an emission line (specifically a strong line which likely cannot be median-smoothed).

  • smooth_window (int, optional, defaults to 50 pixels) – Width of the sliding window used to compute the iteratively clipped statistics (mean, median, sigma); a magic number. Note: the nominal extraction width (0.8 A) and observed-frame wavelength range (3600-9800 A) corresponds to pixels that are 66-24 km/s. So smooth_window of 50 corresponds to 3300-1200 km/s, which is conservative for all but the broadest lines. A magic number.

  • smooth_step (int, optional, defaults to 10 pixels) – Width of the step size when computing smoothed statistics; a magic number.

  • png (str, optional, defaults to None) – Generate a simple QA plot and write it out to this filename.

Returns:

  • smooth_flux numpy.ndarray [npix]

  • Smooth continuum spectrum which can be subtracted from flux in

  • order to create a pure emission-line spectrum.

stellar_continuum_chi2(resid, ncoeff, vdisp_fitted, split=0, ndof_spec=0, ndof_phot=0)[source]

Compute the reduced spectroscopic and/or photometric chi2.

resid:

Vector of residuals from least-squares fitting

ncoeff:

Number of template coefficients fitted

vdisp_fitted:

True if the velocity dispersion was fitted

split:

Boundary between initial spectroscopy elements of residual and final photometry elements

ndof_spec:

Number of spectroscopy degrees of freedom

ndof_phot:

Number of photometry degrees of freedom

fastspecfit.continuum._continuum_nominal_vdisp(CTools, templates, specflux, specwave, specistd, agekeep, compute_chi2=False)[source]

Support routine to fit a spectrum at the nominal velocity dispersion.

fastspecfit.continuum.can_compute_vdisp(redshift, specwave, min_restrange=(3800.0, 4800.0), fit_restrange=(3800.0, 6000.0))[source]

Determine if we can solve for the velocity dispersion.

min_restrange - minimum required rest wavelength range fit_restrange - fitted rest wavelength range

fastspecfit.continuum.continuum_fastphot(redshift, objflam, objflamivar, CTools, uniqueid=0, nmonte=10, rng=None, debug_plots=False)[source]

Model the broadband photometry.

fastspecfit.continuum.continuum_fastspec(redshift, objflam, objflamivar, CTools, nmonte=10, rng=None, uniqueid=0, no_smooth_continuum=False, debug_plots=False)[source]

Jointly model the spectroscopy and broadband photometry.

fastspecfit.continuum.continuum_specfit(data, fastfit, specphot, templates, igm, phot, nmonte=10, seed=1, constrain_age=False, no_smooth_continuum=False, fitstack=False, fastphot=False, debug_plots=False)[source]

Fit the non-negative stellar continuum of a single spectrum.

Parameters:

data (dict) – Dictionary of input spectroscopy (plus ancillary data) populated by fastspecfit.io.DESISpectra.read().

Returns:

Table with all the continuum-fitting results with columns documented in init_output().

Return type:

astropy.table.Table

Notes

  • Consider using cross-correlation to update the redrock redshift.

  • We solve for velocity dispersion if …

fastspecfit.continuum.vdisp_by_chi2scan(CTools, templates, uniqueid, specflux, specwave, specistd, fitmask, agekeep, deltachi2min=25.0, fit_for_min=False, debug_plots=False)[source]

Determine the velocity dispersion via a chi2 scan.

fastspecfit.cosmo

Cosmology utilities.

class fastspecfit.cosmo.TabulatedDESI[source]

Class to load tabulated z->E(z) and z->comoving_radial_distance(z) relations within DESI fiducial cosmology (in LSS/data/desi_fiducial_cosmology.dat) and perform the (linear) interpolations at any z.

>>> cosmo = TabulatedDESI()
>>> distance = cosmo.comoving_radial_distance([0.1, 0.2])
>>> efunc = cosmo.efunc(0.3)

The cosmology is defined in https://github.com/abacusorg/AbacusSummit/blob/master/Cosmologies/abacus_cosm000/CLASS.ini and the tabulated file was obtained using https://github.com/adematti/cosmoprimo/blob/main/cosmoprimo/fiducial.py.

Notes

Redshift interpolation range is [0, 100].

comoving_radial_distance(z)[source]

Return comoving radial distance, in \(\mathrm{Mpc}/h\).

distance_modulus(z)[source]

Return the distance modulus at the given redshift (Hogg Eq. 24).

efunc(z)[source]

Return \(E(z)\), where the Hubble parameter is defined as \(H(z) = H_{0} E(z)\), unitless.

luminosity_distance(z)[source]

Return luminosity distance, in \(\mathrm{Mpc}/h\).

universe_age(z)[source]

Return the age of the universe at the given redshift.

fastspecfit.emlines

Methods and tools for fitting emission lines.

class fastspecfit.emlines.ParamType(value)[source]

An enumeration.

fastspecfit.emlines.build_coadded_models(data, emlinewave, emlineflux_model, continuum_flux, smooth_continuum_flux)[source]

Package together the output models, coadded across cameras.

Assume constant dispersion in wavelength!

fastspecfit.emlines.emline_specfit(data, fastfit, specphot, continuummodel, smooth_continuum, phot, emline_table, minsnr_balmer_broad=2.5, minsigma_balmer_broad=250.0, continuummodel_monte=None, specflux_monte=None, synthphot=True, broadlinefit=True, debug_plots=False)[source]

Perform the fit minimization / chi2 minimization.

Parameters:
  • data

  • continuummodel

  • smooth_continuum

  • synthphot

  • broadlinefit

  • [km/s] (minsigma_balmer_broad - minimum broad-line sigma)

Returns:

  • results

  • modelflux

fastspecfit.emlines.linefit(EMFit, linemodel, initial_guesses, param_bounds, emlinewave, emlineflux, emlineivar, weights, redshift, resolution_matrix, camerapix, debug=False)[source]

Simple wrapper on EMFit.optimize. Modifies linemodel in place.

fastspecfit.emlines.synthphot_spectrum(phot, data, specphot, modelwave, modelflux)[source]

Synthesize photometry from the best-fitting model (continuum+emission lines).

fastspecfit.emlines.test_broad_model(EMFit, linemodel_nobroad, emlineflux_model_nobroad, linemodel_broad, emlineflux_model_broad, emlinewave, emlineflux, emlineivar, redshift, minsnr_balmer_broad, minsigma_balmer_broad)[source]

Test the broad Balmer-line hypothesis.

fastspecfit.fastspecfit

See sandbox/running-fastspecfit for examples.

fastspecfit.fastspecfit.fastphot(args=None, comm=None, verbose=False)[source]

Main fastphot script.

This script is the engine to model the broadband photometry of one or more DESI objects. It initializes the ContinuumFit class, reads the data, fits each object (with the option of fitting in parallel), and writes out the results as a multi-extension binary FITS table.

Parameters:
  • args (argparse.Namespace or None) – Required and optional arguments parsed via inputs to the command line.

  • comm (mpi4py.MPI.MPI.COMM_WORLD or None) – Intracommunicator used with MPI parallelism.

fastspecfit.fastspecfit.fastspec(fastphot=False, fitstack=False, args=None, comm=None, verbose=False)[source]

Main fastspec script.

This script is the engine to model one or more DESI spectra. It initializes the FastFit class, reads the data, fits each spectrum (with the option of fitting in parallel), and writes out the results as a multi-extension binary FITS table.

Parameters:
  • args (argparse.Namespace or None) – Required and optional arguments parsed via inputs to the command line.

  • comm (mpi4py.MPI.MPI.COMM_WORLD or None) – Intracommunicator used with MPI parallelism.

fastspecfit.fastspecfit.fastspec_one(iobj, data, meta, fastfit_dtype, specphot_dtype, broadlinefit=True, fastphot=False, fitstack=False, constrain_age=False, no_smooth_continuum=False, debug_plots=False, uncertainty_floor=0.01, minsnr_balmer_broad=2.5, nmonte=10, seed=1)[source]

Run fastspec() on a single object.

fastspecfit.fastspecfit.parse(options=None, rank=0)[source]

Parse input arguments to fastspec and fastphot scripts.

fastspecfit.fastspecfit.stackfit(args=None, comm=None, verbose=False)[source]

Wrapper script to fit (model) generic stacked spectra.

Parameters:
  • args (argparse.Namespace or None) – Required and optional arguments parsed via inputs to the command line.

  • comm (mpi4py.MPI.MPI.COMM_WORLD or None) – Intracommunicator used with MPI parallelism.

fastspecfit.igm

Tools for handling intergalactic medium (IGM) attenuation.

class fastspecfit.igm.Inoue14(scale_tau=1.0)[source]

IGM absorption from Inoue et al. (2014)

Parameters:
  • scale_tau (float) – Parameter multiplied to the IGM \(\tau\) values (exponential in the linear absorption fraction). I.e., \(f_\mathrm{igm} = e^{-\mathrm{scale\_tau} \tau}\).

  • Brammer (Copyright (c) 2016-2022 Gabriel)

  • granted (Permission is hereby)

  • charge (free of)

  • copy (to any person obtaining a)

  • "Software") (of this software and associated documentation files (the)

  • deal (to)

  • restriction (in the Software without)

  • rights (including without limitation the)

  • use (to)

  • copy

  • modify

  • merge

  • publish

  • distribute

  • sublicense

  • sell (and/or)

  • Software (copies of the)

  • is (and to permit persons to whom the Software)

  • so (furnished to do)

  • conditions (subject to the following)

  • all (The above copyright notice and this permission notice shall be included in)

  • Software. (copies or substantial portions of the)

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

full_IGM(z, lobs)[source]

Get full Inoue IGM absorption

Parameters:
  • z (float)

  • absorption (Redshift to evaluate IGM)

  • lobs (array)

  • Angstroms. (Observed-frame wavelength(s) in)

Returns:

  • abs (array)

  • IGM absorption

fastspecfit.igm._tLCDLA(zS, lobs)

Lyman continuum, DLA

fastspecfit.igm._tLCLAF(zS, lobs)

Lyman continuum, LAF

fastspecfit.igm._tLSDLA(zS, lobs, lam, cADLA1, cADLA2)

Lyman Series, DLA

fastspecfit.igm._tLSLAF(zS, lobs, lam, cALAF1, cALAF2, cALAF3)

Lyman series, Lyman-alpha forest

fastspecfit.io

Tools for reading DESI spectra and reading and writing fastspecfit files.

fastspecfit.io.create_output_meta(input_meta, phot, fastphot=False, fitstack=False)[source]

Create the fastspecfit output metadata table.

fastspecfit.io.create_output_table(records, meta, units, fitstack=False)[source]

Generate the output FASTSPEC/FASTPHOT or SPECPHOT table.

fastspecfit.io.get_output_dtype(specprod, phot, linetable, ncoeff, cameras=['B', 'R', 'Z'], specphot=False, fastphot=False, fitstack=False)[source]

Get type of one fastspecfit output data record, along with dictionary of units for any fields that have them.

fastspecfit.io.get_qa_filename(metadata, coadd_type, outprefix=None, outdir=None, fastphot=False)[source]

Build the QA filename.

fastspecfit.io.one_desi_spectrum(survey, program, healpix, targetid, specprod='fuji', outdir='.', overwrite=False)[source]

Utility function to write a single DESI spectrum (e.g., for paper figures or unit tests).

fastspecfit.io.one_spectrum(specdata, meta, uncertainty_floor=0.01, RV=3.1, init_sigma_uv=None, init_sigma_narrow=None, init_sigma_balmer=None, init_vshift_uv=None, init_vshift_narrow=None, init_vshift_balmer=None, fastphot=False, synthphot=True, debug_plots=False)[source]

Pre-process the data for a single object.

fastspecfit.io.one_stacked_spectrum(specdata, meta, synthphot=True, debug_plots=False)[source]

Unpack the data for a single stacked spectrum.

fastspecfit.io.read_fastspecfit(fastfitfile, rows=None, metadata_columns=None, specphot_columns=None, fastspec_columns=None, read_models=False)[source]

Read the fitting results.

fastspecfit.io.select(metadata, specphot, fastfit=None, coadd_type='healpix', healpixels=None, tiles=None, nights=None, return_index=False)[source]

Optionally trim to a particular healpix or tile and/or night.

fastspecfit.io.write_fastspecfit(meta, specphot, fastfit, modelspectra=None, outfile=None, specprod=None, coadd_type=None, fphotofile=None, template_file=None, emlinesfile=None, fastphot=False, inputz=False, inputseeds=None, nmonte=10, seed=1, uncertainty_floor=0.01, minsnr_balmer_broad=2.5, no_smooth_continuum=False, ignore_photometry=False, broadlinefit=True, use_quasarnet=True, constrain_age=False, split_hdu=False, verbose=True)[source]

Write out.

fastspecfit.linemasker

Tools for pre-fitting and masking emission lines.

class fastspecfit.linemasker.LineMasker(emline_table)[source]

Compute spectral line mask for continuum estimation, and to get initial guesses at emission line parameters

build_linemask(wave, flux, ivar, resolution_matrix, redshift=0.0, uniqueid=0, minsnr_balmer_broad=1.5, minsnr_linemask=3.5, initsigma_broad=None, initsigma_narrow=None, initsigma_balmer_broad=None, initvshift_broad=None, initvshift_narrow=None, initvshift_balmer_broad=None, niter=2, nsigma_mask=5.0, debug_plots=False)[source]

Generate a mask which identifies pixels impacted by emission lines.

Parameters:
  • wave (numpy.ndarray [npix]) – Observed-frame wavelength array.

  • flux (numpy.ndarray [npix]) – Spectrum corresponding to wave.

  • ivar (numpy.ndarray [npix]) – Inverse variance spectrum corresponding to flux.

  • resolution_matrix (list) – List of fastspecfit.resolution.Resolution objects, one per camera.

  • redshift (float) – Object redshift.

  • uniqueid (int) – Unique identification number.

  • initsigma_broad (float) – Initial guess of the broad-line emission-line width in km/s.

  • initsigma_narrow (float) – Like initsigma_broad but for the forbidden lines.

  • initsigma_balmer_broad (float) – Like initsigma_broad but for the broad Balmer lines.

  • initvshift_broad (float) – Initial guess of the broad-line velocity shift in km/s.

  • initvshift_narrow (float) – Like initvshift_broad but for the forbidden lines.

  • initvshift_balmer_broad (float) – Like initvshift_broad but for the broad Balmer lines.

  • minsnr_balmer_broad (float) – Minimum signal-to-noise ratio for identifying a significant broad Balmer line.

  • niter (int) – Number of fit-in-patches iterations.

  • debug_plots (bool) – Optionally generate and write out various debugging plots.

Return type:

dict with line-masking arrays

static linepix_and_contpix(wave, ivar, linetable, linesigmas, residuals=None, flux=None, linevshifts=None, patchMap=None, redshift=0.0, nsigma=2.5, minlinesigma=50.0, mincontpix=11, get_contpix=True, get_snr=False)[source]

Support routine to determine the pixels potentially containing emission lines and the corresponding (adjacent) continuum.

linesigmas in km/s minlinesigma in kms - minimum line-sigma for the purposes of masking mincontpix - minimum number of continuum pixels per line

fastspecfit.linetable

Emission line table, read from a file.

fastspecfit.logger

Unique logger object, distinct from the one used by the DESI libraries, allocated at startup and used throughout code base. We need to create our own logger to avoid the DESI libraries accidentally changing the log level out from under us.

Having just one logger allows us to initialize its level on startup and have those changes propagate everywhere.

This needs to be in its own file to prevent circular imports with other code.

fastspecfit.logger.getFastspecLogger()[source]

Create a logging object unique to the fastspec tools. Configure it to send its log messages to stdout and to format them identically to the DESIUtil defaults.

Note that every call to this function returns the same log object, so will reset its properties including log level. Hence, it should really only be called once at the start of the program, as we do here, to create a singleton log object.

Returns a log object with default level INFO.

fastspecfit.mpi

MPI tools.

fastspecfit.mpi._domerge(outfiles, outprefix=None, specprod=None, coadd_type=None, mergefile=None, fastphot=False, split_hdu=False, mp=1)[source]

Support routine to merge a set of input files.

fastspecfit.mpi.merge_fastspecfit(specprod=None, coadd_type=None, survey=None, program=None, healpix=None, tile=None, night=None, sample=None, outsuffix=None, fastphot=False, specprod_dir=None, outdir_data='.', split_hdu=False, fastfiles_to_merge=None, merge_suffix=None, mergedir=None, supermerge=False, overwrite=False, mp=1)[source]

Merge all the individual catalogs into a single large catalog. Runs only on rank 0.

supermerge - merge previously merged catalogs

fastspecfit.mpi.plan(comm=None, specprod=None, specprod_dir=None, coadd_type='healpix', survey=None, program=None, healpix=None, tile=None, night=None, sample=None, outdir_data='.', mp=1, merge=False, makeqa=False, fastphot=False, overwrite=False)[source]

Function which determines what files have—and have not—been processed.

fastspecfit.mpi.plan_makeqa(outdir, htmldir, outprefix, coadd_type, survey, program, healpix, tile, night, sample=None, gzip=False)[source]

Simple support routine to build the input and output filenames when building QA.

fastspecfit.mpi.plan_merge(outdir, outprefix, coadd_type, survey, program, healpix, tile, night, sample=None, gzip=False)[source]

Simple support routine to build the input and output filenames when merging.

fastspecfit.photometry

Tools for handling filters and photometry calculations.

class fastspecfit.photometry.Photometry(fphotofile=None, fitstack=False, ignore_photometry=False)[source]

Class to load filters and containing filter- and dust-related methods.

static get_ab_maggies(filters, flux, wave)[source]

This version of get_ab_maggies() is robust to wavelength vectors that do not entirely cover one the response range of one or more filters.

static get_ab_maggies_pre(filters, wave)[source]

Compute preprocessing data for get_ab_maggies_unchecked() for given filter list and target wavelength.

static get_ab_maggies_unchecked(filters, flux, wave, pre=None)[source]

Like get_ab_maggies(), but by-passing the units and, more importantly, the padding and interpolation of wave/flux that speclite does. We assume that the response function for each filter lies strictly within the bounds of wave, and that the response functions don’t change so fast that we would need to interpolate wave to get an accurate integral.

When wave comes from a stellar template, it has a very large wavelength range, so these assumptions are reasonable. When wave comes from an actual camera, however, the filter responses are known to exceed the cameras’ range of observed wavelengths.

flux and wave are assumed to be in erg/s/cm2/A and A, respectively.

static get_dn4000(wave, flam, flam_ivar=None, redshift=None, rest=True)[source]

Compute DN(4000) and, optionally, the inverse variance.

Parameters:
  • wave

  • flam

  • flam_ivar

  • redshift

  • rest

Notes

If rest`=``False` then redshift input is required.

Require full wavelength coverage over the definition of the index.

See eq. 11 in Bruzual 1983 (https://articles.adsabs.harvard.edu/pdf/1983ApJ…273..105B) but with the “narrow” definition of Balogh et al. 1999.

kcorr_and_absmag(nanomaggies, ivar_nanomaggies, redshift, dmod, photsys, zmodelwave, zmodelflux, synth_absmag, synth_maggies_rest, snrmin=2.0)[source]

Compute K-corrected rest-frame photometry.

Parameters:
  • nanomaggies (numpy.ndarray) – Input photometric fluxes in the filters_obs bandpasses.

  • ivar_nanomaggies (numpy.ndarray) – Inverse variance photometry corresponding to nanomaggies.

  • redshift (float) – Galaxy or QSO redshift.

  • dmod (float) – Distance modulus corresponding to redshift.

  • zmodelwave (numpy.ndarray) – Observed-frame (redshifted) model wavelength array.

  • zmodelflux (numpy.ndarray) – Observed-frame (redshifted) model spectrum.

  • synth_absmag (numpy.ndarray) – Absolute magnitudes based on synthesized photometry.

  • synth_maggies_rest (numpy.ndarray) – Synthesized rest-frame photometry.

  • snrmin (float, defaults to 2.) – Minimum signal-to-noise ratio in the input photometry (maggies) in order for that bandpass to be used to compute a K-correction.

Returns:

  • kcorr (numpy.ndarray) – K-corrections for each bandpass in absmag_filters.

  • absmag (numpy.ndarray) – Absolute magnitudes, band-shifted according to band_shift (if provided) for each bandpass in absmag_filters.

  • ivarabsmag (numpy.ndarray) – Inverse variance corresponding to absmag.

  • synth_maggies_obs (numpy.ndarray) – Synthesized observed-frame photometry.

Notes

By default, the K-correction is computed by finding the observed-frame bandpass closest in wavelength (and with a minimum signal-to-noise ratio) to the desired band-shifted absolute magnitude bandpass. In other words, by default we endeavor to minimize the K-correction. The inverse variance, ivarabsmag, is derived from the inverse variance of the K-corrected photometry. If no bandpass is available then ivarabsmag is set to zero and absmag is derived from the synthesized rest-frame photometry.

static parse_photometry(bands, maggies, lambda_eff, ivarmaggies=None, nanomaggies=True, nsigma=2.0, min_uncertainty=None, get_abmag=False)[source]

Parse input (nano)maggies to various outputs and pack into a table.

Parameters:
  • erg/s/cm2/A (flam - 10-17)

  • erg/s/cm2/Hz (fnu - 10-17)

  • mag (abmag - AB)

  • maggies (nanomaggies - input maggies are actually 1e-9)

  • limit (nsigma - magnitude)

  • needs (get_abmag - true iff table will be used by fastqa (which)

  • not) (columns that fastspec does)

Return type:

phot - photometric table

Notes

synth_absmag(redshift, dmod, zmodelwave, zmodelflux)[source]

Synthesize absolute magnitudes from the best-fitting SED.

Parameters:
  • redshift (float) – Galaxy or QSO redshift.

  • dmod (float) – Distance modulus corresponding to redshift.

  • zmodelwave (numpy.ndarray) – Observed-frame (redshifted) model wavelength array.

  • zmodelflux (numpy.ndarray) – Observed-frame (redshifted) model spectrum.

Returns:

  • synth_absmag (numpy.ndarray) – Absolute magnitudes based on synthesized photometry.

  • synth_maggies_rest (numpy.ndarray) – Synthesized rest-frame photometry.

fastspecfit.photometry._gather_tractorphot_onebrick(input_cat, legacysurveydir, radius_match, racolumn, deccolumn, datamodel, restrict_region)[source]

Support routine for gather_tractorphot.

fastspecfit.photometry.gather_tractorphot(input_cat, racolumn='TARGET_RA', deccolumn='TARGET_DEC', legacysurveydir=None, dr9dir=None, radius_match=1.0, restrict_region=None, columns=None)[source]

Retrieve the Tractor catalog for all the objects in this catalog (one brick).

Parameters:
  • input_cat (astropy.table.Table) – input table with the following (required) columns: TARGETID, TARGET_RA, TARGET_DEC. Additional optional columns that will ensure proper matching are BRICKNAME, RELEASE, PHOTSYS, BRICKID, and BRICK_OBJID.

  • legacysurveydir (str) – full path to the location of the Tractor catalogs

  • dr9dir (str) – relegated keyword; please use legacysurveydir

  • radius_match (float, arcsec) – matching radius (default, 1 arcsec)

  • restrict_region (str) – when positional matching, restrict the region to check for photometry, otherwise check both ‘north’ and ‘south’ (default None)

  • columns (str array) – return this subset of columns

Returns a table of Tractor photometry. Matches are identified either using BRICKID and BRICK_OBJID or using positional matching (1 arcsec radius).

fastspecfit.photometry.tractorphot_datamodel(from_file=False, datarelease='dr9')[source]

Initialize the tractorphot data model for a given Legacy Surveys data release.

Parameters:
  • from_file (bool, optional) – read the datamodel from a file on-disk.

  • datarelease (str, optional) – data release to read; currently only dr9 and dr10 are supported.

Returns an astropy.table.Table which follows the Tractor catalog datamodel for the given data release.

fastspecfit.qa

fastspecfit.qa.desiqa_one(data, metadata, specphot, coadd_type, fastfit=None, minspecwave=3500.0, maxspecwave=9900.0, minphotwave=0.1, maxphotwave=35.0, emline_snrmin=0.0, nsmoothspec=1, init_sigma_uv=None, init_sigma_narrow=None, init_sigma_balmer=None, init_vshift_uv=None, init_vshift_narrow=None, init_vshift_balmer=None, fastphot=False, fitstack=False, inputz=False, no_smooth_continuum=False, outdir=None, outprefix=None)[source]

Multiprocessing wrapper to generate QA for a single object.

fastspecfit.qa.fastqa(args=None, comm=None)[source]

Main module.

fastspecfit.qa.format_niceline(line)[source]

Simple function to nicely format the name of a line.

fastspecfit.qa.parse(options=None)[source]

Parse input arguments.

fastspecfit.qa.qa_fastspec(data, templates, metadata, specphot, fastspec=None, coadd_type='healpix', spec_wavelims=(3550, 9900), phot_wavelims=(0.1, 35), fastphot=False, fitstack=False, outprefix=None, no_smooth_continuum=False, emline_snrmin=0.0, nsmoothspec=1, outdir=None, inputz=None)[source]

QA plot the emission-line spectrum and best-fitting model.

fastspecfit.singlecopy

Single-copy (per process) data structures read from files.

fastspecfit.templates

Tools for handling templates.

fastspecfit.util

General utilities.

class fastspecfit.util.BoxedScalar(dtype)[source]

A BoxedScalar is an item of an Numpy structured scalar type that is initialized to all zeros and can then be passed around by reference. Access the .value field to unbox the scalar.

class fastspecfit.util.MPPool(nworkers, initializer=None, init_argdict=None)[source]

A Pool encapsulates paraallel execution with a multiprocessing.Pool, falling back to sequential execution in the current process if just one worker is requested.

Unlike multiprocessing.Pool, our starmap() function takes a list of keyword argument dictionaries, rather than a list of positional arguments.

close()[source]

close our multiprocess pool if we created one

starmap(func, argdicts)[source]

apply function func to each of a list of inputs, represented as a list of keyword argument dictionaries.

class fastspecfit.util.ZWarningMask[source]

Mask bit definitions for zwarning. Taken from Redrock/0.15.4 WARNING on the warnings: not all of these are implemented yet.

#- TODO: Consider using something like desispec.maskbits to provide a more #- convenient wrapper class (probably copy it here; don’t make a dependency) #- That class as-is would bring in a yaml dependency.

fastspecfit.util._trapz_rebin(x, y, edges, ibw, out)

Trapezoidal rebinning

This implementation is optimized for compilation with Numba. It agrees with the earlier fastspecfit implementation to within around 1e-12 and is somewhat faster. It also tolerates the first bin being arbitrarily greater than the first x without performance loss. ibw is the array of inverse bin widths.

We require, as did the original version of the code, that the values of x extend strictly beyond the edges array in both directions.

fastspecfit.util.air2vac(airwave)[source]

http://www.astro.uu.se/valdwiki/Air-to-vacuum%20conversion

fastspecfit.util.centers2edges(centers)

Convert bin centers to bin edges, guessing at what you probably meant.

Parameters:

centers (numpy.ndarray) – bin centers

Returns:

array

Return type:

bin edges, length = len(centers) + 1

fastspecfit.util.find_minima(x)[source]

Return indices of local minima of x, including edges.

The indices are sorted small to large.

Note

this is somewhat conservative in the case of repeated values: find_minima([1,1,1,2,2,2]) -> [0,1,2,4,5]

Parameters:

x (array-like) – The data array.

Returns:

The indices.

Return type:

(array)

fastspecfit.util.ivar2var(ivar, clip=0.001, sigma=False, allmasked_ok=False)[source]

Safely convert an inverse variance to a variance. Note that we clip at 1e-3 by default, not zero.

fastspecfit.util.minfit(x, y, return_coeff=False)[source]

Fits y = y0 + ((x-x0)/xerr)**2

See redrock.zwarning.ZWarningMask.BAD_MINFIT for zwarn failure flags

Parameters:
  • x (array) – x values.

  • y (array) – y values.

Returns:

(x0, xerr, y0, zwarn) where zwarn=0 is good fit.

Return type:

(tuple)

fastspecfit.util.mwdust_transmission(ebv, filtername)[source]

Convert SFD E(B-V) value to dust transmission 0-1 given the bandpass.

Parameters:
  • ebv (float or array-like) – SFD E(B-V) value(s)

  • filtername (str) – Filter name, e.g., ‘decam2014-r’.

Returns:

Scalar or array (same as ebv input), Milky Way dust transmission 0-1.

Note

This function tabulates the total-to-selective extinction ratio, k_X=A(X)/E(B-V) for many different filter bandpasses, X, where A(X)=-2.5*log10(transmission in X band). And so given the ebv, it returns mwdust_transmission=10**(-0.4*k_X*ebv).

Returns:

scalar, total extinction A(band) = -2.5*log10(transmission(band))

Notes

Based on desiutil.dust.mwdust_transmission.

fastspecfit.util.trapz_rebin(src_x, src_y, bin_centers, out=None, pre=None)[source]

Resample an signal src_y sampled at points src_x into into a series of x-axis bins, in an area-conserving way

Parameters:
  • src_y (numpy.ndarray [npix]) – Input signal

  • src_x (numpy.ndarray [npix]) – array of x coordinates corresponing to src_y

  • bin_centers (numpy.ndarray [noutpix]) – center x coordinates of output bins

  • out (numpy.ndarray or None) – if not None, an array of correct size and type that will receive the result of the computation. If None, a new array will be allocated.

  • pre – preprocessing data computed by trapz_rebin_pre(), if available. If not None, it must correspond to the input bin_centers array.

Returns:

Resampled signal at centers of each bin

Return type:

numpy.ndarray [noutpix]

fastspecfit.util.trapz_rebin_pre(bin_centers)[source]

Perform preprocessing on the bin centers passed to trapz_rebin() to avoid some computations each time it is called with these same centers.

Parameters:

bin_centers (numpy.ndarray [npix]) – array of bin centers for trapz_rebin()

Returns:

  • A preprocessing data structure that can be passed as the

  • ’pre’ argument of trapz_rebin whenever it is called with

  • the given bin_centers (for *any src_x and src_y)*

fastspecfit.util.var2ivar(var, sigma=False)[source]

Simple function to safely turn a (scalar, for now) variance into an inverse variance.

if sigma=True then assume that var is a standard deviation