1 from __future__
import division, absolute_import
33 from .selectImages
import WcsSelectImagesTask, SelectStruct
34 from .coaddInputRecorder
import CoaddInputRecorderTask
37 from lsst.meas.mosaic
import applyMosaicResults
39 applyMosaicResults =
None
41 __all__ = [
"CoaddBaseTask",
"getSkyInfo"]
44 """Config for CoaddBaseTask
46 coaddName = pexConfig.Field(
47 doc =
"Coadd name: typically one of deep or goodSeeing.",
51 select = pexConfig.ConfigurableField(
52 doc =
"Image selection subtask.",
53 target = WcsSelectImagesTask,
55 badMaskPlanes = pexConfig.ListField(
57 doc =
"Mask planes that, if set, the associated pixel should not be included in the coaddTempExp.",
58 default = (
"NO_DATA",),
60 inputRecorder = pexConfig.ConfigurableField(
61 doc =
"Subtask that helps fill CoaddInputs catalogs added to the final Exposure",
62 target = CoaddInputRecorderTask
64 doPsfMatch = pexConfig.Field(dtype=bool, doc=
"Match to modelPsf?", default=
False)
65 modelPsf = measAlg.GaussianPsfFactory.makeField(doc =
"Model Psf factory")
66 doApplyUberCal = pexConfig.Field(
68 doc =
"Apply meas_mosaic ubercal results to input calexps?",
75 return pipeBase.TaskRunner.getTargetList(parsedCmd, selectDataList=parsedCmd.selectId.dataList,
79 """Base class for coaddition.
81 Subclasses must specify _DefaultName
83 ConfigClass = CoaddBaseConfig
84 RunnerClass = CoaddTaskRunner
87 pipeBase.Task.__init__(self, *args, **kwargs)
88 self.makeSubtask(
"select")
89 self.makeSubtask(
"inputRecorder")
93 \brief Select exposures to coadd
95 Get the corners of the bbox supplied in skyInfo using \ref afwGeom.Box2D and convert the pixel
96 positions of the bbox corners to sky coordinates using \ref skyInfo.wcs.pixelToSky. Use the
97 \ref WcsSelectImagesTask_ "WcsSelectImagesTask" to select exposures that lie inside the patch
98 indicated by the dataRef.
100 \param[in] patchRef data reference for sky map patch. Must include keys "tract", "patch",
101 plus the camera-specific filter key (e.g. "filter" or "band")
102 \param[in] skyInfo geometry for the patch; output from getSkyInfo
103 \return a list of science exposures to coadd, as butler data references
108 coordList = [skyInfo.wcs.pixelToSky(pos)
for pos
in cornerPosList]
109 return self.select.runDataRef(patchRef, coordList, selectDataList=selectDataList).dataRefList
113 \brief Use \ref getSkyinfo to return the skyMap, tract and patch information, wcs and the outer bbox
116 \param[in] patchRef data reference for sky map. Must include keys "tract" and "patch"
118 \return pipe_base Struct containing:
120 - tractInfo: information for chosen tract of sky map
121 - patchInfo: information about chosen patch of tract
123 - bbox: outer bbox of patch, as an afwGeom Box2I
125 return getSkyInfo(coaddName=self.config.coaddName, patchRef=patchRef)
128 """!Return one "calexp" calibrated exposure
130 @param[in] dataRef a sensor-level data reference
131 @param[in] bgSubtracted return calexp with background subtracted? If False get the
132 calexp's background background model and add it to the calexp.
133 @return calibrated exposure
135 If config.doApplyUberCal, meas_mosaic calibrations will be applied to
136 the returned exposure using applyMosaicResults.
138 exposure = dataRef.get(
"calexp", immediate=
True)
140 background = dataRef.get(
"calexpBackground", immediate=
True)
141 mi = exposure.getMaskedImage()
142 mi += background.getImage()
144 if not self.config.doApplyUberCal:
146 if applyMosaicResults
is None:
148 "Cannot use improved calibrations for %s because meas_mosaic could not be imported."
156 return self.config.coaddName +
"Coadd"
159 return self.config.coaddName +
"Coadd_tempExp"
163 """Create an argument parser
165 parser = pipeBase.ArgumentParser(name=cls._DefaultName)
166 parser.add_id_argument(
"--id",
"deepCoadd", help=
"data ID, e.g. --id tract=12345 patch=1,2",
167 ContainerClass=CoaddDataIdContainer)
168 parser.add_id_argument(
"--selectId",
"calexp", help=
"data ID, e.g. --selectId visit=6789 ccd=0..9",
169 ContainerClass=SelectDataIdContainer)
173 """Return the name of the config dataset
175 return "%s_%s_config" % (self.config.coaddName, self._DefaultName)
178 """Return the name of the metadata dataset
180 return "%s_%s_metadata" % (self.config.coaddName, self._DefaultName)
184 \brief Convenience method to provide the bitmask from the mask plane names
186 return afwImage.MaskU.getPlaneBitMask(self.config.badMaskPlanes)
190 \brief Write a coadd product through the butler
192 \param[in] dataRef data reference for coadd
193 \param[in,out] obj coadd product to write
194 \param[in] suffix suffix to apply to coadd dataset name
197 if suffix
is not None:
198 objName +=
"_" + suffix
199 self.log.info(
"Persisting %s" % objName)
200 dataRef.put(obj, objName)
204 \brief A dataId container for inputs to be selected.
206 Read the header (including the size and Wcs) for all specified
207 inputs and pass those along, ultimately for the SelectImagesTask.
208 This is most useful when used with multiprocessing, as input headers are
212 """Add a dataList containing useful information for selecting images"""
215 for ref
in self.refList:
217 md = ref.get(
"calexp_md", immediate=
True)
219 data = SelectStruct(dataRef=ref, wcs=wcs, dims=(md.get(
"NAXIS1"), md.get(
"NAXIS2")))
220 except FitsError
as e:
221 namespace.log.warn(
"Unable to construct Wcs from %s" % (ref.dataId))
223 self.dataList.append(data)
227 \brief Return the SkyMap, tract and patch information, wcs, and outer bbox of the patch to be coadded.
229 \param[in] coaddName coadd name; typically one of deep or goodSeeing
230 \param[in] patchRef data reference for sky map. Must include keys "tract" and "patch"
232 \return pipe_base Struct containing:
234 - tractInfo: information for chosen tract of sky map
235 - patchInfo: information about chosen patch of tract
237 - bbox: outer bbox of patch, as an afwGeom Box2I
239 skyMap = patchRef.get(coaddName +
"Coadd_skyMap")
240 tractId = patchRef.dataId[
"tract"]
241 tractInfo = skyMap[tractId]
244 patchIndex = tuple(int(i)
for i
in patchRef.dataId[
"patch"].split(
","))
245 patchInfo = tractInfo.getPatchInfo(patchIndex)
247 return pipeBase.Struct(
249 tractInfo = tractInfo,
250 patchInfo = patchInfo,
251 wcs = tractInfo.getWcs(),
252 bbox = patchInfo.getOuterBBox(),
257 \brief Scale the variance in a maskedImage
259 The variance plane in a convolved or warped image (or a coadd derived
260 from warped images) does not accurately reflect the noise properties of
261 the image because variance has been lost to covariance. This function
262 attempts to correct for this by scaling the variance plane to match
263 the observed variance in the image. This is not perfect (because we're
264 not tracking the covariance) but it's simple and is often good enough.
266 @param maskedImage MaskedImage to operate on; variance will be scaled
267 @param maskPlanes List of mask planes for pixels to reject
268 @param log Log for reporting the renormalization factor; or None
269 @return renormalisation factor
271 variance = maskedImage.getVariance()
272 sigNoise = maskedImage.getImage().getArray()/numpy.sqrt(variance.getArray())
273 maskVal = maskedImage.getMask().getPlaneBitMask(maskPlanes)
274 good = (maskedImage.getMask().getArray() & maskVal) == 0
276 q1, q3 = numpy.percentile(sigNoise[good], (25, 75))
277 stdev = 0.74*(q3 - q1)
280 log.info(
"Renormalizing variance by %f" % (ratio,))
def getTempExpDatasetName
def getBadPixelMask
Convenience method to provide the bitmask from the mask plane names.
A dataId container for inputs to be selected.
def getCalExp
Return one "calexp" calibrated exposure.
def scaleVariance
Scale the variance in a maskedImage.
def selectExposures
Select exposures to coadd.
boost::shared_ptr< Wcs > makeWcs(coord::Coord const &crval, geom::Point2D const &crpix, double CD11, double CD12, double CD21, double CD22)
Create a Wcs object from crval, crpix, CD, using CD elements (useful from python) ...
def writeCoaddOutput
Write a coadd product through the butler.
def getSkyInfo
Use getSkyinfo to return the skyMap, tract and patch information, wcs and the outer bbox of the patch...
def getSkyInfo
Return the SkyMap, tract and patch information, wcs, and outer bbox of the patch to be coadded...
A floating-point coordinate rectangle geometry.