【问题标题】:Errors when using metpy to calculate specific humidity使用metpy计算比湿度时出错
【发布时间】:2021-09-07 20:23:05
【问题描述】:

我想使用metpy 使用 ERA5 每小时再分析数据计算近地表(即 2m)比湿度。我昨天刚刚通过 pip 在本地安装了metpy,所以我假设我的代码是最新的。我的问题是我不断遇到以下错误。

这是我目前的代码:

# import modules
import numpy as np
import xarray as xr
from metpy.units import units
import metpy.calc as mpcalc

# read data
d2m = xr.open_dataset('netcdf/ERA5_dewpt2m_1992.nc')
sp = xr.open_dataset('netcdf/ERA5_pres_1992.nc')

# assign units (approach 1)
#d2m = d2m*units.kelvin
#sp = sp*units.pascal

# assign units (approach 2)
d2m = units.Quantity(d2m, "kelvin")
sp = units.Quantity(sp, "pascal")

# calculate specific humidity
aqh2m = mpcalc.specific_humidity_from_dewpoint(sp, d2m)

如果我忽略“单位”步骤,那么该功能当然会抱怨缺少单位。请注意,我已经尝试了两种不同的方法来处理上述单位,但它们都不起作用。

如果我尝试这种方法:

# assign units (approach 1)
d2m = d2m*units.kelvin
sp = sp*units.pascal

# calculate specific humidity
aqh2m = mpcalc.specific_humidity_from_dewpoint(sp, d2m)

然后我收到以下错误:

ValueError: This function changed in 1.0--double check that the function is being called properly.
`specific_humidity_from_dewpoint` given arguments with incorrect units: `pressure` requires "[pressure]" but given "none", `dewpoint` requires "[temperature]" but given "none"
Any variable `x` can be assigned a unit as follows:
    from metpy.units import units
    x = units.Quantity(x, "m/s")

但是,如果我采用错误消息中建议的单元分配方法,即:

# assign units (approach 2)
d2m = units.Quantity(d2m, "kelvin")
sp = units.Quantity(sp, "pascal")

# calculate specific humidity
aqh2m = mpcalc.specific_humidity_from_dewpoint(sp, d2m)

然后我只是收到一条不同的错误消息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-86573b0b8029> in <module>()
----> 1 d2m = units.Quantity(d2m, "kelvin")
      2 sp = units.Quantity(sp, "pascal")
      3 
      4 # calculate specific humidity
      5 aqh2m = mpcalc.specific_humidity_from_dewpoint(sp, d2m)

~/.local/lib/python3.6/site-packages/pint/quantity.py in __new__(cls, value, units)
    201     def __new__(cls, value, units=None):
    202         if is_upcast_type(type(value)):
--> 203             raise TypeError(f"Quantity cannot wrap upcast type {type(value)}")
    204         elif units is None:
    205             if isinstance(value, str):

TypeError: Quantity cannot wrap upcast type xarray.core.dataset.Dataset

我该怎么办?我不知道从这里去哪里。您可以提供的任何建议/指导将不胜感激。

仅供参考,这是我的各种模块的版本。我将全部列出,因为我不确定 metpy 在内部使用什么来支持单位:

  • python 3.6.3
  • numpy 1.19.5
  • xarray 0.16.2
  • 熊猫 1.1.5
  • 品脱 0.17
  • 狗 1.4.0
  • pyproj 3.0.1
  • matplotlib 2.1.2
  • traitlets 4.3.2
  • scipy 1.0.0

提前致谢。

【问题讨论】:

    标签: python netcdf python-xarray metpy era5


    【解决方案1】:

    units.Quantity 似乎无法处理 Xarray 对象。取而代之的是 xarray.DataArray 对象具有 Xarray.DataArray.metpy.quantify 方法将数据转换为metpy pint.Quantity (请注意,这会将非dask数组加载到内存中)。这会将单位从属性移动到数据。

    你可以这样:

    # import modules
    import numpy as np
    import xarray as xr
    from metpy.units import units
    import metpy.calc as mpcalc
    
    # read data
    d2m = xr.open_dataset('netcdf/ERA5_dewpt2m_1992.nc')
    sp = xr.open_dataset('netcdf/ERA5_pres_1992.nc')
    
    # assign units
    d2m = d2m.metpy.quantify()
    sp = sp.metpy.quantify()
    
    # calculate specific humidity
    aqh2m = mpcalc.specific_humidity_from_dewpoint(sp, d2m)
    

    我希望您的 ERA5 数据的格式符合 CF,所以这应该可以工作。如果没有,也许 Xarray 特定 Metpy 功能页面上的“不符合 CF 的数据集”部分可能会有所帮助:https://unidata.github.io/MetPy/latest/tutorials/xarray_tutorial.html

    【讨论】:

      【解决方案2】:

      您遇到的一个问题是xr.open_dataset() 的结果是Dataset,而不是DataArray,这是您真正想要用于MetPy 的计算函数的结果。一旦你有了这些,MetPy 应该能够自动处理带有单元元数据的DataArrays,例如:

      import xarray as xr
      import metpy.calc as mpcalc
      
      # read data
      d2m_ds = xr.open_dataset('netcdf/ERA5_dewpt2m_1992.nc')
      sp_ds = xr.open_dataset('netcdf/ERA5_pres_1992.nc')
      
      # Pull out DataArrays and ask MetPy to parse CF metadata
      d2m_arr = d2m_ds.metpy.parse_cf('dewpt2m')
      sp_arr = sp_ds.metpy.parse_cf('press')
      
      # calculate specific humidity
      aqh2m = mpcalc.specific_humidity_from_dewpoint(sp_arr, d2m_arr)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-18
        • 1970-01-01
        • 2018-09-02
        • 1970-01-01
        • 1970-01-01
        • 2021-07-13
        相关资源
        最近更新 更多