API Reference

kcorrect

class kcorrect.kcorrect.Kcorrect(responses=None, templates=None, responses_out=None, responses_map=None, redshift_range=[0.0, 2.0], nredshift=4000, abcorrect=False, cosmo=None)[source]

Bases: Fitter

K-correction object

Parameters
  • abcorrect (bool) – correct maggies to AB (default False)

  • responses (list of str) – names of input responses to base SED on

  • templates (list of kcorrect.template.SED) – templates to use (if None uses v4 default template set)

  • responses_out (list of str) – output responses for K-corrections (default to “responses”)

  • responses_map (list of str) – input responses to use for K-corrections (default to “responses”)

  • redshift_range (list of np.float32) – minimum and maximum redshifts (default [0., 2.])

  • nredshift (int or np.int32) – number of redshifts in interpolation grid (default 4000)

  • cosmo (astropy.cosmology.FLRW-like object) – object with distmod() method (default Planck18)

Variables
  • abcorrect (bool) – correct maggies to AB

  • Amatrix (scipy.interpolate.interp1d object) – interpolation function for each template and input response

  • AmatrixOut (scipy.interpolate.interp1d object) – interpolation function for each template and output response

  • cosmo (astropy.cosmology.FLRW-like object) – object with luminosity_distance() method

  • imap (ndarray of np.int32) – for each responses_map element, its corresponding index in responses

  • nredshift (int or np.int32) – number of redshifts in interpolation grid

  • redshift_range (list of np.float32) – minimum and maximum redshifts

  • redshifts (ndarray of np.float32) – redshifts in grid

  • responses (list of str) – [Nin] names of input responses to use

  • responses_map (list of str) – [Nout] input responses to use for K-corrections

  • responses_out (list of str) – [Nout] output responses for K-corrections

  • templates (kcorrect.template.Template object) – templates to use

Notes

K-corrections are magnitude shifts to account for the difference in the observed bandpass R and a desired output bandpass Q, denoted K_QR (see Blanton & Roweis 2007). The default behavior finds the K-corrections between each input bandpass with itself, i.e. K_QQ.

In detail:

“responses” corresponds to the observed bandpasses R.

“responses_out” corresponds to the observed bandpasses Q; it defaults to “responses”.

“responses_map” is the same length as “responses_out” and defines which bandpasses in “responses” to use for each output bandpass in “responses_out”. It defaults to “responses”.

Amatrix accepts a redshift as its argument and returns a matrix of shape [nresponses, ntemplates]. This matrix can be dotted into a set of coefficients for each template, and the result will be the observed bandpass maggies at the desired redshift for an SED corresponding to the coefficients. A ValueError results if the input redshift is outside redshift_range.

AmatrixOut is similar but returns a [nresponses_out, ntemplates] matrix for the output bandpasses.

absmag(maggies=None, ivar=None, redshift=None, coeffs=None, band_shift=0.0)[source]

Return absolute magnitude in output bands

Parameters
  • redshift (ndarray of np.float32, or np.float32) – redshift(s) for K-correction

  • maggies (ndarray of np.float32) – fluxes of each band in maggies

  • ivar (ndarray of np.float32) – inverse variance of each band

  • coeffs (ndarray of np.float32) – coefficients for each template for each object

  • band_shift (np.float32) – shift to apply for output responses

Returns

absmag (ndarray of np.float32) – AB absolute magnitude in each band for each object

Notes

Returns the K-corrected absolute magnitude (or -9999 if there is no valid value).

Depends on having run fit_coeffs on a consistent set of maggies and ivars. If ivar=0 or the maggies are negative for any band, it uses the reconstructed absolute magnitude.

Determines the distance modulus with the object’s “cosmo.distmod()” method. By default this is the Planck18 cosmology. This use

If abcorrect is True, calls to_ab() method on input maggies to convert to AB.

derived(redshift=None, coeffs=None, band_shift=0.0)[source]

Return derived quantities based on coefficients

Parameters
  • redshift (ndarray of np.float32, or np.float32) – redshift

  • coeffs (ndarray of np.float32) – coefficients for each template for each object

  • band_shift (np.float32) – band shift to apply for output response

Returns

derived (dict()) – dictionary with derived quantities (see below)

Notes

The derived dictionary contains the following keys with the associated quantities:

‘mremain’ndarray of np.float32, or np.float32

current stellar mass in solar masses

‘intsfh’ndarray of np.float32, or np.float32

current stellar mass in solar masses

‘mtol’ndarray of np.float32, or np.float32

mass-to-light ratio in each output band

‘b300’ndarray of np.float32, or np.float32

current (< 300 Myr) over past star formation

‘b1000’ndarray of np.float32, or np.float32

current (< 1 Gyr) over past star formation

‘metallicity’ :

metallicity in current stars

All of these quantities should be taken with extreme caution and not accepted literally. After all, they are just the result of a 5-template fit to a few bandpasses. See Moustakas et al. (2013) for a comparison of the masses with other estimators.

fit_coeffs(redshift=None, maggies=None, ivar=None)

Fit coefficients

