LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
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
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
daf::base::PropertySet * set
Definition: fits.cc:902
int max
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:57
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
A class that can be used to generate sequences of random numbers according to a number of different a...
Definition: Random.h:57