LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
exposureUtils.py
Go to the documentation of this file.
1# This file is part of afw.
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__ = ['bbox_to_convex_polygon', 'bbox_contains_sky_coords']
23
24import numpy as np
25import astropy.units as units
26import lsst.geom
27
28
29def bbox_to_convex_polygon(bbox, wcs, padding=10):
30 """Convert a bounding box and wcs to a convex polygon on the sky, with paddding.
31
32 The returned polygon has additional padding to ensure that the
33 bounding box is entirely contained within it. The default padding
34 size was chosen to be sufficient for the most warped detectors at
35 the edges of the HyperSuprimeCam focal plane.
36
37 Parameters
38 ----------
39 bbox : `lsst.geom.Box2I`
40 Bounding box to convert.
41 wcs : `lsst.afw.image.SkyWcs`
42 WCS associated with the bounding box.
43 padding : `int`
44 Pixel padding to ensure that bounding box is entirely contained
45 within the resulting polygon.
46
47 Returns
48 -------
49 convex_polygon : `lsst.sphgeom.ConvexPolygon`
50 Will be None if wcs is not valid.
51 """
52 # Convert Box2I to Box2D, without modifying original.
53 _bbox = lsst.geom.Box2D(bbox)
54 _bbox.grow(padding)
55 corners = [wcs.pixelToSky(corner).getVector()
56 for corner in _bbox.getCorners()]
57 return lsst.sphgeom.ConvexPolygon(corners)
58
59
60def bbox_contains_sky_coords(bbox, wcs, ra, dec, padding=10):
61 """Check if a set of sky positions are in the bounding box.
62
63 This uses a two-step process: first check that the coordinates are
64 inside a padded version of the bbox projected on the sky, and then
65 project the remaining points onto the bbox, to avoid inverting
66 the WCS outside of the valid region. The default padding
67 size was chosen to be sufficient for the most warped detectors at
68 the edges of the HyperSuprimeCam focal plane.
69
70 Parameters
71 ----------
72 bbox : `lsst.geom.Box2I`
73 Pixel bounding box to check sky positions in.
74 wcs : `lsst.afw.image.SkyWcs`
75 WCS associated with the bounding box.
76 ra : `astropy.Quantity`, (N,)
77 Array of Right Ascension, angular units.
78 dec : `astropy.Quantity`, (N,)
79 Array of Declination, angular units.
80 padding : `int`
81 Pixel padding to ensure that bounding box is entirely contained
82 within the resulting polygon.
83
84 Returns
85 -------
86 contained : `np.ndarray`, (N,)
87 Boolean array indicating which points are contained in the
88 bounding box.
89 """
90 poly = bbox_to_convex_polygon(bbox, wcs, padding=padding)
91
92 _ra = np.atleast_1d(ra.to(units.radian).value).astype(np.float64)
93 _dec = np.atleast_1d(dec.to(units.radian).value).astype(np.float64)
94
95 radec_contained = poly.contains(_ra, _dec)
96
97 x_in, y_in = wcs.skyToPixelArray(_ra, _dec, degrees=False)
98
99 xy_contained = bbox.contains(x_in, y_in)
100
101 return radec_contained & xy_contained
A floating-point coordinate rectangle geometry.
Definition: Box.h:413
An integer coordinate rectangle.
Definition: Box.h:55
ConvexPolygon is a closed convex polygon on the unit sphere.
Definition: ConvexPolygon.h:57
def bbox_contains_sky_coords(bbox, wcs, ra, dec, padding=10)
def bbox_to_convex_polygon(bbox, wcs, padding=10)