使用 ecCodes 遵循 CF 约定将 GRIB 文件映射到 NetCDF 通用数据模型的 Python 接口。
项目描述
cfgrib:使用 ecCodes 遵循 CF 约定将 GRIB 文件映射到 NetCDF 通用数据模型的 Python 接口
遵循CF 约定将 GRIB 文件映射到 Unidata 的 Common Data Model v4 的Python 接口。高级 API 旨在支持xarray的 GRIB 引擎 ,它的灵感来自netCDF4-python 和h5netcdf。低级访问和解码是通过 ECMWF ecCodes 库和eccodes python 包执行的。
具有开发状态Beta的功能:
启用engine='cfgrib'选项以使用xarray读取 GRIB 文件,
读取大多数 GRIB 1 和 2 文件,包括带有cfgrib.open_datasets的异构文件,
支持所有现代版本的 Python 3.9、3.8、3.7 和 PyPy3,
支持 Python 2 的 0.9.6.x 系列将保持活跃并接收关键错误修正,
适用于 eccodes-python 的任何地方:Linux、MacOS和Windows
所有支持的平台上的 conda-forge 包,
在内存使用和磁盘访问方面懒惰而有效地读取数据,
允许通过xarray和dask进行大于内存和分布式处理,
支持将坐标转换为不同的数据模型和命名约定,
支持将 GRIB 文件的索引写入磁盘,以保存打开时的全文件扫描,
接受实现通用Fieldset接口的对象,如ADVANCED_USAGE.rst中所述。
工作正在进行中:
Beta 版安装了一个cfgrib实用程序,该实用程序可以将 GRIB 文件转换为_netcdf ,并可选择转换为特定的坐标数据模型,请参阅#40。
Alpha/Broken支持将精心设计的xarray.Dataset写入 GRIB1或 GRIB2 文件,请参阅下面的高级写入用法部分,#18 和#156。
限制:
数据变量的 CF 属性依赖于ecCodes ,
与坐标系/ gridType相关的任何事情都依赖于ecCodes,请参见#28。
安装
安装cfgrib及其所有二进制依赖项的最简单方法是通过Conda:
$ conda install -c conda-forge cfgrib
或者,如果您自己安装二进制依赖项,您可以从PyPI安装 Python 包:
$ pip install cfgrib
二进制依赖
cfgrib依赖于eccodes python 包 来访问 ECMWF ecCodes二进制库,当不使用conda时,请遵循那里的系统依赖项部分。
您可以运行一个简单的 selfcheck 命令来确保您的系统设置正确:
$ python -m cfgrib selfcheck Found: ecCodes v2.20.0. Your system is ready.
用法
首先,您需要一个格式良好的 GRIB 文件,如果您手头没有,可以下载我们的 ERA5 压力级别示例:
$ wget http://download.ecmwf.int/test-data/cfgrib/era5-levels-members.grib
只读xarray GRIB 引擎
大多数cfgrib用户希望将 GRIB 文件作为xarray.Dataset打开,并且需要安装xarray:
$ pip install xarray
在 Python 解释器中尝试:
>>> import xarray as xr >>> ds = xr.open_dataset('era5-levels-members.grib', engine='cfgrib') >>> ds <xarray.Dataset> Dimensions: (number: 10, time: 4, isobaricInhPa: 2, latitude: 61, longitude: 120) Coordinates: * number (number) int64 0 1 2 3 4 5 6 7 8 9 * time (time) datetime64[ns] 2017-01-01 ... 2017-01-02T12:00:00 step timedelta64[ns] ... * isobaricInhPa (isobaricInhPa) float64 850.0 500.0 * latitude (latitude) float64 90.0 87.0 84.0 81.0 ... -84.0 -87.0 -90.0 * longitude (longitude) float64 0.0 3.0 6.0 9.0 ... 351.0 354.0 357.0 valid_time (time) datetime64[ns] ... Data variables: z (number, time, isobaricInhPa, latitude, longitude) float32 ... t (number, time, isobaricInhPa, latitude, longitude) float32 ... Attributes: GRIB_edition: 1 GRIB_centre: ecmf GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts GRIB_subCentre: 0 Conventions: CF-1.7 institution: European Centre for Medium-Range Weather Forecasts history: ...
cfgrib 引擎支持xarray的所有只读特性,例如:
使用xarray.open_mfdataset将多个 GRIB 文件的内容合并到一个数据集中,
使用dask处理大于内存的数据集,
允许使用dask.distributed进行分布式处理。
读取任意 GRIB 键
默认情况下, cfgrib从 GRIB 文件中读取一组有限的 ecCodes 识别键,并将它们公开为带有GRIB_前缀的Dataset或DataArray属性。可以通过将read_keys字典键添加到backend_kwargs来让cfgrib读取属性的其他键,其 值是所需的 GRIB 键列表:
>>> ds = xr.open_dataset('era5-levels-members.grib', engine='cfgrib', ... backend_kwargs={'read_keys': ['experimentVersionNumber']}) >>> ds.t.attrs['GRIB_experimentVersionNumber'] '0001'
转换为自定义数据模型
与 netCDF 不同,GRIB 数据格式不是自描述的,映射到Unidata 通用数据模型的一些细节由解码格式的软件组件任意设置。坐标的名称和单位等细节特别重要,因为 xarray广播和选择规则取决于它们。 cf2cfm是一个与cfgrib一起分发的小型坐标转换模块,它可以轻松地将符合 CF 的坐标(如cfgrib提供的坐标)转换为具有 set out_name、units和stored_direction的用户定义的自定义数据模型。
例如,要将cfgrib样式的xr.Dataset转换为经典的ECMWF坐标命名约定,您可以:
>>> import cf2cdm >>> ds = xr.open_dataset('era5-levels-members.grib', engine='cfgrib') >>> cf2cdm.translate_coords(ds, cf2cdm.ECMWF) <xarray.Dataset> Dimensions: (number: 10, time: 4, level: 2, latitude: 61, longitude: 120) Coordinates: * number (number) int64 0 1 2 3 4 5 6 7 8 9 * time (time) datetime64[ns] 2017-01-01 ... 2017-01-02T12:00:00 step timedelta64[ns] ... * level (level) float64 850.0 500.0 * latitude (latitude) float64 90.0 87.0 84.0 81.0 ... -84.0 -87.0 -90.0 * longitude (longitude) float64 0.0 3.0 6.0 9.0 ... 348.0 351.0 354.0 357.0 valid_time (time) datetime64[ns] ... Data variables: z (number, time, level, latitude, longitude) float32 ... t (number, time, level, latitude, longitude) float32 ... Attributes: GRIB_edition: 1 GRIB_centre: ecmf GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts GRIB_subCentre: 0 Conventions: CF-1.7 institution: European Centre for Medium-Range Weather Forecasts history: ...
要转换为气候数据存储的通用数据模型,请使用:
>>> import cf2cdm >>> cf2cdm.translate_coords(ds, cf2cdm.CDS) <xarray.Dataset> Dimensions: (realization: 10, forecast_reference_time: 4, plev: 2, lat: 61, lon: 120) Coordinates: * realization (realization) int64 0 1 2 3 4 5 6 7 8 9 * forecast_reference_time (forecast_reference_time) datetime64[ns] 2017-01... leadtime timedelta64[ns] ... * plev (plev) float64 8.5e+04 5e+04 * lat (lat) float64 -90.0 -87.0 -84.0 ... 84.0 87.0 90.0 * lon (lon) float64 0.0 3.0 6.0 9.0 ... 351.0 354.0 357.0 time (forecast_reference_time) datetime64[ns] ... Data variables: z (realization, forecast_reference_time, plev, lat, lon) float32 ... t (realization, forecast_reference_time, plev, lat, lon) float32 ... Attributes: GRIB_edition: 1 GRIB_centre: ecmf GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts GRIB_subCentre: 0 Conventions: CF-1.7 institution: European Centre for Medium-Range Weather Forecasts history: ...
过滤异构 GRIB 文件
只有当具有相同短名称的所有消息都可以表示为单个超立方体时, xr.open_dataset才能打开 GRIB 文件。例如,变量t不能同时具有isobaricInhPa和混合 typeOfLevel,因为这会导致同一个变量有多个超立方体。打开不符合要求的 GRIB 文件将失败,并出现ValueError: multiple values for unique key... 错误消息,请参阅#2。
此外,如果不同的变量依赖于相同的坐标,例如step,则坐标的值必须完全匹配。例如,如果变量t和z共享相同的步长坐标,则它们必须具有完全相同的步长集。打开不符合要求的 GRIB 文件将失败,并出现ValueError: key present and new value is different... 错误消息,请参阅#13。
在大多数情况下,您可以通过在 backend_kwargs 中传递 filter_by_keys 键来处理包含异构消息的复杂 GRIB 文件,以选择哪些GRIB 消息属于一组格式良好的超立方体。
例如,要打开 美国国家气象局复杂的 GRIB2 文件 ,您可以使用:
>>> xr.open_dataset('nam.t00z.awp21100.tm00.grib2', engine='cfgrib', ... backend_kwargs={'filter_by_keys': {'typeOfLevel': 'surface'}}) <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] ... step timedelta64[ns] ... surface float64 ... latitude (y, x) float64 ... longitude (y, x) float64 ... valid_time datetime64[ns] ... Dimensions without coordinates: y, x Data variables: gust (y, x) float32 ... sp (y, x) float32 ... orog (y, x) float32 ... tp (y, x) float32 ... acpcp (y, x) float32 ... csnow (y, x) float32 ... cicep (y, x) float32 ... cfrzr (y, x) float32 ... crain (y, x) float32 ... cape (y, x) float32 ... cin (y, x) float32 ... hpbl (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP... history: ... >>> xr.open_dataset('nam.t00z.awp21100.tm00.grib2', engine='cfgrib', ... backend_kwargs={'filter_by_keys': {'typeOfLevel': 'heightAboveGround', 'level': 2}}) <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] ... step timedelta64[ns] ... heightAboveGround float64 ... latitude (y, x) float64 ... longitude (y, x) float64 ... valid_time datetime64[ns] ... Dimensions without coordinates: y, x Data variables: t2m (y, x) float32 ... r2 (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP... history: ...
自动过滤
cfgrib还提供了一个函数,可以自动选择适当的filter_by_keys并返回GRIB 文件 中所有有效xarray.Dataset的列表。
>>> import cfgrib >>> cfgrib.open_datasets('nam.t00z.awp21100.tm00.grib2') [<xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 atmosphereSingleLayer float64 0.0 latitude (y, x) float64 ... longitude (y, x) float64 ... valid_time datetime64[ns] ... Dimensions without coordinates: y, x Data variables: pwat (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 cloudBase float64 0.0 latitude (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29 longitude (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6 valid_time datetime64[ns] 2018-09-17 Dimensions without coordinates: y, x Data variables: pres (y, x) float32 ... gh (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 cloudTop float64 0.0 latitude (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29 longitude (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6 valid_time datetime64[ns] 2018-09-17 Dimensions without coordinates: y, x Data variables: pres (y, x) float32 ... t (y, x) float32 ... gh (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 heightAboveGround float64 10.0 latitude (y, x) float64 ... longitude (y, x) float64 ... valid_time datetime64[ns] ... Dimensions without coordinates: y, x Data variables: u10 (y, x) float32 ... v10 (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 heightAboveGround float64 2.0 latitude (y, x) float64 12.19 12.39 12.58 ... 57.68 57.49 57.29 longitude (y, x) float64 226.5 227.2 227.9 ... 308.5 309.6 310.6 valid_time datetime64[ns] 2018-09-17 Dimensions without coordinates: y, x Data variables: t2m (y, x) float32 ... r2 (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (heightAboveGroundLayer: 2, y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 * heightAboveGroundLayer (heightAboveGroundLayer) float64 1e+03 3e+03 latitude (y, x) float64 ... longitude (y, x) float64 ... valid_time datetime64[ns] ... Dimensions without coordinates: y, x Data variables: hlcy (heightAboveGroundLayer, y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (isobaricInhPa: 19, y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 * isobaricInhPa (isobaricInhPa) float64 1e+03 950.0 900.0 ... 150.0 100.0 latitude (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29 longitude (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6 valid_time datetime64[ns] 2018-09-17 Dimensions without coordinates: y, x Data variables: t (isobaricInhPa, y, x) float32 ... u (isobaricInhPa, y, x) float32 ... v (isobaricInhPa, y, x) float32 ... w (isobaricInhPa, y, x) float32 ... gh (isobaricInhPa, y, x) float32 ... r (isobaricInhPa, y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (isobaricInhPa: 5, y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 * isobaricInhPa (isobaricInhPa) float64 1e+03 850.0 700.0 500.0 250.0 latitude (y, x) float64 ... longitude (y, x) float64 ... valid_time datetime64[ns] ... Dimensions without coordinates: y, x Data variables: absv (isobaricInhPa, y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 isothermZero float64 0.0 latitude (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29 longitude (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6 valid_time datetime64[ns] 2018-09-17 Dimensions without coordinates: y, x Data variables: gh (y, x) float32 ... r (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 maxWind float64 0.0 latitude (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29 longitude (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6 valid_time datetime64[ns] 2018-09-17 Dimensions without coordinates: y, x Data variables: pres (y, x) float32 ... u (y, x) float32 ... v (y, x) float32 ... gh (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 meanSea float64 0.0 latitude (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29 longitude (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6 valid_time datetime64[ns] 2018-09-17 Dimensions without coordinates: y, x Data variables: prmsl (y, x) float32 ... mslet (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (pressureFromGroundLayer: 2, y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 * pressureFromGroundLayer (pressureFromGroundLayer) float64 9e+03 1.8e+04 latitude (y, x) float64 12.19 12.39 12.58 ... 57.49 57.29 longitude (y, x) float64 226.5 227.2 227.9 ... 309.6 310.6 valid_time datetime64[ns] 2018-09-17 Dimensions without coordinates: y, x Data variables: cape (pressureFromGroundLayer, y, x) float32 ... cin (pressureFromGroundLayer, y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (pressureFromGroundLayer: 5, y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 * pressureFromGroundLayer (pressureFromGroundLayer) float64 3e+03 ... 1.5e+04 latitude (y, x) float64 12.19 12.39 12.58 ... 57.49 57.29 longitude (y, x) float64 226.5 227.2 227.9 ... 309.6 310.6 valid_time datetime64[ns] 2018-09-17 Dimensions without coordinates: y, x Data variables: t (pressureFromGroundLayer, y, x) float32 ... u (pressureFromGroundLayer, y, x) float32 ... v (pressureFromGroundLayer, y, x) float32 ... r (pressureFromGroundLayer, y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 pressureFromGroundLayer float64 3e+03 latitude (y, x) float64 ... longitude (y, x) float64 ... valid_time datetime64[ns] ... Dimensions without coordinates: y, x Data variables: pli (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 pressureFromGroundLayer float64 1.8e+04 latitude (y, x) float64 ... longitude (y, x) float64 ... valid_time datetime64[ns] ... Dimensions without coordinates: y, x Data variables: 4lftx (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 surface float64 0.0 latitude (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29 longitude (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6 valid_time datetime64[ns] 2018-09-17 Dimensions without coordinates: y, x Data variables: cape (y, x) float32 ... sp (y, x) float32 ... acpcp (y, x) float32 ... cin (y, x) float32 ... orog (y, x) float32 ... tp (y, x) float32 ... crain (y, x) float32 ... cfrzr (y, x) float32 ... cicep (y, x) float32 ... csnow (y, x) float32 ... gust (y, x) float32 ... hpbl (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP , <xarray.Dataset> Dimensions: (y: 65, x: 93) Coordinates: time datetime64[ns] 2018-09-17 step timedelta64[ns] 00:00:00 tropopause float64 0.0 latitude (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29 longitude (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6 valid_time datetime64[ns] 2018-09-17 Dimensions without coordinates: y, x Data variables: t (y, x) float32 ... u (y, x) float32 ... v (y, x) float32 ... trpp (y, x) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: kwbc GRIB_centreDescription: US National Weather Service - NCEP... GRIB_subCentre: 0 Conventions: CF-1.7 institution: US National Weather Service - NCEP ]
高级用法
写支持
请注意,写支持是 Alpha。 目前只能保存规范形式的xarray.Dataset,即坐标名称与cfgrib坐标完全匹配:
>>> from cfgrib.xarray_to_grib import to_grib >>> ds = xr.open_dataset('era5-levels-members.grib', engine='cfgrib').sel(number=0) >>> ds <xarray.Dataset> Dimensions: (time: 4, isobaricInhPa: 2, latitude: 61, longitude: 120) Coordinates: number int64 0 * time (time) datetime64[ns] 2017-01-01 ... 2017-01-02T12:00:00 step timedelta64[ns] ... * isobaricInhPa (isobaricInhPa) float64 850.0 500.0 * latitude (latitude) float64 90.0 87.0 84.0 81.0 ... -84.0 -87.0 -90.0 * longitude (longitude) float64 0.0 3.0 6.0 9.0 ... 351.0 354.0 357.0 valid_time (time) datetime64[ns] ... Data variables: z (time, isobaricInhPa, latitude, longitude) float32 ... t (time, isobaricInhPa, latitude, longitude) float32 ... Attributes: GRIB_edition: 1 GRIB_centre: ecmf GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts GRIB_subCentre: 0 Conventions: CF-1.7 institution: European Centre for Medium-Range Weather Forecasts history: ... >>> to_grib(ds, 'out1.grib', grib_keys={'edition': 2}) >>> xr.open_dataset('out1.grib', engine='cfgrib') <xarray.Dataset> Dimensions: (time: 4, isobaricInhPa: 2, latitude: 61, longitude: 120) Coordinates: number ... * time (time) datetime64[ns] 2017-01-01 ... 2017-01-02T12:00:00 step timedelta64[ns] ... * isobaricInhPa (isobaricInhPa) float64 850.0 500.0 * latitude (latitude) float64 90.0 87.0 84.0 81.0 ... -84.0 -87.0 -90.0 * longitude (longitude) float64 0.0 3.0 6.0 9.0 ... 351.0 354.0 357.0 valid_time (time) datetime64[ns] ... Data variables: z (time, isobaricInhPa, latitude, longitude) float32 ... t (time, isobaricInhPa, latitude, longitude) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: ecmf GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts GRIB_subCentre: 0 Conventions: CF-1.7 institution: European Centre for Medium-Range Weather Forecasts history: ...
可以通过使用GRIB_前缀的键设置attrs变量来设置每个变量的 GRIB 键,例如:
>>> import numpy as np >>> import xarray as xr >>> ds2 = xr.DataArray( ... np.zeros((5, 6)) + 300., ... coords=[ ... np.linspace(90., -90., 5), ... np.linspace(0., 360., 6, endpoint=False), ... ], ... dims=['latitude', 'longitude'], ... ).to_dataset(name='skin_temperature') >>> ds2.skin_temperature.attrs['GRIB_shortName'] = 'skt' >>> to_grib(ds2, 'out2.grib') >>> xr.open_dataset('out2.grib', engine='cfgrib') <xarray.Dataset> Dimensions: (latitude: 5, longitude: 6) Coordinates: time datetime64[ns] ... step timedelta64[ns] ... surface float64 ... * latitude (latitude) float64 90.0 45.0 0.0 -45.0 -90.0 * longitude (longitude) float64 0.0 60.0 120.0 180.0 240.0 300.0 valid_time datetime64[ns] ... Data variables: skt (latitude, longitude) float32 ... Attributes: GRIB_edition: 2 GRIB_centre: consensus GRIB_centreDescription: Consensus GRIB_subCentre: 0 Conventions: CF-1.7 institution: Consensus history: ...
数据集/变量 API
xarray的使用不是强制性的,您可以使用 Python 解释器中的高级 API 作为超立方体访问 GRIB 文件的内容:
>>> ds = cfgrib.open_file('era5-levels-members.grib') >>> ds.attributes['GRIB_edition'] 1 >>> sorted(ds.dimensions.items()) [('isobaricInhPa', 2), ('latitude', 61), ('longitude', 120), ('number', 10), ('time', 4)] >>> sorted(ds.variables) ['isobaricInhPa', 'latitude', 'longitude', 'number', 'step', 't', 'time', 'valid_time', 'z'] >>> var = ds.variables['t'] >>> var.dimensions ('number', 'time', 'isobaricInhPa', 'latitude', 'longitude') >>> var.data[:, :, :, :, :].mean() 262.92133 >>> ds = cfgrib.open_file('era5-levels-members.grib') >>> ds.attributes['GRIB_edition'] 1 >>> sorted(ds.dimensions.items()) [('isobaricInhPa', 2), ('latitude', 61), ('longitude', 120), ('number', 10), ('time', 4)] >>> sorted(ds.variables) ['isobaricInhPa', 'latitude', 'longitude', 'number', 'step', 't', 'time', 'valid_time', 'z'] >>> var = ds.variables['t'] >>> var.dimensions ('number', 'time', 'isobaricInhPa', 'latitude', 'longitude') >>> var.data[:, :, :, :, :].mean() 262.92133
GRIB 索引文件
默认情况下, cfgrib将 GRIB 文件的索引保存到磁盘,并将.idx附加 到 GRIB 文件名。索引文件是一项实验性且完全可选的功能,请随时删除它们并在出现问题时重试。索引文件保存可以禁用将indexpath=''添加到backend_kwargs关键字参数。
项目资源
发展 |
|
下载 |
|
用户支持 |
|
代码质量 |
贡献
主存储库托管在 GitHub 上,非常欢迎和赞赏测试、错误报告和贡献:
https://github.com/ecmwf/cfgrib
请参阅 CONTRIBUTING.rst 文档以获取最佳帮助方式。
主要开发人员:
Baudouin Raoult - ECMWF
主要贡献者:
Aurelana Barghini - B公开赛
莱昂纳多·巴卡罗利- B-Open
另请参阅参与此项目的贡献者列表。
执照
版权所有 2017-2021 欧洲中期天气预报中心 (ECMWF)。
根据 Apache 许可证 2.0 版(“许可证”)获得许可;除非遵守许可,否则您不得使用此文件。您可以在以下网址获取许可证副本:http: //www.apache.org/licenses/LICENSE-2.0。除非适用法律要求或书面同意,否则根据许可分发的软件将按“原样”分发,没有任何明示或暗示的保证或条件。有关许可下的特定语言管理权限和限制,请参阅许可。
cfgrib 的变更日志
0.9.10.2(未发布)
什么都没有改变。
0.9.10.1 (2022-03-16)
0.9.10.0 (2022-01-31)
大型内部重构以添加对类似于 Metview的通用Fieldset的支持。见#243。
0.9.9.1 (2021-09-29)
0.9.9.0 (2021-04-09)
0.9.8.5 (2020-11-11)
发生错误时更简单、更清晰的消息。
如果存在,请使用ECCODES_DIR环境变量。由 xavierabellan从eccodes-python 移植。参见:#162。
在设置CFGRIB_USE_EXTERNAL_ECCODES_BINDINGS=1时修复使用当前 ecCodes 绑定。
0.9.8.4 (2020-08-03)
如果存在,请使用ecmwflibs来查找ecCodes安装。
0.9.8.3 (2020-06-25)
添加了对indexingDate、indexingTime时间坐标的支持。
lambert_azimuthal_equal_area网格现在作为二维数组返回。参见:#119。
0.9.8.2 (2020-05-22)
0.9.8.1 (2020-03-13)
始终以二进制模式打开 GRIB 文件,作者 @b8raoult
0.9.8.0 (2020-03-12)
通过 @b8raoult 添加对实验性 pyeccodes 低级驱动程序的支持
0.9.7.7 (2020-01-24)
在cf2cdm.translate_coords中添加对forecastMonth的支持。
0.9.7.6 (2019-12-05)
修复自述文件。
0.9.7.5 (2019-12-05)
弃用ensure_valid_time和配置选项preferred_time_dimension现在可以通过time_dims更好地处理。
0.9.7.4 (2019-11-22)
添加更多选项到time_dims预测产品可以表示为 ('time', 'verifying_time')或('time', 'forecastMonth')。见:#97。
0.9.7.3 (2019-11-04)
添加对通过time_dims选择时间坐标以用作维度的支持。预测产品可以表示为('time', 'step')(默认)、 ('time', 'valid_time')或('valid_time', 'step')。见:#97。
减少FieldIndex的内存占用和.idx文件的大小。
0.9.7.2 (2019-09-24)
0.9.7.1 (2019-07-08)
0.9.7 (2019-05-27)
0.9.7rc1 (2019-05-14)
0.9.6.2 (2019-04-15)
改进将变量合并到数据集中。见:#63。
0.9.6.1.post1 (2019-03-17)
修复 README 格式的问题。
0.9.6.1 (2019-03-17)
固定(真实)多字段消息,请参阅:#45。
向索引文件添加了协议版本。必须删除旧的*.idx文件。
0.9.6.post1 (2019-03-07)
修正 README 中的一个重要错字。见:#64。
0.9.6 (2019-02-26)
0.9.5.7 (2019-02-24)
0.9.5.6 (2019-02-04)
不要以单位设置显式时区,以避免某些版本的xarray崩溃。见:#44。
0.9.5.5 (2019-02-02)
默认情况下启用 ecCodes 隐式多字段支持,NCEP 的 NAM 产品需要。见:#45。
添加了对depthBelowLand坐标的支持。
0.9.5.4 (2019-01-25)
添加对从错误的时间步长超立方体构建valid_time的支持。
0.9.5.3 (2019-01-25)
convert is valid_time也可以索引translate_coords中的所有时间和步骤。
0.9.5.2 (2019-01-24)
将valid_time设置为 CDS 数据模型的首选时间维度。
当找不到更好的选项时,回退到使用通用GRIB2 ecCodes模板。见:#39。
0.9.5.1 (2018-12-27)
0.9.5 (2018-12-20)
放弃对v0.11之前的xarray版本的支持以降低复杂性。(这实际上只是 v0.10.9)。见:#32。
通过 Conventions全局属性将数据声明为符合CF-1.7 。见:#36。
通过dask和dask.distributed测试大于内存和分布式处理。见:#33。
通过cfgrib.to_grib将写入支持提升到Alpha。见:#18。
提供cf2cdm.translate_coords实用函数来转换符合 CF 的数据模型之间的坐标,由out_name、units和store_direction定义。见:#24。
提供cfgrib.__version__。见:#31。
当用户尝试打开不是 GRIB 的文件时引发更好的错误消息。见:#34。
为rotate_ll和rotate_gg gridType制作 2D 网格。见:#35。
0.9.4.1 (2018-11-08)
修复 PyPI 页面的格式。
0.9.4 (2018-11-08)
0.9.3.1 (2018-10-28)
各种 README 修复,特别是将索引文件支持宣传为 alpha。
0.9.3 (2018-10-28)
性能大幅提升:添加 alpha 支持以在第一次打开时将全文件扫描生成的 GRIB 索引保存到磁盘和从磁盘读取。见:#20。
0.9.2 (2018-10-22)
将坐标air_pressure重命名为isobaricInhPa以与所有其他垂直水平坐标保持一致。见:#25。
0.9.1.post1 (2018-10-19)
修复 PyPI 描述。
0.9.1 (2018-10-19)
更改 cfgrib.open_dataset 的用法以将其与xarray.open_dataset 对齐,特别是filter_by_key必须添加到backend_kwargs字典中。见:#21。
0.9.0 (2018-10-14)
具有读取支持的 Beta 版本。