152 matchTolerance=None):
153 """Match sources to position reference stars.
157 refCat : `lsst.afw.table.SimpleCatalog`
158 Reference catalog to match.
159 sourceCat : `lsst.afw.table.SourceCatalog`
160 Catalog of sources found on an exposure. This should already be
161 down-selected to "good"/"usable" sources in the calling Task.
162 wcs : `lsst.afw.geom.SkyWcs`
163 Current WCS of the exposure containing the sources.
164 sourceFluxField : `str`
165 Field of the sourceCat to use for flux
167 Field of the refCat to use for flux
168 matchTolerance : `lsst.meas.astrom.MatchTolerance`
169 Object containing information from previous
170 `lsst.meas.astrom.AstrometryTask` match/fit cycles for use in
171 matching. If `None` is config defaults.
175 matchResult : `lsst.pipe.base.Struct`
176 Result struct with components
178 - ``matches`` : List of matches with distance below the maximum match
179 distance (`list` of `lsst.afw.table.ReferenceMatch`).
180 - ``useableSourceCat`` : Catalog of sources matched and suited for
181 WCS fitting (`lsst.afw.table.SourceCatalog`).
182 - ``matchTolerance`` : MatchTolerance object updated from this
183 match iteration (`lsst.meas.astrom.MatchTolerance`).
188 preNumObj = len(refCat)
190 numRefObj = len(refCat)
193 self.log.info(
"filterStars purged %d reference stars, leaving %d stars",
194 preNumObj - numRefObj, numRefObj)
196 if matchTolerance
is None:
201 usableSourceCat = sourceCat
203 numUsableSources = len(usableSourceCat)
205 if len(usableSourceCat) == 0:
206 raise pipeBase.TaskError(
"No sources are usable")
208 minMatchedPairs = min(self.config.minMatchedPairs,
209 int(self.config.minFracMatchedPairs * min([len(refCat), len(usableSourceCat)])))
214 sourceCat=usableSourceCat,
216 refFluxField=refFluxField,
217 numUsableSources=numUsableSources,
218 minMatchedPairs=minMatchedPairs,
219 maxMatchDist=matchTolerance.maxMatchDist,
220 sourceFluxField=sourceFluxField,
221 verbose=debug.verbose,
227 for match
in usableMatches:
230 matches.append(match)
232 self.log.debug(
"Found %d usable matches, of which %d had good sources",
233 len(usableMatches), len(matches))
235 if len(matches) == 0:
236 raise RuntimeError(
"Unable to match sources")
238 self.log.info(
"Matched %d sources", len(matches))
239 if len(matches) < minMatchedPairs:
240 self.log.warning(
"Number of matches is smaller than request")
242 return pipeBase.Struct(
244 usableSourceCat=usableSourceCat,
245 matchTolerance=matchTolerance,
282 def _doMatch(self, refCat, sourceCat, wcs, refFluxField, numUsableSources, minMatchedPairs,
283 maxMatchDist, sourceFluxField, verbose):
284 """Implementation of matching sources to position reference stars.
286 Unlike matchObjectsToSources, this method does not check if the sources
291 refCat : `lsst.afw.table.SimpleCatalog`
292 Catalog of reference objects.
293 sourceCat : `lsst.afw.table.SourceCatalog`
294 Catalog of detected sources.
295 wcs : `lsst.afw.geom.SkyWcs`
296 Current best WCS of the image.
297 refFluxFioeld : `str`
298 Name of flux field in refCat to use.
299 numUsableSources : `int`
300 Total number of source usable for matching.
301 mintMatchPairs : `int`
302 Minimum number of objects to match between the refCat and sourceCat
303 to consider a valid match.
304 maxMatchDist : `lsst.geom.Angle`
305 Maximum separation to considering a reference and a source a match.
306 sourceFluxField : `str`
307 Name of source catalog flux field.
309 Print diagnostic information std::cout
313 matches : `list` of `lsst.afw.table.ReferenceMatch`
315 numSources = len(sourceCat)
316 posRefBegInd = numUsableSources - numSources
317 if maxMatchDist
is None:
318 maxMatchDistArcSec = self.config.maxMatchDistArcSec
320 maxMatchDistArcSec = min(maxMatchDist.asArcseconds(), self.config.maxMatchDistArcSec)
321 configMatchDistPix = maxMatchDistArcSec/wcs.getPixelScale().asArcseconds()
323 matchControl = MatchOptimisticBControl()
324 matchControl.refFluxField = refFluxField
325 matchControl.sourceFluxField = sourceFluxField
326 matchControl.numBrightStars = self.config.numBrightStars
327 matchControl.minMatchedPairs = self.config.minMatchedPairs
328 matchControl.maxOffsetPix = self.config.maxOffsetPix
329 matchControl.numPointsForShape = self.config.numPointsForShape
330 matchControl.maxDeterminant = self.config.maxDeterminant
332 for maxRotInd
in range(4):
333 matchControl.maxRotationDeg = self.config.maxRotationDeg * math.pow(2.0, 0.5*maxRotInd)
334 for matchRadInd
in range(3):
335 matchControl.matchingAllowancePix = configMatchDistPix * math.pow(1.25, matchRadInd)
337 for angleDiffInd
in range(3):
338 matchControl.allowedNonperpDeg = self.config.allowedNonperpDeg*(angleDiffInd+1)
339 matches = matchOptimisticB(
347 if matches
is not None and len(matches) > 0:
348 setMatchDistance(matches)