Skip to content

Commit

Permalink
New approach to nJy+Calib support
Browse files Browse the repository at this point in the history
  • Loading branch information
parejkoj committed Mar 8, 2019
1 parent 8ca1f6a commit 7a75a90
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
7 changes: 4 additions & 3 deletions python/lsst/pipe/tasks/colorterms.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@ def getFluxes(fluxField):
primaryFlux, primaryErr = getFluxes(self.primary + "_flux")
secondaryFlux, secondaryErr = getFluxes(self.secondary + "_flux")

primaryMag = u.Quantity(primaryFlux, u.Jy).to_value(u.ABmag)
secondaryMag = u.Quantity(secondaryFlux, u.Jy).to_value(u.ABmag)
primaryMag = u.Quantity(primaryFlux, u.nJy).to_value(u.ABmag)
secondaryMag = u.Quantity(secondaryFlux, u.nJy).to_value(u.ABmag)

refMag = self.transformMags(primaryMag, secondaryMag)
refFluxErrArr = self.propagateFluxErrors(primaryErr, secondaryErr)

refMagErr = abMagErrFromFluxErr(refFluxErrArr, primaryFlux)
# HACK convert to Jy until we have a replacement for this (DM-16903)
refMagErr = abMagErrFromFluxErr(refFluxErrArr*1e-9, primaryFlux*1e-9)

return refMag, refMagErr

Expand Down
33 changes: 11 additions & 22 deletions python/lsst/pipe/tasks/photoCal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import lsst.pex.config as pexConf
import lsst.pipe.base as pipeBase
from lsst.afw.image import abMagFromFlux, abMagErrFromFluxErr, Calib
from lsst.afw.image import abMagErrFromFluxErr, Calib
import lsst.afw.table as afwTable
from lsst.meas.astrom import DirectMatchTask, DirectMatchConfigWithoutLoader
import lsst.afw.display as afwDisplay
Expand Down Expand Up @@ -306,10 +306,10 @@ def extractMagArrays(self, matches, filterName, sourceKeys):
self.log.warn("Source catalog does not have flux uncertainties; using sqrt(flux).")
srcInstFluxErrArr = np.sqrt(srcInstFluxArr)

# convert source instFlux from DN to an estimate of Jy
JanskysPerABFlux = 3631.0
srcInstFluxArr = srcInstFluxArr*JanskysPerABFlux
srcInstFluxErrArr = srcInstFluxErrArr*JanskysPerABFlux
# convert source instFlux from DN to an estimate of nJy
referenceFlux = (0*u.ABmag).to_value(u.nJy)
srcInstFluxArr = srcInstFluxArr * referenceFlux
srcInstFluxErrArr = srcInstFluxErrArr * referenceFlux

if not matches:
raise RuntimeError("No reference stars are available")
Expand Down Expand Up @@ -360,14 +360,16 @@ def extractMagArrays(self, matches, filterName, sourceKeys):
fluxField)
refFluxErrArr = np.sqrt(refFluxArr)

refMagArr = u.Quantity(refFluxArr, u.Jy).to_value(u.ABmag)
refMagErrArr = abMagErrFromFluxErr(refFluxErrArr, refFluxArr)
refMagArr = u.Quantity(refFluxArr, u.nJy).to_value(u.ABmag)
# HACK convert to Jy until we have a replacement for this (DM-16903)
refMagErrArr = abMagErrFromFluxErr(refFluxErrArr*1e-9, refFluxArr*1e-9)

# compute the source catalog magnitudes and errors
srcMagArr = np.array([abMagFromFlux(sf) for sf in srcInstFluxArr])
srcMagArr = u.Quantity(srcInstFluxArr, u.nJy).to_value(u.ABmag)
# Fitting with error bars in both axes is hard
# for now ignore reference flux error, but ticket DM-2308 is a request for a better solution
magErrArr = abMagErrFromFluxErr(srcInstFluxErrArr, srcInstFluxArr)
# HACK convert to Jy until we have a replacement for this (DM-16903)
magErrArr = abMagErrFromFluxErr(srcInstFluxErrArr*1e-9, srcInstFluxArr*1e-9)
if self.config.magErrFloor != 0.0:
magErrArr = (magErrArr**2 + self.config.magErrFloor**2)**0.5

Expand Down Expand Up @@ -443,22 +445,9 @@ def run(self, exposure, sourceCat, expId=0):

filterName = exposure.getFilter().getName()

def convert_refCat_to_jansky(matches):
"""Convert fluxes in matches[*][0] (the refCat) from nJy to Jy.
Schema will still read nJy.
HACK to continue supporting Calib, which assumes Jy fluxes.
To be removed in DM-10156.
"""
fields = [field for field in matches[0][0].schema if field.field.getUnits() == 'nJy']
for match in matches:
for field in fields:
match[0][field.key] *= 1e-9

# Match sources
matchResults = self.match.run(sourceCat, filterName)
matches = matchResults.matches
# HACK to support Calib for the remainder of its now brief existence.
convert_refCat_to_jansky(matches)

reserveResults = self.reserve.run([mm.second for mm in matches], expId=expId)
if displaySources:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_colorterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ def testGetCorrectedMagnitudes(self):
flux = 100
refCat = make_fake_refcat(center, flux)

expectMag = self.colorterm.transformMags((u.Jy*refCat['f1_flux']).to_value(u.ABmag),
(u.Jy*refCat['f2_flux']).to_value(u.ABmag))
expectMag = self.colorterm.transformMags((u.nJy*refCat['f1_flux']).to_value(u.ABmag),
(u.nJy*refCat['f2_flux']).to_value(u.ABmag))
refMag, refMagErr = self.colorterm.getCorrectedMagnitudes(refCat, 'r')
self.assertEqual(refMag, expectMag)
# TODO DM-17692: Not testing the returned errors, as I do not trust `propagateFluxErrors()`
Expand Down
3 changes: 2 additions & 1 deletion tests/test_photoCal.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import unittest

import numpy as np
import astropy.units as u

from lsst.daf.persistence import Butler
from lsst.meas.algorithms import LoadIndexedReferenceObjectsTask
Expand Down Expand Up @@ -121,7 +122,7 @@ def _runTask(self):
refFlux = m[0].get(refFluxField) # reference catalog flux
if refFlux <= 0:
continue
refMag = afwImage.abMagFromFlux(refFlux) # reference catalog mag
refMag = u.Quantity(refFlux, u.nJy).to_value(u.ABmag)
instFlux = m[1].getPsfInstFlux() # Instrumental Flux
if instFlux <= 0:
continue
Expand Down

0 comments on commit 7a75a90

Please sign in to comment.