Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def contains(self, skycoord, wcs):
"""
Check whether a sky coordinate falls inside the region
Parameters
----------
skycoord : `~astropy.coordinates.SkyCoord`
The position or positions to check
wcs : `~astropy.wcs.WCS` instance
The world coordinate system transformation to assume
"""
from .pixcoord import PixCoord
pixel_region = self.to_pixel(wcs)
pixcoord = PixCoord.from_sky(skycoord, wcs)
return pixel_region.contains(pixcoord)
def to_region(self):
"""
Return a `~regions.RectanglePixelRegion` that
represents the bounding box.
"""
from ..shapes import RectanglePixelRegion
from .pixcoord import PixCoord
xpos = (self.extent[1] + self.extent[0]) / 2.
ypos = (self.extent[3] + self.extent[2]) / 2.
xypos = PixCoord(xpos, ypos)
h, w = self.shape
return RectanglePixelRegion(center=xypos, width=w, height=h)
def image(self):
"""Counts image (`~astropy.io.fits.ImageHDU`)."""
events = self.event_table
skycoord = SkyCoord(events['GLON'], events['GLAT'], unit='deg', frame='galactic')
pixcoord = PixCoord.from_sky(skycoord=skycoord, wcs=self.wcs)
shape = self.config['shape']
bins = [np.arange(shape[0] + 1), np.arange(shape[1] + 1)]
sample = np.vstack((pixcoord.y, pixcoord.x)).T
data, _ = np.histogramdd(sample=sample, bins=bins)
data = data.astype('float32')
header = self.wcs.to_header()
return fits.ImageHDU(data=data, header=header, name='image')
def __iter__(self):
"""Allows iteration for array-valued pixcoord
Yields scalar `PixCoord` objects.
"""
for (x, y) in zip(self.x, self.y):
yield PixCoord(x=x, y=y)
def __getitem__(self, key):
"""Define indexing and slicing."""
if self.isscalar:
raise IndexError('Scalar PixCoord cannot be indexed or sliced.')
# Let Numpy do the slicing
x = self.x[key]
y = self.y[key]
return PixCoord(x=x, y=y)
Parameters
----------
val : `PixCoord`
The object to check
name : str
Parameter name (used for error messages)
expected : {'any', 'scalar', 'not scalar'}
What kind of PixCoord to check for
Returns
-------
val : `PixCoord`
The input object (at the moment unmodified, might do fix-ups here later)
"""
if not isinstance(val, PixCoord):
raise TypeError(f'{name} must be a PixCoord')
if expected == 'any':
pass
elif expected == 'scalar':
if not val.isscalar:
raise ValueError(f'{name} must be a scalar PixCoord')
elif expected == 'not scalar':
if val.isscalar:
raise ValueError(f'{name} must be a non-scalar PixCoord')
else:
raise ValueError(f'Invalid argument for `expected`: {expected}')
return val
Parameters
----------
val : `PixCoord`
The object to check
name : str
Parameter name (used for error messages)
expected : {'any', 'scalar', 'not scalar'}
What kind of PixCoord to check for
Returns
-------
val : `PixCoord`
The input object (at the moment unmodified, might do fix-ups here later)
"""
if not isinstance(val, PixCoord):
raise TypeError('{} must be a PixCoord'.format(name))
if expected == 'any':
pass
elif expected == 'scalar':
if not val.isscalar:
raise ValueError('{} must be a scalar PixCoord'.format(name))
elif expected == 'not scalar':
if val.isscalar:
raise ValueError('{} must be a non-scalar PixCoord'.format(name))
else:
raise ValueError('Invalid argument for `expected`: {}'.format(expected))
return val
small_offset : `~astropy.units.Quantity`
A small offset to use to compute the angle
Returns
-------
pixcoord : `~regions.PixCoord`
Pixel coordinates
scale : float
The pixel scale at each location, in degrees/pixel
angle : `~astropy.units.Quantity`
The position angle of the celestial coordinate system in pixel space.
"""
# Convert to pixel coordinates
x, y = skycoord_to_pixel(skycoord, wcs, mode=skycoord_to_pixel_mode)
pixcoord = PixCoord(x=x, y=y)
# We take a point directly 'above' (in latitude) the position requested
# and convert it to pixel coordinates, then we use that to figure out the
# scale and position angle of the coordinate system at the location of
# the points.
# Find the coordinates as a representation object
r_old = skycoord.represent_as('unitspherical')
# Add a a small perturbation in the latitude direction (since longitude
# is more difficult because it is not directly an angle).
dlat = small_offset
r_new = UnitSphericalRepresentation(r_old.lon, r_old.lat + dlat)
coords_offset = skycoord.realize_frame(r_new)
# Find pixel coordinates of offset coordinates
def _validate(self, value):
if not (isinstance(value, PixCoord) and not value.isscalar
and value.x.ndim == 1):
raise ValueError('The {} must be a 1D PixCoord object'
.format(self.name))