【问题标题】:Create Iris Cube from a fileobject or netCDF4 dataset从文件对象或 netCDF4 数据集创建 Iris Cube
【发布时间】:2019-09-26 02:24:24
【问题描述】:

有没有办法使用文件对象(二进制流)或从 netCDF4 数据集对象创建(打开/加载)鸢尾花立方体?

具体来说,我有一个通过 URL 提供的文件,但不是由 OpenDAP 服务器提供的; iris.load_cube() 和朋友在这方面失败了。

我意识到 Iris 更喜欢延迟加载,因此使用 URI 而不是内存中的数据,但这并不总是可行的。

对于普通的 netCDF4 Dataset 对象,我可以执行以下操作:

from urllib.request import urlopen
import netCDF4 as nc

url = 'https://crudata.uea.ac.uk/cru/data/temperature/HadCRUT.4.6.0.0.median.nc'
with urlopen(url) as stream:
    ds = nc.Dataset('HadCRUT', memory=stream.read())

所以我希望为 Iris Cube 做类似的事情,或者将 netCDF4 数据集读入多维数据集,而无需通过磁盘上的临时文件。我曾希望 Iris 功能中存在某些内容,但我(还)无法在参考文档中找到它。

【问题讨论】:

    标签: python python-iris


    【解决方案1】:

    要读取.nc 文件,Iris 在内部使用您提到的same netcdf4-python 库。

    这意味着理论上你可以:

    1. 子类 CFReader 覆盖它的 __init__ 方法,唯一更改为行 self._dataset = netCDF4.Dataset(self._filename, mode='r')

    2. 要么编写您自己的 load_cube 函数 (based on this code) 以使用您的自定义 CFReader,或者您可以使用自定义的 CFReader 对 iris 进行猴子补丁。

    猴子补丁的总体思路:

    from urllib.request import urlopen
    
    import iris.fileformats.cf
    import netCDF4 as nc
    
    
    def __patch_CFReader():
        if getattr(iris.fileformats.cf.CFReader, '_HACKY_PATCHED'):
            return
    
        from iris.fileformats.cf import CFReader
    
        class CustomCFReader(CFReader):
            _HACKY_PATCHED = True
    
            def __init__(self, uri, *args, **kwargs):
                # ... other code copied
                with urlopen(url) as stream:
                    self._dataset = nc.Dataset('HadCRUT', memory=stream.read())
                # ... other code copied
    
        iris.fileformats.cf.CFReader = CustomCFReader
    
    
    __patch_CFReader()
    
    import iris
    cube = iris.load_cube('https://crudata.uea.ac.uk/cru/data/temperature/HadCRUT.4.6.0.0.median.nc')
    
    

    警告!根据项目中导入的方式,猴子补丁可能 并不总是像您最初想的那样工作。所以也许你应该更喜欢使用一些图书馆 专为猴子补丁设计,例如大猩猩:

    https://gorilla.readthedocs.io/en/latest/tutorial.html

    # my_patches.py:
    from urllib.request import urlopen
    
    import gorilla
    import iris.fileformats.cf
    import netCDF4 as nc
    
    settings = gorilla.Settings(allow_hit=True)
    
    @gorilla.patch(iris.fileformats.cf.CFReader, settings=settings)
    def __init__(self, uri, *args, **kwargs):
        # ... other code copied
        with urlopen(url) as stream:
            self._dataset = nc.Dataset('HadCRUT', memory=stream.read())
        # ... other code copied
    
    # earliest_imported_module.py:
    import gorilla
    import my_patches
    
    for patch in gorilla.find_patches([my_patches]):
        gorilla.apply(patch)
    

    【讨论】:

    • 我想你可以直接拨打iris.fileformats.netcdf.load_cubes。我认为文档不赞成直接使用这些方法,但如果你打算使用猴子补丁,那么这是你的问题中最少的。
    • 遗憾的是,在更详细地研究了它之后,我认为这也不是要走的路:load_cubes 调用 _load_cube,这也需要一个文件名,以将 netCDF 变量读入单独的尽可能多的立方体。乍一看,要对内存中的 netCDF 数据集执行此操作,需要覆盖 netcdf.py 中的各种(私有)函数。在这种情况下,覆盖load_cubes 本质上就是实现自己的文件格式模块。
    猜你喜欢
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    相关资源
    最近更新 更多