Skip to content

Instantly share code, notes, and snippets.

@kareiva
Last active November 30, 2021 22:25
Show Gist options
  • Select an option

  • Save kareiva/6672b4c664f65f6477423ac376fe6989 to your computer and use it in GitHub Desktop.

Select an option

Save kareiva/6672b4c664f65f6477423ac376fe6989 to your computer and use it in GitHub Desktop.
Calculate rough distance of high altitude balloon trip from WSPR data + generate KML
#!/usr/bin/env python
# Calculate rough distance of the balloon trip from WSPR data
#
# Run `pip install maidenhead psycopg2-binary pyproj simplekml` for prereqs
#
# Simonas Kareiva <[email protected]>
import maidenhead as mh
import simplekml
import psycopg2
import logging
import pyproj
def main():
trip_km = 0
balloon = "LY1BWB"
launch_date = "2021-11-09T07:00:00Z"
kml = simplekml.Kml()
ls = kml.newlinestring(name="LY1BWB path")
try:
conn = psycopg2.connect(
"host=logs2.wsprdaemon.org dbname=wsprnet user=wdread password=JTWSPR2008"
)
except psycopg2.OperationalError as err:
logging.error("PostgreSQL connect error: " + str(err))
conn = None
cursor = conn.cursor()
cursor.execute(
'SELECT "Grid", "wd_time" FROM (select DISTINCT ON ("Grid") "Grid", "wd_time" \
FROM spots WHERE "CallSign"=\''
+ balloon
+ "' AND \"wd_time\" > '"
+ launch_date
+ '\' ORDER BY "Grid", "wd_time" ASC) dt ORDER BY "wd_time" ASC;'
)
sqldata = cursor.fetchall()
conn.close()
for idx, row in enumerate(sqldata):
from_square = row[0]
if idx < len(sqldata) - 1:
to_square = sqldata[idx + 1][0]
to_time = sqldata[idx + 1][1]
(x1, y1) = mh.to_location(from_square)
(x2, y2) = mh.to_location(to_square)
kml.newpoint(name=to_square, description=to_time, coords=[(y2, x2)])
ls.coords.addcoordinates([(y2, x2)])
geo = pyproj.Geod(ellps="WGS84")
_fw, _bg, distance = geo.inv(y1, x1, y2, x2)
distance_km = distance / 1000
print(
"%s: %.2f,%.2f --> %.2f,%.2f (%d km)."
% (to_time, x1, y1, x2, y2, distance_km)
)
trip_km = trip_km + distance_km
print("Total trip length for %s: %d km" % (balloon, trip_km))
kml.save("trip.kml")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment