LSSTApplications  19.0.0-14-gb0260a2+599893a4c6,20.0.0+126303c00d,20.0.0+2f3d0e5c40,20.0.0+36ef800059,20.0.0+5ac7adcc0c,20.0.0+693a64958a,20.0.0+bebc1f60e8,20.0.0+cad136aba6,20.0.0+e2e26847c2,20.0.0+e69b5d60e7,20.0.0-1-g10df615+11b215b765,20.0.0-1-g253301a+36ef800059,20.0.0-1-g2b7511a+bebc1f60e8,20.0.0-1-g4d801e7+aeeb640673,20.0.0-1-g5b95a8c+f111d5f02f,20.0.0-1-g660595b+f45b7d88f4,20.0.0-1-gc96f8cb+e3b38461e6,20.0.0-1-gd1c87d7+85c46248f3,20.0.0-1-gedffbd8+d0b27f8bcb,20.0.0-16-g111fe95+e3b38461e6,20.0.0-16-g18096c8+d1a4df0137,20.0.0-16-g233ea98+a4df35922d,20.0.0-17-ga9337b4+41f27cfd54,20.0.0-2-g4dae9ad+e3b38461e6,20.0.0-2-g7818986+85c46248f3,20.0.0-2-gec03fae+ff10c6d78d,20.0.0-28-g282f9e7e+feda6aebd8,20.0.0-3-g4cc78c6+63636aeed8,20.0.0-3-g6a8623c+d1a4df0137,20.0.0-3-g750bffe+f5427621ce,20.0.0-4-gfea843c+f45b7d88f4,20.0.0-5-g357b56b+f45b7d88f4,20.0.0-5-gfcebe35+e2b15ed341,20.0.0-52-g73d9071+9bf1eb8e0a,20.0.0-7-gcda7bf1+773ba852cb,20.0.0-8-g4540fe2a+952f6d3c43,20.0.0-9-g61a2a9a3d+14f89e4eca,w.2020.40
LSSTDataManagementBasePackage
positionStarFakes.py
Go to the documentation of this file.
1 import numpy as np
2 from astropy.io import fits
3 
4 import lsst.afw.geom as afwGeom
5 import lsst.afw.math as afwMath
6 import lsst.afw.image as afwImage
7 import lsst.pex.config as afwConfig
8 
9 from lsst.pipe.tasks.fakes import BaseFakeSourcesConfig, BaseFakeSourcesTask
10 from lsst.pex.exceptions import InvalidParameterError
11 
12 
14  starList = afwConfig.Field(dtype=str,
15  doc="Catalog of stars with mags ra/dec")
16  seed = afwConfig.Field(dtype=int, default=1,
17  doc="Seed for random number generator")
18 
19 
21  ConfigClass = PositionStarFakesConfig
22 
23  def __init__(self, **kwargs):
24  BaseFakeSourcesTask.__init__(self, **kwargs)
25  print("RNG seed:", self.config.seed)
26  self.rng = afwMath.Random(seed=self.config.seed)
27  self.npRand = np.random.RandomState(self.config.seed)
28  try:
29  self.starData = fits.open(self.config.starList)[1].data
30  except Exception:
31  raise
32 
33  def run(self, exposure, background):
34 
35  self.log.info("Adding fake stars at real positions")
36  psf = exposure.getPsf()
37  psfBBox = psf.computeImage().getBBox()
38  margin = max(psfBBox.getWidth(), psfBBox.getHeight())/2 + 1
39 
40  PARENT = afwImage.PARENT
41  md = exposure.getMetadata()
42  expBBox = exposure.getBBox(PARENT)
43  wcs = exposure.getWcs()
44 
45  for istar, star in enumerate(self.starData):
46  try:
47  starident = star["ID"]
48  except KeyError:
49  starident = istar + 1
50 
51  try:
52  flux = exposure.getCalib().getFlux(float(star['mag']))
53  except KeyError:
54  raise KeyError("No mag column in %s" % self.config.starList)
55 
56  try:
57  starCoord = afwGeom.SpherePoint(star['RA'], star['DEC'], afwGeom.degrees)
58  except KeyError:
59  raise KeyError("No RA/DEC column in table".format(self.config.starList))
60 
61  starXY = wcs.skyToPixel(starCoord)
62  bboxI = exposure.getBBox(PARENT)
63  bboxI.grow(int(margin))
64  if not bboxI.contains(afwGeom.Point2I(starXY)):
65  continue
66 
67  try:
68  starImage = psf.computeImage(starXY)
69  except InvalidParameterError:
70  # This means an image was computed in an area where there was no data
71  # continue on to the next star. This is most likely to occur when inserting
72  # into coadds
73  logmsg = "Skipping fake {} because no input images present at point {}"
74  self.log.info(logmsg.format(starident, starXY))
75  continue
76 
77  starImage *= flux
78  starBBox = starImage.getBBox(PARENT)
79 
80  # Check that we're within the larger exposure, otherwise crop
81  if expBBox.contains(starBBox) is False:
82  newBBox = starImage.getBBox(PARENT)
83  newBBox.clip(expBBox)
84  if newBBox.getArea() <= 0:
85  self.log.info("Skipping fake %d" % starident)
86  continue
87  self.log.info("Cropping FAKE%d from %s to %s" % (starident,
88  str(starBBox), str(newBBox)))
89  starImage = starImage.Factory(starImage, newBBox, PARENT)
90  starBBox = newBBox
91 
92  starMaskedImage = afwImage.MaskedImageF(starImage.convertF())
93 
94  starMaskedImage.getMask().set(self.bitmask)
95 
96  md.set("FAKE%s" % str(starident), "%.3f, %.3f" % (starXY.getX(),
97  starXY.getY()))
98  self.log.info("Adding fake %s at: %.1f,%.1f" % (str(starident),
99  starXY.getX(),
100  starXY.getY()))
101 
102  maskedImage = exposure.getMaskedImage()
103  BBox = starMaskedImage.getBBox(PARENT)
104  subMaskedImage = maskedImage.Factory(exposure.getMaskedImage(),
105  BBox,
106  PARENT)
107  subMaskedImage += starMaskedImage
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::log.log.logContinued.info
def info(fmt, *args)
Definition: logContinued.py:201
lsst.synpipe.positionStarFakes.PositionStarFakesTask.__init__
def __init__(self, **kwargs)
Definition: positionStarFakes.py:23
lsst.synpipe.positionStarFakes.PositionStarFakesTask.rng
rng
Definition: positionStarFakes.py:26
lsst.synpipe.positionStarFakes.PositionStarFakesTask.npRand
npRand
Definition: positionStarFakes.py:27
lsst.pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst.pipe.tasks.fakes.BaseFakeSourcesTask.bitmask
bitmask
Definition: fakes.py:61
lsst.pipe.tasks.fakes.BaseFakeSourcesConfig
Definition: fakes.py:29
lsst.synpipe.positionStarFakes.PositionStarFakesTask.run
def run(self, exposure, background)
Definition: positionStarFakes.py:33
lsst.pipe.tasks.fakes.BaseFakeSourcesTask
Definition: fakes.py:36
lsst.synpipe.positionStarFakes.PositionStarFakesTask.starData
starData
Definition: positionStarFakes.py:29
lsst.pex.config
Definition: __init__.py:1
lsst.pipe.base.task.Task.config
config
Definition: task.py:149
lsst.pipe.base.task.Task.log
log
Definition: task.py:148
lsst.synpipe.positionStarFakes.PositionStarFakesTask
Definition: positionStarFakes.py:20
max
int max
Definition: BoundedField.cc:104
lsst.pipe.tasks.fakes
Definition: fakes.py:1
lsst.synpipe.positionStarFakes.PositionStarFakesConfig
Definition: positionStarFakes.py:13
lsst::afw::math::Random
A class that can be used to generate sequences of random numbers according to a number of different a...
Definition: Random.h:57
lsst::afw::math
Definition: statistics.dox:6
lsst.pex::exceptions
Definition: Exception.h:37
set
daf::base::PropertySet * set
Definition: fits.cc:912
lsst::afw::geom
Definition: frameSetUtils.h:40