LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
coaddDriver.py
Go to the documentation of this file.
1import lsst.afw.image as afwImage
2import lsst.afw.geom as afwGeom
3from lsst.afw.fits import FitsError
4from lsst.coadd.utils.getGen3CoaddExposureId import getGen3CoaddExposureId
5import lsst.geom as geom
6from lsst.ctrl.pool.parallel import BatchPoolTask
7from lsst.ctrl.pool.pool import Pool, abortOnError, NODE
8import lsst.sphgeom
9from lsst.pex.config import Config, Field, ConfigurableField
10from lsst.pipe.base import Struct, ArgumentParser, ConfigDatasetType
11from lsst.pipe.tasks.coaddBase import CoaddTaskRunner
12from lsst.pipe.tasks.makeCoaddTempExp import MakeCoaddTempExpTask
13from lsst.pipe.tasks.multiBand import DetectCoaddSourcesTask
14from lsst.pipe.tasks.selectImages import WcsSelectImagesTask
15from lsst.pipe.tasks.assembleCoadd import SafeClipAssembleCoaddTask
16from lsst.pipe.drivers.utils import getDataRef, NullSelectImagesTask, TractDataIdContainer
17
18
20 coaddName = Field(dtype=str, default="deep", doc="Name for coadd")
22 target=WcsSelectImagesTask, doc="Select images to process")
23 makeCoaddTempExp = ConfigurableField(
24 target=MakeCoaddTempExpTask, doc="Warp images to sky")
25 doBackgroundReference = Field(
26 dtype=bool, default=False, doc="Build background reference?")
27 backgroundReference = ConfigurableField(
28 target=NullSelectImagesTask, doc="Build background reference")
29 assembleCoadd = ConfigurableField(
30 target=SafeClipAssembleCoaddTask, doc="Assemble warps into coadd")
31 doDetection = Field(dtype=bool, default=True,
32 doc="Run detection on the coaddition product")
33 detectCoaddSources = ConfigurableField(
34 target=DetectCoaddSourcesTask, doc="Detect sources on coadd")
35 hasFakes = Field(dtype=bool, default=False,
36 doc="Should be set to True if fake sources were added to the data before processing.")
37 calexpType = Field(dtype=str, default="calexp",
38 doc="Should be set to fakes_calexp if you want to process calexps with fakes in.")
39
40 def setDefaults(self):
41 self.makeCoaddTempExpmakeCoaddTempExp.select.retarget(NullSelectImagesTask)
42 self.assembleCoaddassembleCoadd.select.retarget(NullSelectImagesTask)
43 self.assembleCoaddassembleCoadd.doWrite = False
44
45 def validate(self):
46 if self.makeCoaddTempExpmakeCoaddTempExp.coaddName != self.coaddNamecoaddName:
47 raise RuntimeError(
48 "makeCoaddTempExp.coaddName and coaddName don't match")
49 if self.assembleCoaddassembleCoadd.coaddName != self.coaddNamecoaddName:
50 raise RuntimeError(
51 "assembleCoadd.coaddName and coaddName don't match")
52 if self.assembleCoaddassembleCoadd.matchingKernelSize != self.makeCoaddTempExpmakeCoaddTempExp.matchingKernelSize:
53 message = ("assembleCoadd.matchingKernelSize (%s) and makeCoaddTempExp.matchingKernelSize (%s)"
54 " don't match" % (self.assembleCoaddassembleCoadd.matchingKernelSize,
55 self.makeCoaddTempExpmakeCoaddTempExp.matchingKernelSize))
56 raise RuntimeError(message)
57
58
60
61 def __init__(self, TaskClass, parsedCmd, doReturnResults=False):
62 CoaddTaskRunner.__init__(self, TaskClass, parsedCmd, doReturnResults)
63 self.reusereuse = parsedCmd.reuse
64
65 def makeTask(self, parsedCmd=None, args=None):
66 return self.TaskClass(config=self.config, log=self.log, reuse=self.reusereuse)
67
68 @staticmethod
69 def getTargetList(parsedCmd, **kwargs):
70 """!Get bare butler into Task
71
72 @param parsedCmd results of parsing command input
73 """
74 kwargs["butler"] = parsedCmd.butler
75 kwargs["selectIdList"] = [
76 ref.dataId for ref in parsedCmd.selectId.refList]
77 return [(parsedCmd.id.refList, kwargs), ]
78
79
80def unpickle(factory, args, kwargs):
81 """Unpickle something by calling a factory"""
82 return factory(*args, **kwargs)
83
84
86 ConfigClass = CoaddDriverConfig
87 _DefaultName = "coaddDriver"
88 RunnerClass = CoaddDriverTaskRunner
89
90 def __init__(self, reuse=tuple(), **kwargs):
91 BatchPoolTask.__init__(self, **kwargs)
92 self.reusereuse = reuse
93 self.makeSubtask("select")
94 self.makeSubtask("makeCoaddTempExp", reuse=("makeCoaddTempExp" in self.reusereuse))
95 self.makeSubtask("backgroundReference")
96 self.makeSubtask("assembleCoadd")
97 self.makeSubtask("detectCoaddSources")
98 if self.config.hasFakes:
99 self.calexpTypecalexpType = "fakes_calexp"
100 else:
101 self.calexpTypecalexpType = "calexp"
102
103 def __reduce__(self):
104 """Pickler"""
105 return unpickle, (self.__class__, [], dict(config=self.config, name=self._name,
106 parentTask=self._parentTask, log=self.log,
107 reuse=self.reusereuse))
108
109 @classmethod
110 def _makeArgumentParser(cls, **kwargs):
111 """!Build argument parser
112
113 Selection references are not cheap (reads Wcs), so are generated
114 only if we're not doing a batch submission.
115 """
116 parser = ArgumentParser(name=cls._DefaultName_DefaultName)
117 parser.add_id_argument("--id", "deepCoadd", help="data ID, e.g. --id tract=12345 patch=1,2",
118 ContainerClass=TractDataIdContainer)
119 datasetType = ConfigDatasetType(name="calexpType")
120 parser.add_id_argument("--selectId", datasetType=datasetType,
121 help="data ID, e.g. --selectId visit=6789 ccd=0..9")
122 parser.addReuseOption(["makeCoaddTempExp", "assembleCoadd", "detectCoaddSources"])
123 return parser
124
125 @classmethod
126 def batchWallTime(cls, time, parsedCmd, numCores):
127 """!
128 Return walltime request for batch job
129
130 @param time: Requested time per iteration
131 @param parsedCmd: Results of argument parsing
132 @param numCores: Number of cores
133 @return float walltime request length
134 """
135 numTargets = len(parsedCmd.selectId.refList)
136 return time*numTargets/float(numCores)
137
138 @abortOnError
139 def runDataRef(self, tractPatchRefList, butler, selectIdList=[]):
140 """!Determine which tracts are non-empty before processing
141
142 @param tractPatchRefList: List of tracts and patches to include in the coaddition
143 @param butler: butler reference object
144 @param selectIdList: List of data Ids (i.e. visit, ccd) to consider when making the coadd
145 @return list of references to sel.runTract function evaluation for each tractPatchRefList member
146 """
147 pool = Pool("tracts")
148 pool.storeSet(butler=butler, skymap=butler.get(
149 self.config.coaddName + "Coadd_skyMap"))
150 tractIdList = []
151 for patchRefList in tractPatchRefList:
152 tractSet = set([patchRef.dataId["tract"]
153 for patchRef in patchRefList])
154 assert len(tractSet) == 1
155 tractIdList.append(tractSet.pop())
156
157 selectDataList = [data for data in pool.mapNoBalance(self.readSelectionreadSelection, selectIdList) if
158 data is not None]
159 nonEmptyList = pool.mapNoBalance(
160 self.checkTractcheckTract, tractIdList, selectDataList)
161 tractPatchRefList = [patchRefList for patchRefList, nonEmpty in
162 zip(tractPatchRefList, nonEmptyList) if nonEmpty]
163 self.log.info("Non-empty tracts (%d): %s" % (len(tractPatchRefList),
164 [patchRefList[0].dataId["tract"] for patchRefList in
165 tractPatchRefList]))
166 # Install the dataRef in the selectDataList
167 for data in selectDataList:
168 data.dataRef = getDataRef(butler, data.dataId, self.calexpTypecalexpType)
169
170 # Process the non-empty tracts
171 return [self.runrun(patchRefList, butler, selectDataList) for patchRefList in tractPatchRefList]
172
173 @abortOnError
174 def run(self, patchRefList, butler, selectDataList=[]):
175 """!Run stacking on a tract
176
177 This method only runs on the master node.
178
179 @param patchRefList: List of patch data references for tract
180 @param butler: Data butler
181 @param selectDataList: List of SelectStruct for inputs
182 """
183 pool = Pool("stacker")
184 pool.cacheClear()
185 pool.storeSet(butler=butler, warpType=self.config.coaddName + "Coadd_directWarp",
186 coaddType=self.config.coaddName + "Coadd")
187 patchIdList = [patchRef.dataId for patchRef in patchRefList]
188
189 selectedData = pool.map(self.warpwarp, patchIdList, selectDataList)
190 if self.config.doBackgroundReference:
191 self.backgroundReference.runDataRef(patchRefList, selectDataList)
192
193 def refNamer(patchRef):
194 return tuple(map(int, patchRef.dataId["patch"].split(",")))
195
196 lookup = dict(zip(map(refNamer, patchRefList), selectedData))
197 coaddData = [Struct(patchId=patchRef.dataId, selectDataList=lookup[refNamer(patchRef)]) for
198 patchRef in patchRefList]
199 pool.map(self.coaddcoadd, coaddData)
200
201 def readSelection(self, cache, selectId):
202 """!Read Wcs of selected inputs
203
204 This method only runs on slave nodes.
205 This method is similar to SelectDataIdContainer.makeDataRefList,
206 creating a Struct like a SelectStruct, except with a dataId instead
207 of a dataRef (to ease MPI).
208
209 @param cache: Pool cache
210 @param selectId: Data identifier for selected input
211 @return a SelectStruct with a dataId instead of dataRef
212 """
213 try:
214 ref = getDataRef(cache.butler, selectId, self.calexpTypecalexpType)
215 self.log.info("Reading Wcs from %s" % (selectId,))
216 md = ref.get("calexp_md", immediate=True)
217 wcs = afwGeom.makeSkyWcs(md)
218 data = Struct(dataId=selectId, wcs=wcs, bbox=afwImage.bboxFromMetadata(md))
219 except FitsError:
220 self.log.warn("Unable to construct Wcs from %s" % (selectId,))
221 return None
222 return data
223
224 def checkTract(self, cache, tractId, selectIdList):
225 """!Check whether a tract has any overlapping inputs
226
227 This method only runs on slave nodes.
228
229 @param cache: Pool cache
230 @param tractId: Data identifier for tract
231 @param selectDataList: List of selection data
232 @return whether tract has any overlapping inputs
233 """
234 def makePolygon(wcs, bbox):
235 """Return a polygon for the image, given Wcs and bounding box"""
236 boxPixelCorners = geom.Box2D(bbox).getCorners()
237 boxSkyCorners = wcs.pixelToSky(boxPixelCorners)
238 return lsst.sphgeom.ConvexPolygon.convexHull([coord.getVector() for coord in boxSkyCorners])
239
240 skymap = cache.skymap
241 tract = skymap[tractId]
242 tractWcs = tract.getWcs()
243 tractPoly = makePolygon(tractWcs, tract.getBBox())
244
245 for selectData in selectIdList:
246 if not hasattr(selectData, "poly"):
247 selectData.poly = makePolygon(selectData.wcs, selectData.bbox)
248 if tractPoly.intersects(selectData.poly):
249 return True
250 return False
251
252 def warp(self, cache, patchId, selectDataList):
253 """!Warp all images for a patch
254
255 Only slave nodes execute this method.
256
257 Because only one argument may be passed, it is expected to
258 contain multiple elements, which are:
259
260 @param patchRef: data reference for patch
261 @param selectDataList: List of SelectStruct for inputs
262 @return selectDataList with non-overlapping elements removed
263 """
264 patchRef = getDataRef(cache.butler, patchId, cache.coaddType)
265 selectDataList = self.selectExposuresselectExposures(patchRef, selectDataList)
266 with self.logOperationlogOperation("warping %s" % (patchRef.dataId,), catch=True):
267 self.makeCoaddTempExp.runDataRef(patchRef, selectDataList)
268 return selectDataList
269
270 def coadd(self, cache, data):
271 """!Construct coadd for a patch and measure
272
273 Only slave nodes execute this method.
274
275 Because only one argument may be passed, it is expected to
276 contain multiple elements, which are:
277
278 @param patchRef: data reference for patch
279 @param selectDataList: List of SelectStruct for inputs
280 """
281 patchRef = getDataRef(cache.butler, data.patchId, cache.coaddType)
282 selectDataList = data.selectDataList
283 coadd = None
284
285 # We skip the assembleCoadd step if either the *Coadd dataset exists
286 # or we aren't configured to write it, we're supposed to reuse
287 # detectCoaddSources outputs too, and those outputs already exist.
288 canSkipDetection = (
289 "detectCoaddSources" in self.reusereuse and
290 patchRef.datasetExists(self.detectCoaddSources.config.coaddName+"Coadd_det", write=True)
291 )
292 if "assembleCoadd" in self.reusereuse:
293 if patchRef.datasetExists(cache.coaddType, write=True):
294 self.log.info("%s: Skipping assembleCoadd for %s; outputs already exist." %
295 (NODE, patchRef.dataId))
296 coadd = patchRef.get(cache.coaddType, immediate=True)
297 elif not self.config.assembleCoadd.doWrite and self.config.doDetection and canSkipDetection:
298 self.log.info(
299 "%s: Skipping assembleCoadd and detectCoaddSources for %s; outputs already exist." %
300 (NODE, patchRef.dataId)
301 )
302 return
303 if coadd is None:
304 with self.logOperationlogOperation("coadding %s" % (patchRef.dataId,), catch=True):
305 coaddResults = self.assembleCoadd.runDataRef(patchRef, selectDataList)
306 if coaddResults is not None:
307 coadd = coaddResults.coaddExposure
308 canSkipDetection = False # can't skip it because coadd may have changed
309 if coadd is None:
310 return
311
312 # The section of code below determines if the detection task should be
313 # run. If detection is run, then the products are written out as
314 # deepCoadd_calexp. If detection is not run, then the outputs of the
315 # assemble task are written out as deepCoadd.
316 if self.config.doDetection:
317 if canSkipDetection:
318 self.log.info("%s: Skipping detectCoaddSources for %s; outputs already exist." %
319 (NODE, patchRef.dataId))
320 return
321 with self.logOperationlogOperation("detection on {}".format(patchRef.dataId),
322 catch=True):
323 idFactory = self.detectCoaddSources.makeIdFactory(patchRef)
325 patchRef, coaddName=self.detectCoaddSources.config.coaddName, log=self.log)
326 # This includes background subtraction, so do it before writing
327 # the coadd
328 detResults = self.detectCoaddSources.run(coadd, idFactory, expId=expId)
329 self.detectCoaddSources.write(detResults, patchRef)
330 else:
331 if self.config.hasFakes:
332 patchRef.put(coadd, "fakes_" + self.assembleCoadd.config.coaddName + "Coadd")
333 else:
334 patchRef.put(coadd, self.assembleCoadd.config.coaddName + "Coadd")
335
336 def selectExposures(self, patchRef, selectDataList):
337 """!Select exposures to operate upon, via the SelectImagesTask
338
339 This is very similar to CoaddBaseTask.selectExposures, except we return
340 a list of SelectStruct (same as the input), so we can plug the results into
341 future uses of SelectImagesTask.
342
343 @param patchRef data reference to a particular patch
344 @param selectDataList list of references to specific data products (i.e. visit, ccd)
345 @return filtered list of SelectStruct
346 """
347 def key(dataRef):
348 return tuple(dataRef.dataId[k] for k in sorted(dataRef.dataId))
349 inputs = dict((key(select.dataRef), select)
350 for select in selectDataList)
351 skyMap = patchRef.get(self.config.coaddName + "Coadd_skyMap")
352 tract = skyMap[patchRef.dataId["tract"]]
353 patch = tract[(tuple(int(i)
354 for i in patchRef.dataId["patch"].split(",")))]
355 bbox = patch.getOuterBBox()
356 wcs = tract.getWcs()
357 cornerPosList = geom.Box2D(bbox).getCorners()
358 coordList = [wcs.pixelToSky(pos) for pos in cornerPosList]
359 dataRefList = self.select.runDataRef(
360 patchRef, coordList, selectDataList=selectDataList).dataRefList
361 return [inputs[key(dataRef)] for dataRef in dataRefList]
362
363 def writeMetadata(self, dataRef):
364 pass
def logOperation(self, operation, catch=False, trace=True)
Provide a context manager for logging an operation.
Definition: parallel.py:502
A floating-point coordinate rectangle geometry.
Definition: Box.h:413
def readSelection(self, cache, selectId)
Read Wcs of selected inputs.
Definition: coaddDriver.py:201
def coadd(self, cache, data)
Construct coadd for a patch and measure.
Definition: coaddDriver.py:270
def runDataRef(self, tractPatchRefList, butler, selectIdList=[])
Determine which tracts are non-empty before processing.
Definition: coaddDriver.py:139
def batchWallTime(cls, time, parsedCmd, numCores)
Return walltime request for batch job.
Definition: coaddDriver.py:126
def checkTract(self, cache, tractId, selectIdList)
Check whether a tract has any overlapping inputs.
Definition: coaddDriver.py:224
def selectExposures(self, patchRef, selectDataList)
Select exposures to operate upon, via the SelectImagesTask.
Definition: coaddDriver.py:336
def warp(self, cache, patchId, selectDataList)
Warp all images for a patch.
Definition: coaddDriver.py:252
def run(self, patchRefList, butler, selectDataList=[])
Run stacking on a tract.
Definition: coaddDriver.py:174
def __init__(self, reuse=tuple(), **kwargs)
Definition: coaddDriver.py:90
def getTargetList(parsedCmd, **kwargs)
Get bare butler into Task.
Definition: coaddDriver.py:69
def __init__(self, TaskClass, parsedCmd, doReturnResults=False)
Definition: coaddDriver.py:61
def makeTask(self, parsedCmd=None, args=None)
Definition: coaddDriver.py:65
static ConvexPolygon convexHull(std::vector< UnitVector3d > const &points)
convexHull returns the convex hull of the given set of points if it exists and throws an exception ot...
Definition: ConvexPolygon.h:65
daf::base::PropertySet * set
Definition: fits.cc:912
std::shared_ptr< SkyWcs > makeSkyWcs(daf::base::PropertySet &metadata, bool strip=false)
Construct a SkyWcs from FITS keywords.
Definition: SkyWcs.cc:521
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
lsst::geom::Box2I bboxFromMetadata(daf::base::PropertySet &metadata)
Determine the image bounding box from its metadata (FITS header)
Definition: Image.cc:721
def getGen3CoaddExposureId(dataRef, coaddName="deep", includeBand=True, log=None)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
def unpickle(factory, args, kwargs)
Definition: coaddDriver.py:80
def getDataRef(butler, dataId, datasetType="raw")
Definition: utils.py:16
def write(self, patchRef, catalog)
Write the output.