Note
Go to the end to download the full example code.
Parse angles#
Demonstrate how to convert direction strings to angles.
The code below shows how to parse directional text into angles. It also demonstrates the function’s flexibility in handling various string formatting.
import metpy.calc as mpcalc
Create a test value of a directional text
SOUTH SOUTH EAST
Now throw that string into the function to calculate the corresponding angle
angle_deg = mpcalc.parse_angle(dir_str)
print(angle_deg)
157.5 degree
The function can also handle arrays of strings with different abbreviations and capitalizations
dir_str_list = ['ne', 'NE', 'NORTHEAST', 'NORTH_EAST', 'NORTH east']
angle_deg_list = mpcalc.parse_angle(dir_str_list)
print(angle_deg_list)
[45.0 45.0 45.0 45.0 45.0] degree
Total running time of the script: (0 minutes 0.002 seconds)