LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
references.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # LSST Data Management System
4 # Copyright 2008-2014 LSST Corporation.
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 <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 """
24 Subtasks for creating the reference catalogs used in forced measurement.
25 """
26 
27 import lsst.afw.geom
28 import lsst.pex.config
29 import lsst.pipe.base
30 
31 __all__ = ("BaseReferencesTask", "CoaddSrcReferencesTask")
32 
33 class BaseReferencesConfig(lsst.pex.config.Config):
34  removePatchOverlaps = lsst.pex.config.Field(
35  doc = "Only include reference sources for each patch that lie within the patch's inner bbox",
36  dtype = bool,
37  default = True
38  )
39  filter = lsst.pex.config.Field(
40  doc = "Bandpass for reference sources; None indicates chi-squared detections.",
41  dtype = str,
42  optional = True
43  )
44 
45 class BaseReferencesTask(lsst.pipe.base.Task):
46  """!
47  Base class for forced photometry subtask that retrieves reference sources.
48 
49  BaseReferencesTask defines the required API for the references task, which includes:
50  - getSchema(butler)
51  - fetchInPatches(butler, tract, filter, patchList)
52  - fetchInBox(self, butler, tract, filter, bbox, wcs)
53  - the removePatchOverlaps config option
54 
55  It also provides the subset() method, which may be of use to derived classes when
56  reimplementing fetchInBox.
57  """
58 
59  ConfigClass = BaseReferencesConfig
60 
61  def getSchema(self, butler):
62  """!
63  Return the schema for the reference sources.
64 
65  Must be available even before any data has been processed.
66  """
67  raise NotImplementedError("BaseReferencesTask is pure abstract, and cannot be used directly.")
68 
69  def getWcs(self, dataRef):
70  """!
71  Return the WCS for reference sources. The given dataRef must include the tract in its dataId.
72  """
73  raise NotImplementedError("BaseReferencesTask is pure abstract, and cannot be used directly.")
74 
75  def fetchInBox(self, dataRef, bbox, wcs):
76  """!
77  Return reference sources that overlap a region defined by a pixel-coordinate bounding box
78  and corresponding Wcs.
79 
80  @param[in] dataRef ButlerDataRef; the implied data ID must contain the 'tract' key.
81  @param[in] bbox a afw.geom.Box2I or Box2D that defines the region in pixel coordinates
82  @param[in] wcs afw.image.Wcs that maps the bbox to sky coordinates
83 
84  @return an iterable of reference sources
85 
86  It is not required that the returned object be a SourceCatalog; it may be any Python iterable
87  containing SourceRecords (including a lazy iterator).
88 
89  The returned set of sources should be complete and close to minimal.
90  """
91  raise NotImplementedError("BaseReferencesTask is pure abstract, and cannot be used directly.")
92 
93  def fetchInPatches(self, dataRef, patchList):
94  """!
95  Return reference sources that overlap a region defined by one or more SkyMap patches.
96 
97  @param[in] dataRef ButlerDataRef; the implied data ID must contain the 'tract' key.
98  @param[in] patchList list of skymap.PatchInfo instances for which to fetch reference sources
99 
100  @return an iterable of reference sources
101 
102  It is not required that the returned object be a SourceCatalog; it may be any Python sequence
103  containing SourceRecords (including a lazy iterator).
104 
105  The returned set of sources should be complete and close to minimal. If
106  config.removePatchOverlaps is True, only sources within each patch's "inner" bounding box
107  should be returned.
108  """
109  raise NotImplementedError("BaseReferencesTask is pure abstract, and cannot be used directly.")
110 
111  def subset(self, sources, bbox, wcs):
112  """!
113  Filter sources to contain only those within the given box, defined in the coordinate system
114  defined by the given Wcs.
115 
116  @param[in] sources input iterable of SourceRecords
117  @param[in] bbox bounding box with which to filter reference sources (Box2I or Box2D)
118  @param[in] wcs afw.image.Wcs that defines the coordinate system of bbox
119 
120  @return an iterable of filtered reference sources
121 
122  This is not a part of the required BaseReferencesTask interface; it's a convenience function
123  used in implementing fetchInBox that may be of use to subclasses.
124  """
125  boxD = lsst.afw.geom.Box2D(bbox)
126  for source in sources:
127  pixel = wcs.skyToPixel(source.getCoord())
128  if boxD.contains(pixel):
129  yield source
130 
131 class CoaddSrcReferencesConfig(BaseReferencesTask.ConfigClass):
132  coaddName = lsst.pex.config.Field(
133  doc = "Coadd name: typically one of deep or goodSeeing.",
134  dtype = str,
135  default = "deep",
136  )
137 
138  def validate(self):
139  if (self.coaddName == "chiSquared") != (self.filter is None):
140  raise lsst.pex.config.FieldValidationError(
141  field=CoaddSrcReferencesConfig.coaddName,
142  config=self,
143  msg="filter may be None if and only if coaddName is chiSquared"
144  )
145 
147  """!
148  A references task implementation that loads the coadd_src dataset directly from disk using
149  the butler.
150  """
151 
152  ConfigClass = CoaddSrcReferencesConfig
153 
154  def getSchema(self, butler):
155  """Return the schema of the reference catalog"""
156  return butler.get(self.config.coaddName + "Coadd_src_schema", immediate=True).getSchema()
157 
158  def getWcs(self, dataRef):
159  """Return the WCS for reference sources. The given dataRef must include the tract in its dataId.
160  """
161  skyMap = dataRef.get(self.config.coaddName + "Coadd_skyMap", immediate=True)
162  return skyMap[dataRef.dataId["tract"]].getWcs()
163 
164  def fetchInPatches(self, dataRef, patchList):
165  """!
166  An implementation of BaseReferencesTask.fetchInPatches that loads 'coadd_src' catalogs
167  using the butler.
168 
169  The given dataRef must include the tract in its dataId.
170  """
171  dataset = self.config.coaddName + "Coadd_src"
172  tract = dataRef.dataId["tract"]
173  butler = dataRef.butlerSubset.butler
174  for patch in patchList:
175  dataId = {'tract': tract, 'patch': "%d,%d" % patch.getIndex()}
176  if self.config.filter is not None:
177  dataId['filter'] = self.config.filter
178 
179  if not butler.datasetExists(dataset, dataId):
180  raise lsst.pipe.base.TaskError("Reference %s doesn't exist" % (dataId,))
181  self.log.info("Getting references in %s" % (dataId,))
182  catalog = butler.get(dataset, dataId, immediate=True)
183  if self.config.removePatchOverlaps:
184  bbox = lsst.afw.geom.Box2D(patch.getInnerBBox())
185  for source in catalog:
186  if bbox.contains(source.getCentroid()):
187  yield source
188  else:
189  for source in catalog:
190  yield source
191 
192  def fetchInBox(self, dataRef, bbox, wcs, pad=0):
193  """!
194  Return reference sources that overlap a region defined by a pixel-coordinate bounding box
195  and corresponding Wcs.
196 
197  @param[in] dataRef ButlerDataRef; the implied data ID must contain the 'tract' key.
198  @param[in] bbox a afw.geom.Box2I or Box2D that defines the region in pixel coordinates
199  @param[in] wcs afw.image.Wcs that maps the bbox to sky coordinates
200  @param[in] pad a buffer to grow the bounding box by after catalogs have been loaded, but
201  before filtering them to include just the given bounding box.
202 
203  @return an iterable of reference sources
204  """
205  skyMap = dataRef.get(self.config.coaddName + "Coadd_skyMap", immediate=True)
206  tract = skyMap[dataRef.dataId["tract"]]
207  coordList = [wcs.pixelToSky(corner) for corner in lsst.afw.geom.Box2D(bbox).getCorners()]
208  self.log.info("Getting references in region with corners %s [degrees]" %
209  ", ".join("(%s)" % coord.getPosition(lsst.afw.geom.degrees) for coord in coordList))
210  patchList = tract.findPatchList(coordList)
211  # After figuring out which patch catalogs to read from the bbox, pad out the bbox if desired
212  # But don't add any new patches while padding
213  if pad:
214  bbox.grow(pad)
215  return self.subset(self.fetchInPatches(dataRef, patchList), bbox, wcs)
def getSchema
Return the schema for the reference sources.
Definition: references.py:61
def getWcs
Return the WCS for reference sources.
Definition: references.py:69
Base class for forced photometry subtask that retrieves reference sources.
Definition: references.py:45
A references task implementation that loads the coadd_src dataset directly from disk using the butler...
Definition: references.py:146
def subset
Filter sources to contain only those within the given box, defined in the coordinate system defined b...
Definition: references.py:111
def fetchInBox
Return reference sources that overlap a region defined by a pixel-coordinate bounding box and corresp...
Definition: references.py:192
def fetchInBox
Return reference sources that overlap a region defined by a pixel-coordinate bounding box and corresp...
Definition: references.py:75
def fetchInPatches
Return reference sources that overlap a region defined by one or more SkyMap patches.
Definition: references.py:93
A floating-point coordinate rectangle geometry.
Definition: Box.h:271
def fetchInPatches
An implementation of BaseReferencesTask.fetchInPatches that loads &#39;coadd_src&#39; catalogs using the butl...
Definition: references.py:164