Skip to content

Fix conversion of cartesian to unit-spherical coordinates with velocities - #11469

Merged
mhvk merged 3 commits into
astropy:masterfrom
mhvk:representation-unit-differential-scale
Apr 3, 2021
Merged

mhvk merged 3 commits into
astropy:masterfrom
mhvk:representation-unit-differential-scale

Conversation

@mhvk

@mhvk mhvk commented Apr 2, 2021

Copy link
Copy Markdown
Contributor

Previously, the following gave the wrong unit for the proper motion:

from astropy.coordinates import SkyCoord
from astropy import units as u

c = SkyCoord(x=1*u.kpc, y=0*u.kpc, z=0*u.kpc,
             v_x=10*u.km/u.s, v_y=0*u.km/u.s, v_z=4.74*u.km/u.s,
             representation_type='cartesian')
c.representation_type = 'unitspherical'
c
#  <SkyCoord (ICRS): (ra, dec) in deg
#      (0., 0.)
#   (pm_ra, pm_dec) in km rad / s
#      (0., 4.74)>

This was because CartesianDifferential was incorrectly transformed to UnitSphericalDifferential.

p.s. Not quite sure about the milestone.

cc @nstarman - this may be relevant for getting #11467 to work as well...

@mhvk mhvk added this to the v4.0.6 milestone Apr 2, 2021
@mhvk
mhvk requested a review from adrn April 2, 2021 23:45
raise u.UnitsError('d_lon and d_lat should have equivalent units.')

@classmethod
def from_cartesian(cls, other, base):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is new API, probably wanna re-milestone to 4.3.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is actually a bug fix... I had not quite realized that the base class method would not work correctly for the UnitSphericalDifferental. Though if difficult to backport, it can be for 4.3.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if this bug is in LTS, I think the current milestone is fine and we'll leave it up to the release manager(s) to decide later. Thanks!

@adrn adrn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, thanks for this fix! Looks good to me, except for an ignorable nitpick!

Comment thread astropy/coordinates/representation.py Outdated
base : `BaseRepresentation`
The points for which the differentials are to be converted: each of
the components is multiplied by its unit vectors and scale factors.
Will be converted to ``self.base_representation`` if needed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but should this be

Suggested change
Will be converted to ``self.base_representation`` if needed.
Will be converted to ``cls.base_representation`` if needed.

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks! I made the change as part of rebasing - might as well check that more tests pass now...

mhvk added 3 commits April 3, 2021 12:22
Previously, the distance was ignored, which meant the units of the
proper motions would be wrong:

c = CartesianRepresentation([1, 0, 0]*u.kpc)
cd = CartesianDifferential([-10, 0, 4.74]*u.km/u.s)
cd.represent_as(UnitSphericalDifferential, base=c)
<UnitSphericalDifferential (d_lon, d_lat) in km rad / s
    (0., 4.74)>
Previously, the following gave the wrong unit for the proper motion:
c = SkyCoord(x=1*u.kpc, y=0*u.kpc, z=0*u.kpc,
             v_x=10*u.km/u.s, v_y=0*u.km/u.s, v_z=4.74*u.km/u.s,
             representation_type='cartesian')
c.representation_type = 'unitspherical'
repr(c)
<SkyCoord (ICRS): (ra, dec) in deg
    (0., 0.)
 (pm_ra, pm_dec) in km rad / s
    (0., 4.74)>
@mhvk
mhvk force-pushed the representation-unit-differential-scale branch from f9d7501 to ce45028 Compare April 3, 2021 16:22
@mhvk

mhvk commented Apr 3, 2021

Copy link
Copy Markdown
Contributor Author

CI failure is unrelated, so merging. Thanks for the quick review, @adrn!

@mhvk
mhvk merged commit 02f0503 into astropy:master Apr 3, 2021
@mhvk
mhvk deleted the representation-unit-differential-scale branch April 3, 2021 17:00
astrofrog pushed a commit that referenced this pull request Aug 16, 2021
…scale

Fix conversion of cartesian to unit-spherical coordinates with velocities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants