Skip to content

Transforming an AltAz coordinate generated from values of another AltAz frame does not work. #12965

Closed

Description

@maxnoe

Description

Assume I have the pointing position of my telescope in AltAz frame and I know that it did not track (i.e. the coordinates stay constant in AltAz) and I want to know the ICRS coordinates of my pointing position as time evolves:

import numpy as np
from astropy.coordinates import SkyCoord, AltAz, EarthLocation
from astropy.time import Time
import astropy.units as u

obstime = Time.now()
location = EarthLocation.of_site('Roque de Los Muchachos')

frame1 = AltAz(location=location, obstime=obstime)
coord1 = SkyCoord(alt=80 * u.deg, az=0 * u.deg, frame=frame1)

print(coord1.icrs)

frame2 = AltAz(location=location, obstime=obstime + np.linspace(0, 15, 50) * u.min)
coord2 = SkyCoord(alt=coord1.alt, az=coord1.az, frame=frame2)

print(coord2.icrs)

This raises the following error:

<SkyCoord (ICRS): (ra, dec) in deg
    (68.09412325, 38.71031085)>
Traceback (most recent call last):
  File "/home/maxnoe/Projects/astropy/astropy/coordinates/attributes.py", line 112, in __get__
    out = out._apply(np.broadcast_to, shape=instance_shape,
  File "/home/maxnoe/Projects/astropy/astropy/time/core.py", line 1070, in _apply
    jd1 = apply_method(jd1)
  File "/home/maxnoe/Projects/astropy/astropy/time/core.py", line 1060, in <lambda>
    apply_method = lambda array: method(array, *args, **kwargs)
  File "<__array_function__ internals>", line 180, in broadcast_to
  File "/home/maxnoe/.local/anaconda/envs/astropy-dev/lib/python3.9/site-packages/numpy/lib/stride_tricks.py", line 412, in broadcast_to
    return _broadcast_to(array, shape, subok=subok, readonly=True)
  File "/home/maxnoe/.local/anaconda/envs/astropy-dev/lib/python3.9/site-packages/numpy/lib/stride_tricks.py", line 343, in _broadcast_to
    raise ValueError('cannot broadcast a non-scalar to a scalar array')
ValueError: cannot broadcast a non-scalar to a scalar array

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/maxnoe/Projects/gammapy/gammapy/test_altaz.py", line 15, in <module>
    coord2 = SkyCoord(alt=coord1.alt, az=coord1.az, frame=frame2)
  File "/home/maxnoe/Projects/astropy/astropy/coordinates/sky_coordinate.py", line 347, in __init__
    self._sky_coord_frame = frame_cls(copy=copy, **frame_kwargs)
  File "/home/maxnoe/Projects/astropy/astropy/coordinates/builtin_frames/altaz.py", line 109, in __init__
    super().__init__(*args, **kwargs)
  File "/home/maxnoe/Projects/astropy/astropy/coordinates/baseframe.py", line 314, in __init__
    values[fnm] = getattr(self, fnm)
  File "/home/maxnoe/Projects/astropy/astropy/coordinates/attributes.py", line 118, in __get__
    raise ValueError(
ValueError: attribute obstime should be scalar or have shape (), but is has shape (50,) and could not be broadcast.

Expected behavior

Getting the ICRS coordinates at the specified timestamps

Actual behavior

The Error above

Steps to Reproduce

Run the above snippet.

System Details

Tested using astropy 4.3 and a recent development version

Activity

  1. maxnoe commented on Mar 18, 2022

    @maxnoe
    MemberAuthor

    Replacing the creation of the second coordinate with the somewhat cumbersome:

    coord2 = SkyCoord(
        alt=np.full(obstime2.shape, coord1.alt.deg) * u.deg,
        az=np.full(obstime2.shape, coord1.az.deg) * u.deg,
        frame=frame2,
    )

    works

  2. ayshih commented on Mar 19, 2022

    @ayshih
    Contributor

    I'll note that this issue is essentially a duplicate of #11184 and strongly related to #5982, and I suspect that #12968 is likely too brute force of a solution.

  3. mhvk commented on Mar 20, 2022

    @mhvk
    Contributor

    Hmm, yes, similar to #11184, though I like the explicitness of this example. I think we've been moving to the idea that it should be possible for the attributes to have more dimensions than the actual data, but the question remains how best to deal with it. The real question is whether the shape of the frame should always equal the shape of the underlying data. If so, then the data should be broadcast as needed in BaseFrame.__init__. But perhaps one can get away with the frame's shape being the broadcast shape of the data and all the attributes (as the PR does)? Overall, I think that will add too much complication, e.g., in dealing with slices, etc. (where currently data is always just slices, while attributes are either passed on if they are scalar, or sliced as well; the attribute shapes are also either the frame shape or scalar).

    So, bottom line is that I think one should internally do broadcasting of the data if needed.

  4. added 5 commits that reference this issue on Aug 3, 2023
    3797b5b
    ba38c2e
    aa1989c
    1d45f88
    37cfed6
  5. added 5 commits that reference this issue on Aug 18, 2023
    99035b2
    9081d6a
    2f04b20
    7b82928
    5d84c35
  6. added 3 commits that reference this issue on Sep 30, 2023
    b19fb2a
    1f3a2b6
    33cca6f
  7. added a commit that references this issue on Oct 3, 2023
    00e64a9
  8. maxnoe commented on Oct 3, 2023

    @maxnoe
    MemberAuthor

    Oh wow, thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions