LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.45
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