This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add python_extras/metar2glm.py (#965)
- Loading branch information
David P. Chassin
authored
Jul 31, 2021
1 parent
5627921
commit daada70
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("}") |