22 from __future__
import absolute_import, division, print_function
23 from builtins
import object
26 import lsst.pex.config
as pexConfig
27 import lsst.afw.table
as afwTable
28 import lsst.afw.image
as afwImage
29 import lsst.pipe.base
as pipeBase
30 from lsst.meas.algorithms
import CoaddPsf, makeCoaddApCorrMap
32 __all__ = [
"CoaddInputRecorderTask"]
36 """Config for CoaddInputRecorderTask 38 The inputRecorder section of the various coadd tasks' configs should generally agree, 39 or the schemas created by earlier tasks (like MakeCoaddTempExpTask) will not contain 40 the fields filled by later tasks (like AssembleCoaddTask). 42 saveEmptyCcds = pexConfig.Field(
43 dtype=bool, default=
False, optional=
False,
44 doc=(
"Add records for CCDs we iterated over but did not add a coaddTempExp" 45 " due to a lack of unmasked pixels in the coadd footprint.")
47 saveErrorCcds = pexConfig.Field(
48 dtype=bool, default=
False, optional=
False,
49 doc=(
"Add records for CCDs we iterated over but did not add a coaddTempExp" 50 " due to an exception (often due to the calexp not being found on disk).")
52 saveVisitGoodPix = pexConfig.Field(
53 dtype=bool, default=
True, optional=
False,
54 doc=(
"Save the total number of good pixels in each coaddTempExp (redundant with a sum of" 55 " good pixels in associated CCDs)")
57 saveCcdWeights = pexConfig.Field(
58 dtype=bool, default=
True, optional=
False,
59 doc=(
"Save weights in the CCDs table as well as the visits table?" 60 " (This is necessary for easy construction of CoaddPsf, but otherwise duplicate information.)")
65 """A helper class for CoaddInputRecorderTask, managing the CoaddInputs object for that a single 66 CoaddTempExp. This will contain single 'visit' record for the CoaddTempExp and a number of 'ccd' 69 Should generally be created by calling CoaddInputRecorderTask.makeCoaddTempExp(). 75 @param task The CoaddInputRecorderTask that is utilising us 76 @param visitId Identifier (integer) for the visit 77 @param num Number of CCDs for this visit that overlap this 78 patch (for reserving memory) 89 """Add a 'ccd' record for a calexp just added to the CoaddTempExp 91 @param[in] calExp Calibrated exposure just added to the CoaddTempExp, or None in case of 92 failures that should nonetheless be tracked. Should be the original 93 calexp, in that it should contain the original Psf and Wcs, not the 94 warped and/or matched ones. 95 @param[in] ccdId A unique numeric ID for the Exposure. 96 @param[in] nGoodPix Number of good pixels this image will contribute to the CoaddTempExp. 97 If saveEmptyCcds is not set and this value is zero, no record will be 100 if nGoodPix == 0
and not self.
task.config.saveEmptyCcds:
106 record.setI(self.
task.ccdCcdKey, calExp.getDetector().getId())
108 self.
task.log.warn(
"Error getting detector serial number in visit %d; using -1" 110 record.setI(self.
task.ccdCcdKey, -1)
111 record.setI(self.
task.ccdGoodPixKey, nGoodPix)
112 if calExp
is not None:
114 if self.
task.config.saveCcdWeights:
115 record.setD(self.
task.ccdWeightKey, 1.0)
117 def finish(self, coaddTempExp, nGoodPix=None):
118 """Finish creating the CoaddInputs for a CoaddTempExp. 120 @param[in,out] coaddTempExp Exposure object from which to obtain the PSF, WCS, and bounding 121 box for the entry in the 'visits' table. On return, the completed 122 CoaddInputs object will be attached to it. 123 @param[in] nGoodPix Total number of good pixels in the CoaddTempExp; ignored unless 124 saveVisitGoodPix is true. 127 if self.
task.config.saveVisitGoodPix:
129 coaddTempExp.getInfo().setCoaddInputs(self.
coaddInputs)
130 wcs = coaddTempExp.getWcs()
133 coaddTempExp.setPsf(CoaddPsf(self.
coaddInputs.ccds, wcs))
134 apCorrMap = makeCoaddApCorrMap(self.
coaddInputs.ccds, coaddTempExp.getBBox(afwImage.PARENT), wcs)
135 coaddTempExp.getInfo().setApCorrMap(apCorrMap)
137 def _setExposureInfoInRecord(self, exposure, record):
138 """Set exposure info and bbox in an ExposureTable record 140 @param[in] exposure exposure whose info is to be recorded 141 @param[in,out] record record of an ExposureTable to set 143 info = exposure.getInfo()
144 record.setPsf(info.getPsf())
145 record.setWcs(info.getWcs())
146 record.setCalib(info.getCalib())
147 record.setApCorrMap(info.getApCorrMap())
148 record.setValidPolygon(info.getValidPolygon())
149 record.setVisitInfo(info.getVisitInfo())
150 record.setBBox(exposure.getBBox())
153 """Subtask that handles filling a CoaddInputs object for a coadd exposure, tracking the CCDs and 154 visits that went into a coadd. 156 The interface here is a little messy, but I think this is at least partly a product of a bit of 157 messiness in the coadd code it's plugged into. I hope #2590 might result in a better design. 160 ConfigClass = CoaddInputRecorderConfig
163 pipeBase.Task.__init__(self, *args, **kwargs)
165 if self.config.saveVisitGoodPix:
167 doc=
"Number of good pixels in the coaddTempExp")
169 doc=
"Weight for this visit in the coadd")
170 self.
ccdSchema = afwTable.ExposureTable.makeMinimalSchema()
171 self.
ccdCcdKey = self.
ccdSchema.addField(
"ccd", type=numpy.int32, doc=
"cameraGeom CCD serial number")
173 doc=
"Foreign key for the visits (coaddTempExp) catalog")
175 doc=
"Number of good pixels in this CCD")
176 if self.config.saveCcdWeights:
178 doc=
"Weight for this visit in the coadd")
181 """Return a CoaddTempExpInputRecorder instance to help with saving a CoaddTempExp's inputs. 183 The visitId may be any number that is unique for each CoaddTempExp that goes into a coadd, 184 but ideally should be something more meaningful that can be used to reconstruct a data ID. 189 """Create a CoaddInputs object with schemas defined by the task configuration""" 193 """Called by AssembleCoaddTask when adding (a subset of) a coaddTempExp to a coadd. The 194 base class impementation extracts the CoaddInputs from the coaddTempExp and appends 195 them to the given coaddInputs, filling in the weight column(s). 197 Note that the passed coaddTempExp may be a subimage, but that this method will only be 198 called for the first subimage 200 Returns the record for the visit to allow subclasses to fill in additional fields. 201 Warns and returns None if the inputRecorder catalogs for the coaddTempExp are not usable. 203 tempExpInputs = coaddTempExp.getInfo().getCoaddInputs()
204 if len(tempExpInputs.visits) != 1:
205 self.log.warn(
"CoaddInputs for coaddTempExp should have exactly one record in visits table " 206 "(found %d). CoaddInputs for this visit will not be saved." 207 % len(tempExpInputs.visits))
209 inputVisitRecord = tempExpInputs.visits[0]
210 outputVisitRecord = coaddInputs.visits.addNew()
211 outputVisitRecord.assign(inputVisitRecord)
213 for inputCcdRecord
in tempExpInputs.ccds:
214 if inputCcdRecord.getL(self.
ccdVisitKey) != inputVisitRecord.getId():
215 self.log.warn(
"CoaddInputs for coaddTempExp with id %d contains CCDs with visit=%d. " 216 "CoaddInputs may be unreliable." 217 % (inputVisitRecord.getId(), inputCcdRecord.getL(self.
ccdVisitKey)))
218 outputCcdRecord = coaddInputs.ccds.addNew()
219 outputCcdRecord.assign(inputCcdRecord)
220 if self.config.saveCcdWeights:
222 return inputVisitRecord