30 __all__ = [
"CoaddInputRecorderTask"]
33 """Config for CoaddInputRecorderTask
35 The inputRecorder section of the various coadd tasks' configs should generally agree,
36 or the schemas created by earlier tasks (like MakeCoaddTempExpTask) will not contain
37 the fields filled by later tasks (like AssembleCoaddTask).
39 saveEmptyCcds = pexConfig.Field(
40 dtype=bool, default=
False, optional=
False,
41 doc=(
"Add records for CCDs we iterated over but did not add a coaddTempExp"
42 " due to a lack of unmasked pixels in the coadd footprint.")
44 saveErrorCcds = pexConfig.Field(
45 dtype=bool, default=
False, optional=
False,
46 doc=(
"Add records for CCDs we iterated over but did not add a coaddTempExp"
47 " due to an exception (often due to the calexp not being found on disk).")
49 saveVisitGoodPix = pexConfig.Field(
50 dtype=bool, default=
False, optional=
False,
51 doc=(
"Save the total number of good pixels in each coaddTempExp (redundant with a sum of"
52 " good pixels in associated CCDs)")
54 saveCcdWeights = pexConfig.Field(
55 dtype=bool, default=
True, optional=
False,
56 doc=(
"Save weights in the CCDs table as well as the visits table?"
57 " (This is necessary for easy construction of CoaddPsf, but otherwise duplicate information.)")
61 """A helper class for CoaddInputRecorderTask, managing the CoaddInputs object for that a single
62 CoaddTempExp. This will contain single 'visit' record for the CoaddTempExp and a number of 'ccd'
65 Should generally be created by calling CoaddInputRecorderTask.makeCoaddTempExp().
72 self.visitRecord.setId(visitId)
75 """Add a 'ccd' record for a calexp just added to the CoaddTempExp
77 @param[in] calExp Calibrated exposure just added to the CoaddTempExp, or None in case of
78 failures that should nonetheless be tracked. Should be the original
79 calexp, in that it should contain the original Psf and Wcs, not the
80 warped and/or matched ones.
81 @param[in] ccdId A unique numeric ID for the Exposure.
82 @param[in] nGoodPix Number of good pixels this image will contribute to the CoaddTempExp.
83 If saveEmptyCcds is not set and this value is zero, no record will be
86 if nGoodPix == 0
and not self.task.config.saveEmptyCcds:
88 record = self.coaddInputs.ccds.addNew()
90 record.setL(self.task.ccdVisitKey, self.visitRecord.getId())
92 record.setI(self.task.ccdCcdKey, calExp.getDetector().getId())
94 self.task.log.warn(
"Error getting detector serial number in visit %d; using -1"
95 % self.visitRecord.getId())
96 record.setI(self.task.ccdCcdKey, -1)
97 record.setI(self.task.ccdGoodPixKey, nGoodPix)
98 if calExp
is not None:
99 record.setPsf(calExp.getPsf())
100 record.setWcs(calExp.getWcs())
101 record.setBBox(calExp.getBBox())
103 def finish(self, coaddTempExp, nGoodPix=None):
104 """Finish creating the CoaddInputs for a CoaddTempExp.
106 @param[in,out] coaddTempExp Exposure object from which to obtain the PSF, WCS, and bounding
107 box for the entry in the 'visits' table. On return, the completed
108 CoaddInputs object will be attached to it.
109 @param[in] nGoodPix Total number of good pixels in the CoaddTempExp; ignored unless
110 saveVisitGoodPix is true.
112 self.visitRecord.setPsf(coaddTempExp.getPsf())
113 self.visitRecord.setWcs(coaddTempExp.getWcs())
114 self.visitRecord.setBBox(coaddTempExp.getBBox())
115 if self.task.config.saveVisitGoodPix:
116 self.visitRecord.setI(self.task.visitGoodPixKey, nGoodPix)
117 coaddTempExp.getInfo().setCoaddInputs(self.
coaddInputs)
120 """Subtask that handles filling a CoaddInputs object for a coadd exposure, tracking the CCDs and
121 visits that went into a coadd.
123 The interface here is a little messy, but I think this is at least partly a product of a bit of
124 messiness in the coadd code it's plugged into. I hope #2590 might result in a better design.
127 ConfigClass = CoaddInputRecorderConfig
130 pipeBase.Task.__init__(self, *args, **kwargs)
132 if self.config.saveVisitGoodPix:
134 doc=
"Number of good pixels in the coaddTempExp")
136 doc=
"Weight for this visit in the coadd")
137 self.
ccdSchema = afwTable.ExposureTable.makeMinimalSchema()
138 self.
ccdCcdKey = self.ccdSchema.addField(
"ccd", type=int, doc=
"cameraGeom CCD serial number")
139 self.
ccdVisitKey = self.ccdSchema.addField(
"visit", type=numpy.int64,
140 doc=
"Foreign key for the visits (coaddTempExp) catalog")
142 doc=
"Number of good pixels in this CCD")
143 if self.config.saveCcdWeights:
145 doc=
"Weight for this visit in the coadd")
148 """Return a CoaddTempExpInputRecorder instance to help with saving a CoaddTempExp's inputs.
150 The visitId may be any number that is unique for each CoaddTempExp that goes into a coadd,
151 but ideally should be something more meaningful that can be used to reconstruct a data ID.
156 """Create a CoaddInputs object with schemas defined by the task configuration"""
160 """Called by AssembleCoaddTask when adding (a subset of) a coaddTempExp to a coadd. The
161 base class impementation extracts the CoaddInputs from the coaddTempExp and appends
162 them to the given coaddInputs, filling in the weight column(s).
164 Note that the passed coaddTempExp may be a subimage, but that this method will only be
165 called for the first subimage
167 Returns the record for the visit to allow subclasses to fill in additional fields.
168 Warns and returns None if the inputRecorder catalogs for the coaddTempExp are not usable.
170 tempExpInputs = coaddTempExp.getInfo().getCoaddInputs()
171 if len(tempExpInputs.visits) != 1:
172 self.log.warn(
"CoaddInputs for coaddTempExp should have exactly one record in visits table "
173 "(found %d). CoaddInputs for this visit will not be saved."
174 % len(tempExpInputs.visits))
176 inputVisitRecord = tempExpInputs.visits[0];
177 outputVisitRecord = coaddInputs.visits.addNew()
178 outputVisitRecord.assign(inputVisitRecord)
180 for inputCcdRecord
in tempExpInputs.ccds:
181 if inputCcdRecord.getL(self.
ccdVisitKey) != inputVisitRecord.getId():
182 self.log.warn(
"CoaddInputs for coaddTempExp with id %d contains CCDs with visit=%d. "
183 "CoaddInputs may be unreliable."
184 % (inputVisitRecord.getId(), inputCcdRecord.getL(self.
ccdVisitKey)))
185 outputCcdRecord = coaddInputs.ccds.addNew()
186 outputCcdRecord.assign(inputCcdRecord)
187 if self.config.saveCcdWeights:
189 return inputVisitRecord