Parameters
  • redshift (np.float32 or ndarray of np.float32) – redshift(s)

  • maggies (ndarray of np.float32) – fluxes of each band in AB maggies

  • ivar (ndarray of np.float32) – inverse variance of each band

Returns

coeffs (ndarray of np.float32) – coefficients for each template

Notes

maggies are assumed to be Galactic-extinction corrected already.

Calls this class’s to_ab() method on input maggies.

If redshift is an array, even with just one element, coeffs is returned as an [nredshift, ntemplate] array.

Otherwise coeffs is returned as an [ntemplate] array.

Occasionally the optimizer will report “NNLS quitting on iteration count.” This indicates that the default number of iterations for scipy.optimize.nnls was not enough. Under these conditions, this code tries a much larger number of iterations. If that still fails, you will receive a traceback.

kcorrect(redshift=None, coeffs=None, band_shift=0.0)[source]

Return K-correction in all bands

Parameters
  • redshift (ndarray of np.float32, or np.float32) – redshift for K-correction

  • coeffs (ndarray of np.float32) – coefficients for each template for each object

  • band_shift (np.float32) – shift to apply for output responses

Returns

kcorrect (ndarray of np.float32) – K-correction from input to output magnitudes

reconstruct(redshift=None, coeffs=None, band_shift=0.0)

Reconstruct AB maggies associated with coefficients

Parameters
  • redshift (np.float32 or ndarray of np.float32) – redshift

  • coeffs (ndarray of np.float32) – coefficients

  • band_shift (np.float32) – blueshift to apply to reconstructed bandpasses

Returns

maggies (ndarray of np.float32) – AB maggies in each band

Notes

Returns AB maggies, but note that if to_ab() is non-trivial, these may not be directly comparable to the input maggies.

reconstruct_out(redshift=None, coeffs=None, band_shift=0.0)[source]

Reconstruct output maggies associated with coefficients

Parameters
  • redshift (np.float32) – redshift

  • coeffs (ndarray of np.float32) – coefficients

  • band_shift (np.float32) – blueshift to apply to reconstructed bandpasses

Returns

maggies (ndarray of np.float32) – AB maggies in each output band

set_Amatrix()

Set Amatrix, interpolator for the design matrix

to_ab(maggies=None, ivar=None)

Convert input maggies to AB

Parameters
  • maggies (ndarray of np.float32) – array of fluxes in standard SDSS system

  • ivar (ndarray of np.float32) – inverse variances in standard SDSS system (optional)

Returns

  • ab_maggies (ndarray of np.float32) – array of fluxes converted to AB

  • ab_ivar (ndarray of np.float32) – inverse variances converted to AB (if ivar input)

Notes

This method just returns maggies and/or ivar unchanged, as for this object we expect AB maggies on input.

fit_coeffs() calls this on its inputs.

class kcorrect.kcorrect.KcorrectGST(responses=['galex_FUV', 'galex_NUV', 'sdss_u0', 'sdss_g0', 'sdss_r0', 'sdss_i0', 'sdss_z0', 'twomass_J', 'twomass_H', 'twomass_Ks'], templates=None, responses_out=None, responses_map=None, redshift_range=[0.0, 2.0], nredshift=4000, abcorrect=False, cosmo=None)[source]

Bases: Kcorrect

K-correction object for GALEX, SDSS, 2MASS data

Parameters
  • abcorrect (bool) – correct maggies to AB (default True)

  • templates (list of kcorrect.template.SED) – templates to use (if None uses v4 default template set)

  • responses (list of str) – names of input responses to base SED on (default to FNugrizJHK)

  • responses_out (list of str) – output responses for K-corrections (default to “responses”)

  • responses_map (list of str) – input responses to use for K-corrections (default to “responses”)

  • redshift_range (list of np.float32) – minimum and maximum redshifts (default [0., 2.])

  • nredshift (int or np.int32) – number of redshifts in interpolation grid (default 4000)

  • cosmo (astropy.cosmology.FLRW-like object) – object with distmod() method (default Planck18)

Variables
  • abcorrect (bool) – correct maggies to AB

  • Amatrix (scipy.interpolate.interp1d object) – interpolation function for each template and input response

  • AmatrixOut (scipy.interpolate.interp1d object) – interpolation function for each template and output response

  • cosmo (astropy.cosmology.FLRW-like object) – object with luminosity_distance() method

  • imap (ndarray of np.int32) – for each responses_map element, its corresponding index in responses

  • nredshift (int or np.int32) – number of redshifts in interpolation grid

  • redshift_range (list of np.float32) – minimum and maximum redshifts

  • redshifts (ndarray of np.float32) – redshifts in grid

  • responses (list of str) – [Nin] names of input responses to use

  • responses_map (list of str) – [Nout] input responses to use for K-corrections

  • responses_out (list of str) – [Nout] output responses for K-corrections

  • templates (kcorrect.template.Template object) – templates to use

Notes

The ‘responses’ input defaults to [‘galex_FUV’, ‘galex_NUV’, ‘sdss_u0’, ‘sdss_g0’, ‘sdss_r0’, ‘sdss_i0’, ‘sdss_z0’, ‘twomass_J’, ‘twomass_H’, ‘twomass_Ks’]

