pythonmetpy

metpy.calc error, absolutely no clue on what to do


I'm using the import metpy.calc package to find the absolute momentum of wind given it's u and v components. The problem is that even though the u and v xarray are identical, metpy keeps saying they are of different lenghts. Any ideas? Thank you very much.Image ds. I have checked that both u and v array are the same in any way, but still can't understand what metpy wants from me.

d2 = d2.metpy.parse_cf()

u = d2.u[0]
v = d2.v[0]
potential_tmp = mp.calc.potential_temperature(pr * units.Pa, tmp * units.kelvin)
abs_momentum = mp.calc.absolute_momentum(u, v)

---------------------------------------------------------------------------
GeodError                                 Traceback (most recent call last)
Input In [239], in <cell line: 1>()
----> 1 abs_momentum = mp.calc.absolute_momentum(u, v)

File ~\Anaconda3\envs\ox\lib\site-packages\metpy\xarray.py:1304, in check_matching_coordinates.<locals>.wrapper(*args, **kwargs)
   1302         if not first.metpy.coordinates_identical(other):
   1303             raise ValueError('Input DataArray arguments must be on same coordinates.')
-> 1304 return func(*args, **kwargs)

File ~\Anaconda3\envs\ox\lib\site-packages\metpy\calc\cross_sections.py:295, in absolute_momentum(u, v, index)
    259 r"""Calculate cross-sectional absolute momentum (also called pseudoangular momentum).
    260 
    261 As given in [Schultz1999]_, absolute momentum (also called pseudoangular momentum) is
   (...)
    292 
    293 """
    294 # Get the normal component of the wind
--> 295 norm_wind = normal_component(u.metpy.quantify(), v.metpy.quantify(), index=index)
    297 # Get other pieces of calculation (all as ndarrays matching shape of norm_wind)
    298 latitude = latitude_from_cross_section(norm_wind)

File ~\Anaconda3\envs\ox\lib\site-packages\metpy\xarray.py:1304, in check_matching_coordinates.<locals>.wrapper(*args, **kwargs)
   1302         if not first.metpy.coordinates_identical(other):
   1303             raise ValueError('Input DataArray arguments must be on same coordinates.')
-> 1304 return func(*args, **kwargs)

File ~\Anaconda3\envs\ox\lib\site-packages\metpy\calc\cross_sections.py:202, in normal_component(data_x, data_y, index)
    176 r"""Obtain the normal component of a cross-section of a vector field.
    177 
    178 Parameters
   (...)
    199 
    200 """
    201 # Get the unit vectors
--> 202 _, unit_norm = unit_vectors_from_cross_section(data_x, index=index)
    204 # Take the dot products
    205 component_norm = data_x * unit_norm[0] + data_y * unit_norm[1]

File ~\Anaconda3\envs\ox\lib\site-packages\metpy\calc\cross_sections.py:125, in unit_vectors_from_cross_section(cross, index)
     95 @exporter.export
     96 def unit_vectors_from_cross_section(cross, index='index'):
     97     r"""Calculate the unit tangent and unit normal vectors from a cross-section.
     98 
     99     Given a path described parametrically by :math:`\vec{l}(i) = (x(i), y(i))`, we can find
   (...)
    123 
    124     """
--> 125     x, y = distances_from_cross_section(cross)
    126     dx_di = first_derivative(x, axis=index).values
    127     dy_di = first_derivative(y, axis=index).values

File ~\Anaconda3\envs\ox\lib\site-packages\metpy\calc\cross_sections.py:43, in distances_from_cross_section(cross)
     40 lon = cross.metpy.x
     41 lat = cross.metpy.y
---> 43 forward_az, _, distance = g.inv(lon[0].values * np.ones_like(lon),
     44                                 lat[0].values * np.ones_like(lat),
     45                                 lon.values,
     46                                 lat.values)
     47 x = distance * np.sin(np.deg2rad(forward_az))
     48 y = distance * np.cos(np.deg2rad(forward_az))

File ~\Anaconda3\envs\ox\lib\site-packages\pyproj\geod.py:331, in Geod.inv(self, lons1, lats1, lons2, lats2, radians)
    329 inz, z_data_type = _copytobuffer(lons2)
    330 ind = _copytobuffer(lats2)[0]
--> 331 self._inv(inx, iny, inz, ind, radians=radians)
    332 # if inputs were lists, tuples or floats, convert back.
    333 outx = _convertback(x_data_type, inx)

File pyproj\_geod.pyx:171, in pyproj._geod.Geod._inv()

GeodError: Array lengths are not the same.

Solution

  • As indicated in the docs for absolute_momentum, this calculation only makes sense when given a cross-section of data.

    To use this function, you'll first need to get a cross-section of interest using metpy.interpolate.cross_section. For more information, see MetPy's cross-section example.