-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
from astropy.coordinates import Angle, SkyCoord | ||
from regions.core.attributes import QuantityLength | ||
from ..core import SkyRegion, PixelRegion, PixCoord | ||
|
||
__all__ = ["RangePixelRegion", "RangeSphericalRegion"] | ||
|
||
inf = Angle(float("inf"), "deg") | ||
|
||
|
||
# TODO: needed? | ||
class RangePixelRegion(PixelRegion): | ||
""" | ||
A range in pixel coordinates. | ||
See `RangeSphericalRegion`. | ||
""" | ||
|
||
|
||
class RangeSphericalRegion(SkyRegion): | ||
""" | ||
A range in longitude and latitude. | ||
Use case: http://www.ivoa.net/documents/SIA/20151223/REC-SIA-2.0-20151223.html#toc12 | ||
Parameters | ||
---------- | ||
lon_min, lon_max : `~astropy.coordinates.Angle` | ||
Range in longitude | ||
lat_min, lat_max : `~astropy.coordinates.Angle` | ||
Range in latitude | ||
frame : `~astropy.coordinates.BaseCoordinateFrame` | ||
Sky frame | ||
meta : `~regions.RegionMeta` object, optional | ||
A dictionary which stores the meta attributes of this region. | ||
visual : `~regions.RegionVisual` object, optional | ||
A dictionary which stores the visual meta attributes of this region. | ||
""" | ||
# TODO: should we use these properties, or SkyCoord for min / max, | ||
# or a center-based representation? | ||
_params = ('lon_min', 'lon_max', 'lat_min', 'lat_max') | ||
lon_min = QuantityLength('lon_min') | ||
lon_max = QuantityLength('lon_max') | ||
lat_min = QuantityLength('lat_min') | ||
lat_max = QuantityLength('lat_max') | ||
|
||
def __init__(self, lon_min=-inf, lon_max=inf, lat_min=-inf, lat_max=inf, frame="icrs", meta=None, visual=None): | ||
self.lon_min = lon_min | ||
self.lon_max = lon_max | ||
self.lat_min = lat_min | ||
self.lat_max = lat_max | ||
# TODO: Is there a helper function to make a frame without a | ||
self.frame = SkyCoord(0, 0, unit="deg", frame=frame).frame | ||
self.meta = meta or {} | ||
self.visual = visual or {} | ||
|
||
def to_pixel(self, wcs): | ||
sky_min = SkyCoord(self.lon_min, self.lat_min, frame=self.frame) | ||
sky_max = SkyCoord(self.lon_max, self.lat_max, frame=self.frame) | ||
|
||
pix_min = PixCoord.from_sky(sky_min, self.wcs) | ||
pix_max = PixCoord.from_sky(sky_max, self.wcs) | ||
return RangePixelRegion(pix_min.x, pix_max.x, pix_min.y, pix_max.y, self.meta, self.visual) | ||
|
||
def contains(self, skycoord, wcs=None): | ||
skycoord = skycoord.transform_to(self.frame) | ||
return ( | ||
(self.lon_min <= skycoord.data.lon) & | ||
(self.lon_max >= skycoord.data.lon) & | ||
(self.lat_min <= skycoord.data.lat) & | ||
(self.lat_max >= skycoord.data.lat) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
from numpy.testing import assert_equal | ||
import astropy.units as u | ||
from astropy.coordinates import SkyCoord | ||
from ..range import RangeSphericalRegion | ||
|
||
# TODO: use test cases with finite & infinite values from here: | ||
# http://www.ivoa.net/documents/SIA/20151223/REC-SIA-2.0-20151223.html#toc12 | ||
|
||
def test_range_default(): | ||
region = RangeSphericalRegion() | ||
assert region.contains(SkyCoord("0d 0d")) | ||
|
||
def test_range_finite(): | ||
region = RangeSphericalRegion(12 * u.deg, 12.5 * u.deg, 34 * u.deg, 36.0 * u.deg) | ||
assert region.contains(SkyCoord("12d 34d")) | ||
assert not region.contains(SkyCoord("11d 34d")) | ||
assert not region.contains(SkyCoord("13d 34d")) | ||
assert not region.contains(SkyCoord("12d 33d")) | ||
assert not region.contains(SkyCoord("12d 37d")) | ||
|
||
mask = region.contains(SkyCoord([12, 13] * u.deg, [34, 34] * u.deg)) | ||
assert_equal(mask, [True, False]) |