abcorrect is by default False and the input maggies are assumed to be AB. If abcorrect is set to True, the to_ab() method is applied to the maggies input for absmag() and fit_coeffs() and fit_coeffs_asinh(), which adjusts from the SDSS system to the AB system. However, there is no change applied to the 2MASS or or GALEX inputs.

absmag(maggies=None, ivar=None, redshift=None, coeffs=None, band_shift=0.0)

Return absolute magnitude in output bands

Parameters
  • redshift (ndarray of np.float32, or np.float32) – redshift(s) for K-correction

  • maggies (ndarray of np.float32) – fluxes of each band in maggies

  • ivar (ndarray of np.float32) – inverse variance of each band

  • coeffs (ndarray of np.float32) – coefficients for each template for each object

  • band_shift (np.float32) – shift to apply for output responses

Returns

absmag (ndarray of np.float32) – AB absolute magnitude in each band for each object

Notes

Returns the K-corrected absolute magnitude (or -9999 if there is no valid value).

Depends on having run fit_coeffs on a consistent set of maggies and ivars. If ivar=0 or the maggies are negative for any band, it uses the reconstructed absolute magnitude.

Determines the distance modulus with the object’s “cosmo.distmod()” method. By default this is the Planck18 cosmology. This use

If abcorrect is True, calls to_ab() method on input maggies to convert to AB.

derived(redshift=None, coeffs=None, band_shift=0.0)

Return derived quantities based on coefficients

Parameters
  • redshift (ndarray of np.float32, or np.float32) – redshift

  • coeffs (ndarray of np.float32) – coefficients for each template for each object

  • band_shift (np.float32) – band shift to apply for output response

Returns

derived (dict()) – dictionary with derived quantities (see below)

Notes

The derived dictionary contains the following keys with the associated quantities:

‘mremain’ndarray of np.float32, or np.float32

current stellar mass in solar masses

‘intsfh’ndarray of np.float32, or np.float32

current stellar mass in solar masses

‘mtol’ndarray of np.float32, or np.float32

mass-to-light ratio in each output band

‘b300’ndarray of np.float32, or np.float32

current (< 300 Myr) over past star formation

‘b1000’ndarray of np.float32, or np.float32

current (< 1 Gyr) over past star formation

‘metallicity’ :

metallicity in current stars

All of these quantities should be taken with extreme caution and not accepted literally. After all, they are just the result of a 5-template fit to a few bandpasses. See Moustakas et al. (2013) for a comparison of the masses with other estimators.

fit_coeffs(redshift=None, maggies=None, ivar=None)

Fit coefficients

Parameters
  • redshift (np.float32 or ndarray of np.float32) – redshift(s)

  • maggies (ndarray of np.float32) – fluxes of each band in AB maggies

  • ivar (ndarray of np.float32) – inverse variance of each band

Returns

coeffs (ndarray of np.float32) – coefficients for each template

Notes

maggies are assumed to be Galactic-extinction corrected already.

Calls this class’s to_ab() method on input maggies.

If redshift is an array, even with just one element, coeffs is returned as an [nredshift, ntemplate] array.

Otherwise coeffs is returned as an [ntemplate] array.

Occasionally the optimizer will report “NNLS quitting on iteration count.” This indicates that the default number of iterations for scipy.optimize.nnls was not enough. Under these conditions, this code tries a much larger number of iterations. If that still fails, you will receive a traceback.

kcorrect(redshift=None, coeffs=None, band_shift=0.0)

Return K-correction in all bands

Parameters
  • redshift (ndarray of np.float32, or np.float32) – redshift for K-correction

  • coeffs (ndarray of np.float32) – coefficients for each template for each object

  • band_shift (np.float32) – shift to apply for output responses

Returns

kcorrect (ndarray of np.float32) – K-correction from input to output magnitudes

reconstruct(redshift=None, coeffs=None, band_shift=0.0)

Reconstruct AB maggies associated with coefficients

Parameters
  • redshift (np.float32 or ndarray of np.float32) – redshift

  • coeffs (ndarray of np.float32) – coefficients

  • band_shift (np.float32) – blueshift to apply to reconstructed bandpasses

Returns

maggies (ndarray of np.float32) – AB maggies in each band

Notes

Returns AB maggies, but note that if to_ab() is non-trivial, these may not be directly comparable to the input maggies.

reconstruct_out(redshift=None, coeffs=None, band_shift=0.0)

Reconstruct output maggies associated with coefficients

Parameters
  • redshift (np.float32) – redshift

  • coeffs (ndarray of np.float32) – coefficients

  • band_shift (np.float32) – blueshift to apply to reconstructed bandpasses

Returns

maggies (ndarray of np.float32) – AB maggies in each output band

set_Amatrix()

Set Amatrix, interpolator for the design matrix

to_ab(maggies=None, ivar=None)[source]

Convert FNugrizJHK input maggies to AB

Parameters
  • maggies (ndarray of np.float32) – array of fluxes in standard SDSS system

  • ivar (ndarray of np.float32) – inverse variances in standard SDSS system (optional)

