Python 解码 NDFD Grib 二进制文件类似于使用 Open(path, 'wb') 和 xarray.load_dataset(path, engine='cfgrib') 写入然后读取文件
Python Decoding NDFD Grib Binary File Similar to Writing then Reading the File Using Open(path, 'wb') and xarray.load_dataset(path, engine='cfgrib')
我有以下有效的代码:
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
以及尝试不同的二进制编码。
我检查了 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')
我有以下有效的代码:
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
以及尝试不同的二进制编码。
我检查了 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')