LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
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
daf::base::PropertySet * set
Definition: fits.cc:832
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
int max
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:57
A class that can be used to generate sequences of random numbers according to a number of different a...
Definition: Random.h:57