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
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
daf::base::PropertySet * set
Definition: fits.cc:902
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
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
daf::base::PropertyList * list
Definition: fits.cc:903
A class that can be used to generate sequences of random numbers according to a number of different a...
Definition: Random.h:57