Home > Enterprise >  Skyfield returning incorrect latitude and longitude coordinates?
Skyfield returning incorrect latitude and longitude coordinates?

Time:05-27

I'm trying to make a program which takes latitude and longitude coordinates of the ISS (from the Skyfield API) and plots it onto a scatterplot (using matplotlib) to show its estimated orbit path. The problem is that when it's plotted onto the scatterplot, there are a bunch of outliers (which initially I thought were just random, but after plotting data points for every second of one orbit, turned out to be some other sort of wave pattern).

Skyfield returns its latitude and longitude coordinates in the degrees minutes hours (DMS) format, and in order to plot them onto a graph, I convert them into decimal degrees (DD) and insert each pair of lat lon coords into a list - so maybe it could be a problem with some of that math? The image below shows the plot for the ISS' latitude and longitude for every second of an orbit, and the code is the DMS to DD converter function followed by the (essentially default matplotlib scatterplot code.

ISS lat lon scatterplot image

def dms2dd(latdms, londms):
latdd = str(latdms)
for num in range(1, 11):
    if '0'   str(num) in latdd:
        latdd = latdd.replace('0', '')
if ' deg' in latdd or " '" in latdd or ' "' in latdd or ' ."' in latdd:
    latdd = latdd.replace(' deg', '(')
    latdd = latdd.replace(" '", '')
    latdd = latdd.replace(' "', '(')
    latdd = latdd.replace(' ."', '')
if latdd.startswith('-'):
    latdd = latdd.replace('deg', '   (-')
    latdd = latdd.replace("'", '/60)   (-')
    latdd = latdd.replace('"', '/3600)')
else:
    latdd = latdd.replace('-', '')
    latdd = latdd.replace('deg', '   (')
    latdd = latdd.replace("'", '/60)   (')
    latdd = latdd.replace('"', '/3600)')
for x in range(0, 4):
    latdd = latdd.removesuffix('  (-')
    latdd = latdd.removesuffix('   (')
latdd = latdd.replace(' ', '')
latdd = eval(latdd)

londd = str(londms)
for num in range(1, 11):
    if '0'   str(num) in londd:
        londd = londd.replace('0', '')
if ' deg' in londd or " '" in londd or ' "' in londd or ' ."' in londd:
    londd = londd.replace(' deg', '(')
    londd = londd.replace(" '", '')
    londd = londd.replace(' "', '(')
    londd = londd.replace(' ."', '')
if londd.startswith('-'):
    londd = londd.replace('deg', '   (-')
    londd = londd.replace("'", '/60)   (-')
    londd = londd.replace('"', '/3600)')
else:
    londd = londd.replace('-', '')
    londd = londd.replace('deg', '   (')
    londd = londd.replace("'", '/60)   (')
    londd = londd.replace('"', '/3600)')
for x in range(0, 4):
    londd = londd.removesuffix('  (-')
    londd = londd.removesuffix('   (')
londd = londd.replace(' ', '')
londd = eval(londd)

return latdd, londd



plt.style.use('_mpl-gallery')

# datalat and datalon are just the list versions of the latitude and longitude coords
y = datalat
x = datalon
colors = np.random.uniform(15, 80, len(x))
fig, ax = plt.subplots()
ax.scatter(x, y, s=5, c=colors, vmin=100, vmax=100)
ax.set(xlim=(-180, 180), xticks=np.arange(-180, 180),
       ylim=(-90, 90), yticks=np.arange(-180, 180))
plt.show()

CodePudding user response:

Happily, Skyfield makes decimal degrees directly available for longitude and latitude, so there is no need for you to parse a string. Both longitude and latitude are instances of the Angle class, according to the documentation here:

https://rhodesmill.org/skyfield/api-topos.html#skyfield.toposlib.GeographicPosition.latitude

And an Angle directly offers degrees, hours, and radians:

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

So given a geographic position g, you should be able to ask for its g.longitude.degrees and g.latitude.degrees and use those decimal numbers directly.

  • Related