30import lsst.pipe.base.connectionTypes
as connectionTypes
31import lsst.utils
as utils
35from .coaddBase
import CoaddBaseTask, makeSkyInfo, reorderAndPadList
36from .warpAndPsfMatch
import WarpAndPsfMatchTask
37from .coaddHelpers
import groupPatchExposures, getGroupDataRef
38from collections.abc
import Iterable
40__all__ = [
"MakeCoaddTempExpTask",
"MakeWarpTask",
"MakeWarpConfig"]
42log = logging.getLogger(__name__.partition(
".")[2])
46 """Raised when data cannot be retrieved for an exposure.
47 When processing patches, sometimes one exposure is missing; this lets us
48 distinguish bewteen that case,
and other errors.
54 """Config for MakeCoaddTempExpTask
56 warpAndPsfMatch = pexConfig.ConfigurableField(
57 target=WarpAndPsfMatchTask,
58 doc="Task to warp and PSF-match calexp",
60 doWrite = pexConfig.Field(
61 doc=
"persist <coaddName>Coadd_<warpType>Warp",
65 bgSubtracted = pexConfig.Field(
66 doc=
"Work with a background subtracted calexp?",
70 coaddPsf = pexConfig.ConfigField(
71 doc=
"Configuration for CoaddPsf",
74 makeDirect = pexConfig.Field(
75 doc=
"Make direct Warp/Coadds",
79 makePsfMatched = pexConfig.Field(
80 doc=
"Make Psf-Matched Warp/Coadd?",
85 doWriteEmptyWarps = pexConfig.Field(
88 doc=
"Write out warps even if they are empty"
91 hasFakes = pexConfig.Field(
92 doc=
"Should be set to True if fake sources have been inserted into the input data.",
96 doApplySkyCorr = pexConfig.Field(dtype=bool, default=
False, doc=
"Apply sky correction?")
99 CoaddBaseTask.ConfigClass.validate(self)
101 raise RuntimeError(
"At least one of config.makePsfMatched and config.makeDirect must be True")
104 log.warning(
"Config doPsfMatch deprecated. Setting makePsfMatched=True and makeDirect=False")
109 CoaddBaseTask.ConfigClass.setDefaults(self)
110 self.
warpAndPsfMatchwarpAndPsfMatch.psfMatch.kernel.active.kernelSize = self.matchingKernelSize
121 r"""!Warp and optionally PSF-Match calexps onto an a common projection.
123 @anchor MakeCoaddTempExpTask_
125 @section pipe_tasks_makeCoaddTempExp_Contents Contents
127 -
@ref pipe_tasks_makeCoaddTempExp_Purpose
128 -
@ref pipe_tasks_makeCoaddTempExp_Initialize
129 -
@ref pipe_tasks_makeCoaddTempExp_IO
130 -
@ref pipe_tasks_makeCoaddTempExp_Config
131 -
@ref pipe_tasks_makeCoaddTempExp_Debug
132 -
@ref pipe_tasks_makeCoaddTempExp_Example
134 @section pipe_tasks_makeCoaddTempExp_Purpose Description
136 Warp
and optionally PSF-Match calexps onto a common projection, by
137 performing the following operations:
138 - Group calexps by visit/run
139 - For each visit, generate a Warp by calling method
@ref makeTempExp.
140 makeTempExp loops over the visit
's calexps calling @ref WarpAndPsfMatch
143 The result is a `directWarp` (
and/
or optionally a `psfMatchedWarp`).
145 @section pipe_tasks_makeCoaddTempExp_Initialize Task Initialization
147 @copydoc \_\_init\_\_
149 This task has one special keyword argument: passing reuse=
True will cause
150 the task to skip the creation of warps that are already present
in the
153 @section pipe_tasks_makeCoaddTempExp_IO Invoking the Task
155 This task
is primarily designed to be run
from the command line.
157 The main method
is `runDataRef`, which takes a single butler data reference
for the
patch(es)
162 WarpType identifies the types of convolutions applied to Warps (previously CoaddTempExps).
163 Only two types are available: direct (
for regular Warps/Coadds)
and psfMatched
164 (
for Warps/Coadds
with homogenized PSFs). We expect to add a third type, likelihood,
165 for generating likelihood Coadds
with Warps that have been correlated
with their own PSF.
167 @section pipe_tasks_makeCoaddTempExp_Config Configuration parameters
169 See
@ref MakeCoaddTempExpConfig
and parameters inherited
from
172 @subsection pipe_tasks_MakeCoaddTempExp_psfMatching Guide to PSF-Matching Configs
174 To make `psfMatchedWarps`, select `config.makePsfMatched=
True`. The subtask
176 is responsible
for the PSF-Matching,
and its config
is accessed via `config.warpAndPsfMatch.psfMatch`.
177 The optimal configuration depends on aspects of dataset: the pixel scale, average PSF FWHM
and
178 dimensions of the PSF kernel. These configs include the requested model PSF, the matching kernel size,
179 padding of the science PSF thumbnail
and spatial sampling frequency of the PSF.
181 *Config Guidelines*: The user must specify the size of the model PSF to which to match by setting
182 `config.modelPsf.defaultFwhm`
in units of pixels. The appropriate values depends on science case.
183 In general,
for a set of input images, this config should equal the FWHM of the visit
184 with the worst seeing. The smallest it should be set to
is the median FWHM. The defaults
185 of the other config options offer a reasonable starting point.
186 The following list presents the most common problems that arise
from a misconfigured
188 and corresponding solutions. All assume the default Alard-Lupton kernel,
with configs accessed via
189 ```config.warpAndPsfMatch.psfMatch.kernel[
'AL']```. Each item
in the list
is formatted
as:
190 Problem: Explanation. *Solution*
192 *Troublshooting PSF-Matching Configuration:*
193 - Matched PSFs look boxy: The matching kernel
is too small. _Increase the matching kernel size.
196 config.warpAndPsfMatch.psfMatch.kernel[
'AL'].kernelSize=27
198 Note that increasing the kernel size also increases runtime.
199 - Matched PSFs look ugly (dipoles, quadropoles, donuts): unable to find good solution
200 for matching kernel. _Provide the matcher
with more data by either increasing
201 the spatial sampling by decreasing the spatial cell size,_
203 config.warpAndPsfMatch.psfMatch.kernel[
'AL'].sizeCellX = 64
204 config.warpAndPsfMatch.psfMatch.kernel[
'AL'].sizeCellY = 64
206 _or increasing the padding around the Science PSF,
for example:_
208 config.warpAndPsfMatch.psfMatch.autoPadPsfTo=1.6
210 Increasing `autoPadPsfTo` increases the minimum ratio of input PSF dimensions to the
211 matching kernel dimensions, thus increasing the number of pixels available to fit
212 after convolving the PSF
with the matching kernel.
213 Optionally,
for debugging the effects of padding, the level of padding may be manually
214 controlled by setting turning off the automatic padding
and setting the number
215 of pixels by which to pad the PSF:
217 config.warpAndPsfMatch.psfMatch.doAutoPadPsf =
False
218 config.warpAndPsfMatch.psfMatch.padPsfBy = 6
220 - Deconvolution: Matching a large PSF to a smaller PSF produces
221 a telltale noise pattern which looks like ripples
or a brain.
222 _Increase the size of the requested model PSF. For example:_
224 config.modelPsf.defaultFwhm = 11
226 - High frequency (sometimes checkered) noise: The matching basis functions are too small.
227 _Increase the width of the Gaussian basis functions. For example:_
229 config.warpAndPsfMatch.psfMatch.kernel[
'AL'].alardSigGauss=[1.5, 3.0, 6.0]
233 @section pipe_tasks_makeCoaddTempExp_Debug Debug variables
235 MakeCoaddTempExpTask has no debug output, but its subtasks do.
237 @section pipe_tasks_makeCoaddTempExp_Example A complete example of using MakeCoaddTempExpTask
239 This example uses the package ci_hsc to show how MakeCoaddTempExp fits
240 into the larger Data Release Processing.
246 python $(which scons)
248 The following assumes that `processCcd.py`
and `makeSkyMap.py` have previously been run
249 (e.g. by building `ci_hsc` above) to generate a repository of calexps
and an
250 output respository
with the desired SkyMap. The command,
252 makeCoaddTempExp.py $CI_HSC_DIR/DATA --rerun ci_hsc \
253 --id patch=5,4 tract=0 filter=HSC-I \
254 --selectId visit=903988 ccd=16 --selectId visit=903988 ccd=17 \
255 --selectId visit=903988 ccd=23 --selectId visit=903988 ccd=24 \
256 --config doApplyExternalPhotoCalib=
False doApplyExternalSkyWcs=
False \
257 makePsfMatched=
True modelPsf.defaultFwhm=11
259 writes a direct
and PSF-Matched Warp to
260 - `$CI_HSC_DIR/DATA/rerun/ci_hsc/deepCoadd/HSC-I/0/5,4/warp-HSC-I-0-5,4-903988.fits`
and
261 - `$CI_HSC_DIR/DATA/rerun/ci_hsc/deepCoadd/HSC-I/0/5,4/psfMatchedWarp-HSC-I-0-5,4-903988.fits`
264 @note PSF-Matching
in this particular dataset would benefit
from adding
265 `--configfile ./matchingConfig.py` to
266 the command line arguments where `matchingConfig.py`
is defined by:
269 config.warpAndPsfMatch.psfMatch.kernel['AL'].kernelSize=27
270 config.warpAndPsfMatch.psfMatch.kernel[
'AL'].alardSigGauss=[1.5, 3.0, 6.0]
" > matchingConfig.py
273 Add the option `--help` to see more options.
275 ConfigClass = MakeCoaddTempExpConfig
276 _DefaultName = "makeCoaddTempExp"
279 CoaddBaseTask.__init__(self, **kwargs)
281 self.makeSubtask(
"warpAndPsfMatch")
282 if self.config.hasFakes:
289 """!Produce <coaddName>Coadd_<warpType>Warp images by warping and optionally PSF-matching.
291 @param[
in] patchRef: data reference
for sky map patch. Must include keys
"tract",
"patch",
292 plus the camera-specific filter key (e.g.
"filter" or "band")
293 @return: dataRefList: a list of data references
for the new <coaddName>Coadd_directWarps
294 if direct
or both warp types are requested
and <coaddName>Coadd_psfMatchedWarps
if only psfMatched
297 @warning: this task assumes that all exposures
in a warp (coaddTempExp) have the same filter.
299 @warning: this task sets the PhotoCalib of the coaddTempExp to the PhotoCalib of the first calexp
300 with any good pixels
in the patch. For a mosaic camera the resulting PhotoCalib should be ignored
301 (assembleCoadd should determine zeropoint scaling without referring to it).
306 if self.config.makePsfMatched
and not self.config.makeDirect:
311 calExpRefList = self.
selectExposuresselectExposures(patchRef, skyInfo, selectDataList=selectDataList)
313 if len(calExpRefList) == 0:
314 self.log.warning(
"No exposures to coadd for patch %s", patchRef.dataId)
316 self.log.info(
"Selected %d calexps for patch %s", len(calExpRefList), patchRef.dataId)
317 calExpRefList = [calExpRef
for calExpRef
in calExpRefList
if calExpRef.datasetExists(self.
calexpTypecalexpType)]
318 self.log.info(
"Processing %d existing calexps for patch %s", len(calExpRefList), patchRef.dataId)
322 self.log.info(
"Processing %d warp exposures for patch %s", len(groupData.groups), patchRef.dataId)
325 for i, (tempExpTuple, calexpRefList)
in enumerate(groupData.groups.items()):
327 tempExpTuple, groupData.keys)
328 if self.
reusereuse
and tempExpRef.datasetExists(datasetType=primaryWarpDataset, write=
True):
329 self.log.info(
"Skipping makeCoaddTempExp for %s; output already exists.", tempExpRef.dataId)
330 dataRefList.append(tempExpRef)
332 self.log.info(
"Processing Warp %d/%d: id=%s", i, len(groupData.groups), tempExpRef.dataId)
338 visitId = int(tempExpRef.dataId[
"visit"])
339 except (KeyError, ValueError):
346 for calExpInd, calExpRef
in enumerate(calexpRefList):
347 self.log.info(
"Reading calexp %s of %s for Warp id=%s", calExpInd+1, len(calexpRefList),
350 ccdId = calExpRef.get(
"ccdExposureId", immediate=
True)
357 calExpRef = calExpRef.butlerSubset.butler.dataRef(self.
calexpTypecalexpType,
358 dataId=calExpRef.dataId,
359 tract=skyInfo.tractInfo.getId())
360 calExp = self.
getCalibratedExposuregetCalibratedExposure(calExpRef, bgSubtracted=self.config.bgSubtracted)
361 except Exception
as e:
362 self.log.warning(
"Calexp %s not found; skipping it: %s", calExpRef.dataId, e)
365 if self.config.doApplySkyCorr:
368 calExpList.append(calExp)
369 ccdIdList.append(ccdId)
370 dataIdList.append(calExpRef.dataId)
372 exps = self.
runrun(calExpList, ccdIdList, skyInfo, visitId, dataIdList).exposures
374 if any(exps.values()):
375 dataRefList.append(tempExpRef)
377 self.log.warning(
"Warp %s could not be created", tempExpRef.dataId)
379 if self.config.doWrite:
380 for (warpType, exposure)
in exps.items():
381 if exposure
is not None:
388 def run(self, calExpList, ccdIdList, skyInfo, visitId=0, dataIdList=None, **kwargs):
389 """Create a Warp from inputs
391 We iterate over the multiple calexps in a single exposure to construct
392 the warp (previously called a coaddTempExp) of that exposure to the
393 supplied tract/patch.
395 Pixels that receive no pixels are set to NAN; this
is not correct
396 (violates LSST algorithms group policy), but will be fixed up by
397 interpolating after the coaddition.
399 @param calexpRefList: List of data references
for calexps that (may)
400 overlap the patch of interest
401 @param skyInfo: Struct
from CoaddBaseTask.getSkyInfo()
with geometric
402 information about the patch
403 @param visitId: integer identifier
for visit,
for the table that will
405 @return a pipeBase Struct containing:
406 - exposures: a dictionary containing the warps requested:
407 "direct": direct warp
if config.makeDirect
408 "psfMatched": PSF-matched warp
if config.makePsfMatched
412 totGoodPix = {warpType: 0 for warpType
in warpTypeList}
413 didSetMetadata = {warpType:
False for warpType
in warpTypeList}
414 coaddTempExps = {warpType: self.
_prepareEmptyExposure_prepareEmptyExposure(skyInfo)
for warpType
in warpTypeList}
415 inputRecorder = {warpType: self.inputRecorder.makeCoaddTempExpRecorder(visitId, len(calExpList))
416 for warpType
in warpTypeList}
418 modelPsf = self.config.modelPsf.apply()
if self.config.makePsfMatched
else None
419 if dataIdList
is None:
420 dataIdList = ccdIdList
422 for calExpInd, (calExp, ccdId, dataId)
in enumerate(zip(calExpList, ccdIdList, dataIdList)):
423 self.log.info(
"Processing calexp %d of %d for this Warp: id=%s",
424 calExpInd+1, len(calExpList), dataId)
427 warpedAndMatched = self.warpAndPsfMatch.
run(calExp, modelPsf=modelPsf,
428 wcs=skyInfo.wcs, maxBBox=skyInfo.bbox,
429 makeDirect=self.config.makeDirect,
430 makePsfMatched=self.config.makePsfMatched)
431 except Exception
as e:
432 self.log.warning(
"WarpAndPsfMatch failed for calexp %s; skipping it: %s", dataId, e)
435 numGoodPix = {warpType: 0
for warpType
in warpTypeList}
436 for warpType
in warpTypeList:
437 exposure = warpedAndMatched.getDict()[warpType]
440 coaddTempExp = coaddTempExps[warpType]
441 if didSetMetadata[warpType]:
442 mimg = exposure.getMaskedImage()
443 mimg *= (coaddTempExp.getPhotoCalib().getInstFluxAtZeroMagnitude()
444 / exposure.getPhotoCalib().getInstFluxAtZeroMagnitude())
446 numGoodPix[warpType] = coaddUtils.copyGoodPixels(
447 coaddTempExp.getMaskedImage(), exposure.getMaskedImage(), self.
getBadPixelMaskgetBadPixelMask())
448 totGoodPix[warpType] += numGoodPix[warpType]
449 self.log.debug(
"Calexp %s has %d good pixels in this patch (%.1f%%) for %s",
450 dataId, numGoodPix[warpType],
451 100.0*numGoodPix[warpType]/skyInfo.bbox.getArea(), warpType)
452 if numGoodPix[warpType] > 0
and not didSetMetadata[warpType]:
453 coaddTempExp.setPhotoCalib(exposure.getPhotoCalib())
454 coaddTempExp.setFilterLabel(exposure.getFilterLabel())
455 coaddTempExp.getInfo().setVisitInfo(exposure.getInfo().getVisitInfo())
457 coaddTempExp.setPsf(exposure.getPsf())
458 didSetMetadata[warpType] =
True
461 inputRecorder[warpType].addCalExp(calExp, ccdId, numGoodPix[warpType])
463 except Exception
as e:
464 self.log.warning(
"Error processing calexp %s; skipping it: %s", dataId, e)
467 for warpType
in warpTypeList:
468 self.log.info(
"%sWarp has %d good pixels (%.1f%%)",
469 warpType, totGoodPix[warpType], 100.0*totGoodPix[warpType]/skyInfo.bbox.getArea())
471 if totGoodPix[warpType] > 0
and didSetMetadata[warpType]:
472 inputRecorder[warpType].finish(coaddTempExps[warpType], totGoodPix[warpType])
473 if warpType ==
"direct":
474 coaddTempExps[warpType].setPsf(
475 CoaddPsf(inputRecorder[warpType].coaddInputs.ccds, skyInfo.wcs,
476 self.config.coaddPsf.makeControl()))
478 if not self.config.doWriteEmptyWarps:
480 coaddTempExps[warpType] =
None
485 result = pipeBase.Struct(exposures=coaddTempExps)
489 """Return one calibrated Exposure, possibly with an updated SkyWcs.
491 @param[
in] dataRef a sensor-level data reference
492 @param[
in] bgSubtracted
return calexp
with background subtracted? If
False get the
493 calexp
's background background model and add it to the calexp.
494 @return calibrated exposure
496 @raises MissingExposureError If data
for the exposure
is not available.
498 If config.doApplyExternalPhotoCalib
is `
True`, the photometric calibration
499 (`photoCalib`)
is taken
from `config.externalPhotoCalibName` via the
500 `name_photoCalib` dataset. Otherwise, the photometric calibration
is
501 retrieved
from the processed exposure. When
502 `config.doApplyExternalSkyWcs`
is `
True`, the astrometric calibration
503 is taken
from `config.externalSkyWcsName`
with the `name_wcs` dataset.
504 Otherwise, the astrometric calibration
is taken
from the processed
508 exposure = dataRef.get(self.
calexpTypecalexpType, immediate=
True)
509 except dafPersist.NoResults
as e:
513 background = dataRef.get(
"calexpBackground", immediate=
True)
514 mi = exposure.getMaskedImage()
515 mi += background.getImage()
518 if self.config.doApplyExternalPhotoCalib:
519 source = f
"{self.config.externalPhotoCalibName}_photoCalib"
520 self.log.debug(
"Applying external photoCalib to %s from %s", dataRef.dataId, source)
521 photoCalib = dataRef.get(source)
522 exposure.setPhotoCalib(photoCalib)
524 photoCalib = exposure.getPhotoCalib()
526 if self.config.doApplyExternalSkyWcs:
527 source = f
"{self.config.externalSkyWcsName}_wcs"
528 self.log.debug(
"Applying external skyWcs to %s from %s", dataRef.dataId, source)
529 skyWcs = dataRef.get(source)
530 exposure.setWcs(skyWcs)
532 exposure.maskedImage = photoCalib.calibrateImage(exposure.maskedImage,
533 includeScaleUncertainty=self.config.includeCalibVar)
534 exposure.maskedImage /= photoCalib.getCalibrationMean()
540 def _prepareEmptyExposure(skyInfo):
541 """Produce an empty exposure for a given patch"""
542 exp = afwImage.ExposureF(skyInfo.bbox, skyInfo.wcs)
543 exp.getMaskedImage().set(numpy.nan, afwImage.Mask
544 .getPlaneBitMask(
"NO_DATA"), numpy.inf)
548 """Return list of requested warp types per the config.
551 if self.config.makeDirect:
552 warpTypeList.append(
"direct")
553 if self.config.makePsfMatched:
554 warpTypeList.append(
"psfMatched")
558 """Apply correction to the sky background level
560 Sky corrections can be generated with the
'skyCorrection.py'
561 executable
in pipe_drivers. Because the sky model used by that
562 code extends over the entire focal plane, this can produce
563 better sky subtraction.
565 The calexp
is updated
in-place.
569 dataRef : `lsst.daf.persistence.ButlerDataRef`
570 Data reference
for calexp.
574 bg = dataRef.get("skyCorr")
575 self.log.debug(
"Applying sky correction to %s", dataRef.dataId)
576 if isinstance(calexp, afwImage.Exposure):
577 calexp = calexp.getMaskedImage()
578 calexp -= bg.getImage()
582 dimensions=(
"tract",
"patch",
"skymap",
"instrument",
"visit"),
583 defaultTemplates={
"coaddName":
"deep",
584 "skyWcsName":
"jointcal",
585 "photoCalibName":
"fgcm",
587 calExpList = connectionTypes.Input(
588 doc=
"Input exposures to be resampled and optionally PSF-matched onto a SkyMap projection/patch",
589 name=
"{calexpType}calexp",
590 storageClass=
"ExposureF",
591 dimensions=(
"instrument",
"visit",
"detector"),
595 backgroundList = connectionTypes.Input(
596 doc=
"Input backgrounds to be added back into the calexp if bgSubtracted=False",
597 name=
"calexpBackground",
598 storageClass=
"Background",
599 dimensions=(
"instrument",
"visit",
"detector"),
602 skyCorrList = connectionTypes.Input(
603 doc=
"Input Sky Correction to be subtracted from the calexp if doApplySkyCorr=True",
605 storageClass=
"Background",
606 dimensions=(
"instrument",
"visit",
"detector"),
609 skyMap = connectionTypes.Input(
610 doc=
"Input definition of geometry/bbox and projection/wcs for warped exposures",
611 name=BaseSkyMap.SKYMAP_DATASET_TYPE_NAME,
612 storageClass=
"SkyMap",
613 dimensions=(
"skymap",),
615 externalSkyWcsTractCatalog = connectionTypes.Input(
616 doc=(
"Per-tract, per-visit wcs calibrations. These catalogs use the detector "
617 "id for the catalog id, sorted on id for fast lookup."),
618 name=
"{skyWcsName}SkyWcsCatalog",
619 storageClass=
"ExposureCatalog",
620 dimensions=(
"instrument",
"visit",
"tract"),
622 externalSkyWcsGlobalCatalog = connectionTypes.Input(
623 doc=(
"Per-visit wcs calibrations computed globally (with no tract information). "
624 "These catalogs use the detector id for the catalog id, sorted on id for "
626 name=
"{skyWcsName}SkyWcsCatalog",
627 storageClass=
"ExposureCatalog",
628 dimensions=(
"instrument",
"visit"),
630 externalPhotoCalibTractCatalog = connectionTypes.Input(
631 doc=(
"Per-tract, per-visit photometric calibrations. These catalogs use the "
632 "detector id for the catalog id, sorted on id for fast lookup."),
633 name=
"{photoCalibName}PhotoCalibCatalog",
634 storageClass=
"ExposureCatalog",
635 dimensions=(
"instrument",
"visit",
"tract"),
637 externalPhotoCalibGlobalCatalog = connectionTypes.Input(
638 doc=(
"Per-visit photometric calibrations computed globally (with no tract "
639 "information). These catalogs use the detector id for the catalog id, "
640 "sorted on id for fast lookup."),
641 name=
"{photoCalibName}PhotoCalibCatalog",
642 storageClass=
"ExposureCatalog",
643 dimensions=(
"instrument",
"visit"),
645 direct = connectionTypes.Output(
646 doc=(
"Output direct warped exposure (previously called CoaddTempExp), produced by resampling ",
647 "calexps onto the skyMap patch geometry."),
648 name=
"{coaddName}Coadd_directWarp",
649 storageClass=
"ExposureF",
650 dimensions=(
"tract",
"patch",
"skymap",
"visit",
"instrument"),
652 psfMatched = connectionTypes.Output(
653 doc=(
"Output PSF-Matched warped exposure (previously called CoaddTempExp), produced by resampling ",
654 "calexps onto the skyMap patch geometry and PSF-matching to a model PSF."),
655 name=
"{coaddName}Coadd_psfMatchedWarp",
656 storageClass=
"ExposureF",
657 dimensions=(
"tract",
"patch",
"skymap",
"visit",
"instrument"),
660 wcsList = connectionTypes.Input(
661 doc=
"WCSs of calexps used by SelectImages subtask to determine if the calexp overlaps the patch",
662 name=
"{calexpType}calexp.wcs",
664 dimensions=(
"instrument",
"visit",
"detector"),
667 bboxList = connectionTypes.Input(
668 doc=
"BBoxes of calexps used by SelectImages subtask to determine if the calexp overlaps the patch",
669 name=
"{calexpType}calexp.bbox",
670 storageClass=
"Box2I",
671 dimensions=(
"instrument",
"visit",
"detector"),
674 visitSummary = connectionTypes.Input(
675 doc=
"Consolidated exposure metadata from ConsolidateVisitSummaryTask",
676 name=
"{calexpType}visitSummary",
677 storageClass=
"ExposureCatalog",
678 dimensions=(
"instrument",
"visit",),
680 srcList = connectionTypes.Input(
681 doc=
"Source catalogs used by PsfWcsSelectImages subtask to further select on PSF stability",
683 storageClass=
"SourceCatalog",
684 dimensions=(
"instrument",
"visit",
"detector"),
688 def __init__(self, *, config=None):
689 super().__init__(config=config)
690 if config.bgSubtracted:
691 self.inputs.remove(
"backgroundList")
692 if not config.doApplySkyCorr:
693 self.inputs.remove(
"skyCorrList")
694 if config.doApplyExternalSkyWcs:
695 if config.useGlobalExternalSkyWcs:
696 self.inputs.remove(
"externalSkyWcsTractCatalog")
698 self.inputs.remove(
"externalSkyWcsGlobalCatalog")
700 self.inputs.remove(
"externalSkyWcsTractCatalog")
701 self.inputs.remove(
"externalSkyWcsGlobalCatalog")
702 if config.doApplyExternalPhotoCalib:
703 if config.useGlobalExternalPhotoCalib:
704 self.inputs.remove(
"externalPhotoCalibTractCatalog")
706 self.inputs.remove(
"externalPhotoCalibGlobalCatalog")
708 self.inputs.remove(
"externalPhotoCalibTractCatalog")
709 self.inputs.remove(
"externalPhotoCalibGlobalCatalog")
710 if not config.makeDirect:
711 self.outputs.remove(
"direct")
712 if not config.makePsfMatched:
713 self.outputs.remove(
"psfMatched")
715 if config.select.target != lsst.pipe.tasks.selectImages.PsfWcsSelectImagesTask:
716 self.inputs.remove(
"visitSummary")
717 self.inputs.remove(
"srcList")
718 elif not config.select.doLegacyStarSelectionComputation:
720 self.inputs.remove(
"srcList")
724 pipelineConnections=MakeWarpConnections):
731 """Warp and optionally PSF-Match calexps onto an a common projection
733 ConfigClass = MakeWarpConfig
734 _DefaultName = "makeWarp"
736 @utils.inheritDoc(pipeBase.PipelineTask)
737 def runQuantum(self, butlerQC, inputRefs, outputRefs):
741 Construct warps for requested warp type
for single epoch
743 PipelineTask (Gen3) entry point to warp
and optionally PSF-match
744 calexps. This method
is analogous to `runDataRef`.
748 detectorOrder = [ref.datasetRef.dataId[
'detector']
for ref
in inputRefs.calExpList]
749 inputRefs = reorderRefs(inputRefs, detectorOrder, dataIdKey=
'detector')
752 inputs = butlerQC.get(inputRefs)
756 skyMap = inputs.pop(
"skyMap")
757 quantumDataId = butlerQC.quantum.dataId
758 skyInfo =
makeSkyInfo(skyMap, tractId=quantumDataId[
'tract'], patchId=quantumDataId[
'patch'])
761 dataIdList = [ref.datasetRef.dataId
for ref
in inputRefs.calExpList]
763 ccdIdList = [dataId.pack(
"visit_detector")
for dataId
in dataIdList]
768 coordList = [skyInfo.wcs.pixelToSky(pos)
for pos
in cornerPosList]
769 goodIndices = self.select.run(**inputs, coordList=coordList, dataIds=dataIdList)
770 inputs = self.filterInputs(indices=goodIndices, inputs=inputs)
773 inputs[
'calExpList'] = [ref.get()
for ref
in inputs[
'calExpList']]
776 visits = [dataId[
'visit']
for dataId
in dataIdList]
779 if self.config.doApplyExternalSkyWcs:
780 if self.config.useGlobalExternalSkyWcs:
781 externalSkyWcsCatalog = inputs.pop(
"externalSkyWcsGlobalCatalog")
783 externalSkyWcsCatalog = inputs.pop(
"externalSkyWcsTractCatalog")
785 externalSkyWcsCatalog =
None
787 if self.config.doApplyExternalPhotoCalib:
788 if self.config.useGlobalExternalPhotoCalib:
789 externalPhotoCalibCatalog = inputs.pop(
"externalPhotoCalibGlobalCatalog")
791 externalPhotoCalibCatalog = inputs.pop(
"externalPhotoCalibTractCatalog")
793 externalPhotoCalibCatalog =
None
795 completeIndices = self.prepareCalibratedExposures(**inputs,
796 externalSkyWcsCatalog=externalSkyWcsCatalog,
797 externalPhotoCalibCatalog=externalPhotoCalibCatalog)
799 inputs = self.filterInputs(indices=completeIndices, inputs=inputs)
801 results = self.run(**inputs, visitId=visitId,
802 ccdIdList=[ccdIdList[i]
for i
in goodIndices],
803 dataIdList=[dataIdList[i]
for i
in goodIndices],
805 if self.config.makeDirect
and results.exposures[
"direct"]
is not None:
806 butlerQC.put(results.exposures[
"direct"], outputRefs.direct)
807 if self.config.makePsfMatched
and results.exposures[
"psfMatched"]
is not None:
808 butlerQC.put(results.exposures[
"psfMatched"], outputRefs.psfMatched)
810 def filterInputs(self, indices, inputs):
811 """Return task inputs with their lists filtered by indices
815 indices : `list` of integers
816 inputs : `dict` of `list` of input connections to be passed to run
818 for key
in inputs.keys():
820 if isinstance(inputs[key], list):
821 inputs[key] = [inputs[key][ind]
for ind
in indices]
824 def prepareCalibratedExposures(self, calExpList, backgroundList=None, skyCorrList=None,
825 externalSkyWcsCatalog=None, externalPhotoCalibCatalog=None,
827 """Calibrate and add backgrounds to input calExpList in place
832 Sequence of calexps to be modified in place
833 backgroundList : `list` of `lsst.afw.math.backgroundList`, optional
834 Sequence of backgrounds to be added back
in if bgSubtracted=
False
835 skyCorrList : `list` of `lsst.afw.math.backgroundList`, optional
836 Sequence of background corrections to be subtracted
if doApplySkyCorr=
True
838 Exposure catalog
with external skyWcs to be applied
839 if config.doApplyExternalSkyWcs=
True. Catalog uses the detector id
840 for the catalog id, sorted on id
for fast lookup.
842 Exposure catalog
with external photoCalib to be applied
843 if config.doApplyExternalPhotoCalib=
True. Catalog uses the detector
844 id
for the catalog id, sorted on id
for fast lookup.
848 indices : `list` [`int`]
849 Indices of calExpList
and friends that have valid photoCalib/skyWcs
851 backgroundList = len(calExpList)*[None]
if backgroundList
is None else backgroundList
852 skyCorrList = len(calExpList)*[
None]
if skyCorrList
is None else skyCorrList
854 includeCalibVar = self.config.includeCalibVar
857 for index, (calexp, background, skyCorr)
in enumerate(zip(calExpList,
860 mi = calexp.maskedImage
861 if not self.config.bgSubtracted:
862 mi += background.getImage()
864 if externalSkyWcsCatalog
is not None or externalPhotoCalibCatalog
is not None:
865 detectorId = calexp.getInfo().getDetector().getId()
868 if externalPhotoCalibCatalog
is not None:
869 row = externalPhotoCalibCatalog.find(detectorId)
871 self.log.warning(
"Detector id %s not found in externalPhotoCalibCatalog "
872 "and will not be used in the warp.", detectorId)
874 photoCalib = row.getPhotoCalib()
875 if photoCalib
is None:
876 self.log.warning(
"Detector id %s has None for photoCalib in externalPhotoCalibCatalog "
877 "and will not be used in the warp.", detectorId)
879 calexp.setPhotoCalib(photoCalib)
881 photoCalib = calexp.getPhotoCalib()
882 if photoCalib
is None:
883 self.log.warning(
"Detector id %s has None for photoCalib in the calexp "
884 "and will not be used in the warp.", detectorId)
888 if externalSkyWcsCatalog
is not None:
889 row = externalSkyWcsCatalog.find(detectorId)
891 self.log.warning(
"Detector id %s not found in externalSkyWcsCatalog "
892 "and will not be used in the warp.", detectorId)
894 skyWcs = row.getWcs()
896 self.log.warning(
"Detector id %s has None for skyWcs in externalSkyWcsCatalog "
897 "and will not be used in the warp.", detectorId)
899 calexp.setWcs(skyWcs)
901 skyWcs = calexp.getWcs()
903 self.log.warning(
"Detector id %s has None for skyWcs in the calexp "
904 "and will not be used in the warp.", detectorId)
908 calexp.maskedImage = photoCalib.calibrateImage(calexp.maskedImage,
909 includeScaleUncertainty=includeCalibVar)
910 calexp.maskedImage /= photoCalib.getCalibrationMean()
915 if self.config.doApplySkyCorr:
916 mi -= skyCorr.getImage()
918 indices.append(index)
923def reorderRefs(inputRefs, outputSortKeyOrder, dataIdKey):
924 """Reorder inputRefs per outputSortKeyOrder
926 Any inputRefs which are lists will be resorted per specified key e.g.,
927 'detector.' Only iterables will be reordered,
and values can be of type
928 `lsst.pipe.base.connections.DeferredDatasetRef`
or
929 `lsst.daf.butler.core.datasets.ref.DatasetRef`.
930 Returned lists of refs have the same length
as the outputSortKeyOrder.
931 If an outputSortKey
not in the inputRef, then it will be padded
with None.
932 If an inputRef contains an inputSortKey that
is not in the
933 outputSortKeyOrder it will be removed.
937 inputRefs : `lsst.pipe.base.connections.QuantizedConnection`
938 Input references to be reordered
and padded.
939 outputSortKeyOrder : iterable
940 Iterable of values to be compared
with inputRef
's dataId[dataIdKey]
942 dataIdKey in the dataRefs to compare
with the outputSortKeyOrder.
946 inputRefs: `lsst.pipe.base.connections.QuantizedConnection`
947 Quantized Connection
with sorted DatasetRef values sorted
if iterable.
949 for connectionName, refs
in inputRefs:
950 if isinstance(refs, Iterable):
951 if hasattr(refs[0],
"dataId"):
952 inputSortKeyOrder = [ref.dataId[dataIdKey]
for ref
in refs]
954 inputSortKeyOrder = [ref.datasetRef.dataId[dataIdKey]
for ref
in refs]
955 if inputSortKeyOrder != outputSortKeyOrder:
956 setattr(inputRefs, connectionName,
Configuration parameters for CoaddBaseTask.
Base class for coaddition.
def getTempExpDatasetName(self, warpType="direct")
def selectExposures(self, patchRef, skyInfo=None, selectDataList=[])
Select exposures to coadd.
def getCoaddDatasetName(self, warpType="direct")
def getSkyInfo(self, patchRef)
Use getSkyinfo to return the skyMap, tract and patch information, wcs and the outer bbox of the patch...
def getBadPixelMask(self)
Convenience method to provide the bitmask from the mask plane names.
Warp and optionally PSF-Match calexps onto an a common projection.
def getCalibratedExposure(self, dataRef, bgSubtracted)
def run(self, calExpList, ccdIdList, skyInfo, visitId=0, dataIdList=None, **kwargs)
def __init__(self, reuse=False, **kwargs)
def _prepareEmptyExposure(skyInfo)
def runDataRef(self, patchRef, selectDataList=[])
Produce <coaddName>Coadd_<warpType>Warp images by warping and optionally PSF-matching.
def getWarpTypeList(self)
def applySkyCorr(self, dataRef, calexp)
def reorderAndPadList(inputList, inputKeys, outputKeys, padWith=None)
def makeSkyInfo(skyMap, tractId, patchId)
def getGroupDataRef(butler, datasetType, groupTuple, keys)
def groupPatchExposures(patchDataRef, calexpDataRefList, coaddDatasetType="deepCoadd", tempExpDatasetType="deepCoadd_directWarp")