lsst.meas.algorithms  16.0-22-gf55172b7+1
psfSelectionFromMatchList.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 #
4 # Copyright 2008-2017 AURA/LSST.
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 <https://www.lsstcorp.org/LegalNotices/>.
22 #
23 import numpy
24 import lsst.afw.math as afwMath
25 import lsst.meas.algorithms as measAlg
26 import lsst.afw.display.ds9 as ds9
27 
28 args = [None, "MatchList", None] # allow the user to probe for this signature
29 
30 
31 def selectPsfSources(exposure, matches, psfPolicy):
32  """Get a list of suitable stars to construct a PSF."""
33 
34  import lsstDebug
35  display = lsstDebug.Info(__name__).display
36  displayExposure = lsstDebug.Info(__name__).displayExposure # display the Exposure + spatialCells
37  #
38  # Unpack policy
39  #
40  kernelSize = psfPolicy.get("kernelSize")
41  borderWidth = psfPolicy.get("borderWidth")
42  sizePsfCellX = psfPolicy.get("sizeCellX")
43  sizePsfCellY = psfPolicy.get("sizeCellY")
44  #
45  mi = exposure.getMaskedImage()
46 
47  if display and displayExposure:
48  frame = 0
49  ds9.mtv(mi, frame=frame, title="PSF candidates")
50 
51  psfCellSet = afwMath.SpatialCellSet(mi.getBBox(), sizePsfCellX, sizePsfCellY)
52  psfStars = []
53 
54  for val in matches:
55  ref, source = val[0:2]
56  if not (ref.getFlagForDetection() & measAlg.Flags.STAR) or \
57  (source.getFlagForDetection() & measAlg.Flags.BAD):
58  continue
59 
60  try:
61  cand = measAlg.makePsfCandidate(source, mi)
62  #
63  # The setXXX methods are class static, but it's convenient to call them on
64  # an instance as we don't know Exposure's pixel type (and hence cand's exact type)
65  if cand.getWidth() == 0:
66  cand.setBorderWidth(borderWidth)
67  cand.setWidth(kernelSize + 2*borderWidth)
68  cand.setHeight(kernelSize + 2*borderWidth)
69 
70  im = cand.getMaskedImage().getImage()
71  max = afwMath.makeStatistics(im, afwMath.MAX).getValue()
72  if not numpy.isfinite(max):
73  continue
74 
75  psfCellSet.insertCandidate(cand)
76 
77  if display and displayExposure:
78  ds9.dot("+", source.getXAstrom() - mi.getX0(), source.getYAstrom() - mi.getY0(),
79  size=4, frame=frame, ctype=ds9.CYAN)
80  ds9.dot("o", source.getXAstrom() - mi.getX0(), source.getYAstrom() - mi.getY0(),
81  size=4, frame=frame, ctype=ds9.CYAN)
82  except Exception:
83  continue
84 
85  source.setFlagForDetection(source.getFlagForDetection() | measAlg.Flags.STAR)
86  psfStars += [source]
87 
88  return psfStars, psfCellSet
Fit spatial kernel using approximate fluxes for candidates, and solving a linear system of equations...
Definition: mainpage.dox:13