title: 加载/释放 GRIB 消息

weight: 3

需要使用如下函数:

  • codes_grib_new_from_file
  • codes_any_new_from_file
  • codes_new_from_file
  • codes_new_from_samples
  • codes_new_from_message
  • codes_release
  1. gid = codes_grib_new_from_file (file, headers_only=False)
  2. codes_any_new_from_file (file, headers_only=False)
  3. codes_new_from_file (file, product_kind, headers_only)

product_kind 接受如下值:

  • CODES_PRODUCT_GRIB
  • CODES_PRODUCT_BUFR
  • CODES_PRODUCT_ANY

上述的 codes_grib_new_from_file 函数返回 GRIB 消息的句柄。 输入文件必须是 Python 的文件对象。现在不推荐使用 headers_only 参数。

codes_new_from_samples

  1. gid = codes_new_from_samples (samplename)

返回样例目录的文件中消息的句柄。

codes_new_from_message

  1. gid = codes_new_from_message (message)

返回内存中消息的句柄。

codes_release

  1. codes_release (gid)

释放句柄。

示例:读取 GRIB 文件

  1. from __future__ import print_function
  2. import sys
  3. import click
  4. import eccodes
  5. @click.command()
  6. @click.argument('file_path')
  7. def cli(file_path):
  8. with open(file_path, 'rb') as f:
  9. handle = eccodes.codes_grib_new_from_file(f, headers_only=False)
  10. if handle is None:
  11. print("ERROR: unable to create handle from file " + file_path)
  12. sys.exit(-1)
  13. eccodes.codes_release(handle)
  14. if __name__ == "__main__":
  15. cli()