我已经从这个列表中下载了一个bz2压缩的文件https://opendata.dwd.de/weather/nwp/icon-eu/grib/03/t_2m/ (实际的文件名每天都会改变)。
我可以使用例如来读取解压缩的文件。
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')但这是行不通的。
我正在寻找任何方法,以编程方式读取压缩文件。
发布于 2020-10-27 20:46:35
我在处理数值天气预报数据时也遇到了同样的问题。
我在这里所做的是下载文件并将其保存为二进制对象(例如,使用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下。现在你应该能够打开这个文件了。
https://stackoverflow.com/questions/64452549
复制相似问题