lsst.meas.astrom  16.0-13-g5f6c0b1+2
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 astropy.time
26 
27 import lsst.geom
28 from lsst.daf.base import DateTime
29 import lsst.afw.math as afwMath
30 import lsst.pex.config as pexConfig
31 import lsst.pipe.base as pipeBase
32 from lsst.meas.algorithms import ScienceSourceSelectorTask, ReferenceSourceSelectorTask
33 from .matchOptimisticB import MatchOptimisticBTask
34 from .display import displayAstrometry
35 from . import makeMatchStatistics
36 
37 
38 class RefMatchConfig(pexConfig.Config):
39  matcher = pexConfig.ConfigurableField(
40  target=MatchOptimisticBTask,
41  doc="reference object/source matcher",
42  )
43  matchDistanceSigma = pexConfig.RangeField(
44  doc="the maximum match distance is set to "
45  " mean_match_distance + matchDistanceSigma*std_dev_match_distance; " +
46  "ignored if not fitting a WCS",
47  dtype=float,
48  default=2,
49  min=0,
50  )
51  sourceSelection = pexConfig.ConfigurableField(target=ScienceSourceSelectorTask,
52  doc="Selection of science sources")
53  referenceSelection = pexConfig.ConfigurableField(target=ReferenceSourceSelectorTask,
54  doc="Selection of reference sources")
55 
56 # The following block adds links to this task from the Task Documentation page.
57 
63 
64 
65 class RefMatchTask(pipeBase.Task):
66  """!Match an input source catalog with objects from a reference catalog
67 
68  @anchor RefMatchTask_
69  """
70  ConfigClass = RefMatchConfig
71  _DefaultName = "calibrationBaseClass"
72 
73  def __init__(self, refObjLoader, schema=None, **kwargs):
74  """!Construct a RefMatchTask
75 
76  @param[in] refObjLoader A reference object loader object
77  @param[in] schema ignored; available for compatibility with an older astrometry task
78  @param[in] kwargs additional keyword arguments for pipe_base Task.\_\_init\_\_
79  """
80  pipeBase.Task.__init__(self, **kwargs)
81  self.refObjLoader = refObjLoader
82  self.makeSubtask("matcher")
83  self.makeSubtask("sourceSelection")
84  self.makeSubtask("referenceSelection")
85 
86  @pipeBase.timeMethod
87  def loadAndMatch(self, exposure, sourceCat):
88  """!Load reference objects overlapping an exposure and match to sources detected on that exposure
89 
90  @param[in] exposure exposure that the sources overlap
91  @param[in] sourceCat catalog of sources detected on the exposure (an lsst.afw.table.SourceCatalog)
92 
93  @return an lsst.pipe.base.Struct with these fields:
94  - refCat reference object catalog of objects that overlap the exposure (with some margin)
95  (an lsst::afw::table::SimpleCatalog)
96  - matches a list of lsst.afw.table.ReferenceMatch
97  - matchMeta metadata needed to unpersist matches (an lsst.daf.base.PropertyList)
98 
99  @note ignores config.matchDistanceSigma
100  """
101  import lsstDebug
102  debug = lsstDebug.Info(__name__)
103 
104  expMd = self._getExposureMetadata(exposure)
105 
106  sourceSelection = self.sourceSelection.run(sourceCat)
107 
108  loadRes = self.refObjLoader.loadPixelBox(
109  bbox=expMd.bbox,
110  wcs=expMd.wcs,
111  filterName=expMd.filterName,
112  calib=expMd.calib,
113  )
114 
115  refSelection = self.referenceSelection.run(loadRes.refCat)
116 
117  matchMeta = self.refObjLoader.getMetadataBox(
118  bbox=expMd.bbox,
119  wcs=expMd.wcs,
120  filterName=expMd.filterName,
121  calib=expMd.calib,
122  )
123 
124  matchRes = self.matcher.matchObjectsToSources(
125  refCat=refSelection.sourceCat,
126  sourceCat=sourceSelection.sourceCat,
127  wcs=expMd.wcs,
128  refFluxField=loadRes.fluxField,
129  match_tolerance=None,
130  )
131 
132  distStats = self._computeMatchStatsOnSky(matchRes.matches)
133  self.log.info(
134  "Found %d matches with scatter = %0.3f +- %0.3f arcsec; " %
135  (len(matchRes.matches), distStats.distMean.asArcseconds(), distStats.distStdDev.asArcseconds())
136  )
137 
138  if debug.display:
139  frame = int(debug.frame)
141  refCat=refSelection.sourceCat,
142  sourceCat=sourceSelection.sourceCat,
143  matches=matchRes.matches,
144  exposure=exposure,
145  bbox=expMd.bbox,
146  frame=frame,
147  title="Matches",
148  )
149 
150  return pipeBase.Struct(
151  refCat=loadRes.refCat,
152  refSelection=refSelection,
153  sourceSelection=sourceSelection,
154  matches=matchRes.matches,
155  matchMeta=matchMeta,
156  )
157 
158  def _computeMatchStatsOnSky(self, matchList):
159  """Compute on-sky radial distance statistics for a match list
160 
161  @param[in] matchList list of matches between reference object and sources;
162  the distance field is the only field read and it must be set to distance in radians
163 
164  @return a pipe_base Struct containing these fields:
165  - distMean clipped mean of on-sky radial separation
166  - distStdDev clipped standard deviation of on-sky radial separation
167  - maxMatchDist distMean + self.config.matchDistanceSigma*distStdDev
168  """
169  distStatsInRadians = makeMatchStatistics(matchList, afwMath.MEANCLIP | afwMath.STDEVCLIP)
170  distMean = distStatsInRadians.getValue(afwMath.MEANCLIP)*lsst.geom.radians
171  distStdDev = distStatsInRadians.getValue(afwMath.STDEVCLIP)*lsst.geom.radians
172  return pipeBase.Struct(
173  distMean=distMean,
174  distStdDev=distStdDev,
175  maxMatchDist=distMean + self.config.matchDistanceSigma*distStdDev,
176  )
177 
178  def _getExposureMetadata(self, exposure):
179  """!Extract metadata from an exposure
180 
181  @return an lsst.pipe.base.Struct containing the following exposure metadata:
182  - bbox: parent bounding box
183  - wcs: WCS (an lsst.afw.geom.Wcs)
184  - calib calibration (an lsst.afw.image.Calib), or None if unknown
185  - filterName: name of filter, or None if unknown
186  - epoch: date of exposure (an astropy.time.Time), or None
187  """
188  exposureInfo = exposure.getInfo()
189  filterName = exposureInfo.getFilter().getName() or None
190  if filterName == "_unknown_":
191  filterName = None
192  epoch = None
193  if exposure.getInfo().hasVisitInfo():
194  epochTaiMjd = exposure.getInfo().getVisitInfo().getDate().get(system=DateTime.MJD,
195  scale=DateTime.TAI)
196  epoch = astropy.time.Time(epochTaiMjd, scale="tai", format="mjd")
197 
198  return pipeBase.Struct(
199  bbox=exposure.getBBox(),
200  wcs=exposureInfo.getWcs(),
201  calib=exposureInfo.getCalib() if exposureInfo.hasCalib() else None,
202  filterName=filterName,
203  epoch=epoch,
204  )
def _computeMatchStatsOnSky(self, matchList)
Definition: ref_match.py:158
def __init__(self, refObjLoader, schema=None, kwargs)
Construct a RefMatchTask.
Definition: ref_match.py:73
def _getExposureMetadata(self, exposure)
Extract metadata from an exposure.
Definition: ref_match.py:178
Match an input source catalog with objects from a reference catalog.
Definition: ref_match.py:65
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:87