Skip to content

Instantly share code, notes, and snippets.

@major
Created April 24, 2020 20:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save major/eaaa30928b5da1759c0dc1c0ef886a50 to your computer and use it in GitHub Desktop.
Save major/eaaa30928b5da1759c0dc1c0ef886a50 to your computer and use it in GitHub Desktop.
METAR Weather
#!/usr/bin/env python
import json
from metar import Metar
import metpy.calc as mpcalc
from metpy.units import units
import requests
observation_station = "KRND"
alerts_zone = "TXZ207"
# KRND Observations: https://api.weather.gov/stations/KRND/observations/latest
# 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('KSAT'))
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()
condition = metar_data.present_weather().upper()
pressure = metar_data.press.value('mb')
formatted_weather = [
f"{condition} {description}",
f" {temp_f:.0f}°F/{temp_c:.0f}°C",
f"{humidity:.0f}% RH {pressure:.0f}mb",
f" {wind_dir} {wind_speed:.0f} mph",
formatted_alerts
]
print(' '.join(formatted_weather))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment