lsst.synpipe  15.0-3-g11fe1a0+3
FakeSourceLib.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # encoding: utf-8
3 
4 import numpy as np
5 
6 import lsst.afw.geom
7 import lsst.afw.math
8 import lsst.pex.config
9 import lsst.afw.image
11 import lsst.pipe.base as pipeBase
12 
13 
14 """
15 Helper functions for making fake sources
16 """
17 
18 
19 class SkyMapIdContainer(pipeBase.DataIdContainer):
20  """A version of lsst.pipe.base.DataIdContainer specialized for loading a skyMap
21  in the make fake source catalog scripts. These scripts use the data id in a
22  unique way, such that they only need a tract number. This class supports that
23  use case and should not be used in any other contexts in the LSST stack.
24  Required because butler.subset does not support only tract
25  """
26 
27  def makeDataRefList(self, namespace):
28  """Make self.refList from self.idList
29  """
30 
31  for dataId in self.idList:
32  if "tract" not in dataId:
33  raise RuntimeError("id must specify which tract to process tract")
34  # warn about unused options
35  for key in dataId:
36  if key != "tract":
37  namespace.log.warn("'{}' specified in --id is unused and will be ignored".format(key))
38  addList = [dataId]
39 
40  self.refList += [namespace.butler.dataRef(datasetType="deepCoadd_skyMap", dataId=addId)
41  for addId in addList]
42 
43 def cropFakeImage(fakeImage, expBBox):
44  """
45  Crops the Fake image to fit inside the exposure BBox
46  Note that the bboxes need to have the correct offsets applied
47  Args:
48  fakeImage: fake image object
49  expBBox: bounding box for CCD exposure (integer type, BBoxI)
50  and with offsets applied
51 
52  Returns:
53  New cropped fake image
54  """
55  fakeBBox = fakeImage.getBBox(lsst.afw.image.PARENT)
56 
57  if not expBBox.contains(fakeBBox):
58  newBBox = fakeImage.getBBox(lsst.afw.image.PARENT)
59  newBBox.clip(expBBox)
60  fakeImage = fakeImage.Factory(fakeImage, newBBox,
61  lsst.afw.image.PARENT)
62  # TODO: finish this up
63 
64 
65 def addNoise(galImage, detector, rand_gen=None):
66  """
67  adds noise to the the image and returns a variance plane
68  INPUT: image to add noise to
69  detector where the image will be located, this sets the gain
70  NOTE: this assumes float type images and will break if given doubles
71  RETURN: a MaskedImageF with the image with additional noise and the
72  variance plane
73  giving the variance due to the object
74  """
75  # TODO: this is gaussian noise right now, probably good enough
76  varImage = galImage.Factory(galImage, True)
77  if rand_gen is None:
78  rand_gen = np.random
79  scale = np.sqrt(np.abs(varImage.getArray())) + 1e-12
80  noiseArray = rand_gen.normal(loc=0.0,
81  scale=scale,
82  size=(galImage.getHeight(),
83  galImage.getWidth()))
84  noiseImage = lsst.afw.image.ImageF(noiseArray.astype(np.float32))
85  galImage += noiseImage
86 
87  return lsst.afw.image.MaskedImageF(galImage, None, varImage)
def cropFakeImage(fakeImage, expBBox)
def addNoise(galImage, detector, rand_gen=None)