Returns

  • ab_maggies (ndarray of np.float32) – array of fluxes converted to AB

  • ab_ivar (ndarray of np.float32) – inverse variances converted to AB (if ivar input)

Notes

Leaves FN and JHK alone, and fixes ugriz with kcorrect.utils.sdss_ab_correct(), which does the following:

Uses the AB conversions produced by D. Eisenstein, in his message sdss-calib/1152

u(AB,2.5m) = u(database, 2.5m) - 0.036
g(AB,2.5m) = g(database, 2.5m) + 0.012
r(AB,2.5m) = r(database, 2.5m) + 0.010
i(AB,2.5m) = i(database, 2.5m) + 0.028
z(AB,2.5m) = z(database, 2.5m) + 0.040

fit_coeffs() and absmag() call this on their inputs if abcorrect is True.

class kcorrect.kcorrect.KcorrectSDSS(responses=['sdss_u0', 'sdss_g0', 'sdss_r0', 'sdss_i0', 'sdss_z0'], templates=None, responses_out=None, responses_map=None, redshift_range=[0.0, 2.0], nredshift=4000, abcorrect=True, cosmo=None)[source]

Bases: Kcorrect

K-correction object for SDSS data

Parameters
  • abcorrect (bool) – correct maggies to AB (default True)

  • templates (list of kcorrect.template.SED) – templates to use (if None uses v4 default template set)

  • responses (list of str) – names of input responses to base SED on (default to SDSS ugriz)

  • responses_out (list of str) – output responses for K-corrections (default to “responses”)

  • responses_map (list of str) – input responses to use for K-corrections (default to “responses”)

  • redshift_range (list of np.float32) – minimum and maximum redshifts (default [0., 2.])

  • nredshift (int or np.int32) – number of redshifts in interpolation grid (default 4000)

  • cosmo (astropy.cosmology.FLRW-like object) – object with distmod() method (default Planck18)

Variables
  • abcorrect (bool) – correct maggies to AB

  • Amatrix (scipy.interpolate.interp1d object) – interpolation function for each template and input response

  • AmatrixOut (scipy.interpolate.interp1d object) – interpolation function for each template and output response

  • cosmo (astropy.cosmology.FLRW-like object) – object with luminosity_distance() method

  • imap (ndarray of np.int32) – for each responses_map element, its corresponding index in responses

  • nredshift (int or np.int32) – number of redshifts in interpolation grid

  • redshift_range (list of np.float32) – minimum and maximum redshifts

  • redshifts (ndarray of np.float32) – redshifts in grid

  • responses (list of str) – [Nin] names of input responses to use

  • responses_map (list of str) – [Nout] input responses to use for K-corrections

  • responses_out (list of str) – [Nout] output responses for K-corrections

  • templates (kcorrect.template.Template object) – templates to use

Notes

The ‘responses’ input defaults to [‘sdss_u0’, ‘sdss_g0’, ‘sdss_r0’, ‘sdss_i0’, ‘sdss_z0’]

This class provides the method fit_coeffs_asinh() to use SDSS-style asinh magnitudes (these are the magnitudes that the SDSS imaging reports).

If abcorrect is True, the to_ab() method is applied to the maggies input for absmag() and fit_coeffs() and fit_coeffs_asinh(), which adjusts from the SDSS system to the AB system.

absmag(maggies=None, ivar=None, redshift=None, coeffs=None, band_shift=0.0)

Return absolute magnitude in output bands

Parameters
  • redshift (ndarray of np.float32, or np.float32) – redshift(s) for K-correction

  • maggies (ndarray of np.float32) – fluxes of each band in maggies

  • ivar (ndarray of np.float32) – inverse variance of each band

  • coeffs (ndarray of np.float32) – coefficients for each template for each object

  • band_shift (np.float32) – shift to apply for output responses

Returns

absmag (ndarray of np.float32) – AB absolute magnitude in each band for each object

Notes

Returns the K-corrected absolute magnitude (or -9999 if there is no valid value).

Depends on having run fit_coeffs on a consistent set of maggies and ivars. If ivar=0 or the maggies are negative for any band, it uses the reconstructed absolute magnitude.

Determines the distance modulus with the object’s “cosmo.distmod()” method. By default this is the Planck18 cosmology. This use

If abcorrect is True, calls to_ab() method on input maggies to convert to AB.

derived(redshift=None, coeffs=None, band_shift=0.0)

Return derived quantities based on coefficients

Parameters
  • redshift (ndarray of np.float32, or np.float32) – redshift

  • coeffs (ndarray of np.float32) – coefficients for each template for each object

  • band_shift (np.float32) – band shift to apply for output response

Returns

derived (dict()) – dictionary with derived quantities (see below)

Notes

The derived dictionary contains the following keys with the associated quantities:

‘mremain’ndarray of np.float32, or np.float32

current stellar mass in solar masses

‘intsfh’ndarray of np.float32, or np.float32

current stellar mass in solar masses

‘mtol’ndarray of np.float32, or np.float32

mass-to-light ratio in each output band

‘b300’ndarray of np.float32, or np.float32

current (< 300 Myr) over past star formation

