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
randomGalSimFakes.py
Go to the documentation of this file.
1 import numpy as np
2 from astropy.io import fits
3 
4 import lsst.afw.image
5 import lsst.afw.geom
6 import lsst.afw.math
8 import lsst.pex.config
9 from lsst.pipe.tasks.fakes import BaseFakeSourcesConfig, BaseFakeSourcesTask
10 import lsst.synpipe.makeFakeGalaxy as makeFake
11 
12 
14  galList = lsst.pex.config.Field(dtype=str, doc="catalog of galaxies to add")
15  margin = lsst.pex.config.Field(dtype=int, default=None, optional=True,
16  doc="Size of margin at edge that should not be added")
17  seed = lsst.pex.config.Field(dtype=int, default=1,
18  doc="Seed for random number generator")
19  galType = lsst.pex.config.ChoiceField(dtype=str, default='sersic',
20  allowed={'dsersic': 'double sersic galaxies added',
21  'sersic': 'single sersic galaxies added',
22  'real': 'real HST galaxy images added'},
23  doc='type of GalSim galaxies to add')
24  nGal = lsst.pex.config.Field(dtype=int, doc="""number of galaxies to add, if 0, then everything in catalog,
25  otherwise a random subset of nGal from the catalog""", default=0)
26 
27 
29  ConfigClass = RandomGalSimFakesConfig
30 
31  def __init__(self, **kwargs):
32  BaseFakeSourcesTask.__init__(self, **kwargs)
33  print("RNG seed:", self.config.seed)
34  self.rng = lsst.afw.math.Random(seed=self.config.seed)
35  self.npRand = np.random.RandomState(self.config.seed)
36  self.galData = fits.open(self.config.galList)[1].data
37 
38  def run(self, exposure, background):
39 
40  self.log.info("Adding fake random galaxies")
41  psf = exposure.getPsf()
42  psfBBox = psf.computeImage().getBBox()
43  minMargin = int(np.floor(max(psfBBox.getWidth(), psfBBox.getHeight())/2)) + 1
44  md = exposure.getMetadata()
45  expBBox = exposure.getBBox()
46  scalingMatrix = np.array([[0.0, 1.0], [1.0, 0.0]]) / exposure.getWcs().pixelScale().asArcseconds()
47 
48  if self.config.nGal == 0:
49  doGal = enumerate(self.galData)
50  else:
51  inds = self.npRand.choice(list(range(len(self.galData))), size=self.config.nGal, replace=False)
52  doGal = list(zip(inds, self.galData[inds]))
53 
54  for igal, gal in doGal:
55  try:
56  galident = gal["ID"]
57  except KeyError:
58  galident = igal + 1
59 
60  try:
61  flux = exposure.getCalib().getFlux(float(gal['mag']))
62  except KeyError:
63  raise KeyError("No mag column in %s table"%self.config.galList)
64 
65  # don't put the galaxy within one PSF box of the edge
66  # or within the given pixel margin
67  if self.config.margin is not None:
68  margin = self.config.margin
69  else:
70  margin = minMargin
71  bboxI = (exposure.getBBox(lsst.afw.image.PARENT))
72  bboxI.grow(-margin)
73  bboxD = lsst.afw.geom.BoxD(bboxI)
74  x = self.rng.flat(bboxD.getMinX(), bboxD.getMaxX())
75  y = self.rng.flat(bboxD.getMinY(), bboxD.getMaxY())
76  # TODO: check for overlaps here and regenerate x,y if necessary
77 
78  psfImage = psf.computeKernelImage(lsst.afw.geom.Point2D(x, y))
79  galArray = makeFake.makeGalaxy(flux, gal, psfImage.getArray(), self.config.galType,
80  transform=scalingMatrix)
81  galImage = lsst.afw.image.ImageF(galArray.astype(np.float32))
82  galBBox = galImage.getBBox(lsst.afw.image.PARENT)
83  galImage = lsst.afw.math.offsetImage(galImage,
84  x - galBBox.getWidth()/2.0 + 0.5,
85  y - galBBox.getHeight()/2.0 + 0.5,
86  'lanczos3')
87  galBBox = galImage.getBBox(lsst.afw.image.PARENT)
88 
89  # check that we're within the larger exposure, otherwise crop
90  if expBBox.contains(galImage.getBBox(lsst.afw.image.PARENT)) is False:
91  newBBox = galImage.getBBox(lsst.afw.image.PARENT)
92  newBBox.clip(expBBox)
93  self.log.info("Cropping FAKE%d from %s to %s"%(galident, str(galBBox), str(newBBox)))
94  galImage = galImage.Factory(galImage, newBBox, lsst.afw.image.PARENT)
95  galBBox = newBBox
96 
97  galMaskedImage = lsst.afw.image.MaskedImageF(galImage)
98 
99  mask = galMaskedImage.getMask()
100  mask.set(self.bitmask)
101 
102  md.set("FAKE%d" % gal['ID'], "%.3f, %.3f" % (x, y))
103  self.log.info("Adding fake at: %.1f,%.1f" % (x, y))
104 
105  # TODO: set the mask
106  galMaskedImage.getMask().set(self.bitmask)
107  subMaskedImage = exposure.getMaskedImage().Factory(exposure.getMaskedImage(),
108  galMaskedImage.getBBox(lsst.afw.image.PARENT),
109  lsst.afw.image.PARENT)
110  subMaskedImage += galMaskedImage
A floating-point coordinate rectangle geometry.
Definition: Box.h:294
daf::base::PropertySet * set
Definition: fits.cc:832
int max
std::shared_ptr< ImageT > offsetImage(ImageT const &image, float dx, float dy, std::string const &algorithmName="lanczos5", unsigned int buffer=0)
Return an image offset by (dx, dy) using the specified algorithm.
Definition: offsetImage.cc:41
daf::base::PropertyList * list
Definition: fits.cc:833
A class that can be used to generate sequences of random numbers according to a number of different a...
Definition: Random.h:57