LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
LSST Data Management Base Package
Loading...
Searching...
No Matches
vignette.py
Go to the documentation of this file.
1# This file is part of ip_isr.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22__all__ = ('VignetteConfig', 'VignetteTask', 'setValidPolygonCcdIntersect',
23 'maskVignettedRegion')
24
25import logging
26import numpy as np
27
28import lsst.geom as geom
29import lsst.afw.cameraGeom as cameraGeom
30import lsst.afw.geom as afwGeom
31
32from lsst.pex.config import Config, Field
33from lsst.pipe.base import Task
34
35
37 """Settings to define vignetting pattern.
38 """
39 xCenter = Field(
40 dtype=float,
41 doc="Center of vignetting pattern, in focal plane x coordinates.",
42 default=0.0,
43 )
44 yCenter = Field(
45 dtype=float,
46 doc="Center of vignetting pattern, in focal plane y coordinates.",
47 default=0.0,
48 )
49 radius = Field(
50 dtype=float,
51 doc="Radius of vignetting pattern, in focal plane coordinates.",
52 default=100.0,
53 check=lambda x: x >= 0
54 )
55 numPolygonPoints = Field(
56 dtype=int,
57 doc="Number of points used to define the vignette polygon.",
58 default=100,
59 )
60
61
62class VignetteTask(Task):
63 """Define a simple circular vignette pattern and optionally update mask
64 plane.
65 """
66 ConfigClass = VignetteConfig
67 _DefaultName = "isrVignette"
68
69 def run(self, exposure=None, doUpdateMask=True, maskPlane="NO_DATA", vignetteValue=None, log=None):
70 """Generate circular vignette pattern.
71
72 Parameters
73 ----------
74 exposure : `lsst.afw.image.Exposure`, optional
75 Exposure to construct, apply, and optionally mask vignette for.
76 doUpdateMask : `bool`, optional
77 If true, the mask will be updated to mask the vignetted region.
78 maskPlane : `str`, optional
79 Mask plane to assign vignetted pixels to.
80 vignetteValue : `float` or `None`, optional
81 Value to assign to the image array pixels within the ``polygon``
82 region. If `None`, image pixel values are not replaced.
83 log : `logging.Logger`, optional
84 Log object to write to.
85
86 Returns
87 -------
88 polygon : `lsst.afw.geom.Polygon`
89 Polygon defining the boundary of the vignetted region.
90 """
91 theta = np.linspace(0, 2*np.pi, num=self.config.numPolygonPoints, endpoint=False)
92 x = self.config.radius*np.cos(theta) + self.config.xCenter
93 y = self.config.radius*np.sin(theta) + self.config.yCenter
94 points = np.array([x, y]).transpose()
95 fpPolygon = afwGeom.Polygon([geom.Point2D(x1, y1) for x1, y1 in reversed(points)])
96 if exposure is None:
97 return fpPolygon
98
99 # Exposure was provided, so attach the validPolygon associated with the
100 # vignetted region.
101 setValidPolygonCcdIntersect(exposure, fpPolygon, log=log)
102
103 if doUpdateMask:
104 polygon = exposure.getInfo().getValidPolygon()
105 maskVignettedRegion(exposure, polygon, maskPlane="NO_DATA", vignetteValue=vignetteValue, log=log)
106 return fpPolygon
107
108
109def setValidPolygonCcdIntersect(ccdExposure, fpPolygon, log=None):
110 """Set valid polygon on ccdExposure associated with focal plane polygon.
111
112 The ccd exposure's valid polygon is the intersection of fpPolygon,
113 a valid polygon in focal plane coordinates, and the ccd corners,
114 in ccd pixel coordinates.
115
116 Parameters
117 ----------
118 ccdExposure : `lsst.afw.image.Exposure`
119 Exposure to process.
120 fpPolygon : `lsst.afw.geom.Polygon`
121 Polygon in focal plane coordinates.
122 log : `logging.Logger`, optional
123 Log object to write to.
124 """
125 # Get ccd corners in focal plane coordinates
126 ccd = ccdExposure.getDetector()
127 fpCorners = ccd.getCorners(cameraGeom.FOCAL_PLANE)
128 ccdPolygon = afwGeom.Polygon(fpCorners)
129 # Get intersection of ccd corners with fpPolygon
130 try:
131 intersect = ccdPolygon.intersectionSingle(fpPolygon)
133 intersect = None
134 if intersect is not None:
135 # Transform back to pixel positions and build new polygon
136 ccdPoints = ccd.transform(intersect, cameraGeom.FOCAL_PLANE, cameraGeom.PIXELS)
137 validPolygon = afwGeom.Polygon(ccdPoints)
138 ccdExposure.getInfo().setValidPolygon(validPolygon)
139 else:
140 if log is not None:
141 log.info("Ccd exposure does not overlap with focal plane polygon. Not setting validPolygon.")
142
143
144def maskVignettedRegion(exposure, polygon, maskPlane="NO_DATA", vignetteValue=None, log=None):
145 """Add mask bit to image pixels according to vignetted polygon region.
146
147 NOTE: this function could be used to mask and replace pixels associated
148 with any polygon in the exposure pixel coordinates.
149
150 Parameters
151 ----------
152 exposure : `lsst.afw.image.Exposure`
153 Image whose mask plane is to be updated.
154 polygon : `lsst.afw.geom.Polygon`
155 Polygon region defining the vignetted region in the pixel coordinates
156 of ``exposure``.
157 maskPlane : `str`, optional
158 Mask plane to assign vignetted pixels to.
159 vignetteValue : `float` or `None`, optional
160 Value to assign to the image array pixels within the ``polygon``
161 region. If `None`, image pixel values are not replaced.
162 log : `logging.Logger`, optional
163 Log object to write to.
164
165 Raises
166 ------
167 RuntimeError
168 Raised if no valid polygon exists.
169 """
170 log = log if log else logging.getLogger(__name__)
171 if not polygon:
172 log.info("No polygon provided. Masking nothing.")
173 return
174
175 fullyIlluminated = True
176 if not all(polygon.contains(exposure.getBBox().getCorners())):
177 fullyIlluminated = False
178 log.info("Exposure is fully illuminated? %s", fullyIlluminated)
179
180 if not fullyIlluminated:
181 # Scan pixels.
182 mask = exposure.getMask()
183 xx, yy = np.meshgrid(np.arange(0, mask.getWidth(), dtype=float),
184 np.arange(0, mask.getHeight(), dtype=float))
185
186 vignMask = ~(polygon.contains(xx, yy))
187
188 bitMask = mask.getPlaneBitMask(maskPlane)
189 maskArray = mask.getArray()
190 maskArray[vignMask] |= bitMask
191 log.info("Exposure contains {} vignetted pixels which are now masked with mask plane {}.".
192 format(np.count_nonzero(vignMask), maskPlane))
193
194 if vignetteValue is not None:
195 imageArray = exposure.getImage().getArray()
196 imageArray[vignMask] = vignetteValue
197 log.info("Vignetted pixels in image array have been replaced with {}.".format(vignetteValue))
Cartesian polygons.
Definition Polygon.h:59
An exception that indicates the single-polygon assumption has been violated.
Definition Polygon.h:53
run(self, exposure=None, doUpdateMask=True, maskPlane="NO_DATA", vignetteValue=None, log=None)
Definition vignette.py:69
maskVignettedRegion(exposure, polygon, maskPlane="NO_DATA", vignetteValue=None, log=None)
Definition vignette.py:144
setValidPolygonCcdIntersect(ccdExposure, fpPolygon, log=None)
Definition vignette.py:109