计算MetPy中多个垂直水平的涡度

问题描述 投票:2回答:1

我正在尝试计算MetPy中多个(连续)垂直水平的涡度。当我尝试将其计算为单个级别时,一切正常。

这是代码;我用https://unidata.github.io/MetPy/latest/examples/cross_section.html#sphx-glr-examples-cross-section-py的横截面示例。

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)

然后进行涡量的计算。

vort = mpcalc.vorticity(data['u_wind'], data['v_wind'], dx, dy)

追溯:

Traceback (most recent call last):
  File "E:\Временные файлы\cross_section (1).py", line 63, in <module>
    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

我绝对卡住了。搜索“metpy multiple levels calculation”(没有实际报价)不会给出相关结果。医生说:

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

我得出结论,输入可以有2个以上的维度,但是3维输入(就像我的情况一样)会产生错误。可以做些什么来修复它们?

我是Python的新手,所以我可能犯了一个愚蠢的错误。

python metpy
1个回答
2
投票

不幸的是,如果您不知道要查找什么,出现的错误消息在这种情况下没有用处!

在您的示例中,vorticity函数调用的问题是输入变量的维度不匹配。 data['u_wind']data['v_wind']是具有形状(29, 118, 292)的3D阵列,但dxdy,因为它们是从lat_lon_grid_deltas计算的,分别是具有形状(118, 291)(117, 292)的2D阵列。因此,我们需要获得适当广播的数组......有许多不同的方法可以做到这一点,但我建议这里有两个选项:

选项1:手动广播

由于缺少dxdy的“额外”维度是第一个维度(在垂直方向),我们可以通过插入一个大小一个前导维度,使dxdy成为正确对齐的3D数组:

dx, dy = mpcalc.lat_lon_grid_deltas(lon, lat, initstring=data_crs.proj4_init)
dx = dx[None, :]
dy = dy[None, :]

vort = mpcalc.vorticity(data['u_wind'], data['v_wind'], dx, dy)

选项2:使用grid_deltas_from_dataarray()辅助函数

MetPy还有一个辅助函数,可以轻松地从xarray DataArray中提取网格增量。它还可以确保广播正常进行,因此您无需自己进行广播。在您的示例中使用它,它将是:

dx, dy = mpcalc.grid_deltas_from_dataarray(data['u_wind'])

vort = mpcalc.vorticity(data['u_wind'], data['v_wind'], dx, dy)
© www.soinside.com 2019 - 2024. All rights reserved.