1

Would you like to know how the divergence calculation is done in Metpy? We know that in Grads the calculation of divergence is performed by the finite difference method as follows:

pi = 3,14159265359 

dtr = pi / 180 

a = 6,371e6 

dx = a * cos (dtr * lat) * dtr * cdiff (lon, x) 

dy = a * dtr * cdiff (lat, y) 

div = cdiff (u, x) / dx + cdiff (v, y) / dy-v * tan (dtr * lat) / a

However, in Metpy I didn't find anything detailed (as it is in the Grads) of how this calculation of divergence is performed.

guzmonne
  • 2,490
  • 1
  • 16
  • 22

2 Answers2

2

MetPy's divergence calculation (metpy.calc.divergence) internally uses the 3-point differencing first_derivative function to calculate divergence in Cartesian form as

As referenced in the MetPy documentation, this explicitly handles irregular spacing according to the Bowen and Smith (2005) formulation. However, the final coordinate-correction term which GrADS includes, v * tan (dtr * lat) / a, is not included in the current MetPy calculation (this is part of an open issue on GitHub).

Jon Thielen
  • 479
  • 2
  • 7
1

Divergence in MetPy is calculated by using the first_derivative function in MetPy, which is center-based, except at the edge points, to add together dudx and dvdy. If you pass metpy.calc.divergence or metpy.calc.first_derivative an Xarray.Dataset with coordinates/projection information, it will calculate the divergence across the varying grid. Otherwise, you need to provide the grid spacing. See https://unidata.github.io/MetPy/latest/_modules/metpy/calc/kinematics.html#divergence and https://unidata.github.io/MetPy/latest/api/generated/metpy.calc.first_derivative.html#metpy.calc.first_derivative for more information on the divergence function and coordinate-array handling.

zbruick
  • 316
  • 1
  • 9
  • As of MetPy 0.10.2, [`metpy.calc.divergence`](https://unidata.github.io/MetPy/latest/api/generated/metpy.calc.divergence.html#metpy.calc.divergence) does not yet pull out required metadata when using xarray; you still have to provide the grid spacings yourself. There is, however, the convenience function [`grid_deltas_from_dataarray`](https://unidata.github.io/MetPy/latest/api/generated/metpy.calc.grid_deltas_from_dataarray.html#metpy.calc.grid_deltas_from_dataarray) which makes this straightforward. – Jon Thielen Jul 21 '19 at 21:16