我从这个网站上得到了示例代码:https://unidata.github.io/MetPy/latest/examples/cross_section.html#sphx-glr示例截面py
我想知道为什么示例程序不能在我的计算机上使用xarray-0.15.0或xarray-015.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)
错误信息如下(使用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.
错误信息如下(使用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的文档,该文档目前处于候选发布状态——这意味着您很可能没有运行它,而是MetPy0.12。问题是MetPy 1.0将参数的顺序更改为relative_humidity_from_specific_humidity
。因此,您下载的示例代码不适合您正在运行的MetPy版本。
我已经更新了MetPy文档,将最新的文档指向0.12版本的文档。现在,您应该能够下载适用于您的版本的正确示例代码。