Skip to content

Instantly share code, notes, and snippets.

@major
Created April 22, 2020 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save major/761c6c19c3566bd9b326f89bba60a29a to your computer and use it in GitHub Desktop.
Save major/761c6c19c3566bd9b326f89bba60a29a to your computer and use it in GitHub Desktop.
Weather via METAR for i3wm status bar
#!/usr/bin/env python
import json
from metar import Metar
import metpy.calc as mpcalc
from metpy.units import units
import requests
alerts_zone = "TXZ207"
# Guadalupe county alerts: https://api.weather.gov/alerts/active/zone/TXZ207
# METAR: https://tgftp.nws.noaa.gov/data/observations/metar/stations/KRND.TXT
def get_alerts(zoneid):
r = requests.get(f"https://api.weather.gov/alerts/active/zone/{zoneid}")
return r.json()['features']
def get_metar(airport):
url = (
"https://tgftp.nws.noaa.gov"
f"/data/observations/metar/stations/{airport}.TXT"
)
r = requests.get(url)
return r.text.split('\n')[1]
alerts = [alert['properties']['event'] for alert in get_alerts(alerts_zone)]
if alerts:
formatted_alerts = ' ' + ', '.join(alerts)
else:
formatted_alerts = ''
metar_data = Metar.Metar(get_metar('KRND'))
temp_c = metar_data.temp.value('C')
temp_f = metar_data.temp.value('F')
wind_dir = metar_data.wind_dir.compass()
wind_speed = metar_data.wind_speed.value('MPH')
humidity = 100 * mpcalc.relative_humidity_from_dewpoint(
temp_c * units.celsius,
metar_data.dewpt.value('C') * units.celsius
).m
description = metar_data.sky_conditions().upper()
formatted_weather = [
f"{description}",
f"{temp_f:.0f}°F/{temp_c:.0f}°C",
f"{humidity:.0f}%",
f"{wind_dir} @ {wind_speed:.0f}",
formatted_alerts
]
print(' '.join(formatted_weather))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment