读取压缩的二进制文件(.grib2.bz2)



我已经从这个列表中下载了一个文件https://opendata.dwd.de/weather/nwp/icon-eu/grib/03/t_2m/(实际文件名每天都在变化(,这些文件是bz2压缩的。

我可以使用例如读取解压缩的文件

import xarray as xr
# cfgrib + dependencies are also required
grib1 = xr.open_dataset("icon-eu_europe_regular-lat-lon_single-level_2020101212_001_ASHFL_S.grib2", engine='cfgrib')

但是,我想在压缩文件中阅读。

我试过之类的东西

with bz2.open("icon-eu_europe_regular-lat-lon_single-level_2020101818_002_ASWDIFD_S.grib2.bz2", "rb") as f:
xr.open_dataset(f, engine='cfgrib')

但这并不奏效。

我正在寻找任何以编程方式读取压缩文件的方法。

我在处理数值天气预报数据时遇到了同样的问题。

我在这里所做的是下载文件并将其作为二进制对象保存(例如,使用urlopenrequests(。将此对象传递到以下函数:

import bz2, shutil
from io import BytesIO
from pathlib import Path

def bunzip_store(file: BytesIO, local_intermediate_file: Path):
with bz2.BZ2File(file) as fr, local_intermediate_file.open(mode="wb") as fw:
shutil.copyfileobj(fr, fw)

解压缩后的文件将存储在local_intermediate_file下。现在你应该可以打开这个文件了。

最新更新