LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
forcedPhotCoadd.py
Go to the documentation of this file.
1 from builtins import zip
2 #!/usr/bin/env python
3 #
4 # LSST Data Management System
5 # Copyright 2008-2015 AURA/LSST.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <https://www.lsstcorp.org/LegalNotices/>.
23 #
24 import lsst.pex.config
25 import lsst.pipe.base
26 import lsst.coadd.utils
27 import lsst.afw.table
28 
29 from .forcedPhotImage import ForcedPhotImageConfig, ForcedPhotImageTask
30 
31 __all__ = ("ForcedPhotCoaddConfig", "ForcedPhotCoaddTask")
32 
33 
34 class ForcedPhotCoaddConfig(ForcedPhotImageConfig):
35  footprintDatasetName = lsst.pex.config.Field(
36  doc="Dataset (without coadd prefix) that should be used to obtain (Heavy)Footprints for sources. "
37  "Must have IDs that match those of the reference catalog."
38  "If None, Footprints will be generated by transforming the reference Footprints.",
39  dtype=str,
40  default="meas",
41  optional=True
42  )
43 
44  def setDefaults(self):
45  ForcedPhotImageTask.ConfigClass.setDefaults(self)
46  self.references.removePatchOverlaps = False # see validate() for why
47 
48  def validate(self):
49  ForcedPhotImageTask.ConfigClass.validate(self)
50  if (self.measurement.doReplaceWithNoise and self.footprintDatasetName is not None
51  and self.references.removePatchOverlaps):
52  raise ValueError("Cannot use removePatchOverlaps=True with deblended footprints, as parent "
53  "sources may be rejected while their children are not.")
54 
55 ## @addtogroup LSST_task_documentation
56 ## @{
57 ## @page processForcedCoaddTask
58 ## ForcedPhotCoaddTask
59 ## @copybrief ForcedPhotCoaddTask
60 ## @}
61 
62 
63 class ForcedPhotCoaddTask(ForcedPhotImageTask):
64  """!
65  A command-line driver for performing forced measurement on coadd images
66 
67  This task is a subclass of ForcedPhotImageTask which is specifically for doing forced
68  measurement on a coadd, using as a reference catalog detections which were made on overlapping
69  coadds (i.e. in other bands).
70 
71  The run method (inherited from ForcedPhotImageTask) takes a lsst.daf.persistence.ButlerDataRef
72  argument that corresponds to a coadd image. This is used to provide all the inputs and outputs
73  for the task:
74  - A "*Coadd_src" (e.g. "deepCoadd_src") dataset is used as the reference catalog. This not loaded
75  directly from the passed dataRef, however; only the patch and tract are used, while the filter
76  is set by the configuration for the references subtask (see CoaddSrcReferencesTask).
77  - A "*Coadd_calexp" (e.g. "deepCoadd_calexp") dataset is used as the measurement image. Note that
78  this means that ProcessCoaddTask must be run on an image before ForcedPhotCoaddTask, in order
79  to generate the "*Coadd_calexp" dataset.
80  - A "*Coadd_forced_src" (e.g. "deepCoadd_forced_src") dataset will be written with the output
81  measurement catalog.
82 
83  In addition to the run method, ForcedPhotCcdTask overrides several methods of ForcedPhotImageTask
84  to specialize it for coadd processing, including makeIdFactory() and fetchReferences(). None of these
85  should be called directly by the user, though it may be useful to override them further in subclasses.
86  """
87 
88  ConfigClass = ForcedPhotCoaddConfig
89  RunnerClass = lsst.pipe.base.ButlerInitializedTaskRunner
90  _DefaultName = "forcedPhotCoadd"
91  dataPrefix = "deepCoadd_"
92 
93  def getExposure(self, dataRef):
94  name = self.config.coaddName + "Coadd_calexp"
95  return dataRef.get(name) if dataRef.datasetExists(name) else None
96 
97  def makeIdFactory(self, dataRef):
98  """Create an object that generates globally unique source IDs from per-CCD IDs and the CCD ID.
99 
100  @param dataRef Data reference from butler. The "CoaddId_bits" and "CoaddId"
101  datasets are accessed. The data ID must have tract and patch keys.
102  """
103  # With the default configuration, this IdFactory doesn't do anything, because
104  # the IDs it generates are immediately overwritten by the ID from the reference
105  # catalog (since that's in config.copyColumns). But we create one here anyway, to
106  # allow us to revert back to the old behavior of generating new forced source IDs,
107  # just by renaming the ID in config.copyColumns to "object_id".
108  expBits = dataRef.get(self.config.coaddName + "CoaddId_bits")
109  expId = int(dataRef.get(self.config.coaddName + "CoaddId"))
110  return lsst.afw.table.IdFactory.makeSource(expId, 64 - expBits)
111 
112  def getExposureId(self, dataRef):
113  return int(dataRef.get(self.config.coaddName + "CoaddId"))
114 
115  def fetchReferences(self, dataRef, exposure):
116  """Return an iterable of reference sources which overlap the exposure
117 
118  @param dataRef Data reference from butler corresponding to the image to be measured;
119  should have tract, patch, and filter keys.
120  @param exposure lsst.afw.image.Exposure to be measured (not used by this implementation)
121 
122  All work is delegated to the references subtask; see CoaddSrcReferencesTask for information
123  about the default behavior.
124  """
125  skyMap = dataRef.get(self.dataPrefix + "skyMap", immediate=True)
126  tractInfo = skyMap[dataRef.dataId["tract"]]
127  patch = tuple(int(v) for v in dataRef.dataId["patch"].split(","))
128  patchInfo = tractInfo.getPatchInfo(patch)
129  references = lsst.afw.table.SourceCatalog(self.references.schema)
130  references.extend(self.references.fetchInPatches(dataRef, patchList=[patchInfo]))
131  return references
132 
133  def attachFootprints(self, sources, refCat, exposure, refWcs, dataRef):
134  """For coadd forced photometry, we use the deblended HeavyFootprints from the single-band
135  measurements of the same band - because we've guaranteed that the peaks (and hence child sources)
136  will be consistent across all bands before we get to measurement, this should yield reasonable
137  deblending for most sources. It's most likely limitation is that it will not provide good flux
138  upper limits for sources that were not detected in this band but were blended with sources that
139  were.
140  """
141  if self.config.footprintDatasetName is None:
142  return ForcedPhotImageTask.attachFootprints(self, sources, refCat, exposure, refWcs, dataRef)
143  self.log.info("Loading deblended footprints for sources from %s, %s" %
144  (self.config.footprintDatasetName, dataRef.dataId))
145  fpCat = dataRef.get("%sCoadd_%s" % (self.config.coaddName, self.config.footprintDatasetName),
146  immediate=True)
147  for refRecord, srcRecord in zip(refCat, sources):
148  fpRecord = fpCat.find(refRecord.getId())
149  if fpRecord is None:
150  raise LookupError("Cannot find Footprint for source %s; please check that %sCoadd_%s "
151  "IDs are compatible with reference source IDs" %
152  (srcRecord.getId(), self.config.coaddName,
153  self.config.footprintDatasetName))
154  srcRecord.setFootprint(fpRecord.getFootprint())
155 
156  @classmethod
158  parser = lsst.pipe.base.ArgumentParser(name=cls._DefaultName)
159  parser.add_id_argument("--id", "deepCoadd_forced_src", help="data ID, with raw CCD keys + tract",
160  ContainerClass=lsst.coadd.utils.CoaddDataIdContainer)
161  return parser
static boost::shared_ptr< IdFactory > makeSource(RecordId expId, int reserved)
Return an IdFactory that includes another, fixed ID in the higher-order bits.
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
Definition: fwd.h:55
A command-line driver for performing forced measurement on coadd images.