Skip to content

Commit abe6f04

Browse files
committed
Print warning when initializing an Orbit with a SkyCoord that does not have the Sun's positional and velocity parameters set; fixes #709
1 parent fc9500e commit abe6f04

File tree

2 files changed

+221
-0
lines changed

2 files changed

+221
-0
lines changed

galpy/orbit/Orbits.py

+40
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,46 @@ def _setup_parse_coordtransform(self, vxvv, ro, vo, zo, solarmotion, radec, lb):
478478
raise ImportError(
479479
"Orbit initialization using an astropy SkyCoord requires astropy >3.0"
480480
)
481+
# Print warning when SkyCoord doesn't come with values for galcen_distance, z_sun, and galcen_v_sun (issue #709)
482+
if (
483+
vxvv.z_sun is None
484+
or vxvv.galcen_distance is None
485+
or vxvv.galcen_v_sun is None
486+
):
487+
apy_coordframe_warning = "Supplied SkyCoord does not contain ("
488+
apy_coordframe_warning_keywords = ""
489+
print_apy_coordframe_warning = 0
490+
print_apy_coordframe_warning_dict = {
491+
"are": "is",
492+
"these": "this",
493+
"were": "was",
494+
"values": "value",
495+
}
496+
if vxvv.galcen_distance is None and ro is None:
497+
apy_coordframe_warning += "galcen_distance, "
498+
apy_coordframe_warning_keywords += "ro, "
499+
print_apy_coordframe_warning += 1
500+
if vxvv.z_sun is None and zo is None:
501+
apy_coordframe_warning += "z_sun, "
502+
apy_coordframe_warning_keywords += "zo, "
503+
print_apy_coordframe_warning += 1
504+
if vxvv.galcen_v_sun is None and solarmotion is None:
505+
apy_coordframe_warning += "galcen_v_sun, "
506+
apy_coordframe_warning_keywords += "vo, solarmotion, "
507+
print_apy_coordframe_warning += 1
508+
if print_apy_coordframe_warning:
509+
if print_apy_coordframe_warning > 1:
510+
print_apy_coordframe_warning_dict = {
511+
"are": "are",
512+
"these": "these",
513+
"were": "were",
514+
"values": "values",
515+
}
516+
warnings.warn(
517+
apy_coordframe_warning[:-2]
518+
+ f") and {print_apy_coordframe_warning_dict['these']} {print_apy_coordframe_warning_dict['were']} not explicitly set in the Orbit initialization using the keywords ({apy_coordframe_warning_keywords[:-2]}); {print_apy_coordframe_warning_dict['these']} {print_apy_coordframe_warning_dict['are']} required for Orbit initialization; proceeding with default {print_apy_coordframe_warning_dict['values']}",
519+
galpyWarning,
520+
)
481521
if zo is None and not vxvv.z_sun is None:
482522
zo = vxvv.z_sun.to(units.kpc).value
483523
elif not vxvv.z_sun is None:

tests/test_orbit.py

