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

Fix metar2glm #1302

Merged
merged 3 commits into from
Jun 26, 2023
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
41 changes: 41 additions & 0 deletions docs/Utilities/Metar2glm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[[/Utilities/Metar2glm]] - METAR to GLM realtime weather

# Synopsis

Shell:

~~~
$ gridlabd python -m metar2glm -m metar2glm [--index|STATION ...] [-c|-class CLASSNAME]
~~~

GLM:

~~~
#python -m metar2glm -m metar2glm [--index|STATION ...] [-c|-class CLASSNAME]
~~~

Options:

- `-h|--help|help`: Get this help information (exits with code 0)

- `-c|--class CLASSNAME`: Name the GLM class to use for the weather data class

- `--index`: Output a list of valid METAR stations

# Description

The `metar2glm` tool creates an object that represents the realtime weather at the specifies weather stations.

# Example

The following outputs the realtime weather for San Francisco airport.

~~~
$ gridlabd python -m metar2glm KSFO
~~~

# See also

* [https://github.com/python-metar/python-metar]
* [https://www.ucar.edu]

17 changes: 10 additions & 7 deletions tools/metar2glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

station_list = []
station_data = ucar_weather.stations()
syntax = "python3 -m metar2glm [--index|STATION ...]"
syntax = "python3 -m metar2glm [--index|STATION ...] [-c|-class CLASSNAME]"
classname = "weather"

n = 1
while n < len(sys.argv):
Expand All @@ -19,6 +20,9 @@
elif sys.argv[n] in ["--index"]:
print("\n".join(station_data.keys()))
exit(0)
elif sys.argv[n] in ["-c","--class"]:
classname = sys.argv[n+1]
n = n+1
else:
station_list.append(sys.argv[n])
n += 1
Expand All @@ -34,9 +38,9 @@
"deg" : "deg",
}
print(f"// generated by metar2glm.py on {datetime.datetime.utcnow().isoformat()}Z")
print("""
class weather
{
print(f"""
class {classname}
{{
char8 country;
char8 region;
char32 station;
Expand All @@ -50,14 +54,13 @@ class weather
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(f"object {classname}")
print("{")
for attr,data in station_data[station].items():
print(f" {attr} \"{data}\";");
Expand Down
13 changes: 11 additions & 2 deletions tools/ucar_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,17 @@ def get(station):
raise Exception("request error (no observation)")
else:
raise Exception(r"request error (no observation),, query='{query}',' response='{r.text}'")
result = obs.to_dict()
result["metar"] = data
result = {
"station" : station,
"temperature" : f"{obs.temp.value()} degC",
"dew_point" : f"{obs.dewpt.value()} degC",
"wind_speed" : f"{obs.wind_speed.value()} knot",
"wind_dir" : f"{obs.wind_dir.value()} deg",
"visibility" : f"{obs.vis.value()} mile",
"pressure" : f"{obs.press.value()} inH2O",
"clouds" : obs.sky_conditions(),
}
result["metar"] = data.replace("\n"," ")
return result

def stations():
Expand Down