通过使用 MetPy 的 XArray accessor 来简化基于“时间”和“垂直”等通用维度的选择,这个过程可以变得更加清晰。我们还可以获取使用 np.intersect1d
:
形成的公共级别上的值
import datetime
import metpy
import numpy as np
import xarray
xds0 = xarray.open_dataset('https://thredds.ucar.edu/thredds/dodsC/grib/NCEP/GFS/Global_onedeg/Best')
temp_da = xds0.metpy.parse_cf('Temperature_isobaric')
rh_da = xds0.metpy.parse_cf('Relative_humidity_isobaric')
# Formulate subset
subset0 = {'method' : 'nearest','lon': 238.,'lat': 38.,# Can use 'vertical' here if we pass to metpy's version of sel
# Use the array of values for pressure that are present in both
'vertical': np.intersect1d(temp_da.metpy.vertical,rh_da.metpy.vertical),'time': datetime.datetime.utcnow()}
temp = temp_da.metpy.sel(**subset0)
rh = rh_da.metpy.sel(**subset0)
# Need to rename the coordinate on RH to match that of temperature
rh = rh.rename({rh_da.metpy.vertical.name: temp_da.metpy.vertical.name})
# Get vertical coordinate (pressure) as a unitted array
p = temp.metpy.vertical.metpy.unit_array
# Calculate dewpoint
td = metpy.calc.dewpoint_from_relative_humidity(temp,rh)
# Plot on SkewT
skewt1 = metpy.plots.SkewT()
# For some reason matplotlib doesn't like the temp array with units inside
# a DataArray
skewt1.plot(p,temp.metpy.unit_array,'r')
skewt1.plot(p,td,'g')
上面的代码需要 MetPy 1.0 才能原生支持 XArray DataArray
。对于早期版本,对 dewpoint_from_relative_humidity
的调用应改为:
td = metpy.calc.dewpoint_from_relative_humidity(temp.metpy.unit_array,rh.metpy.unit_array)
本文链接:https://www.f2er.com/856461.html