LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
directMatch.py
Go to the documentation of this file.
1 
2 __all__ = ["DirectMatchConfig", "DirectMatchTask", "DirectMatchConfigWithoutLoader"]
3 
4 from lsst.pex.config import Config, Field, ConfigurableField
5 from lsst.pipe.base import Task, Struct
6 from lsst.meas.algorithms import (LoadIndexedReferenceObjectsTask, ScienceSourceSelectorTask,
7  ReferenceSourceSelectorTask)
8 import lsst.afw.table as afwTable
9 from lsst.afw.geom import arcseconds, averageSpherePoint
10 
11 
13  """Configuration for DirectMatchTask when an already-initialized
14  refObjLoader will be passed to this task."""
15  matchRadius = Field(dtype=float, default=0.25, doc="Matching radius, arcsec")
16  sourceSelection = ConfigurableField(target=ScienceSourceSelectorTask,
17  doc="Selection of science sources")
18  referenceSelection = ConfigurableField(target=ReferenceSourceSelectorTask,
19  doc="Selection of reference sources")
20 
21 
23  """Configuration for DirectMatchTask"""
24  refObjLoader = ConfigurableField(target=LoadIndexedReferenceObjectsTask, doc="Load reference objects")
25 
26 
28  """Simple, brute force matching of a source catalog to a reference catalog.
29 
30  Parameters
31  ----------
32  butler : `lsst.daf.persistence.Butler`
33  Data butler containing the relevant reference catalog data.
34  refObjLoader : `lsst.meas.algorithms.LoadReferenceObjectsTask` or `None`
35  For loading reference objects
36  **kwargs :
37  Other keyword arguments required for instantiating a Task (e.g., 'config')
38  """
39  ConfigClass = DirectMatchConfig
40  _DefaultName = "directMatch"
41 
42  def __init__(self, butler=None, refObjLoader=None, **kwargs):
43  Task.__init__(self, **kwargs)
44  if not refObjLoader:
45  if butler:
46  if not isinstance(self.config, DirectMatchConfig):
47  raise RuntimeError("DirectMatchTask must be initialized with DirectMatchConfig "
48  "if a refObjLoader is not supplied at initialization")
49  self.makeSubtask("refObjLoader", butler=butler)
50  else:
51  self.refObjLoader = None
52 
53  else:
54  self.refObjLoader = refObjLoader
55  self.makeSubtask("sourceSelection")
56  self.makeSubtask("referenceSelection")
57 
58  def setRefObjLoader(self, refObjLoader):
59  """Sets the reference object loader for the task
60 
61  Parameters
62  ----------
63  refObjLoader : `lsst.meas.algorithms.LoadReferenceObjectsTask` or
64  `lsst.meas.algorithms.ReferenceObjectLoader`
65  An instance of a reference object loader task or class. A task can be used as a subtask
66  and is generally used in gen2 middleware. The class is designed to be used with gen3
67  middleware and is initialized outside the normal task framework.
68  """
69  self.refObjLoader = refObjLoader
70 
71  def run(self, catalog, filterName=None, epoch=None):
72  """Load reference objects and match to them.
73 
74  Parameters
75  ----------
76  catalog : `lsst.afw.table.SourceCatalog`
77  Catalog to match.
78  filterName : `str`
79  Name of filter loading fluxes
80  epoch : `astropy.time.Time` or `None`
81  Epoch to which to correct proper motion and parallax,
82  or `None` to not apply such corrections.
83 
84  Returns
85  -------
86  result : `lsst.pipe.base.Struct`
87  Result struct with components:
88 
89  - matches : Matched sources with associated reference
90  (`lsst.afw.table.SourceMatchVector`)
91  - matchMeta : Match metadata (`lsst.meas.astrom.MatchMetadata`)
92  """
93  if self.refObjLoader is None:
94  raise RuntimeError("Running matcher task with no refObjLoader set in __ini__ or setRefObjLoader")
95  circle = self.calculateCircle(catalog)
96  matchMeta = self.refObjLoader.getMetadataCircle(circle.center, circle.radius, filterName, epoch=epoch)
97  emptyResult = Struct(matches=[], matchMeta=matchMeta)
98  sourceSelection = self.sourceSelection.run(catalog)
99  if len(sourceSelection.sourceCat) == 0:
100  self.log.warn("No objects selected from %d objects in source catalog", len(catalog))
101  return emptyResult
102  refData = self.refObjLoader.loadSkyCircle(circle.center, circle.radius, filterName, epoch=epoch)
103  refCat = refData.refCat
104  refSelection = self.referenceSelection.run(refCat)
105  if len(refSelection.sourceCat) == 0:
106  self.log.warn("No objects selected from %d objects in reference catalog", len(refCat))
107  return emptyResult
108  matches = afwTable.matchRaDec(refSelection.sourceCat, sourceSelection.sourceCat,
109  self.config.matchRadius*arcseconds)
110  self.log.info("Matched %d from %d/%d input and %d/%d reference sources" %
111  (len(matches), len(sourceSelection.sourceCat), len(catalog),
112  len(refSelection.sourceCat), len(refCat)))
113  return Struct(matches=matches, matchMeta=matchMeta, refCat=refCat, sourceSelection=sourceSelection,
114  refSelection=refSelection)
115 
116  def calculateCircle(self, catalog):
117  """Calculate a circle enclosing the catalog
118 
119  Parameters
120  ----------
121  catalog : `lsst.afw.table.SourceCatalog`
122  Catalog to encircle
123 
124  Returns
125  -------
126  result : `lsst.pipe.base.Struct`
127  Result struct with components:
128 
129  - center : ICRS center coordinate (`lsst.afw.geom.SpherePoint`)
130  - radius : Radius of the circle (`lsst.geom.Angle`)
131  """
132  coordList = [src.getCoord() for src in catalog]
133  center = averageSpherePoint(coordList)
134  radius = max(center.separation(coord) for coord in coordList)
135  return Struct(center=center, radius=radius + self.config.matchRadius*arcseconds)
def makeSubtask(self, name, keyArgs)
Definition: task.py:275
Fit spatial kernel using approximate fluxes for candidates, and solving a linear system of equations...
def run(self, catalog, filterName=None, epoch=None)
Definition: directMatch.py:71
template SourceMatchVector matchRaDec(SourceCatalog const &, lsst::geom::Angle, MatchControl const &)
int max
def __init__(self, butler=None, refObjLoader=None, kwargs)
Definition: directMatch.py:42
SpherePoint averageSpherePoint(std::vector< SpherePoint > const &coords)
Return the average of a list of coordinates.
Definition: SpherePoint.cc:235