Unidata / MetPy

MetPy is a collection of tools in Python for reading, visualizing and performing calculations with weather data.

Home Page:https://unidata.github.io/MetPy/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: wind_direction_categorical

raybellwaves opened this issue · comments

What should we add?

I would like to have a function that takes wind direction and converts it to a categorical value e.g. "N", "E", "S", "W"

Reference

Gave this a little thought this afternoon.

The function called be called wind_direction_categorical and it takes an arg direction e.g. an array of wind directions. It could have an options of n_categories with a default value of 4 and also take 8 or 16 e.g. split the data into ["N", "E", "S", "W"], ["N", "NE", "E", "SE", "S", "SW", "W", "NW"] or ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "SW", "WSW", "W", "WNW", "NW", "NNW"], respectively.

I created a quick function to apply to a numpy.array. I'm sure there are extra checks that need to be added to ensure it works on scalars and xarray.DataArray's.

import numpy as np

arr = np.array(([360, 90, 180, 270], [360, 90, 180, 270]))

def f(x):
    if 45 <= x < 135:
        return "E"
    elif 135 <= x < 225:
        return "S"
    elif 225 <= x < 315:
        return "W"
    else:
        return "N"

f_vec = np.vectorize(f)
result = f_vec(arr)
result
array([['N', 'E', 'S', 'W'],
       ['N', 'E', 'S', 'W']], dtype='<U1')

Are you aware of metpy.calc.angle_to_direction?

Oh man I missed this. Looks like it fits the bill. Thanks. I'll close this for now