Skip to content

Commit d27e8af

Browse files
committed
Simplify workaround for astropy bug
I was working around astropy/astropy#11277 by implementing the matrix rotations myself, but a comment in the bug suggested a simpler workaround.
1 parent 1fe7834 commit d27e8af

1 file changed

Lines changed: 8 additions & 32 deletions

File tree

‎test/test_primary_beam.py‎

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@
2222

2323
import astropy.units as u
2424
from astropy import constants
25-
from astropy.coordinates import (
26-
EarthLocation, AltAz, SkyCoord,
27-
CartesianRepresentation, UnitSphericalRepresentation)
25+
from astropy.coordinates import EarthLocation, AltAz, SkyCoord, CartesianRepresentation
2826
from astropy.time import Time
29-
from astropy.coordinates.matrix_utilities import rotation_matrix
3027
import numpy as np
3128
try:
3229
from numpy.typing import ArrayLike
@@ -450,47 +447,26 @@ def test_sample_out(
450447
np.testing.assert_array_equal(out, expected)
451448

452449

453-
def _skyoffset_matrix(origin: SkyCoord):
454-
"""Reproduce the matrix used by :class:`astropy.coordinates.SkyOffsetFrame`.
455-
456-
This is the matrix for transforming from a reference frame to an offset
457-
frame. It does not support the rotation attribute of
458-
:class:`astropy.coordinates.SkyOffsetFrame`.
459-
"""
460-
# Based on reference_to_skyoffset in the astropy code.
461-
origin_sph = origin.spherical
462-
maty = rotation_matrix(-origin_sph.lat, 'y')
463-
matz = rotation_matrix(origin_sph.lon, 'z')
464-
return maty @ matz
465-
466-
467450
def _coords_to_lm(coords: SkyCoord, origin: SkyCoord) -> np.ndarray:
468451
"""Convert sky coordinates to l/m direction cosines.
469452
470453
The `coords` and `origin` must be in the same frame.
471-
472-
This would be simpler with SkyOffsetFrame, but unfortunately
473-
https://github.com/astropy/astropy/issues/11277 makes it break randomly.
474454
"""
455+
# Go via .frame as workaround for https://github.com/astropy/astropy/issues/11277
475456
assert coords.frame.is_equivalent_frame(origin.frame)
476-
mat = _skyoffset_matrix(origin)
477-
offsets = coords.represent_as(CartesianRepresentation).transform(mat)
478-
return offsets.xyz[1:] # astropy spherical (0, 0) at +x, so y, z are l, m
457+
skyoffset = coords.frame.transform_to(origin.skyoffset_frame())
458+
offsets = skyoffset.represent_as(CartesianRepresentation)
459+
return offsets.xyz[1:] # astropy puts spherical (0, 0) at +x, so y, z are l, m
479460

480461

481462
def _lm_to_coords(l: ArrayLike, m: ArrayLike, origin: SkyCoord) -> SkyCoord:
482-
"""Convert l/m cooordinates relative to an origin into coordinates.
483-
484-
This would be simpler with SkyOffsetFrame, but unfortunately
485-
https://github.com/astropy/astropy/issues/11277 makes it break randomly.
486-
"""
463+
"""Convert l/m cooordinates relative to an origin into coordinates."""
487464
l = np.asarray(l)
488465
m = np.asarray(m)
489466
n = np.sqrt(1 - (l * l + m * m))
490467
nlm = CartesianRepresentation(np.stack([n, l, m]))
491-
mat = _skyoffset_matrix(origin).T # Transposing a rotation matrix inverts it
492-
xyz = nlm.transform(mat)
493-
return SkyCoord(xyz.represent_as(UnitSphericalRepresentation), frame=origin)
468+
skyoffset = SkyCoord(nlm, frame=origin.skyoffset_frame())
469+
return skyoffset.transform_to(origin)
494470

495471

496472
@pytest.mark.parametrize('lat', [-30 * u.deg, 35 * u.deg])

0 commit comments

Comments
 (0)