+181
Original file line numberDiff line numberDiff line change
@@ -9722,3 +9722,184 @@ def test_call_internal_kwargs():
97229722
"Orbit._call_internal(t0) and Orbit._call_internal(t=t0) return different results"
97239723
)
97249724
return None
9725+
9726+
9727+
def test_apy_sunkeywords_not_supplied():
9728+
# Test for issues #709: print warning when a SkyCoord is used to initialize an
9729+
# Orbit object, but the Sun's position and velocity are not specified through
9730+
# galcen_distance, galcen_v_sun, and z_sun
9731+
from astropy import units as u
9732+
from astropy.coordinates import SkyCoord
9733+
9734+
from galpy.orbit import Orbit
9735+
9736+
# Just missing galcen_distance
9737+
vxvv = SkyCoord(
9738+
ra=1 * u.deg,
9739+
dec=1 * u.deg,
9740+
distance=20.8 * u.pc,
9741+
pm_ra_cosdec=0.0 * u.mas / u.yr,
9742+
pm_dec=0.0 * u.mas / u.yr,
9743+
radial_velocity=0.0 * u.km / u.s,
9744+
# galcen_distance=8.3 * u.kpc,
9745+
z_sun=0.025 * u.kpc,
9746+
galcen_v_sun=[-11.1, 220.0, 7.25] * u.km / u.s,
9747+
)
9748+
with pytest.warns(galpyWarning) as record:
9749+
o = Orbit(vxvv)
9750+
raisedWarning = False
9751+
for rec in record:
9752+
# check that the message matches
9753+
raisedWarning += (
9754+
str(rec.message.args[0])
9755+
== "Supplied SkyCoord does not contain (galcen_distance) and this was not explicitly set in the Orbit initialization using the keywords (ro); this is required for Orbit initialization; proceeding with default value"
9756+
)
9757+
assert raisedWarning, (
9758+
"Orbit initialization without galcen_distance should have thrown a warning, but didn't"
9759+
)
9760+
# Just missing z_sun
9761+
vxvv = SkyCoord(
9762+
ra=1 * u.deg,
9763+
dec=1 * u.deg,
9764+
distance=20.8 * u.pc,
9765+
pm_ra_cosdec=0.0 * u.mas / u.yr,
9766+
pm_dec=0.0 * u.mas / u.yr,
9767+
radial_velocity=0.0 * u.km / u.s,
9768+
galcen_distance=8.3 * u.kpc,
9769+
# z_sun=0.025 * u.kpc,
9770+
galcen_v_sun=[-11.1, 220.0, 7.25] * u.km / u.s,
9771+
)
9772+
with pytest.warns(galpyWarning) as record:
9773+
o = Orbit(vxvv)
9774+
raisedWarning = False
9775+
for rec in record:
9776+
# check that the message matches
9777+
raisedWarning += (
9778+
str(rec.message.args[0])
9779+
== "Supplied SkyCoord does not contain (z_sun) and this was not explicitly set in the Orbit initialization using the keywords (zo); this is required for Orbit initialization; proceeding with default value"
9780+
)
9781+
assert raisedWarning, (
9782+
"Orbit initialization without z_sun should have thrown a warning, but didn't"
9783+
)
9784+
# Just missing galcen_v_sun
9785+
vxvv = SkyCoord(
9786+
ra=1 * u.deg,
9787+
dec=1 * u.deg,
9788+
distance=20.8 * u.pc,
9789+
pm_ra_cosdec=0.0 * u.mas / u.yr,
9790+
pm_dec=0.0 * u.mas / u.yr,
9791+
radial_velocity=0.0 * u.km / u.s,
9792+
galcen_distance=8.3 * u.kpc,
9793+
z_sun=0.025 * u.kpc,
9794+
# galcen_v_sun=[-11.1, 220.0, 7.25] * u.km / u.s,
9795+
)
9796+
with pytest.warns(galpyWarning) as record:
9797+
o = Orbit(vxvv)
9798+
raisedWarning = False
9799+
for rec in record:
9800+
# check that the message matches
9801+
raisedWarning += (
9802+
str(rec.message.args[0])
9803+
== "Supplied SkyCoord does not contain (galcen_v_sun) and this was not explicitly set in the Orbit initialization using the keywords (vo, solarmotion); this is required for Orbit initialization; proceeding with default value"
9804+
)
9805+
assert raisedWarning, (
9806+
"Orbit initialization without galcen_v_sun should have thrown a warning, but didn't"
9807+
)
9808+
# Missing galcen_distance and z_sun
9809+
vxvv = SkyCoord(
9810+
ra=1 * u.deg,
9811+
dec=1 * u.deg,
9812+
distance=20.8 * u.pc,
9813+
pm_ra_cosdec=0.0 * u.mas / u.yr,
9814+
pm_dec=0.0 * u.mas / u.yr,
9815+
radial_velocity=0.0 * u.km / u.s,
9816+
# galcen_distance=8.3 * u.kpc,
9817+
# z_sun=0.025 * u.kpc,
9818+
galcen_v_sun=[-11.1, 220.0, 7.25] * u.km / u.s,
9819+
)
9820+
with pytest.warns(galpyWarning) as record:
9821+
o = Orbit(vxvv)
9822+
raisedWarning = False
9823+
for rec in record:
9824+
# check that the message matches
9825+
raisedWarning += (
9826+
str(rec.message.args[0])
9827+
== "Supplied SkyCoord does not contain (galcen_distance, z_sun) and these were not explicitly set in the Orbit initialization using the keywords (ro, zo); these are required for Orbit initialization; proceeding with default values"
9828+
)
9829+
assert raisedWarning, (
9830+
"Orbit initialization without galcen_distance and z_sun should have thrown a warning, but didn't"
9831+
)
9832+
# Missing galcen_distance and galcen_v_sun
9833+
vxvv = SkyCoord(
9834+
ra=1 * u.deg,
9835+
dec=1 * u.deg,
9836+
distance=20.8 * u.pc,
9837+
pm_ra_cosdec=0.0 * u.mas / u.yr,
9838+
pm_dec=0.0 * u.mas / u.yr,
9839+
radial_velocity=0.0 * u.km / u.s,
9840+
# galcen_distance=8.3 * u.kpc,
9841+
z_sun=0.025 * u.kpc,
9842+
# galcen_v_sun=[-11.1, 220.0, 7.25] * u.km / u.s,
9843+
)
9844+
with pytest.warns(galpyWarning) as record:
9845+
o = Orbit(vxvv)
9846+
raisedWarning = False
9847+
for rec in record:
9848+
# check that the message matches
9849+
raisedWarning += (
9850+
str(rec.message.args[0])
9851+
== "Supplied SkyCoord does not contain (galcen_distance, galcen_v_sun) and these were not explicitly set in the Orbit initialization using the keywords (ro, vo, solarmotion); these are required for Orbit initialization; proceeding with default values"
9852+
)
9853+
assert raisedWarning, (
9854+
"Orbit initialization without galcen_distance and galcen_v_sun should have thrown a warning, but didn't"
9855+
)
9856+
# Missing z_sun and galcen_v_sun
9857+
vxvv = SkyCoord(
9858+
ra=1 * u.deg,
9859+
dec=1 * u.deg,
9860+
distance=20.8 * u.pc,
9861+
pm_ra_cosdec=0.0 * u.mas / u.yr,
9862+
pm_dec=0.0 * u.mas / u.yr,
9863+
radial_velocity=0.0 * u.km / u.s,
9864+
galcen_distance=8.3 * u.kpc,
9865+
# z_sun=0.025 * u.kpc,
9866+
# galcen_v_sun=[-11.1, 220.0, 7.25] * u.km / u.s,
9867+
)
9868+
with pytest.warns(galpyWarning) as record:
9869+
o = Orbit(vxvv)
9870+
raisedWarning = False
9871+
for rec in record:
9872+
# check that the message matches
9873+
raisedWarning += (
9874+
str(rec.message.args[0])
9875+
== "Supplied SkyCoord does not contain (z_sun, galcen_v_sun) and these were not explicitly set in the Orbit initialization using the keywords (zo, vo, solarmotion); these are required for Orbit initialization; proceeding with default values"
9876+
)
9877+
assert raisedWarning, (
9878+
"Orbit initialization without z_sun and galcen_v_sun should have thrown a warning, but didn't"
9879+
)
9880+
# Missing all: galcen_distance, z_sun, galcen_v_sun
9881+
vxvv = SkyCoord(
9882+
ra=1 * u.deg,
9883+
dec=1 * u.deg,
9884+
distance=20.8 * u.pc,
9885+
pm_ra_cosdec=0.0 * u.mas / u.yr,
9886+
pm_dec=0.0 * u.mas / u.yr,
9887+
radial_velocity=0.0 * u.km / u.s,
9888+
# galcen_distance=8.3 * u.kpc,
9889+
# z_sun=0.025 * u.kpc,
9890+
# galcen_v_sun=[-11.1, 220.0, 7.25] * u.km / u.s,
9891+
)
9892+
with pytest.warns(galpyWarning) as record:
9893+
o = Orbit(vxvv)
9894+
raisedWarning = False
9895+
for rec in record:
9896+
# check that the message matches
9897+
raisedWarning += (
9898+
str(rec.message.args[0])
9899+
== "Supplied SkyCoord does not contain (galcen_distance, z_sun, galcen_v_sun) and these were not explicitly set in the Orbit initialization using the keywords (ro, zo, vo, solarmotion); these are required for Orbit initialization; proceeding with default values"
9900+
)
9901+
assert raisedWarning, (
9902+
"Orbit initialization without galcen_distance, z_sun, and galcen_v_sun should have thrown a warning, but didn't"
9903+
)
9904+
9905+
return None

0 commit comments

Comments
 (0)