/usr/lib/python3/dist-packages/photutils/utils/wcs_helpers.py is in python3-photutils 0.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import numpy as np
from astropy import units as u
from astropy.coordinates import UnitSphericalRepresentation
from astropy.wcs.utils import skycoord_to_pixel, pixel_to_skycoord
skycoord_to_pixel_mode = 'all'
def skycoord_to_pixel_scale_angle(coords, wcs):
"""
Convert a set of SkyCoord coordinates into pixel coordinates, pixel
scales, and position angles.
Parameters
----------
coords : `~astropy.coordinates.SkyCoord`
The coordinates to convert
wcs : `~astropy.wcs.WCS`
The WCS transformation to use
Returns
-------
x, y : `~numpy.ndarray`
The x and y pixel coordinates corresponding to the input coordinates
scale : `~astropy.units.Quantity`
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(coords, wcs, mode=skycoord_to_pixel_mode)
# 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 = coords.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 = 1 * u.arcsec
r_new = UnitSphericalRepresentation(r_old.lon, r_old.lat + dlat)
coords_offset = coords.realize_frame(r_new)
# Find pixel coordinates of offset coordinates
x_offset, y_offset = skycoord_to_pixel(coords_offset, wcs,
mode=skycoord_to_pixel_mode)
# Find vector
dx = x_offset - x
dy = y_offset - y
# Find the length of the vector
scale = np.hypot(dx, dy) * u.pixel / dlat
# Find the position angle
angle = np.arctan2(dy, dx) * u.radian
return x, y, scale, angle
def assert_angle_or_pixel(name, q):
"""
Check that ``q`` is either an angular or a pixel
:class:`~astropy.units.Quantity`.
"""
if isinstance(q, u.Quantity):
if q.unit.physical_type == 'angle' or q.unit is u.pixel:
pass
else:
raise ValueError("{0} should have angular or pixel "
"units".format(name))
else:
raise TypeError("{0} should be a Quantity instance".format(name))
def assert_angle(name, q):
"""
Check that ``q`` is an angular :class:`~astropy.units.Quantity`.
"""
if isinstance(q, u.Quantity):
if q.unit.physical_type == 'angle':
pass
else:
raise ValueError("{0} should have angular units".format(name))
else:
raise TypeError("{0} should be a Quantity instance".format(name))
def pixel_to_icrs_coords(x, y, wcs):
"""
Convert pixel coordinates to ICRS Right Ascension and Declination.
This is merely a convenience function to extract RA and Dec. from a
`~astropy.coordinates.SkyCoord` instance so they can be put in
separate columns in a `~astropy.table.Table`.
Parameters
----------
x : float or array-like
The x pixel coordinate.
y : float or array-like
The y pixel coordinate.
wcs : `~astropy.wcs.WCS`
The WCS transformation to use to convert from pixel coordinates
to ICRS world coordinates.
`~astropy.table.Table`.
Returns
-------
ra : `~astropy.units.Quantity`
The ICRS Right Ascension in degrees.
dec : `~astropy.units.Quantity`
The ICRS Declination in degrees.
"""
icrs_coords = pixel_to_skycoord(x, y, wcs).icrs
icrs_ra = icrs_coords.ra.degree * u.deg
icrs_dec = icrs_coords.dec.degree * u.deg
return icrs_ra, icrs_dec
|