LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
mockObject.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010, 2011, 2012 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 
23 import numpy
24 
25 import lsst.pex.config
26 import lsst.afw.table
27 import lsst.afw.geom
28 import lsst.afw.image
29 import lsst.pipe.base
30 
31 
32 class MockObjectConfig(lsst.pex.config.Config):
33  minMag = lsst.pex.config.Field(dtype=float, default=18.0, doc="Minimum magnitude for mock objects")
34  maxMag = lsst.pex.config.Field(dtype=float, default=20.0, doc="Maximum magnitude for mock objects")
35  maxRadius = lsst.pex.config.Field(
36  dtype=float, default=10.0,
37  doc=("Maximum radius of an object in arcseconds; only used "
38  "when determining which objects are in an exposure.")
39  )
40  spacing = lsst.pex.config.Field(
41  dtype=float, default=20.0,
42  doc="Distance between objects (in arcseconds)."
43  )
44  seed = lsst.pex.config.Field(dtype=int, default=1, doc="Seed for numpy random number generator")
45 
46 
47 class MockObjectTask(lsst.pipe.base.Task):
48  """Task that generates simple mock objects and draws them on images, intended as a subtask of
49  MockCoaddTask.
50 
51  May be subclassed to generate things other than stars.
52  """
53 
54  ConfigClass = MockObjectConfig
55 
56  def __init__(self, **kwds):
57  lsst.pipe.base.Task.__init__(self, **kwds)
59  self.center = lsst.afw.table.Point2DKey.addFields(self.schema, "center",
60  "center position in tract WCS", "pixel")
61  self.magKey = self.schema.addField("mag", type=float, doc="exact true magnitude")
62  self.rng = numpy.random.RandomState(self.config.seed)
63 
64  def run(self, tractInfo, catalog=None):
65  """Add records to the truth catalog and return it, delegating to makePositions and defineObject.
66 
67  If the given catalog is not None, add records to this catalog and return it instead
68  of creating a new one.
69 
70  Subclasses should generally not need to override this method.
71  """
72  if catalog is None:
73  catalog = lsst.afw.table.SimpleCatalog(self.schema)
74  else:
75  if not catalog.getSchema().contains(self.schema):
76  raise ValueError("Catalog schema does not match Task schema")
77  for coord, center in self.makePositions(tractInfo):
78  record = catalog.addNew()
79  record.setCoord(coord)
80  record.set(self.center, center)
81  self.defineObject(record)
82  return catalog
83 
84  def makePositions(self, tractInfo):
85  """Generate the centers (as a (coord, point) tuple) of mock objects (the point returned is
86  in the tract coordinate system).
87 
88  Default implementation puts objects on a grid that is square in the tract's image coordinate
89  system, with spacing approximately given by config.spacings.
90 
91  The return value is a Python iterable over (coord, point) pairs; the default implementation
92  is actually an iterator (i.e. the function is a "generator"), but derived-class overrides may
93  return any iterable.
94  """
95  wcs = tractInfo.getWcs()
96  spacing = self.config.spacing / wcs.pixelScale().asArcseconds() # get spacing in tract pixels
97  bbox = tractInfo.getBBox()
98  for y in numpy.arange(bbox.getMinY() + 0.5 * spacing, bbox.getMaxY(), spacing):
99  for x in numpy.arange(bbox.getMinX() + 0.5 * spacing, bbox.getMaxX(), spacing):
100  yield wcs.pixelToSky(x, y), lsst.afw.geom.Point2D(x, y),
101 
102  def defineObject(self, record):
103  """Fill in additional fields in a truth catalog record (id and coord will already have
104  been set).
105  """
106  mag = self.rng.rand() * (self.config.maxMag - self.config.minMag) + self.config.minMag
107  record.setD(self.magKey, mag)
108 
109  def drawSource(self, record, exposure, buffer=0):
110  """Draw the given truth catalog record on the given exposure, makings use of its Psf, Wcs,
111  Calib, and possibly filter to transform it appropriately.
112 
113  The mask and variance planes of the Exposure are ignored.
114 
115  The 'buffer' parameter is used to expand the source's bounding box when testing whether it
116  is considered fully part of the image.
117 
118  Returns 0 if the object does not appear on the given image at all, 1 if it appears partially,
119  and 2 if it appears fully (including the given buffer).
120  """
121  wcs = exposure.getWcs()
122  center = wcs.skyToPixel(record.getCoord())
123  try:
124  psfImage = exposure.getPsf().computeImage(center).convertF()
125  except:
126  return 0
127  psfBBox = psfImage.getBBox()
128  overlap = exposure.getBBox()
129  overlap.clip(psfBBox)
130  if overlap.isEmpty():
131  return 0
132  flux = exposure.getCalib().getFlux(record.getD(self.magKey))
133  normalization = flux / psfImage.getArray().sum()
134  if psfBBox != overlap:
135  psfImage = psfImage.Factory(psfImage, overlap)
136  result = 1
137  else:
138  result = 2
139  if buffer != 0:
140  bufferedBBox = lsst.afw.geom.Box2I(psfBBox)
141  bufferedBBox.grow(buffer)
142  bufferedOverlap = exposure.getBBox()
143  bufferedOverlap.clip(bufferedBBox)
144  if bufferedOverlap != bufferedBBox:
145  result = 1
146  image = exposure.getMaskedImage().getImage()
147  subImage = image.Factory(image, overlap)
148  subImage.scaledPlus(normalization, psfImage)
149  return result
static Schema makeMinimalSchema()
Return a minimal schema for Simple tables and records.
Definition: Simple.h:121
An integer coordinate rectangle.
Definition: Box.h:53
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
Definition: fwd.h:55