lsst.meas.astrom  16.0-5-g86fb31a+3
ref_match.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-2016 AURA/LSST.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <https://www.lsstcorp.org/LegalNotices/>.
21 #
22 
23 __all__ = ['RefMatchConfig', 'RefMatchTask']
24 
25 import lsst.geom
26 import lsst.afw.math as afwMath
27 import lsst.pex.config as pexConfig
28 import lsst.pipe.base as pipeBase
29 from lsst.meas.algorithms import ScienceSourceSelectorTask, ReferenceSourceSelectorTask
30 from .matchOptimisticB import MatchOptimisticBTask
31 from .display import displayAstrometry
32 from . import makeMatchStatistics
33 
34 
35 class RefMatchConfig(pexConfig.Config):
36  matcher = pexConfig.ConfigurableField(
37  target=MatchOptimisticBTask,
38  doc="reference object/source matcher",
39  )
40  matchDistanceSigma = pexConfig.RangeField(
41  doc="the maximum match distance is set to "
42  " mean_match_distance + matchDistanceSigma*std_dev_match_distance; " +
43  "ignored if not fitting a WCS",
44  dtype=float,
45  default=2,
46  min=0,
47  )
48  sourceSelection = pexConfig.ConfigurableField(target=ScienceSourceSelectorTask,
49  doc="Selection of science sources")
50  referenceSelection = pexConfig.ConfigurableField(target=ReferenceSourceSelectorTask,
51  doc="Selection of reference sources")
52 
53 # The following block adds links to this task from the Task Documentation page.
54 
60 
61 
62 class RefMatchTask(pipeBase.Task):
63  """!Match an input source catalog with objects from a reference catalog
64 
65  @anchor RefMatchTask_
66  """
67  ConfigClass = RefMatchConfig
68  _DefaultName = "calibrationBaseClass"
69 
70  def __init__(self, refObjLoader, schema=None, **kwargs):
71  """!Construct a RefMatchTask
72 
73  @param[in] refObjLoader A reference object loader object
74  @param[in] schema ignored; available for compatibility with an older astrometry task
75  @param[in] kwargs additional keyword arguments for pipe_base Task.\_\_init\_\_
76  """
77  pipeBase.Task.__init__(self, **kwargs)
78  self.refObjLoader = refObjLoader
79  self.makeSubtask("matcher")
80  self.makeSubtask("sourceSelection")
81  self.makeSubtask("referenceSelection")
82 
83  @pipeBase.timeMethod
84  def loadAndMatch(self, exposure, sourceCat):
85  """!Load reference objects overlapping an exposure and match to sources detected on that exposure
86 
87  @param[in] exposure exposure that the sources overlap
88  @param[in] sourceCat catalog of sources detected on the exposure (an lsst.afw.table.SourceCatalog)
89 
90  @return an lsst.pipe.base.Struct with these fields:
91  - refCat reference object catalog of objects that overlap the exposure (with some margin)
92  (an lsst::afw::table::SimpleCatalog)
93  - matches a list of lsst.afw.table.ReferenceMatch
94  - matchMeta metadata needed to unpersist matches (an lsst.daf.base.PropertyList)
95 
96  @note ignores config.matchDistanceSigma
97  """
98  import lsstDebug
99  debug = lsstDebug.Info(__name__)
100 
101  expMd = self._getExposureMetadata(exposure)
102 
103  sourceSelection = self.sourceSelection.run(sourceCat)
104 
105  loadRes = self.refObjLoader.loadPixelBox(
106  bbox=expMd.bbox,
107  wcs=expMd.wcs,
108  filterName=expMd.filterName,
109  calib=expMd.calib,
110  )
111 
112  refSelection = self.referenceSelection.run(loadRes.refCat)
113 
114  matchMeta = self.refObjLoader.getMetadataBox(
115  bbox=expMd.bbox,
116  wcs=expMd.wcs,
117  filterName=expMd.filterName,
118  calib=expMd.calib,
119  )
120 
121  matchRes = self.matcher.matchObjectsToSources(
122  refCat=refSelection.sourceCat,
123  sourceCat=sourceSelection.sourceCat,
124  wcs=expMd.wcs,
125  refFluxField=loadRes.fluxField,
126  match_tolerance=None,
127  )
128 
129  distStats = self._computeMatchStatsOnSky(matchRes.matches)
130  self.log.info(
131  "Found %d matches with scatter = %0.3f +- %0.3f arcsec; " %
132  (len(matchRes.matches), distStats.distMean.asArcseconds(), distStats.distStdDev.asArcseconds())
133  )
134 
135  if debug.display:
136  frame = int(debug.frame)
138  refCat=refSelection.sourceCat,
139  sourceCat=sourceSelection.sourceCat,
140  matches=matchRes.matches,
141  exposure=exposure,
142  bbox=expMd.bbox,
143  frame=frame,
144  title="Matches",
145  )
146 
147  return pipeBase.Struct(
148  refCat=loadRes.refCat,
149  refSelection=refSelection,
150  sourceSelection=sourceSelection,
151  matches=matchRes.matches,
152  matchMeta=matchMeta,
153  )
154 
155  def _computeMatchStatsOnSky(self, matchList):
156  """Compute on-sky radial distance statistics for a match list
157 
158  @param[in] matchList list of matches between reference object and sources;
159  the distance field is the only field read and it must be set to distance in radians
160 
161  @return a pipe_base Struct containing these fields:
162  - distMean clipped mean of on-sky radial separation
163  - distStdDev clipped standard deviation of on-sky radial separation
164  - maxMatchDist distMean + self.config.matchDistanceSigma*distStdDev
165  """
166  distStatsInRadians = makeMatchStatistics(matchList, afwMath.MEANCLIP | afwMath.STDEVCLIP)
167  distMean = distStatsInRadians.getValue(afwMath.MEANCLIP)*lsst.geom.radians
168  distStdDev = distStatsInRadians.getValue(afwMath.STDEVCLIP)*lsst.geom.radians
169  return pipeBase.Struct(
170  distMean=distMean,
171  distStdDev=distStdDev,
172  maxMatchDist=distMean + self.config.matchDistanceSigma*distStdDev,
173  )
174 
175  def _getExposureMetadata(self, exposure):
176  """!Extract metadata from an exposure
177 
178  @return an lsst.pipe.base.Struct containing the following exposure metadata:
179  - bbox: parent bounding box
180  - wcs: WCS (an lsst.afw.geom.Wcs)
181  - calib calibration (an lsst.afw.image.Calib), or None if unknown
182  - filterName: name of filter, or None if unknown
183  """
184  exposureInfo = exposure.getInfo()
185  filterName = exposureInfo.getFilter().getName() or None
186  if filterName == "_unknown_":
187  filterName = None
188  return pipeBase.Struct(
189  bbox=exposure.getBBox(),
190  wcs=exposureInfo.getWcs(),
191  calib=exposureInfo.getCalib() if exposureInfo.hasCalib() else None,
192  filterName=filterName,
193  )
def _computeMatchStatsOnSky(self, matchList)
Definition: ref_match.py:155
def __init__(self, refObjLoader, schema=None, kwargs)
Construct a RefMatchTask.
Definition: ref_match.py:70
def _getExposureMetadata(self, exposure)
Extract metadata from an exposure.
Definition: ref_match.py:175
Match an input source catalog with objects from a reference catalog.
Definition: ref_match.py:62
def displayAstrometry(refCat=None, sourceCat=None, distortedCentroidKey=None, bbox=None, exposure=None, matches=None, frame=1, title="", pause=True)
Definition: display.py:35
def loadAndMatch(self, exposure, sourceCat)
Load reference objects overlapping an exposure and match to sources detected on that exposure...
Definition: ref_match.py:84