metpy 发散模块数组索引错误

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

我正在尝试使用

metpy
发散模块找出水分发散。现在我正在为一本以年份为键的字典做这件事,

HMD = {}

    for year in [2012, 2018, 2019]:
        # Get the resampled dataset for the current year
        resampled_ds = resampled_data[year]
        # Slice latitude and longitude ranges
        # Select latitude values within the desired range(8-12, 75-77)
        lat_range = resampled_ds['latitude'].where((resampled_ds['latitude'] >= 8) & (resampled_ds['latitude'] <= 12), drop=True)
        sliced_ds = resampled_ds.sel(latitude=lat_range, longitude=slice(75, 78))
        # Calculate the moisture flux components
        qu_l = (sliced_ds['Q'] * sliced_ds['U'])
        qv_l = (sliced_ds['Q'] * sliced_ds['V'])
        # Compute the grid deltas (dx, dy) in meters
        dx, dy = mpcalc.lat_lon_grid_deltas(sliced_ds['longitude'], sliced_ds['latitude'])
        # Compute the divergence of the moisture flux
        HMD[year] = mpcalc.divergence(qu_l, qv_l, dx=dx, dy=dy)
        HMD[year] = HMD

现在我收到的错误为

Traceback (most recent call last):
    
      Cell In[35], line 17
        HMD[year] = mpcalc.divergence(qu_l, qv_l, dx=dx, dy=dy)
    
      File ~/anaconda3/lib/python3.9/site-packages/metpy/calc/tools.py:1178 in wrapper
        return func(*bound_args.args, **bound_args.kwargs)
    
      File ~/anaconda3/lib/python3.9/site-packages/metpy/xarray.py:1329 in wrapper
        result = func(*bound_args.args, **bound_args.kwargs)
    
      File ~/anaconda3/lib/python3.9/site-packages/metpy/units.py:332 in wrapper
        return func(*args, **kwargs)
    
      File ~/anaconda3/lib/python3.9/site-packages/metpy/calc/kinematics.py:159 in divergence
        dudx, dvdy = vector_derivative(
    
      File ~/anaconda3/lib/python3.9/site-packages/metpy/calc/tools.py:1178 in wrapper
        return func(*bound_args.args, **bound_args.kwargs)
    
      File ~/anaconda3/lib/python3.9/site-packages/metpy/xarray.py:1329 in wrapper
        result = func(*bound_args.args, **bound_args.kwargs)
    
      File ~/anaconda3/lib/python3.9/site-packages/metpy/units.py:332 in wrapper
        return func(*args, **kwargs)
    
      File ~/anaconda3/lib/python3.9/site-packages/metpy/calc/tools.py:1571 in vector_derivative
        derivatives[component] = first_derivative(scalar, delta=delta, axis=dim)
    
      File ~/anaconda3/lib/python3.9/site-packages/metpy/calc/tools.py:951 in wrapper
        return preprocess_and_wrap()(func)(f, **kwargs)
    
      File ~/anaconda3/lib/python3.9/site-packages/metpy/xarray.py:1329 in wrapper
        result = func(*bound_args.args, **bound_args.kwargs)
    
      File ~/anaconda3/lib/python3.9/site-packages/metpy/calc/tools.py:1248 in first_derivative
        combined_delta = delta[delta_slice0] + delta[delta_slice1]
    
      File ~/anaconda3/lib/python3.9/site-packages/pint/facets/numpy/quantity.py:253 in __getitem__
        return type(self)(self._magnitude[key], self._units)
    
    IndexError: too many indices for array: array is 2-dimensional, but 4 were indexed

这里

qu_l
qv_l
是 4D `xarrays(lon,lat,level,time)我在下面显示 qu_l 的格式

<xarray.DataArray (time: 122, level: 20, latitude: 17, longitude: 13)>
array([[[[-4.19789273e-03, -4.12086723e-03, -4.09764051e-03, ...,
          -3.71682970e-03, -3.73217138e-03, -3.82781331e-03],
         [-4.56113275e-03, -4.45598038e-03, -4.42688400e-03, ...,
          -3.76262050e-03, -3.75747494e-03, -3.89366969e-03],
         [-4.71409503e-03, -4.65912791e-03, -4.63059498e-03, ...,
          -3.87056964e-03, -3.85122397e-03, -3.96054145e-03],
         ...,
         [-4.45869751e-03, -4.44581639e-03, -4.42417897e-03, ...,
          -3.94128077e-03, -3.80358030e-03, -3.72603862e-03],
         [-4.08468908e-03, -4.09023836e-03, -4.06995276e-03, ...,
          -3.74964159e-03, -3.62544949e-03, -3.51385027e-03],
         [-3.59571190e-03, -3.61532927e-03, -3.61628691e-03, ...,
          -3.30728292e-03, -3.25384876e-03, -3.16099427e-03]],

        [[-3.50524322e-03, -3.57510871e-03, -3.70393903e-03, ...,
          -5.11942152e-03, -5.17477514e-03, -5.19334339e-03],
         [-3.62999109e-03, -3.61768738e-03, -3.63567495e-03, ...,
          -4.97422507e-03, -4.89413133e-03, -4.91650915e-03],
         [-3.82309570e-03, -3.69889149e-03, -3.64601682e-03, ...,
          -5.00734057e-03, -4.79878346e-03, -4.74505918e-03],
...
           6.10205606e-02,  8.29948708e-02,  9.58984271e-02],
         [ 1.19615391e-01,  1.20305233e-01,  1.18257053e-01, ...,
           1.16345897e-01,  1.31085232e-01,  1.47202030e-01],
         [ 1.19616143e-01,  1.20933101e-01,  1.21104434e-01, ...,
           1.74180165e-01,  1.87850669e-01,  1.94131300e-01]],

        [[ 5.08086272e-02,  2.28128675e-02,  1.50085404e-03, ...,
           1.51794543e-02,  1.48344487e-02,  1.89040005e-02],
         [ 6.45153522e-02,  4.22009490e-02,  1.40351783e-02, ...,
           9.95836034e-03,  1.04583772e-02,  1.45543003e-02],
         [ 7.31716752e-02,  5.91978468e-02,  3.37371565e-02, ...,
           2.09518019e-02,  2.17951648e-02,  2.08518244e-02],
         ...,
         [ 1.16454110e-01,  1.15208797e-01,  1.13529585e-01, ...,
           3.19227539e-02,  4.88603711e-02,  7.77506232e-02],
         [ 1.16338752e-01,  1.16264150e-01,  1.15892082e-01, ...,
           7.18823373e-02,  9.30104330e-02,  1.29432485e-01],
         [ 1.16498962e-01,  1.16809532e-01,  1.18171051e-01, ...,
           1.51875705e-01,  1.67853177e-01,  1.79388359e-01]]]],
      dtype=float32)
Coordinates:
  * longitude  (longitude) float64 75.0 75.25 75.5 75.75 ... 77.5 77.75 78.0
  * latitude   (latitude) float64 12.0 11.75 11.5 11.25 ... 8.75 8.5 8.25 8.0
  * level      (level) float64 300.0 350.0 400.0 450.0 ... 950.0 975.0 1e+03
  * time       (time) datetime64[ns] 2012-06-01 2012-06-02 ... 2012-09-30 

qv_l 下面

<xarray.DataArray (time: 122, level: 20, latitude: 17, longitude: 13)>
array([[[[-6.47565699e-04, -6.92666334e-04, -7.11454137e-04, ...,
           3.43675456e-05,  7.93513536e-05,  9.99554468e-05],
         [-5.98114333e-04, -6.92268950e-04, -7.08624371e-04, ...,
           2.13778058e-05,  9.44525091e-05,  1.06804677e-04],
         [-4.73025109e-04, -5.89269039e-04, -6.22700143e-04, ...,
           6.42042323e-06,  4.46782178e-05,  5.74382866e-05],
         ...,
         [ 3.80350364e-04,  2.62222486e-04,  1.71314430e-04, ...,
           4.27812301e-06, -2.65385243e-05, -1.54171885e-05],
         [ 4.22323850e-04,  2.98410363e-04,  2.13784966e-04, ...,
           1.68663741e-04,  1.84557546e-04,  1.97871675e-04],
         [ 5.13842097e-04,  3.87011911e-04,  2.92879849e-04, ...,
           3.24941822e-04,  3.70173249e-04,  3.82417551e-04]],

        [[ 1.75689324e-03,  1.64970604e-03,  1.64240529e-03, ...,
           1.78657123e-03,  1.64312439e-03,  1.71366835e-03],
         [ 1.96785224e-03,  1.73268584e-03,  1.63920829e-03, ...,
           1.81653339e-03,  1.52228458e-03,  1.59630203e-03],
         [ 2.24626926e-03,  1.97174773e-03,  1.82870147e-03, ...,
           1.90237688e-03,  1.56884897e-03,  1.52667740e-03],
...
           4.07637330e-03,  1.11457016e-02,  3.93296406e-02],
         [-2.15423908e-02, -2.25263517e-02, -2.33091526e-02, ...,
          -9.87390056e-03,  1.77768152e-02,  5.02842739e-02],
         [-2.38218643e-02, -2.46686693e-02, -2.55033616e-02, ...,
          -2.89178826e-02,  6.09542197e-03,  3.83579805e-02]],

        [[ 2.17485931e-02,  3.29152979e-02,  2.30377540e-02, ...,
          -6.61660591e-03, -1.07659819e-03,  7.52496673e-03],
         [ 1.48354117e-02,  3.08191925e-02,  3.08241658e-02, ...,
           8.02161754e-04,  4.64674085e-03,  7.84640480e-03],
         [ 8.24724324e-03,  2.21066289e-02,  3.19518633e-02, ...,
           1.44633185e-02,  1.17855463e-02,  7.52754183e-03],
         ...,
         [-1.78987961e-02, -1.84495021e-02, -1.84885692e-02, ...,
           2.53087748e-03,  1.50247170e-02,  4.98900302e-02],
         [-2.08280329e-02, -2.19783485e-02, -2.26367787e-02, ...,
          -3.64710577e-03,  2.76481360e-02,  6.56655282e-02],
         [-2.32993308e-02, -2.39235573e-02, -2.48144343e-02, ...,
          -2.18581613e-02,  1.57092009e-02,  4.94669676e-02]]]],
      dtype=float32)
Coordinates:
  * longitude  (longitude) float64 75.0 75.25 75.5 75.75 ... 77.5 77.75 78.0
  * latitude   (latitude) float64 12.0 11.75 11.5 11.25 ... 8.75 8.5 8.25 8.0
  * level      (level) float64 300.0 350.0 400.0 450.0 ... 950.0 975.0 1e+03
  * time       (time) datetime64[ns] 2012-06-01 2012-06-02 ... 2012-09-30

为什么这个索引错误发生在metpy中。是不是因为metpy divergence module对4darray有问题

python data-analysis calculation atmosphere metpy
1个回答
0
投票

这里的问题是您手动传入

dx
dy
,它们来自
lat_lon_grid_deltas()
。因此,这些本质上是常规的 numpy 数组,需要使用标准 numpy 广播规则与
qu_l
qu_v
兼容。如果您希望这些工作正常,则需要添加前导、大小为 1 的维度,以使这些 2D 数组与 4D
qu_l
qv_l
配合使用,例如:

mpcalc.divergence(qu_l, qv_l, dx=dx[None, None], dy=dy[None, None])

或者最简单的是,由于您已经使用 xarray

DataArray
实例获得了数据,因此不要手动计算
dx
/
dy
,或者根本不传入它们。相反,只有 MetPy 可以为您处理:

mpcalc.divergence(qu_l, qv_l)
© www.soinside.com 2019 - 2024. All rights reserved.