Skip to content

Commit c334465

Browse files
committed
to_ephem was in the wrong spot
1 parent 7cc3bfd commit c334465

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

sbpy/dynamics/state.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,21 @@ def to_skycoord(self) -> SkyCoord:
294294
else:
295295
return SkyCoord(self._data, obstime=self.t, representation_type="cartesian")
296296

297-
def to_ephem(self, observer: StateType | None = None) -> Ephem:
297+
def to_ephem(
298+
self, observer: StateType | None = None, coords: SkyCoord | None = None
299+
) -> Ephem:
298300
r"""Convert to an sbpy ephemeris object.
299301
300302
301303
Parameters
302304
----------
303305
observer : `State`, optional
304-
Calculate RA/longitude, Dec/latitude, distance, etc for this
305-
observer.
306+
Include this observer in the metadata. If ``coords`` is ``None``,
307+
then also calculate RA/longitude, Dec/latitude, distance, etc for
308+
this observer.
309+
310+
coords : `~astropy.coordinates.SkyCoord`, optional
311+
Include these observer-based coordinates in the result.
306312
307313
308314
Returns
@@ -364,20 +370,23 @@ def to_ephem(self, observer: StateType | None = None) -> Ephem:
364370

365371
if observer is not None:
366372
meta["observer"] = {
367-
"r": self.observer.r,
368-
"v": self.observer.v,
369-
"t": self.observer.t,
370-
"frame": self.observer.frame,
373+
"r": observer.r,
374+
"v": observer.v,
375+
"t": observer.t,
376+
"frame": observer.frame,
371377
}
372378

373-
coords = observer.observe(self)
379+
if coords is None:
380+
coords = observer.observe(self)
374381

375-
# use SkyCoords's to_table() method, which will account for when
382+
# use SkyCoord's to_table() method, which will account for when
376383
# RA/Dec vs lon/lat are used.
377384
tab = coords.to_table()
378385

379-
# convert SkyCoords's column names to Ephem's field names
380-
skycoords_to_ephem = {
386+
# convert SkyCoord's column names to Ephem's field names
387+
skycoord_to_ephem = {
388+
"pm_ra": "ra_rate",
389+
"pm_lon": "lon_rate",
381390
"pm_ra_cosdec": "ra*cos(dec)_rate",
382391
"pm_lon_coslat": "lon*cos(lat)_rate",
383392
"pm_dec": "dec_rate",
@@ -386,7 +395,7 @@ def to_ephem(self, observer: StateType | None = None) -> Ephem:
386395
"radial_velocity": "deltadot",
387396
}
388397
for col in tab.colnames:
389-
field = skycoords_to_ephem.get(col, col)
398+
field = skycoord_to_ephem.get(col, col)
390399
data[field] = tab[col]
391400

392401
return Ephem.from_dict(data, meta=meta)

sbpy/dynamics/syndynes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def to_ephem(self) -> Ephem:
133133
134134
"""
135135

136-
eph = super().to_ephem(observer=self.observer)
136+
eph = super().to_ephem(observer=self.observer, coords=self.coords)
137137

138138
eph["beta_rad"] = self.betas
139139
eph["age"] = self.ages

sbpy/dynamics/tests/test_state.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ def test_from_ephem(self):
562562
with pytest.raises(ValueError):
563563
State.from_ephem(incomplete)
564564

565-
def test_to_ephm(self):
565+
def test_to_ephem(self):
566566
r = np.arange(1, 7).reshape([2, 3]) * u.au
567567
v = r.value / 10 * u.km / u.s
568568
t = [0, 1] * u.day
@@ -582,3 +582,16 @@ def test_to_ephm(self):
582582
eph = state.to_ephem()
583583
assert np.allclose(eph["date"].mjd, [51544.0, 51545.0])
584584
assert isinstance(eph.meta["frame"], ICRS)
585+
586+
# observer with an observer
587+
observer = State(
588+
[0, 0, 1] * u.au, [0, 0, 0] * u.km / u.s, state.t[0], frame="icrs"
589+
)
590+
eph = state.to_ephem(observer=observer)
591+
coords = observer.observe(state)
592+
assert all(eph["ra"] == coords.ra)
593+
assert all(eph["dec"] == coords.dec)
594+
assert all(eph["ra_rate"] == coords.pm_ra)
595+
assert all(eph["dec_rate"] == coords.pm_dec)
596+
assert all(eph["delta"] == coords.distance)
597+
assert all(eph["deltadot"] == coords.radial_velocity)

0 commit comments

Comments
 (0)