1

I calculated the wind direction using Metpy. How can I extract the values and use the values as input in another part of my program?

In the sample code below, I would like to display the direction as a numpy variable, so I can use it in my program.

import metpy.calc as mpcalc
import numpy as np

# Make some fake data for us to work with
np.random.seed(19990503)  # So we all have the same data
u = np.random.randint(0, 15, 10) * units('m/s')
v = np.random.randint(0, 15, 10) * units('m/s')
direction = mpcalc.wind_direction(u, v)
print(direction)
Tee
  • 41
  • 7

2 Answers2

1

To convert a metpy result from a wind calculation to a numpy array, simply use the numpy np.array function.

import metpy.calc as mpcalc
from metpy.units import units
import numpy as np

np.random.seed(19990503)
u = np.random.randint(0, 15, 10)*units("m/s")
v = np.random.randint(0, 15, 10)*units("m/s")
direction = mpcalc.wind_direction(u, v)
direction_numpy = np.array(direction)
jared
  • 4,165
  • 1
  • 8
  • 31
1

Another option, which I would recommend would be to use the .magnitude attribute (or .m for short), which gives you the underlying data as appropriate (numpy array or xarray DataArray):

import metpy.calc as mpcalc
import numpy as np

# Make some fake data for us to work with
np.random.seed(19990503)  # So we all have the same data
u = np.random.randint(0, 15, 10) * units('m/s')
v = np.random.randint(0, 15, 10) * units('m/s')
direction = mpcalc.wind_direction(u, v)
print(direction.magnitude)

You can also use .m_as() to convert to desired units before printing:

print(direction.m_as('mph'))
DopplerShift
  • 5,472
  • 1
  • 21
  • 20