Python cfgrib无法识别数据变量
免责声明:我是python和争论气候数据集的初学者。我尽了最大的努力搜索网络,以找到一个解决方案,但运气不佳。我希望我提供了足够的信息 数据来源:Python cfgrib无法识别数据变量,python,python-xarray,grib,cfgrib,Python,Python Xarray,Grib,Cfgrib,免责声明:我是python和争论气候数据集的初学者。我尽了最大的努力搜索网络,以找到一个解决方案,但运气不佳。我希望我提供了足够的信息 数据来源: 使用2001年数据集中的第一个文件“MRMS_PrecipRate_00.00_20010101-120000.grib2” 问题:我知道grib文件中的'PrecipRate'属性无法识别,导致na数据值。我使用pygrib库确认了这些数据,但我需要这些数据与cgrib一起使用,这样我就可以使用“open_mfdataset”函数 import
- 使用2001年数据集中的第一个文件“MRMS_PrecipRate_00.00_20010101-120000.grib2”
import xarray as xr
ds = xr.open_dataset('MRMS_PrecipRate_00.00_20010101-120000.grib2', engine = 'cfgrib')
print(ds)
<xarray.Dataset>
Dimensions: (latitude: 3500, longitude: 7000)
Coordinates:
time datetime64[ns] 2001-01-01T12:00:00
step timedelta64[ns] 00:00:00
surface float64 0.0
* latitude (latitude) float64 54.99 54.98 54.98 54.97 ... 20.03 20.02 20.01
* longitude (longitude) float64 230.0 230.0 230.0 ... 300.0 300.0 300.0
valid_time datetime64[ns] 2001-01-01T12:00:00
Data variables:
unknown (latitude, longitude) float32 ...
Attributes:
GRIB_edition: 2
GRIB_centre: 161
GRIB_centreDescription: 161
GRIB_subCentre: 0
Conventions: CF-1.7
institution: 161
history: 2021-05-04T13:11 GRIB to CDM+CF via cfgrib-0.9.9...
print(ds['unknown'].values)
[[nan nan nan ... nan nan nan]
[nan nan nan ... nan nan nan]
[nan nan nan ... nan nan nan]
...
[nan nan nan ... nan nan nan]
[nan nan nan ... nan nan nan]
[nan nan nan ... nan nan nan]]
这是处理grib数据的常见故障。提取grib数据需要grib表。在这些表中,参数标识符被解释并映射到它们的名称 因此,您需要做的是与grib文件的发布者/提供者联系,并询问相应的grib表。我猜pygrib使用的是ncep的公共grib表 此外,我建议结合使用xarray。它基于ECMWF强大的ECCODE库。在这里,您必须为存储grib表的位置设置一个额外的
ECCODES\u definition\u路径
。
默认情况下,ECCODE可能包含正确的grib表。否则,请联系grib文件提供商
from metpy.units import units
import pygrib
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
grbs = pygrib.open('MRMS_PrecipRate_00.00_20010101-120000.grib2')
grb = grbs[1]
prcp = grb.values
prcp = units('mm/hr') * prcp
# plt.imshow(prcp.to('in/hr'))
# plt.colorbar()
lats, lons = grb.latlons()
map_crs = ccrs.LambertConformal(central_longitude=-100,
central_latitude = 35,
standard_parallels=(30, 60))
data_crs = ccrs.PlateCarree()
fig = plt.figure(1, figsize=(14,12))
ax = plt.subplot(projection = map_crs)
fig.subplots_adjust(hspace=0, wspace=0, top=0.925, left=0.1)
im = ax.contourf(lons, lats, prcp.to('in/hr'), transform=data_crs,
cmap = 'Greens')
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax.add_feature(cfeature.STATES.with_scale('50m'))
cb=plt.colorbar(im, shrink = 0.7, label='precipitation (in/hr)')
ax.set_extent([-85, -74, 34, 40.5], data_crs)
plt.show()