‘b1000’ndarray of np.float32, or np.float32

current (< 1 Gyr) over past star formation

‘metallicity’ :

metallicity in current stars

All of these quantities should be taken with extreme caution and not accepted literally. After all, they are just the result of a 5-template fit to a few bandpasses. See Moustakas et al. (2013) for a comparison of the masses with other estimators.

fit_coeffs(redshift=None, maggies=None, ivar=None)

Fit coefficients

Parameters
  • redshift (np.float32 or ndarray of np.float32) – redshift(s)

  • maggies (ndarray of np.float32) – fluxes of each band in AB maggies

  • ivar (ndarray of np.float32) – inverse variance of each band

Returns

coeffs (ndarray of np.float32) – coefficients for each template

Notes

maggies are assumed to be Galactic-extinction corrected already.

Calls this class’s to_ab() method on input maggies.

If redshift is an array, even with just one element, coeffs is returned as an [nredshift, ntemplate] array.

Otherwise coeffs is returned as an [ntemplate] array.

Occasionally the optimizer will report “NNLS quitting on iteration count.” This indicates that the default number of iterations for scipy.optimize.nnls was not enough. Under these conditions, this code tries a much larger number of iterations. If that still fails, you will receive a traceback.

fit_coeffs_asinh(redshift=None, mag=None, mag_err=None, extinction=None)[source]

Fit coefficients to asinh mags

Parameters
  • redshift (np.float32 or ndarray of np.float32) – [N] or scalar redshift(s)

  • mag (ndarray of np.float32) – [N, 5] or [5] asinh magnitudes of each SDSS band

  • mag_err (ndarray of np.float32) – [N, 5] or [5] inverse variance of each band

  • extinction (ndarray of np.float32) – [N, 5] or [5] Galactic extinction in each band

Returns

coeffs (ndarray of np.float32) – coefficients for each template

Notes

Converts mag, mag_err, and extinction to extinction-corrected maggies and ivar, and then (if abcorrect is True) calls to_ab() method to create AB maggies and ivar.

If redshift is an array, even with just one element, coeffs is returned as an [nredshift, ntemplate] array.

Otherwise coeffs is returned as an [ntemplate] array.

kcorrect(redshift=None, coeffs=None, band_shift=0.0)

Return K-correction in all bands

Parameters
  • redshift (ndarray of np.float32, or np.float32) – redshift for K-correction

  • coeffs (ndarray of np.float32) – coefficients for each template for each object

  • band_shift (np.float32) – shift to apply for output responses

Returns

kcorrect (ndarray of np.float32) – K-correction from input to output magnitudes

reconstruct(redshift=None, coeffs=None, band_shift=0.0)

Reconstruct AB maggies associated with coefficients

Parameters
  • redshift (np.float32 or ndarray of np.float32) – redshift

  • coeffs (ndarray of np.float32) – coefficients

  • band_shift (np.float32) – blueshift to apply to reconstructed bandpasses

Returns

maggies (ndarray of np.float32) – AB maggies in each band

Notes

Returns AB maggies, but note that if to_ab() is non-trivial, these may not be directly comparable to the input maggies.

reconstruct_out(redshift=None, coeffs=None, band_shift=0.0)

Reconstruct output maggies associated with coefficients

Parameters
  • redshift (np.float32) – redshift

  • coeffs (ndarray of np.float32) – coefficients

  • band_shift (np.float32) – blueshift to apply to reconstructed bandpasses

Returns

maggies (ndarray of np.float32) – AB maggies in each output band

set_Amatrix()

Set Amatrix, interpolator for the design matrix

to_ab(maggies=None, ivar=None)[source]

Convert input maggies to AB

Parameters
  • maggies (ndarray of np.float32) – array of fluxes in standard SDSS system

  • ivar (ndarray of np.float32) – inverse variances in standard SDSS system (optional)

Returns

  • ab_maggies (ndarray of np.float32) – array of fluxes converted to AB

  • ab_ivar (ndarray of np.float32) – inverse variances converted to AB (if ivar input)

Notes

Calls kcorrect.utils.sdss_ab_correct(), which does the following:

Uses the AB conversions produced by D. Eisenstein, in his message sdss-calib/1152

u(AB,2.5m) = u(database, 2.5m) - 0.036
g(AB,2.5m) = g(database, 2.5m) + 0.012
r(AB,2.5m) = r(database, 2.5m) + 0.010
i(AB,2.5m) = i(database, 2.5m) + 0.028
z(AB,2.5m) = z(database, 2.5m) + 0.040

fit_coeffs() and absmag() call this on their inputs if abcorrect is True.

response

class kcorrect.response.Response(filename=None, wave=None, response=None)[source]

Bases: object

Astronomical bandpass description

Parameters
  • filename (str) – file name to read from

  • wave (ndarray of np.float32) – wavelength grid in Angstroms

  • response (ndarray of np.float32) – response function

