title: Calculating vorticity for multiple vertical levels in MetPy
body: <p>I'm trying to calculate vorticity in MetPy for multiple (consecutive) vertical levels. When I try to calculate it for a single level, everything works fine.</p>
  
  <p>Here's the code; I've used the example for cross sections from <a href="https://unidata.github.io/MetPy/latest/examples/cross_section.html#sphx-glr-examples-cross-section-py" rel="nofollow noreferrer">https://unidata.github.io/MetPy/latest/examples/cross_section.html#sphx-glr-examples-cross-section-py</a>.</p>
  
  <pre><code>import cartopy.crs as ccrs
  import cartopy.feature as cfeature
  import matplotlib.pyplot as plt
  import numpy as np
  import xarray as xr
  
  import metpy.calc as mpcalc
  from metpy.cbook import get_test_data
  from metpy.interpolate import cross_section
  from metpy.units import units
  
  data = xr.open_dataset(get_test_data('narr_example.nc', False))
  data = data.metpy.parse_cf().squeeze()
  
  data_crs = data['Temperature'].metpy.cartopy_crs
  lat = data['lat']
  lon = data['lon']
  f = mpcalc.coriolis_parameter(lat)
  dx, dy = mpcalc.lat_lon_grid_deltas(lon, lat, initstring=data_crs.proj4_init)
  </code></pre>
  
  <p>Then the calculation of vorticity is performed.</p>
  
  <pre><code>vort = mpcalc.vorticity(data['u_wind'], data['v_wind'], dx, dy)
  </code></pre>
  
  <p>The traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "E:\Временные файлы\cross_section (1).py", line 63, in &lt;module&gt;
      vort = mpcalc.vorticity(data['u_wind'], data['v_wind'], dx, dy)
    File "C:\ProgramData\Miniconda3\lib\site-packages\metpy\xarray.py", line 436, in wrapper
      return func(*args, **kwargs)
    File "C:\ProgramData\Miniconda3\lib\site-packages\metpy\calc\kinematics.py", line 60, in wrapper
      ret = func(*args, **kwargs)
    File "C:\ProgramData\Miniconda3\lib\site-packages\metpy\calc\kinematics.py", line 121, in vorticity
      dudy = first_derivative(u, delta=dy, axis=-2)
    File "C:\ProgramData\Miniconda3\lib\site-packages\metpy\calc\tools.py", line 920, in wrapper
      return preprocess_xarray(func)(f, **kwargs)
    File "C:\ProgramData\Miniconda3\lib\site-packages\metpy\xarray.py", line 436, in wrapper
      return func(*args, **kwargs)
    File "C:\ProgramData\Miniconda3\lib\site-packages\metpy\calc\tools.py", line 1014, in first_derivative
      combined_delta = delta[tuple(delta_slice0)] + delta[tuple(delta_slice1)]
    File "C:\ProgramData\Miniconda3\lib\site-packages\pint\quantity.py", line 1400, in __getitem__
      value = self._magnitude[key]
  IndexError: too many indices for array
  </code></pre>
  
  <p>I'm absolutely stuck. Searching "metpy multiple levels calculations" (no actual quotes) gives no relevant results.
  The doc says:</p>
  
  <pre><code>metpy.calc.vorticity(u, v, dx, dy)[source]
  Calculate the vertical vorticity of the horizontal wind.
  
  Parameters: 
  u ((M, N) ndarray) – x component of the wind
  v ((M, N) ndarray) – y component of the wind
  dx (float or ndarray) – The grid spacing(s) in the x-direction. If an array, there should be one item less than the size of u along the applicable axis.
  dy (float or ndarray) – The grid spacing(s) in the y-direction. If an array, there should be one item less than the size of u along the applicable axis.
  dim_order (str or None, optional) – The ordering of dimensions in passed in arrays. Can be one of None, 'xy', or 'yx'. 'xy' indicates that the dimension corresponding to x is the leading dimension, followed by y. 'yx' indicates that x is the last dimension, preceded by y. None indicates that the default ordering should be assumed, which is ‘yx’. Can only be passed as a keyword argument, i.e. func(…, dim_order=’xy’).
  Returns:    
  (M, N) ndarray – vertical vorticity
  </code></pre>
  
  <p>I conclude that the input can have more than 2 dimensions, but 3-dimensional input (as it is in my case) gives errors. What can be done to fix them?</p>
  
  <p>I'm absolutely new to Python, so I could've made a stupid mistake.</p>