Skip to content

Instantly share code, notes, and snippets.

@NelsonMinar
Created December 14, 2011 23:41
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 NelsonMinar/1479133 to your computer and use it in GitHub Desktop.
Save NelsonMinar/1479133 to your computer and use it in GitHub Desktop.
Code to parse METARs with Tom Pollard's library
def extractWind(s):
"""Parse a metar and return wind speed, direction.
If gusts, reports peak speed. Variable wind direction reports prevailing."""
# clean the input
if s.endswith("="):
s = s[:-1]
if s.endswith(" LAST"):
s = s[:-5]
metar = Metar.Metar(s, month=12) # fake out month
# Take peak speed in case of gust
if metar.wind_gust:
speed = int(round(metar.wind_gust.value("KT")))
else:
if metar.wind_speed is None:
raise Exception("Wind speed was None: %s" % s)
speed = int(round(metar.wind_speed.value("KT")))
# Extract the wind direction: not sure what happens in winds varying 60deg
if metar.wind_dir is not None:
direction = int(metar.wind_dir.value())
else:
direction = None
# Treat 00000KT as no direction
if direction == 0 and speed == 0:
direction = None
if direction is not None:
if direction == 0:
raise Exception("Wind direction was 0: %s" % s)
if direction % 10 != 0:
raise Exception("Wind direction not a multiple of 10: %s" % s)
return (speed, direction)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment