LSST Applications g180d380827+78227d2bc4,g2079a07aa2+86d27d4dc4,g2305ad1205+bdd7851fe3,g2bbee38e9b+c6a8a0fb72,g337abbeb29+c6a8a0fb72,g33d1c0ed96+c6a8a0fb72,g3a166c0a6a+c6a8a0fb72,g3d1719c13e+260d7c3927,g3ddfee87b4+723a6db5f3,g487adcacf7+29e55ea757,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+9443c4b912,g62aa8f1a4b+7e2ea9cd42,g858d7b2824+260d7c3927,g864b0138d7+8498d97249,g95921f966b+dffe86973d,g991b906543+260d7c3927,g99cad8db69+4809d78dd9,g9c22b2923f+e2510deafe,g9ddcbc5298+9a081db1e4,ga1e77700b3+03d07e1c1f,gb0e22166c9+60f28cb32d,gb23b769143+260d7c3927,gba4ed39666+c2a2e4ac27,gbb8dafda3b+e22341fd87,gbd998247f1+585e252eca,gc120e1dc64+713f94b854,gc28159a63d+c6a8a0fb72,gc3e9b769f7+385ea95214,gcf0d15dbbd+723a6db5f3,gdaeeff99f8+f9a426f77a,ge6526c86ff+fde82a80b9,ge79ae78c31+c6a8a0fb72,gee10cc3b42+585e252eca,w.2024.18
LSST Data Management Base Package
Loading...
Searching...
No Matches
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 contained = np.zeros_like(radec_contained, dtype=bool)
98
99 x_in, y_in = wcs.skyToPixelArray(_ra[radec_contained], _dec[radec_contained], degrees=False)
100 xy_contained = bbox.contains(x_in, y_in)
101
102 # contained[not radec_contained] is already false
103 contained[radec_contained] = xy_contained
104 return contained
A floating-point coordinate rectangle geometry.
Definition Box.h:413
ConvexPolygon is a closed convex polygon on the unit sphere.
bbox_contains_sky_coords(bbox, wcs, ra, dec, padding=10)
bbox_to_convex_polygon(bbox, wcs, padding=10)