【问题标题】:Reading xarray goes16 data directly from S3 without downloading into the system直接从 S3 读取 xarray go16 数据,无需下载到系统中
【发布时间】:2022-11-08 09:18:52
【问题描述】:

直接从 S3 读取 xarray go16 数据,无需下载到系统中。问题是我无法连接 S3Files。我正在从 S3 中调用 24 个文件,并希望在时间范围内读取和提取这些文件的数据:

这是代码:

import datetime as dt
import xarray as xr
import fsspec
import s3fs

fs = fsspec.filesystem('s3', anon=True)

urls1=[]

for i in range (2):
    urls = [
        's3://' + f
        for f in fs.glob(f"s3://noaa-goes16/ABI-L2ACMC/2022/001/{i:02}/*.nc")
    ]
    urls1 = urls1+ urls

with fs.open(urls1[0]) as fileObj:
    ds = xr.open_dataset(fileObj, engine='h5netcdf')

但是,我遇到了I/O operation on closed file 的问题。

【问题讨论】:

    标签: python amazon-s3 fsspec


    【解决方案1】:

    与 python 中的大多数文件对象接口类似,使用上下文管理器打开类似文件的对象会在退出时关闭文件。所以在下面的例子中:

    # use fs.open to create an S3File object
    with fs.open(urls1[0], mode="rb") as fileObj:
        # open the netcdf for reading, but don't load the data - instead, just
        # establish a lazy-load connection to the underlying S3File object
        ds = xr.open_dataset(fileObj, engine='h5netcdf')
    
    # <--
    # exit the context, thereby closing the S3File object
    
    # attempt to access the data again, after the stream is closed
    ds.load()  # raises IOError
    

    相反,您应该在上下文管理器中加载所有数据:

    with fs.open(urls1[0], mode="rb") as fileObj:
        with xr.open_dataset(fileObj, engine='h5netcdf') as ds:
            ds = ds.load()
    

    或者,如果您打算在以后的代码中使用数据集而不加载:

    fileObj = fs.open(urls1[0], mode="rb")
    ds = xr.open_dataset(fileObj, engine='h5netcdf')
    
    # other data operations
    
    # be sure to close the connections when you're done
    ds.close()
    fileObj.close()
    

    【讨论】:

    • 谢谢@迈克尔。好像不支持netcdf4。这是错误: ValueError: can only read bytes or file-like objects with engine='scipy' or 'h5netcdf'
    • 你是对的 - 你需要使用阅读模式,例如mode="rb"。我已经更新了我的答案。在我的辩护中,我从你那里复制了那部分代码;)希望有帮助!
    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多