132 def run(self, catalog, apCorrMap):
133 """Apply aperture corrections to a catalog of sources
135 @param[in,out] catalog catalog of sources
136 @param[in] apCorrMap aperture correction map (an lsst.afw.image.ApCorrMap)
138 If you show debug-level log messages then you will see statistics for the effects of
141 self.log.info(
"Applying aperture corrections to %d flux fields" % (len(self.
apCorrInfoDict),))
142 if UseNaiveFluxSigma:
143 self.log.info(
"Use naive flux sigma computation")
145 self.log.info(
"Use complex flux sigma computation that double-counts photon noise "
146 " and thus over-estimates flux uncertainty")
147 for apCorrInfo
in self.apCorrInfoDict.itervalues():
148 apCorrModel = apCorrMap.get(apCorrInfo.fluxName)
149 apCorrSigmaModel = apCorrMap.get(apCorrInfo.fluxSigmaName)
150 if None in (apCorrModel, apCorrSigmaModel):
151 missingNames = [(apCorrInfo.fluxName, apCorrInfo.fluxSigmaName)[i]
152 for i, model
in enumerate((apCorrModel, apCorrSigmaModel))
if model
is None]
153 self.log.warn(
"Could not find %s in apCorrMap" % (
" or ".join(missingNames),))
154 for source
in catalog:
155 source.set(apCorrInfo.apCorrFlagKey,
True)
158 for source
in catalog:
159 center = source.getCentroid()
161 source.set(apCorrInfo.apCorrFlagKey,
True)
162 oldFluxFlagState =
False
163 if self.config.doFlagApCorrFailures:
164 oldFluxFlagState = source.get(apCorrInfo.fluxFlagKey)
165 source.set(apCorrInfo.fluxFlagKey,
True)
170 apCorr = apCorrModel.evaluate(center)
171 if not UseNaiveFluxSigma:
172 apCorrSigma = apCorrSigmaModel.evaluate(center)
173 except lsst.pex.exceptions.DomainError:
176 source.set(apCorrInfo.apCorrKey, apCorr)
177 source.set(apCorrInfo.apCorrSigmaKey, apCorrSigma)
178 if apCorr <= 0.0
or apCorrSigma < 0.0:
181 flux = source.get(apCorrInfo.fluxKey)
182 fluxSigma = source.get(apCorrInfo.fluxSigmaKey)
183 source.set(apCorrInfo.fluxKey, flux*apCorr)
184 if UseNaiveFluxSigma:
185 source.set(apCorrInfo.fluxSigmaKey, fluxSigma*apCorr)
188 b = apCorrSigma/apCorr
189 source.set(apCorrInfo.fluxSigmaKey, abs(flux*apCorr)*math.sqrt(a*a + b*b))
190 source.set(apCorrInfo.apCorrFlagKey,
False)
191 if self.config.doFlagApCorrFailures:
192 source.set(apCorrInfo.fluxFlagKey, oldFluxFlagState)
194 if self.log.getThreshold() <= self.log.DEBUG:
196 apCorrArr = numpy.array([s.get(apCorrInfo.apCorrKey)
for s
in catalog])
197 apCorrSigmaArr = numpy.array([s.get(apCorrInfo.apCorrSigmaKey)
for s
in catalog])
198 self.log.logdebug(
"For flux field %r: mean apCorr=%s, stdDev apCorr=%s,"
199 " mean apCorrSigma=%s, stdDev apCorrSigma=%s for %s sources" %
200 (apCorrInfo.name, apCorrArr.mean(), apCorrArr.std(),
201 apCorrSigmaArr.mean(), apCorrSigmaArr.std(), len(catalog)))