在 Metpy 上绘制横截面分析时 ValueError 的原因

Cause of ValueError while plotting Cross Section Analysis on Metpy

我从这个网站得到示例代码:https://unidata.github.io/MetPy/latest/examples/cross_section.html#sphx-glr-examples-cross-section-py

我想知道为什么示例程序无法在我的计算机上使用 xarray-0.15.0 或 xarray-0.15.1 运行。

原代码如下

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

data = xr.open_dataset(get_test_data('narr_example.nc', False))
data = data.metpy.parse_cf().squeeze()
print(data)

start = (37.0, -105.0)
end = (35.5, -65.0)

cross = cross_section(data, start, end).set_coords(('lat', 'lon'))
print(cross)

temperature, pressure, specific_humidity = xr.broadcast(cross['Temperature'],
                                                        cross['isobaric'],
                                                        cross['Specific_humidity'])

theta = mpcalc.potential_temperature(pressure, temperature)
rh = mpcalc.relative_humidity_from_specific_humidity(pressure, temperature, specific_humidity)

# These calculations return unit arrays, so put those back into DataArrays in our Dataset
cross['Potential_temperature'] = xr.DataArray(theta,
                                              coords=temperature.coords,
                                              dims=temperature.dims,
                                              attrs={'units': theta.units})
cross['Relative_humidity'] = xr.DataArray(rh,
                                          coords=specific_humidity.coords,
                                          dims=specific_humidity.dims,
                                          attrs={'units': rh.units})

cross['u_wind'].metpy.convert_units('knots')
cross['v_wind'].metpy.convert_units('knots')
cross['t_wind'], cross['n_wind'] = mpcalc.cross_section_components(cross['u_wind'],
                                                                   cross['v_wind'])

print(cross)

错误信息如下(with xarray-0.15.1)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-506a7aabdc3e> in <module>
      1 data = xr.open_dataset(get_test_data('narr_example.nc', False))
----> 2 data = data.metpy.parse_cf().squeeze()
      3 print(data)

/opt/anaconda3/lib/python3.7/site-packages/metpy/xarray.py in parse_cf(self, varname, coordinates)
    502             # If non-string iterable is given, apply recursively across the varnames
    503             subset = xr.merge([self.parse_cf(single_varname, coordinates=coordinates)
--> 504                                for single_varname in varname])
    505             subset.attrs = self._dataset.attrs
    506             return subset

/opt/anaconda3/lib/python3.7/site-packages/metpy/xarray.py in <listcomp>(.0)
    502             # If non-string iterable is given, apply recursively across the varnames
    503             subset = xr.merge([self.parse_cf(single_varname, coordinates=coordinates)
--> 504                                for single_varname in varname])
    505             subset.attrs = self._dataset.attrs
    506             return subset

/opt/anaconda3/lib/python3.7/site-packages/metpy/xarray.py in parse_cf(self, varname, coordinates)
    518                 var.coords['crs'] = CFProjection(proj_var.attrs)
    519 
--> 520         self._fixup_coords(var)
    521 
    522         # Trying to guess whether we should be adding a crs to this variable's coordinates

/opt/anaconda3/lib/python3.7/site-packages/metpy/xarray.py in _fixup_coords(self, var)
    547                     and not check_axis(data_array, 'longitude', 'latitude')):
    548                 try:
--> 549                     var.coords[coord_name].metpy.convert_units('meters')
    550                 except DimensionalityError:  # Radians!
    551                     if 'crs' in var.coords:

/opt/anaconda3/lib/python3.7/site-packages/metpy/xarray.py in convert_units(self, units)
    143     def convert_units(self, units):
    144         """Convert the data values to different units in-place."""
--> 145         self.unit_array = self.unit_array.to(units)
    146 
    147     @property

/opt/anaconda3/lib/python3.7/site-packages/metpy/xarray.py in unit_array(self, values)
    138     def unit_array(self, values):
    139         """Set data values from a `pint.Quantity`."""
--> 140         self._data_array.values = values.magnitude
    141         self._units = self._data_array.attrs['units'] = str(values.units)
    142 

/opt/anaconda3/lib/python3.7/site-packages/xarray/core/common.py in __setattr__(self, name, value)
    260         """
    261         try:
--> 262             object.__setattr__(self, name, value)
    263         except AttributeError as e:
    264             # Don't accidentally shadow custom AttributeErrors, e.g.

/opt/anaconda3/lib/python3.7/site-packages/xarray/core/dataarray.py in values(self, value)
    560     @values.setter
    561     def values(self, value: Any) -> None:
--> 562         self.variable.values = value
    563 
    564     @property

/opt/anaconda3/lib/python3.7/site-packages/xarray/core/variable.py in values(self, values)
   2113     def values(self, values):
   2114         raise ValueError(
-> 2115             f"Cannot assign to the .values attribute of dimension coordinate a.k.a IndexVariable {self.name!r}. "
   2116             f"Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate."
   2117         )

ValueError: Cannot assign to the .values attribute of dimension coordinate a.k.a IndexVariable 'y'. Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate.

错误信息如下(with xarray-0.15.0)

Traceback (most recent call last):
  File "cross_section.py", line 57, in <module>
    rh = mpcalc.relative_humidity_from_specific_humidity(pressure, temperature, specific_humidity)
  File "/opt/anaconda3/lib/python3.7/site-packages/metpy/xarray.py", line 655, in wrapper
    return func(*args, **kwargs)
  File "/opt/anaconda3/lib/python3.7/site-packages/metpy/units.py", line 319, in wrapper
    raise ValueError(msg)
ValueError: `relative_humidity_from_specific_humidity` given arguments with incorrect units: `specific_humidity` requires "[dimensionless]" but given "hectopascal", `pressure` requires "[pressure]" but given "dimensionless".

xarray 0.15.1 的问题是一个已知问题,应在下一版本中修复。

您在 0.15.0 中得到的错误肯定令人困惑。问题是您使用的示例代码来自文档,它指向 1.0 的文档,目前处于发布候选状态——这意味着您很可能不是 运行 它,但是MetPy 0.12。问题在于 MetPy 1.0 将参数顺序更改为 relative_humidity_from_specific_humidity。因此,您下载的示例代码不适合您 运行.

的 MetPy 版本

我更新了 MetPy 文档以将最新文档指向 0.12 版本文档。您现在应该可以下载适用于您的版本的 correct example code