Skip to main content

使用 ecCodes 遵循 CF 约定将 GRIB 文件映射到 NetCDF 通用数据模型的 Python 接口。

项目描述

cfgrib:使用 ecCodes 遵循 CF 约定将 GRIB 文件映射到 NetCDF 通用数据模型的 Python 接口

https://img.shields.io/pypi/v/cfgrib.svg

遵循CF 约定将 GRIB 文件映射到 Unidata 的 Common Data Model v4 的Python 接口。高级 API 旨在支持xarray的 GRIB 引擎 ,它的灵感来自netCDF4-pythonh5netcdf。低级访问和解码是通过 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 的任何地方:LinuxMacOSWindows

  • 所有支持的平台上的 conda-forge 包,

  • 在内存使用和磁盘访问方面懒惰而有效地读取数据,

  • 允许通过xarraydask进行大于内存和分布式处理,

  • 支持将坐标转换为不同的数据模型和命名约定,

  • 支持将 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的所有只读特性,例如:

读取任意 GRIB 键

默认情况下, cfgrib从 GRIB 文件中读取一组有限的 ecCodes 识别键,并将它们公开为带有GRIB_前缀的DatasetDataArray属性。可以通过将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_nameunitsstored_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,则坐标的值必须完全匹配。例如,如果变量tz共享相同的步长坐标,则它们必须具有完全相同的步长集。打开不符合要求的 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关键字参数。

项目资源

发展

https://github.com/ecmwf/cfgrib

下载

https://pypi.org/project/cfgrib

用户支持

https://stackoverflow.com/search?q=cfgrib

代码质量

Codecov 的覆盖状态

贡献

主存储库托管在 GitHub 上,非常欢迎和赞赏测试、错误报告和贡献:

https://github.com/ecmwf/cfgrib

请参阅 CONTRIBUTING.rst 文档以获取最佳帮助方式。

主要开发人员:

主要贡献者:

另请参阅参与此项目的贡献者列表。

执照

版权所有 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)

  • 修复读取索引文件失败的问题。见#292

  • 允许通过 json 格式字符串或通过 -b 的 json 文件路径在 to_netcdf 可执行文件中提供后端 kwargs。见#288

  • 修复了使用 relpath() 可能导致 Windows 出现问题的问题。见#284

  • 修复 pathlib.Path 的传递。见#282

  • 修复了将合奏编号写入 GRIB 文件导致错误的问题。见#278

0.9.10.0 (2022-01-31)

  • 大型内部重构以添加对类似于 Metview的通用Fieldset的支持。#243

0.9.9.1 (2021-09-29)

  • 修复缺少extra_coords的插件界面。见#231

  • 修复extra_coords返回标量时的崩溃。见#238

  • 改进类型提示。#243需要。

0.9.9.0 (2021-04-09)

  • 依赖 ECMWF eccodes python 包来访问低级 ecCodes C 库,删除所有其他 GRIB 解码选项。参见:#95#14#204#147#141

  • 在索引生成期间和数据访问期间的许多性能改进。请参阅:#142#197

  • filter_by_keys现在可以选择ecCodes已知的所有键,而无需显式地将非默认键添加到read_keys。参见:#187

  • 包括对使用xarray 0.18+新后端 API的engine=”cfgrib”的支持。参见:#216

  • 修复了无法加载只有一个网格点的 GRIB 消息的问题。参见:#199

  • 在所有情况下将级别坐标解码为浮点数,修复了非整数级别的问题。参见:#195

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)

  • 添加了对indexingDateindexingTime时间坐标的支持。

  • lambert_azimuthal_equal_area网格现在作为二维数组返回。参见:#119

0.9.8.2 (2020-05-22)

  • 添加对在某些 GRIB 产品中使用的 MULTI-FIELD 消息的支持,以存储 风的uv分量(例如 GFS、NAM 等)。这是cfgrib中报告最多的一个错误,已经有两次尝试修复它失败。让我们看看第三次是否有魅力。请测试!参见:#45#76#111

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)

  • 添加支持以通过read_keys从 GRIB 文件中读取其他键,它们出现在变量attrs中,您可以对其进行filter_by_keys。这是用户知道他们感兴趣的附加键名称的所有问题的通用解决方案。请参阅:#89#101

0.9.7.1 (2019-07-08)

  • 修复尝试在 Windows 上编写 GRIB 时出现的 bytes-in-the-place-of-str 错误。见:#91

  • 在 open_datasets 中设置indexpath 请参阅:#93

0.9.7 (2019-05-27)

  • 大大改进的 cfgrib.open_datasets启发式现在可以读取更多异构 GRIB 文件。该函数现在是受支持的 API。请参阅:#63#66#73#75

  • 修复仅 Python 2 包上的 conda 依赖项,请参阅:#78

0.9.7rc1 (2019-05-14)

  • 根据xarray 0.12.0放弃对 Python 2 的支持。Python 2 用户将长期支持 0.9.6.x 系列。见:#69

  • 将内部 ecCodes 绑定 API 同步到 eccodes-python 中的 API。见:#81

  • 源代码已用black -S -l 99格式化。

  • 添加了对光谱坐标的初始支持。

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)

  • 通过conda安装ecCodes添加对Windows的支持。见:#7

  • 添加了 conda-forge包。见:#5

0.9.5.7 (2019-02-24)

  • 修复了计算非立方 GRIB 文件的建议filter_by_keys的严重错误。结果cfgrib.xarray_store.open_datasets没有找到文件中的所有变量。见:#54

  • 修复了变量命名中的一个严重错误,该错误可能会降低或混合变量的值。再次参见:#54

  • 重新打开#45,因为修复返回了错误的数据。现在我们回到删除除第一个之外的多字段中的所有变量。

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)

  • 修复在具有非维度坐标的数据集上使用cf2cdm.translate_coords时的崩溃。见:#41

  • 添加了可以将 GRIB 转换为 netCDF的cfgrib脚本。参见:#40

0.9.5 (2018-12-20)

  • 放弃对v0.11之前的xarray版本的支持以降低复杂性。(这实际上只是 v0.10.9)。见:#32

  • 通过 Conventions全局属性将数据声明为符合CF-1.7 。见:#36

  • 通过daskdask.distributed测试大于内存和分布式处理。见:#33

  • 通过cfgrib.to_grib将写入支持提升到Alpha。见:#18

  • 提供cf2cdm.translate_coords实用函数来转换符合 CF 的数据模型之间的坐标,由out_nameunitsstore_direction定义。见:#24

  • 提供cfgrib.__version__。见:#31

  • 当用户尝试打开不是 GRIB 的文件时引发更好的错误消息。见:#34

  • 为rotate_llrotate_gg gridType制作 2D 网格。见:#35

0.9.4.1 (2018-11-08)

  • 修复 PyPI 页面的格式。

0.9.4 (2018-11-08)

  • 以更健壮的方式为每组 index_keys保存一个索引文件。

  • 重构 CF 编码并将新的encode_cf选项添加到backend_kwargs。见:#23

  • 重构错误处理和忽略错误的选项(尚未详细记录)。见:#13

  • 不要在已安装的ecCode不完全支持的gridType上崩溃 请参阅:#27

  • 几个较小的错误修复和性能改进。

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 版本。

项目详情