Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Add metar2glm utility #965

Merged
merged 1 commit into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python_extras/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist_pkgdata_DATA += python_extras/example/tstat_commit.py
dist_pkgdata_DATA += python_extras/example/tstat_init.py
dist_pkgdata_DATA += python_extras/volt_dump/meter_record.py
dist_pkgdata_DATA += python_extras/volt_dump/voltdump.py
dist_pkgdata_DATA += python_extras/metar2glm.py
72 changes: 72 additions & 0 deletions python_extras/metar2glm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/local/bin/python3
import sys
import ucar_weather
import datetime

station_list = []
station_data = ucar_weather.stations()
syntax = "python3 -m metar2glm [--index|STATION ...]"

n = 1
while n < len(sys.argv):
if sys.argv[n] in ["-h","--help","help"]:
print(f"Syntax: {syntax}")
exit(0)
elif sys.argv[n] in ["--index"]:
print("\n".join(station_data.keys()))
exit(0)
else:
station_list.append(sys.argv[n])
n += 1
if not station_list:
print(f"Syntax: {syntax}")
exit(1)

unit_conversion = {
"C" : "degC",
"KT" : "knot",
"M" : "m",
"mb" : "mbar",
"deg" : "deg",
}
print(f"// generated by metar2glm.py on {datetime.datetime.utcnow().isoformat()}Z")
print("""
class weather
{
char8 country;
char8 region;
char32 station;
double elevation[ft];
timestamp time;
double temperature[degF];
double dew_point[degF];
double wind_speed[m/s];
double wind_dir[deg];
double visibility[mile];
double pressure[mbar];
char256 clouds;
char1024 metar;
on_init "python:metar_weather.weather_init";
}
""")
for station in station_list:
if not station in station_data.keys():
raise Exception(f"station '{station}' not found in metar index")
weather = ucar_weather.get(station)
print("object weather")
print("{")
for attr,data in station_data[station].items():
print(f" {attr} \"{data}\";");
for attr,data in weather.items():
if not data or attr == "type":
continue
if attr == "station":
attr = "name"
if type(data) is dict and "value" in data.keys():
if "unit" in data.keys():
print(f" {attr} {data['value']} {unit_conversion[data['unit']]};")
elif data['value']:
print(f" {attr} \"{data['value']}\";")
else:
print(f" {attr} \"{data}\";")
print("}")