knam
knam

Reputation: 17

how to convert degrees minutes seconds to decimal degrees (python/skyfield)

I woud like to extract coordinate of a satellite (ISS) in decimal by using this code:

from skyfield.api import EarthSatellite, Topos, load
import time

line1 = '1 25544U 98067A   14020.93268519  .00009878  00000-0  18200-3 0  5082'
line2 = '2 25544  51.6498 109.4756 0003572  55.9686 274.8005 15.49815350868473'

satellite = EarthSatellite(line1, line2, name='ISS (ZARYA)')

while True:
    ts = load.timescale()
    t = ts.now()
    geometry = satellite.at(t)

    subpoint = geometry.subpoint()
    print(subpoint.latitude)
    print('\n')
    print(subpoint.longitude)
    time.sleep(1)

The output is a string: -45deg 44' 13.5".

What is the easiest way to convert it to something like: -77.0089°?

Upvotes: 0

Views: 2830

Answers (2)

Brandon Rhodes
Brandon Rhodes

Reputation: 89415

Happily, the objects latitude and longitude are not simple strings, but are fancy angle objects that merely print themselves as 3-part strings to make them easy to read on the screen. You can learn more about them by asking Python for their documentation. At the end of the loop, try adding:

help(subpoint.latitude)

The documentation for the Angle class will come up. You can also find it on the web here:

https://rhodesmill.org/skyfield/api-units.html#skyfield.units.Angle

You will want to use the attribute degrees, which expresses the angle as a floating point number. Change the print calls in your program to:

print(subpoint.latitude.degrees)
print('\n')
print(subpoint.longitude.degrees)

Upvotes: 2

deadshot
deadshot

Reputation: 9061

Try this

from skyfield.api import EarthSatellite, Topos, load
import time
line1 = '1 25544U 98067A   14020.93268519  .00009878  00000-0  18200-3 0  5082'
line2 = '2 25544  51.6498 109.4756 0003572  55.9686 274.8005 15.49815350868473'

satellite = EarthSatellite(line1, line2, name='ISS (ZARYA)')

def convert(deg):
  d, m, s = str(deg).replace('deg', '').split(" ")
  ans = float(d) + (float(m.strip("'")) / 60) + (float(s.strip('"')) / 3600)
  return str(ans) + chr(176)

while True:
    ts = load.timescale()
    t = ts.now()
    geometry = satellite.at(t)

    subpoint = geometry.subpoint()
    lat = convert(subpoint.latitude)
    lng = convert(subpoint.longitude)
    print(lat, lng)
    time.sleep(1)

Output:

48.522305555555555° 133.80061111111112°
48.49586111111111° 133.89988888888888°
48.46933333333334° 133.99902777777777°
48.44269444444444° 134.09808333333334°
48.416° 134.19702777777778°

Upvotes: 1

Related Questions