Variables
  • filename (str) – source filename, or None

  • interp (scipy.interpolate.interp1d object) – interpolation object

  • lambda_eff (np.float32) – effective wavelength in Angstroms

  • nwave (int) – number of wavelength samples

  • response (ndarray of np.float32) – response function

  • solar_magnitude (np.float32) – absolute magnitude of Sun through filter, or None

  • solar_sed (kcorrect.template.SED object) – SED associated with Sun for solar_magnitude

  • vega2ab (np.float32) – magnitude offset from Vega to AB (m_AB - m_Vega), or None

  • vega_sed (kcorrect.template.SED object) – SED associated with Vega for vega_magnitude

  • wave (ndarray of np.float32) – wavelength grid in Angstroms

Notes

The response should be the relative response of the system (atmosphere, telescope, detector, etc) to a photon at each given wavelength entering the Earth’s atmosphere (for a ground based telescope) or the telescope aperture (space based).

If a file is given it is assumed to be in Yanny parameter format, readable and writeable by the class pydl.pydlutils.yanny.yanny

The attribute interp() takes wavelength in Angstroms as its one positional argument.

The effective wavelength is defined as described in Blanton & Roweis (2007).

fromfits(filename=None, ext='RESPONSE')[source]

Read response from FITS files

Parameters
  • filename (str) – input file name

  • ext (str or int) – extension to read from

frompar(filename=None)[source]

Read response from Yanny parameter file

Parameters

filename (str) – input file name

Notes

If an absolute path, reads that. If not, looks relative to KCORRECT_DIR/python/kcorrect/data/responses

project(sed=None, wave=None, flux=None)[source]

Project spectrum onto response

Parameters
  • sed (kcorrect.template.SED object) – spectrum in kcorrect format

  • wave (ndarray of np.float32) – wavelength grid (used only if sed and func not set)

  • flux (ndarray of np.float32) – flux grid (used only if sed and func not set)

Returns

maggies (np.float32) – [nsed] nmaggies associated with spectrum through bandpass

Notes

Fluxes should be in erg cm^{-2} s^{-1} Angstrom^{-1}

Assumes AB calibration.

If the bandpass is outside the range of the solar model, 0 is returned.

response_dtype()[source]

Returns numpy dtype for SED

set_interp()[source]

Sets attribute interp to interpolation function

set_lambda_eff()[source]

Set effective wavelength

Notes

Sets attribute lambda_eff

set_solar_magnitude()[source]

Set absolute magnitude of Sun through filter

Notes

Uses lcbsun.ori model from Lejeune et al (1997)

If the response function is outside the model wavelength range, solar_magnitude is set to None.

set_vega2ab()[source]

Set Vega to AB magnitude conversion

Notes

Uses lcbvega.ori model from Lejeune et al (1997)

If the response function is outside the model wavelength range, vega2ab is set to None.

tofits(filename=None, ext='FLUX', clobber=True)[source]

Write SED to FITS files

filenamestr

output file name

extstr or int

extension to write to

clobberbool

whether to clobber the existing file or add an HDU

class kcorrect.response.ResponseDict(*args, **kwargs)[source]

Bases: dict

Dictionary of all responses (singleton)

load_response(response=None, reload=False)[source]

Load response into dictionary

Parameters
  • response (str) – response to load

  • reload (bool) – if True, reload the response if already in ResponseDict (default False)

class kcorrect.response.ResponseDictSingleton[source]

Bases: type

kcorrect.response.all_responses(response_dir='/home/docs/checkouts/readthedocs.org/user_builds/kcorrect/envs/5.0.0b/lib/python3.8/site-packages/kcorrect/data/responses', check_validity=False)[source]

List all responses available

Parameters
  • response_dir (str) – path to directory containing responses

  • check_validity (bool) – if True, check the validity of the files

Returns

responses (list of str) – response names for available bandpasses

Notes

Returns all base names of files with the suffix ‘.par’ in the response_dir directory.

By default, response_dir is the “data/responses” directory within the kcorrect Python distribution.

If the user specifies response_dir and check_validity is False, there is no guarantee that the response files in the specified directory are valid!

If check_validity is True, the responses are also loaded into the ResponseDict() singleton.

template

class kcorrect.template.SED(filename=None, wave=None, flux=None, ext='FLUX')[source]

Bases: object

Spectral energy distribution(s)

Parameters
  • filename (str) – name of FITS file to read from

  • wave (ndarray of np.float32) – rest frame wavelength grid in Angstroms

  • flux (ndarray of np.float32) – [nsed, nwave] rest frame flux grid in erg/cm^2/s/A at 10pc

Variables
  • filename (str) – name of FITS file associated with this SED

  • flux (ndarray of np.float32) – [nsed, nwave] flux grid in erg/cm^2/s/A at 10pc and currently set redshift

  • info (dict) – dictionary for storing assorted metadata associated with spectra

  • nsed (np.float32) – number of SEDs

  • nwave (np.int32) – number of wavelengths in grid

  • redshift (np.float32) – redshift of SED

  • restframe_flux (ndarray of np.float32) – [nsed, nwave] rest frame flux grid in erg/cm^2/s/A at 10pc

  • restframe_wave (ndarray of np.float32) – [nwave] rest frame wavelength grid in Angstroms

  • wave (ndarray of np.float32) – [nwave] wavelength grid in Angstroms

Notes

