I am trying to create a weather widget in a Flask application. I am stuck on working with wind direction in degrees (angle).
If we use the following image as a representation:
Wind Directions
When working with wind direction we are limited to a 360 degree range. I would like to assign an icon based on which range within 360 degrees I get for wind direction, to help you understand here is some information:
| Cardinal Direction | Degree Direction |
|:------------------:|------------------|
| N | 348.75 - 11.25 |
| NNE | 11.25 - 33.75 |
| NE | 33.75 - 56.25 |
| ENE | 56.25 - 78.75 |
| E | 78.75 - 101.25 |
| ESE | 101.25 - 123.75 |
| SE | 123.75 - 146.25 |
| SSE | 146.25 - 168.75 |
| S | 168.75 - 191.25 |
| SSW | 191.25 - 213.75 |
| SW | 213.75 - 236.25 |
| WSW | 236.25 - 258.75 |
| W | 258.75 - 281.25 |
| WNW | 281.25 - 303.75 |
| NW | 303.75 - 326.25 |
| NNW | 326.25 - 348.75 |
I will store the keys/values in a dict and create an if elif else loop that will do something like the following:
from numpy import arange
degrees = the_json_response['wind']['deg']
wind_directions = {
'N': arange(348.75, 11.25, 0.25),
...
}
degrees_range = range(0, 360)
if degrees in wind_direction['N']:
dirction = north_icon
elif degrees in wind_direction['NNE']:
direction = north_north_east_icon
The above is only pseudo code.. To summarize, How can I apply this so that we only ever work with a 360 degree range because obviously it will throw errors when working with North wind direction due to it being in a range of 348.75 - 11.25, we need to tell it we are working with 360 degrees.
The reason I cannot for the life of my figure this out is I never learnt complex mathmatics (which to me this is). If anyone has a suggestion on how I can apply this in a better way I am very open to those suggestions.