Repository navigation
Description
What is the problem this feature will solve?
I found that offsetting a set of coordinates by the same value, conveniently stored in a single SkyCoord, requires the longitude and latitude values to spherical_offsets_by to be each of the same length. I had hoped that I could give a single offset, and that that would be broadcasted to the entire coordinate array, i.e.
coord = SkyCoord(ra=np.array([123, 134, 145]), dec=np.array([45, 56, 67]), unit=units.degree)
coord.spherical_offsets_by(2*units.degree, 2*units.degree)
would offset each of (123, 45), (134, 56), (145, 67) by 2 degrees east(?) and north.
Instead, this results in a lenghty traceback, somewhat summarised below:
...
ValueError: cannot broadcast a non-scalar to a scalar array
During handling of the above exception, another exception occurred:
...
ValueError: attribute origin should be scalar or have shape (), but it has shape (3,) and could not be broadcast.
I ended up doing
offsets = 2 * np.ones(3) * units.degree
coord.spherical_offsets_by(offsets, offsets)
but I wondere if it's worth implementing some broadcasting mechanism.
Describe the desired outcome
The results would be that
coord1 = SkyCoord(ra=np.array([123, 134, 145]), dec=np.array([45, 56, 67]), unit=units.degree)
coord.spherical_offsets_by(2*units.degree, 2*units.degree)
yields
<SkyCoord (ICRS): (ra, dec) in deg
[(125.92947215, 46.96384624), (137.76807718, 57.94547037),
(150.56248577, 68.91058429)]>
Of course, it may be possible I've overlooked something, and doing this wrong.
Alternatively, this may be too niche of a case to add this. Although to me, this fits with standard NumPy broadcasting behaviour for cases like np.arange(5) + 20.
There may be other similar functions / methods that have this non-broadcasting behaviour where it could be relevant. Interestingly, SkyCoord.spherical_offsets_to does appear to show the (expected) broadcasting behaviour.
If deemed useful, I'd be happy to give this a go, although it may be quicker for someone else to implement it.
Additional context
No response
Activity
mhvk commentedon Feb 14, 2023
I agree this should work but it is not trivial, as internally what is done is:
astropy/astropy/coordinates/sky_coordinate.py
Lines 1334 to 1336 in 8b7e479
So, a
SkyOffsetFrameis created, with the single coordinate at the origin. That class is not designed to take array-valued origins. That could (and probably should) be changed, but will need some real thought.Broadcast frames over both data and attributes
Add regression test for astropy#14383
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes
Broadcast frames over both data and attributes (#15121)
tjgalvin commentedon Aug 23, 2024
Hi all -- sorry to be a pain, but is this pulled into a stable main? I see that the current set of docs for
spherical_offsets_bysuggest it is not. I'm currently using the approach listed in the original post, so not the biggest deal.pllim commentedon Aug 23, 2024
Hello, @tjgalvin . This issue was closed by #15121 that was milestoned to v6.0 so it would have been in the stable docs. Can you please clarify what you mean by "current set of docs"? What are the exact URL(s) you are referring to and what do you expect to see? Thanks.
tjgalvin commentedon Aug 27, 2024
This was a user error by yours truly. Sorry for the confusion :S