LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
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:884
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
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