如何让 Pygrib 从网站读取 grib2

问题描述 投票:0回答:1

我正在尝试阅读此文件:https://www.ncei.noaa.gov/data/north-american-mesoscale-model/access/forecast/202212/20221216/nam_218_20221216_1200_000.grb2使用Google进行实验科拉布。该链接会引导您从 NOAA 网站下载 .grb2 数据集。 但是,我在代码中遇到此错误:

/usr/local/lib/python3.10/site-packages/pyproj/__init__.py:89: UserWarning: pyproj unable to set database path.
  _pyproj_global_context_initialize()
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-2-54c5584b42fe> in <cell line: 10>()
      8 gribpath='https://www.ncei.noaa.gov/data/north-american-mesoscale-model/access/forecast/202212/20221216/nam_218_20221216_1200_000.grb2'
      9 # grbs = pygrib.open(gribpath)
---> 10 grbs = pygrib.open(gribpath).read()
     11 # gpath=gribpath.read()
     12 # grb = pygrib.fromstring(gpath)

src/pygrib/_pygrib.pyx in pygrib._pygrib.open.__cinit__()

OSError: [Errno could not open %s] https://www.ncei.noaa.gov/data/north-american-mesoscale-model/access/forecast/202212/20221216/nam_218_20221216_1200_000.grb2

我的代码是

import time
import numpy
import pygrib

gribpath='https://www.ncei.noaa.gov/data/north-american-mesoscale-model/access/forecast/202212/20221216/nam_218_20221216_1200_000.grb2'
# grbs = pygrib.open(gribpath)
grbs = pygrib.open(gribpath).read()
# gpath=gribpath.read()
# grb = pygrib.fromstring(gpath)
print (grbs.keys())

这似乎与源有关,因为在大多数情况下,如果下载到笔记本电脑上它就可以工作。但是,由于工作原因,我无法使用 Anaconda 或连接我的 Google Drive。

我尝试了一些基于 在 Python 中从网络加载 GRIB 而不在本地保存文件

import time
import numpy
import pygrib
import urllib
# import cfgrib
# import xarray as xr
# import requests,json

gribdata='https://www.ncei.noaa.gov/data/north-american-mesoscale-model/access/forecast/202212/20221216/nam_218_20221216_1200_000.grb2'
data = urllib.request.urlopen(gribdata)
# grbs = pygrib.open(gribpath)
grbs = pygrib.open(data)
# gpath=gribpath.read()
# grb = pygrib.fromstring(gpath)
print (grbs.keys())

但给了我:

/usr/local/lib/python3.10/site-packages/pyproj/__init__.py:89: UserWarning: pyproj unable to set database path.
  _pyproj_global_context_initialize()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-d31a1c34cf66> in <cell line: 12>()
     10 data = urllib.request.urlopen(gribdata)
     11 # grbs = pygrib.open(gribpath)
---> 12 grbs = pygrib.open(data)
     13 # gpath=gribpath.read()
     14 # grb = pygrib.fromstring(gpath)

src/pygrib/_pygrib.pyx in pygrib._pygrib.open.__cinit__()

TypeError: expected bytes, HTTPResponse found
python urllib grib pygrib
1个回答
0
投票

我相信我已经解决了这个问题:

import time
import numpy as np
import pygrib
import urllib
# import cfgrib
# import xarray as xr
# import requests,json

print('Assign URL to gribdata variable...')
gribdata='https://www.ncei.noaa.gov/data/north-american-mesoscale-model/access/forecast/202212/20221216/nam_218_20221216_1200_000.grb2'
print('...Complete!')

print('urlopen gribdata...')
data = urllib.request.urlopen(gribdata).read()
print('...urlopen Complete!')

# grbs = pygrib.open(gribpath)
print ("PyGrib open")
grbs = pygrib.open(data)
print ("Task complete")
# gpath=gribpath.read()
# grb = pygrib.fromstring(gpath)
# print (grbs.keys())

尽管由于文件的大小,它不断杀死代码。

© www.soinside.com 2019 - 2024. All rights reserved.