The fluxes are a bit funnily defined, in terms of the flux that the galaxy would have at 10pc, analogous to an absolute magnitude. When the redshift is applied, the bolometric flux is conserved (i.e. there is no luminosity distance applied, it is a pure redshifting of the spectrum).

If filename is set, overrides wave and flux.

fromfits(filename=None, ext='FLUX')[source]

Read SED from FITS files

Parameters
  • filename (str) – input file name

  • ext (str or int) – extension to read from

Notes

The FITS table should have two columns:

wave - an [nwave] array of wavelengths in Angstrom flux - an [nsed, nwave] array of fluxes

Only imports the first row of the FITS table.

sed_dtype()[source]

Returns numpy dtype for SED

set_interp()[source]

Sets attribute interp to interpolation function

set_redshift(redshift=0.0)[source]

Set redshift of SED

Parameters

redshift (np.float32) – redshift to shift to

Notes

Conserves bolometric integral of flux.

tofits(filename=None, ext='FLUX', clobber=True)[source]

Write SED to FITS files

Parameters
  • filename (str) – output file name

  • ext (str or int) – extension to write to

  • clobber (bool) – whether to clobber the existing file or add an HDU

Notes

Writes a one-row FITS table with two columns:

wave - an [nwave] array of restframe wavelengths in Angstrom flux - an [nsed, nwave] array of restframe fluxes

Only imports the first row of the FITS table.

class kcorrect.template.Template(filename=None, wave=None, flux=None, ext='FLUX')[source]

Bases: SED

Spectral energy distribution template(s)

Parameters
  • filename (str) – name of FITS file to read from

  • wave (ndarray of np.float32) – rest frame wavelength grid in Angstroms

  • flux (ndarray of np.float32) – [nsed, nwave] rest frame flux grid in erg/cm^2/s/A

Variables
  • filename (str) – name of FITS file associated with this SED

  • flux (ndarray of np.float32) – [nsed, nwave] flux grid in erg/cm^2/s/A

  • info (dict) – dictionary for storing assorted metadata associated with spectra

  • intsfh (ndarray of np.float32) – [nsed] integrated star formation history in solar masses

  • m300 (ndarray of np.float32) – [nsed] stars formed last 300 million years in solar masses

  • m1000 (ndarray of np.float32) – [nsed] stars formed last billion years in solar masses

  • mets (ndarray of np.float32) – [nsed] metallicity in current stars and stellar remnants

  • mremain (ndarray of np.float32) – [nsed] remaining mass in stars and remnants in solar masses

  • nsed (np.float32) – number of SEDs

  • nwave (np.int32) – number of wavelengths in grid

  • redshift (np.float32) – redshift of SED

  • restframe_flux (ndarray of np.float32) – [nsed, nwave] rest frame flux grid in erg/cm^2/s/A

  • restframe_wave (ndarray of np.float32) – [nwave] rest frame wavelength grid in Angstroms

  • wave (ndarray of np.float32) – [nwave] wavelength grid in Angstroms

Notes

If filename is set, overrides wave and flux.

The file should have the HDUs:

FLUXan ndarray with two tags:

‘wave’ : an [nwave]-array of np.float32 with wavelength in Ang. ‘flux’ : an [nsed, nwave]-array of np.float32 with flux in erg/s/cm^2/A

METS : an [nsed]-array with metallicity INTSFH : an [nsed]-array with integrated SF in solar units MREMAIN : an [nsed]-array with current stellar mass in solar units M300 : an [nsed]-array with mass formed within 300 My in solar units M1000 : an [nsed]-array with mass formed within 1 Gy in solar units

utils

kcorrect.utils.error_floor(floor=None, maggies=None, ivar=None)[source]

Calculate new inverse variance with fractional error floors

Parameters
  • floor (ndarray of np.float32) – [Nr] fractional error floor for each response

  • maggies (ndarray of np.float32) – [N, Nr] or [Nr] array of maggies

  • ivar (ndarray of np.float32) – [N, Nr] or [Nr] array of inverse variance of maggies

Returns

ivar (ndarray of np.float32) – [N, Nr] or [Nr] array of inverse variances

kcorrect.utils.maggies2flambda(maggies=None, responses=None)[source]

Return arrays of wave, flambda for maggies

kcorrect.utils.read_basel(filename=None)[source]

Read Basel-style spectrum file

Parameters

filename (str) – file to read from

Returns

  • info (dict) – meta-data for model

  • wave (ndarray of np.float32) – [1221] wavelengths in nm

  • flux (ndarray of np.float32) – [N, 1221] fluxes in erg/cm^2/s/Hz

Notes

1221 hardcoded as number of spectral elements.

Model parameters stored as arrays in info attribute are:

‘modelno’ ‘teff’ ‘logg’ ‘mh’ ‘vturb’ ‘xh’

kcorrect.utils.sdss_ab_correct(maggies=None, ivar=None, abfix=[-0.036, 0.012, 0.01, 0.028, 0.04])[source]

Return AB maggies based on SDSS standard maggies

