3

I'm trying to write a very simple programm, that prints the current distance to any planet in kilometers. I'm using Skyfield. Here's my code for Mars:

from skyfield.api import earth, mars, now
ra, dec, distance = earth(now()).observe(mars).radec()


print(distance)

This will print out the distance in astronomical units. In order to convert to kilometers, i tried to multiply by 149597871:

from skyfield.api import earth, mars, now
ra, dec, distance = earth(now()).observe(mars).radec()


print(distance*149597871)

But this returns an Error:

TypeError: unsupported operand type(s) for *: 'Distance' and 'int'

What can I do?

Monotom
  • 33
  • 4

1 Answers1

5

distance is a Distance object, so you can simply use distance.km if you want the distance in km. If you want to do the conversion yourself, you can use distance.AU * 149597871. (If it doesn't match exactly, it's because Skyfield uses 149597870.700 km/au for conversion).

You can see the implementation of this class in the source here.

aganders3
  • 5,838
  • 26
  • 30