【问题标题】:Load a grib2 file in Python在 Python 中加载 grib2 文件
【发布时间】:2016-10-26 11:04:57
【问题描述】:

我正在尝试加载 grib2 文件,但我不确定这些文件是否存在问题或我尝试加载它们的方式。

这些文件是从here 提取的(我没有从这里下载它们,而是从一个从这里获取它们的同事的文件夹中复制它们 - 所以它们实际上应该是相同的文件......,但我然后也尝试插入ftp地址,得到同样的错误)。

我累了pygrib

grbs = pygrib.open('pgbf2016060100.01.2016053100.grib2')

得到这个错误:

---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-20-674763ffdd1f> in <module>()
----> 1 grbs = pygrib.open('pgbf2016060100.01.2016053100.grib2')

pygrib.pyx in pygrib.open.__cinit__ (pygrib.c:2772)()

IOError: [Errno could not open %s] pgbf2016060100.01.2016053100.grib2

我试过xarray

ds = xr.open_dataset("pgbf2016060100.01.2016053100.grb2",engine='pynio')

我得到了这个错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/software/centos6/x86_64/canopy-1.5.2/Canopy_64bit/User/lib/python2.7/site-    packages/IPython/core/interactiveshell.pyc in run_code(self, code_obj, result)
   3081             if result is not None:
   3082                 result.error_in_exec = sys.exc_info()[1]
-> 3083             self.showtraceback()
   3084         else:
   3085             outflag = 0

/software/centos6/x86_64/canopy-1.5.2/Canopy_64bit/User/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in showtraceback(self, exc_tuple,     filename, tb_offset, exception_only)
   1858                 return
   1859 
-> 1860             if issubclass(etype, SyntaxError):
   1861                 # Though this won't be called by syntax errors in the input
   1862                 # line, there may be SyntaxError cases with imported code.

TypeError: issubclass() arg 1 must be a class

我直接试了NIO

f = nio.open_file("pgbf2016060100.01.2016053100.grb2")

我得到与来自xarray 的错误相同的错误。

有什么建议吗?

【问题讨论】:

  • 通过 wgrib2 检查文件的完整性。并查看pygrib的配置文件,可能是安装时关闭了grib2支持,因为pygrib使用grib_api,没有grib2支持也可以编译grib_api库。

标签: python grib python-xarray


【解决方案1】:

pygrib 有这个问题。问题的根源是JPEG 支持,openjpegjasper 库有问题,经过多次尝试,我通过重新安装pygrib 来解决此问题:

  • 删除这个库:

    yum remove openjpegyum remove jasper(我使用 RHEL 7)

  • 使用以下命令重新安装此库后:

    yum install openjpegyum install jasper

  • 确保不再安装grib_api 或他的替代eccodes。之后,从源代码安装eccodes,按照这个instructions。安装目录对于下一步至关重要,在我的情况下,我将其安装在 /usr/local/lib/eccodes 中(我在 /usr/local/lib 中创建了一个名为 eccodes 的空目录),此目录在 cmake 期间指定,参数为 @987654338 @.

  • 最后从steps之后的源安装pygrib,我只取消setup.cfg中的第15行并设置grib_api_dir = /usr/local/lib/eccodes(这里是安装目录的重要性)。如果安装没有问题test.py问题已经解决了。

【讨论】:

  • 谢谢!我为 xarray 留下了 pygrib,但你有没有在 GitHub 存储库上打开 PR?这似乎是相关的。谢谢!
  • 不,我没有在 GitHub 存储库上打开拉取请求。此错误可能是由于安装openjpegjasper 时出现的问题。我会尝试使用xarray,看起来很棒而且简单。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 2019-03-26
  • 2017-10-15
  • 2013-01-10
  • 1970-01-01
  • 2019-04-21
  • 2016-10-05
相关资源
最近更新 更多