LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
installGaussianPsf.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-2015 AURA/LSST.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <https://www.lsstcorp.org/LegalNotices/>.
21 #
22 import math
23 
24 import lsst.pex.config as pexConfig
25 import lsst.pipe.base as pipeBase
26 from lsst.meas.algorithms import SingleGaussianPsf
27 
28 __all__ = ["InstallGaussianPsfConfig", "InstallGaussianPsfTask"]
29 
30 FwhmPerSigma = 2.0*math.sqrt(2.0*math.log(2.0))
31 
32 
33 class InstallGaussianPsfConfig(pexConfig.Config):
34  """!Config for InstallGaussianPsfTask"""
35  fwhm = pexConfig.Field(
36  dtype=float,
37  default=1.5 * FwhmPerSigma,
38  doc="Estimated FWHM of simple Gaussian PSF model, in pixels. "
39  "Ignored if input exposure has a PSF model."
40  )
41  width = pexConfig.RangeField(
42  dtype=int,
43  doc="Width and height of PSF model, in pixels. Must be odd.",
44  default=11,
45  min=1,
46  )
47 
48  def validate(self):
49  if self.width % 2 == 0:
50  raise RuntimeError("width=%s must be odd" % (self.width,))
51 
52 
53 ## \addtogroup LSST_task_documentation
54 ## \{
55 ## \page InstallGaussianPsfTask
56 ## \ref InstallGaussianPsfTask_ "InstallGaussianPsfTask"
57 ## \copybrief InstallGaussianPsfTask
58 ## \}
59 
60 class InstallGaussianPsfTask(pipeBase.Task):
61  """!Install a Gaussian PSF model in an exposure
62 
63  @anchor InstallGaussianPsfTask_
64 
65  @section pipe_tasks_installGaussianPsf_Contents Contents
66 
67  - @ref pipe_tasks_installGaussianPsf_Purpose
68  - @ref pipe_tasks_installGaussianPsf_Initialize
69  - @ref pipe_tasks_installGaussianPsf_IO
70  - @ref pipe_tasks_installGaussianPsf_Config
71  - @ref pipe_tasks_installGaussianPsf_Metadata
72  - @ref pipe_tasks_installGaussianPsf_Debug
73  - @ref pipe_tasks_installGaussianPsf_Example
74 
75  @section pipe_tasks_installGaussianPsf_Purpose Description
76 
77  Install a Gaussian PSF model in an exposure.
78  If the exposure already has a PSF model then the new model
79  has the same sigma and size (width and height in pixels) of the existing model.
80  If the exposure does not have a PSF model then the PSF sigma and size
81  are taken from the config.
82 
83  At present the produced model is always circularly symmetric, but it is planned
84  to change this to an elliptical PSF model (only for the case that the exposure
85  already has a PSF model), once the necessary PSF object is available.
86 
87  A variant of this task may someday exist to estimate the PSF
88  from the pixel data if no PSF model is present.
89 
90  @section pipe_tasks_installGaussianPsf_Initialize Task initialisation
91 
92  @copydoc \_\_init\_\_
93 
94  @section pipe_tasks_installGaussianPsf_IO Invoking the Task
95 
96  The main method is `run`.
97 
98  @section pipe_tasks_installGaussianPsf_Config Configuration parameters
99 
100  See @ref InstallGaussianPsfConfig
101 
102  @section pipe_tasks_installGaussianPsf_Debug Debug variables
103 
104  This task has no debug display
105 
106  @section pipe_tasks_installGaussianPsf_Example A complete example of using InstallGaussianPsfTask
107 
108  from lsst.afw.image import ExposureF
109  from lsst.meas.algorithms.installGaussianPsf import InstallGaussianPsfTask, FwhmPerSigma
110 
111  exposure = ExposureF(100, 100)
112  task = InstallGaussianPsfTask()
113  task.run(exposure=exposure)
114 
115  # This particular exposure had no PSF model to begin with, so the new PSF model
116  # uses the config's FWHM. However, measured FWHM is based on the truncated
117  # PSF image, so it does not exactly match the input
118  measFwhm = exposure.getPsf().computeShape().getDeterminantRadius() * FwhmPerSigma
119  assert abs(measFwhm - task.config.fwhm) < 1e-3
120  """
121  ConfigClass = InstallGaussianPsfConfig
122  _DefaultName = "installSimplePsfModel"
123 
124  def run(self, exposure):
125  """!Set exposure's PSF to a simple PSF model
126 
127  The sigma and width of the new simple PSF model matches the sigma and width of the current model,
128  if any, else the config parameters are used.
129 
130  @param[in,out] exposure exposure to which to replace or add the PSF model
131  """
132  if exposure.hasPsf():
133  psfModel = exposure.getPsf()
134  psfSigma = psfModel.computeShape().getDeterminantRadius()
135  width, height = psfModel.computeImage().getDimensions()
136  else:
137  psfSigma = self.config.fwhm / FwhmPerSigma
138  width = height = self.config.width
139 
140  if psfSigma <= 0:
141  raise RuntimeError("psfSigma = %s <= 0" % (psfSigma,))
142 
143  self.log.debug("installing a simple Gaussian PSF model with width=%s, height=%s, FWHM=%0.3f",
144  width, height, psfSigma*FwhmPerSigma)
145  psfModel = SingleGaussianPsf(width, height, psfSigma)
146  exposure.setPsf(psfModel)
Represent a PSF as a circularly symmetrical Gaussian.
def run
Set exposure&#39;s PSF to a simple PSF model.