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
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:305
daf::base::PropertySet * set
Definition: fits.cc:884
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:885
A class that can be used to generate sequences of random numbers according to a number of different a...
Definition: Random.h:57