Parameters
  • maggies (ndarray of np.float32) – array of fluxes in standard SDSS system

  • ivar (ndarray of np.float32) – inverse variances in standard SDSS system (optional)

  • abfix (ndarray or list of np.float32) – [5]-array of magnitude offset to apply to standard values

Returns

  • ab_maggies (ndarray of np.float32) – array of fluxes converted to AB

  • ab_ivar (ndarray of np.float32) – inverse variances converted to AB (if ivar input)

Notes

Uses the AB conversions produced by D. Eisenstein, in his message sdss-calib/1152

u(AB,2.5m) = u(database, 2.5m) - 0.036
g(AB,2.5m) = g(database, 2.5m) + 0.012
r(AB,2.5m) = r(database, 2.5m) + 0.010
i(AB,2.5m) = i(database, 2.5m) + 0.028
z(AB,2.5m) = z(database, 2.5m) + 0.040

Unless an alternative is specified with the abfix parameter.

kcorrect.utils.sdss_asinh_to_maggies(mag=None, mag_err=None, extinction=None)[source]

Calculate maggies and ivar based on catalog parameters

Parameters
  • mag (ndarray of np.float32) – [N, 5] or [5] array of asinh magnitudes from SDSS

  • mag_err (ndarray of np.float32) – [N, 5] or [5] array of asinh magnitude errors from SDSS

  • extinction (ndarray or list of np.float32) – [N, 5] or [5] array of Galactic extinctions from SDSS

Returns

  • maggies (ndarray of np.float32) – [N, 5] or [5] array of maggies

  • ivar (ndarray of np.float32) – [N, 5] or [5] array of inverse variances

Notes

If extinction set, applies extinction (after converting to maggies).

Does not apply AB corrections.

If mag_err is None on input, only maggies is returned.

kcorrect.utils.sed_ab(wave=None)[source]

Calculate AB source spectrum

Parameters

wave (ndarray of np.float32) – wavelength in Angstrom

Returns

ab (ndarray of np.float32) – AB flux in erg cm^{-2} s^{-1} Angstrom^{-1}

fitter

class kcorrect.fitter.Fitter(responses=None, templates=None, redshift_range=[0.0, 1.0], nredshift=2000, abcorrect=False)[source]

Bases: object

Nonnegative SED fitting object

Parameters
  • abcorrect (bool) – correct maggies to AB (default False)

  • responses (list of str) – names of responses to use

  • templates (kcorrect.template.Template object) – templates to use

  • redshift_range (list of np.float32) – minimum and maximum redshifts (default [0., 1.])

  • nredshift (int or np.int32) – number of redshifts in interpolation grid

Variables
  • abcorrect (bool) – correct maggies to AB

  • Amatrix (scipy.interpolate.interp1d object) – interpolator to produce A matrix (set to None until set_Amatrix called)

  • responses (list of str) – names of responses to use

  • nredshift (int or np.int32) – number of redshifts in interpolation grid

  • redshift_range (list of np.float32) – minimum and maximum redshifts (default [0., 1.])

  • redshifts (ndarray of np.float32) – redshifts in grid

  • templates (kcorrect.template.Template object) – templates to use

fit_coeffs(redshift=None, maggies=None, ivar=None)[source]

Fit coefficients

Parameters
  • redshift (np.float32 or ndarray of np.float32) – redshift(s)

  • maggies (ndarray of np.float32) – fluxes of each band in AB maggies

  • ivar (ndarray of np.float32) – inverse variance of each band

Returns

coeffs (ndarray of np.float32) – coefficients for each template

Notes

maggies are assumed to be Galactic-extinction corrected already.

Calls this class’s to_ab() method on input maggies.

If redshift is an array, even with just one element, coeffs is returned as an [nredshift, ntemplate] array.

Otherwise coeffs is returned as an [ntemplate] array.

Occasionally the optimizer will report “NNLS quitting on iteration count.” This indicates that the default number of iterations for scipy.optimize.nnls was not enough. Under these conditions, this code tries a much larger number of iterations. If that still fails, you will receive a traceback.

reconstruct(redshift=None, coeffs=None, band_shift=0.0)[source]

Reconstruct AB maggies associated with coefficients

Parameters
  • redshift (np.float32 or ndarray of np.float32) – redshift

  • coeffs (ndarray of np.float32) – coefficients

  • band_shift (np.float32) – blueshift to apply to reconstructed bandpasses

Returns

maggies (ndarray of np.float32) – AB maggies in each band

Notes

Returns AB maggies, but note that if to_ab() is non-trivial, these may not be directly comparable to the input maggies.

set_Amatrix()[source]

Set Amatrix, interpolator for the design matrix

to_ab(maggies=None, ivar=None)[source]

Convert input maggies to AB

Parameters
  • maggies (ndarray of np.float32) – array of fluxes in standard SDSS system

  • ivar (ndarray of np.float32) – inverse variances in standard SDSS system (optional)

Returns

  • ab_maggies (ndarray of np.float32) – array of fluxes converted to AB

  • ab_ivar (ndarray of np.float32) – inverse variances converted to AB (if ivar input)

Notes

This method just returns maggies and/or ivar unchanged, as for this object we expect AB maggies on input.

fit_coeffs() calls this on its inputs.