0

我有以下有效的代码:

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()

我想要的是避免写入然后读取文件。就像是:

grib = r.content.decode()

或者

xr_set = xr.load_dataset(r.content, engine="cfgrib")

两者都给出:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd7 in position 95: invalid continuation byte

我也试过:

grib = resp.content.decode(errors='ignore')
xr_set = xr.open_dataset(grib, engine="cfgrib")

这使:

ValueError: embedded null character

以及尝试不同的二进制编码。

4

1 回答 1

0

我检查了cfgrib代码,特别是open_file方法,它似乎只接受 str 或 os.PathLike[str] 对象。因此,将r.content其读取为路径并且出错了。如果您不想永久存储文件,可以尝试:

import os

import requests
import xarray as xr
from fs.tempfs import TempFS


def download_and_read_grib(url) -> xr.Dataset:
    with TempFS() as tempfs:
        with requests.get(url) as r:
            path = os.path.join(tempfs._temp_dir, "data.grb")
            with open(path, 'wb') as f:
                f.write(r.content)
            ds = xr.open_dataset(path, engine='cfgrib').load()
    return ds


download_and_read_grib('https://tgftp.nws.noaa.gov/SL.us008001/ST.opnl/DF.gr2/DC.ndfd/AR.neast/VP.001-003/ds.wspd.bin')

于 2021-08-26T14:07:10.263 回答