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