Home > Software engineering >  How Can I Create Variables From Text and Convert Coordinates
How Can I Create Variables From Text and Convert Coordinates

Time:06-05

I am aware there are similar questions to mine, but after trying numerous "answers" over several hours I thought my best next step is submit my conundrum here. I respect your time.

After several hours with no success in understanding why my Python script won't work I decided to see if someone could help me. Essentially, the goal is to use the astronomical program, "Stellarium" as a "day and night sky" to practice Celestial Navigation (CelNav) navigating the simulated world of Microsoft Flight Simulator X (FSX). The script actually writes a "startup.ssc" script which initializes Stellarium's date, time, and position.

The process is thus...

  1. Use FSX and save a "flight." This creates a *.FLT file which is a text file which saves the complete situation, including time and location.
  2. Run the FSXtoStellarium.py
  3. Locate the lines of date, time, latitude, longitude, and altitude in the *.FLT text.
  4. Read the data into variables.
  5. Convert the Degrees(°), Minutes('), Seconds(") (DMS) to Decimal Degrees (DD).
  6. Lastly, the script constructs a "startup.ssc" and opens Stellarium at the recorded time and place.

The Problem: I have not been able to read the DMS into variable(s) correctly nor can I format the DMS into Decimal Degrees (DD). According to the "watches" I set in my IDE (PyScripter), the script is reading in an "int" value I can't decipher instead of the text string of the DMS (Example: W157° 27' 23.20").

Here are some excerpts of the file and script.


HMS Bounty.FLT

Various lines of data above...

[SimVars.0]
Latitude=N21° 20' 47.36"
Longitude=W157° 27' 23.20"
Altitude= 000004.93

Various lines of data below...
EOF

FSXtoStellarium.py

Various lines of script above...

# find lat & Lon in the file
start = content.find("SimVars.0")
latstart = content.find("Latitude=")
latend = content.find("Latitude=",latstart 1)
longstart = content.find("Longitude=",start)
longend = content.find(",",longstart)

# convert to dec deg
latitude = float(content[longend 1:latend])/120000
longitude = float(content[longstart 10:longend])/120000

Various lines of script below...

So, what am I missing?

FYI - I am an old man who gets confused. My professional career was in COBOL/DB2/CICS, but you can consider me a Python newbie (it shows, right?). :)

Your help s greatly appreciated and I will gladly provide any additional information.

Calvin

CodePudding user response:

Here is a way to get from the text file (with multiple input lines) all the way to Decimal Degrees in python 2.7:

from __future__ import print_function
content='''
[SimVars.0]
Latitude=N21° 20' 47.36"
Longitude=W157° 27' 23.20"
'''

latKey = "Latitude="
longKey = "Longitude="

latstart = content.index(latKey)   len(latKey)
latend = content.find('"', latstart)   1
longstart = content.find(longKey, latend)   len(longKey)
longend = content.find('"', longstart)   1

lat = content[latstart:latend]
long = content[longstart:longend]

print()
print('lat ', lat)
print('long ', long)

deg, mnt, sec = [float(x[:-1]) for x in lat[1:].split()]
latVal = deg   mnt / 60   sec / 3600

deg, mnt, sec = [float(x[:-1]) for x in long[1:].split()]
longVal = deg   mnt / 60   sec / 3600

print()
print('latVal ', latVal)
print('longVal ', longVal)

Explanation:

  • we start with a multi-line string, content
  • the first index() call finds the start position of the substring "Latitude=" within content, to which we add the length of "Latitude=" since what we care about is the characters following the = character
  • the second index() call searches for the 'seconds' character " (which marks the end of the Latitude substring), to which we add one (for the length of the ")
  • the third index() call does for Longitude= something similar to what we did for latitude, except it starts at the position latend since we expect Longitude= to follow the latitude string following Latitude=
  • the fourth index() call seeks the end of the longitude substring and is completely analogous to the second index() call above for latitude
  • the assignment to lat uses square bracket slice notation for the list content to extract the substring from the end of Latitude= to the subsequent " character
  • the assignment to long is analogous to the previous step
  • the first assignment to deg, mnt, sec is assigning a tuple of 3 values to these variables using a list comprehension:
    • split lat[1:], which is to say lat with the leading cardinal direction character N removed, into space-delimited tokens 21°, 20' and 47.36"
    • for each token, x[:-1] uses slice notation to drop the final character which gives strings 21, 20 and 47.36
    • float() converts these strings to numbers of type float
  • the assignment to latVal does the necessary arithmetic to calculate a quantity in decimal degrees using the degrees, minutes and seconds stored in deg, mnt, sec.
  • the treatment of long to get to longVal is completely analogous to that for lat and latVal above.

Output:

lat  N21° 20' 47.36"
long  W157° 27' 23.20"

latVal  21.34648888888889
longVal  157.45644444444443
  • Related