Created
December 14, 2011 23:41
-
-
Save NelsonMinar/1479133 to your computer and use it in GitHub Desktop.
Code to parse METARs with Tom Pollard's library
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
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