为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

红颜莎娜 2023-12-12 10:24:32
我已经从这个列表中下载了一个文件https://opendata.dwd.de/weather/nwp/icon-eu/grib/03/t_2m/(实际文件名每天都会变化),它们是 bz2 压缩的。我可以使用例如读取解压缩的文件import xarray as xr# cfgrib + dependencies are also requiredgrib1 = 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')但这不起作用。我正在寻找任何以编程方式读取压缩文件的方法。
查看完整描述

1 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

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


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


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. 现在您应该能够打开该文件。


查看完整回答
反对 回复 2023-12-12
  • 1 回答
  • 0 关注
  • 86 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信