Note
Click here to download the full example code
Gradient¶
Use metpy.calc.gradient
.
This example demonstrates the various ways that MetPy’s gradient function can be utilized.
import numpy as np
import metpy.calc as mpcalc
from metpy.units import units
Create some test data to use for our example
data = np.array([[23, 24, 23],
[25, 26, 25],
[27, 28, 27],
[24, 25, 24]]) * units.degC
# Create an array of x position data (the coordinates of our temperature data)
x = np.array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]) * units.kilometer
y = np.array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4]]) * units.kilometer
Calculate the gradient using the coordinates of the data
grad = mpcalc.gradient(data, coordinates=(y, x))
print('Gradient in y direction: ', grad[0])
print('Gradient in x direction: ', grad[1])
Out:
Gradient in y direction: [[ 2. 2. 2. ] [ 2. 2. 2. ] [-0.5 -0.5 -0.5] [-5.5 -5.5 -5.5]] kelvin / kilometer
Gradient in x direction: [[ 2. 0. -2.] [ 2. 0. -2.] [ 2. 0. -2.] [ 2. 0. -2.]] kelvin / kilometer
It’s also possible that we do not have the position of data points, but know that they are evenly spaced. We can then specify a scalar delta value for each axes.
x_delta = 2 * units.km
y_delta = 1 * units.km
grad = mpcalc.gradient(data, deltas=(y_delta, x_delta))
print('Gradient in y direction: ', grad[0])
print('Gradient in x direction: ', grad[1])
Out:
Gradient in y direction: [[ 2. 2. 2. ] [ 2. 2. 2. ] [-0.5 -0.5 -0.5] [-5.5 -5.5 -5.5]] kelvin / kilometer
Gradient in x direction: [[ 1. 0. -1.] [ 1. 0. -1.] [ 1. 0. -1.] [ 1. 0. -1.]] kelvin / kilometer
Finally, the deltas can be arrays for unevenly spaced data.
x_deltas = np.array([[2, 3],
[1, 3],
[2, 3],
[1, 2]]) * units.kilometer
y_deltas = np.array([[2, 3, 1],
[1, 3, 2],
[2, 3, 1]]) * units.kilometer
grad = mpcalc.gradient(data, deltas=(y_deltas, x_deltas))
print('Gradient in y direction: ', grad[0])
print('Gradient in x direction: ', grad[1])
Out:
Gradient in y direction: [[ 0.33333333 0.66666667 2.33333333] [ 1.66666667 0.66666667 1.66666667] [ 0.83333333 -0.16666667 -1.66666667] [-3.83333333 -1.83333333 -4.33333333]] kelvin / kilometer
Gradient in x direction: [[ 0.83333333 0.16666667 -0.83333333] [ 1.33333333 0.66666667 -1.33333333] [ 0.83333333 0.16666667 -0.83333333] [ 1.5 0.5 -1.5 ]] kelvin / kilometer
Total running time of the script: ( 0 minutes 0.026 seconds)