I am trying to read in this file: https://www.ncei.noaa.gov/data/north-american-mesoscale-model/access/forecast/202212/20221216/nam_218_20221216_1200_000.grb2 for an experiment using Google Colab. The link leads you to download a .grb2 dataset from a NOAA website.
However, I get this error in my code:
/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
The code I have is
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())
It seems to have to do with the source as in most cases it would work if being downloaded onto the laptop. However, I cannot use Anaconda or connect my Google Drive for this due to work.
I tried something based on Loading a GRIB from the web in Python without saving the file locally in
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())
but gave me:
/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
I believe I had resolved the issue with this:
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())
Although it keeps killing the code due to the size of the file.