I have the following code which works:
import pandas as pd
import requests
import xarray as xr
import cfgrib
r = requests.get(
'https://tgftp.nws.noaa.gov/SL.us008001/ST.opnl/DF.gr2/DC.ndfd/AR.neast/VP.001-003/ds.wspd.bin',
stream=True)
f = open('..\\001_003wspd.grb', 'wb')
f.write(r.content)
f.close()
xr_set = xr.load_dataset('..\\001_003wspd.grb', engine="cfgrib")
windspeed_df = xr_set.to_dataframe()
What I am is wanting is to avoid writing then reading the file. Something like:
grib = r.content.decode()
or
xr_set = xr.load_dataset(r.content, engine="cfgrib")
which both give:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd7 in position 95: invalid continuation byte
I have also tried:
grib = resp.content.decode(errors='ignore')
xr_set = xr.open_dataset(grib, engine="cfgrib")
which gives:
ValueError: embedded null character
As well as trying different binary encodings.
I checked the
cfgribcode, and in particular theopen_filemethod, and it seems it only accepts str or os.PathLike[str] objects. So, ther.contentis read as a path and it goes wrong. If you do not want to store files permanently, you could try :