LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
diaCatalogSourceSelector.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010 LSST Corporation.
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 <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 import numpy as np
23 import lsst.pex.config as pexConfig
24 import lsst.afw.display.ds9 as ds9
25 import lsst.afw.math as afwMath
26 import lsst.meas.algorithms as measAlg
27 import lsst.pex.logging as pexLog
28 
29 class DiaCatalogSourceSelectorConfig(pexConfig.Config):
30  # Selection cuts on the input source catalog
31  fluxLim = pexConfig.Field(
32  doc = "specify the minimum psfFlux for good Kernel Candidates",
33  dtype = float,
34  default = 0.0,
35  check = lambda x: x >= 0.0,
36  )
37  fluxMax = pexConfig.Field(
38  doc = "specify the maximum psfFlux for good Kernel Candidates (ignored if == 0)",
39  dtype = float,
40  default = 0.0,
41  check = lambda x: x >= 0.0,
42  )
43  badPixelFlags = pexConfig.ListField(
44  doc = "Kernel candidate objects may not have any of these bits set",
45  dtype = str,
46  default = ["flags.pixel.edge", "flags.pixel.interpolated.center", "flags.pixel.saturated.center", "flags.badcentroid"],
47  )
48  # Selection cuts on the reference catalog
49  selectStar = pexConfig.Field(
50  doc = "Select objects that are flagged as stars",
51  dtype = bool,
52  default = True
53  )
54  selectGalaxy = pexConfig.Field(
55  doc = "Select objects that are flagged as galaxies",
56  dtype = bool,
57  default = False
58  )
59  includeVariable = pexConfig.Field(
60  doc = "Include objects that are known to be variable",
61  dtype = bool,
62  default = False
63  )
64  grMin = pexConfig.Field(
65  doc = "Minimum g-r color for selection (inclusive)",
66  dtype = float,
67  default = 0.0
68  )
69  grMax = pexConfig.Field(
70  doc = "Maximum g-r color for selection (inclusive)",
71  dtype = float,
72  default = 3.0
73  )
74 
75 class CheckSource(object):
76  """A functor to check whether a source has any flags set that should cause it to be labeled bad."""
77 
78  def __init__(self, table, fluxLim, fluxMax, badPixelFlags):
79  self.keys = [table.getSchema().find(name).key for name in badPixelFlags]
80  self.fluxLim = fluxLim
81  self.fluxMax = fluxMax
82 
83  def __call__(self, source):
84  for k in self.keys:
85  if source.get(k):
86  return False
87  if self.fluxLim != None and source.getPsfFlux() < self.fluxLim: # ignore faint objects
88  return False
89  if self.fluxMax != 0.0 and source.getPsfFlux() > self.fluxMax: # ignore bright objects
90  return False
91  return True
92 
94  ConfigClass = DiaCatalogSourceSelectorConfig
95 
96  def __init__(self, config=None):
97  """Construct a source selector that uses a reference catalog
98 
99  @param[in] config: An instance of ConfigClass
100  """
101  if not config:
102  config = DiaCatalogSourceSelector.ConfigClass()
103  self.config = config
104  self.log = pexLog.Log(pexLog.Log.getDefaultLog(),
105  'lsst.ip.diffim.DiaCatalogSourceSelector', pexLog.Log.INFO)
106 
107  def selectSources(self, exposure, sources, matches=None):
108  """Return a list of Sources for Kernel candidates
109 
110  @param[in] exposure: the exposure containing the sources
111  @param[in] sources: a source list containing sources that may be candidates
112  @param[in] matches: a match vector as produced by meas_astrom; not optional
113  (passing None just allows us to handle the exception better here
114  than in calling code)
115 
116  @return kernelCandidateSourceList: a list of sources to be used as kernel candidates
117 
118  """
119  import lsstDebug
120  display = lsstDebug.Info(__name__).display
121  displayExposure = lsstDebug.Info(__name__).displayExposure
122  pauseAtEnd = lsstDebug.Info(__name__).pauseAtEnd
123 
124  if matches is None:
125  raise RuntimeError(
126  "Cannot use catalog source selector without running astrometry."
127  )
128 
129  mi = exposure.getMaskedImage()
130 
131  if display:
132  frames = {}
133  if displayExposure:
134  ds9.mtv(mi, title="Kernel candidates", frame=lsstDebug.frame)
135  #
136  # Look for flags in each Source
137  #
138  isGoodSource = CheckSource(sources, self.config.fluxLim, self.config.fluxMax, self.config.badPixelFlags)
139 
140  #
141  # Go through and find all the acceptable candidates in the catalogue
142  #
143  kernelCandidateSourceList = []
144 
145  doColorCut = True
146  with ds9.Buffering():
147  for ref, source, d in matches:
148  if not isGoodSource(source):
149  symb, ctype = "+", ds9.RED
150  else:
151  isStar = ref.get("stargal")
152  isVar = not ref.get("photometric")
153  gMag = None
154  rMag = None
155  if doColorCut:
156  try:
157  gMag = -2.5 * np.log10(ref.get("g"))
158  rMag = -2.5 * np.log10(ref.get("r"))
159  except KeyError:
160  self.log.warn("Cannot cut on color info; fields 'g' and 'r' do not exist")
161  doColorCut = False
162  isRightColor = True
163  else:
164  isRightColor = (gMag-rMag) >= self.config.grMin and (gMag-rMag) <= self.config.grMax
165 
166  isRightType = (self.config.selectStar and isStar) or (self.config.selectGalaxy and not isStar)
167  isRightVar = (self.config.includeVariable) or (self.config.includeVariable is isVar)
168  if isRightType and isRightVar and isRightColor:
169  kernelCandidateSourceList.append(source)
170  symb, ctype = "+", ds9.GREEN
171  else:
172  symb, ctype = "o", ds9.BLUE
173 
174  if display and displayExposure:
175  ds9.dot(symb, source.getX() - mi.getX0(), source.getY() - mi.getY0(),
176  size=4, ctype=ctype, frame=lsstDebug.frame)
177 
178  if display:
179  lsstDebug.frame += 1
180  if pauseAtEnd:
181  raw_input("Continue? y[es] p[db] ")
182 
183  return kernelCandidateSourceList
184 
185 measAlg.starSelectorRegistry.register("diacatalog", DiaCatalogSourceSelector)
186 
a place to record messages and descriptions of the state of processing.
Definition: Log.h:154