0 0*_*0 0 10 python python-iris
有没有一种方法可以使用文件对象(二进制流)或者通过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())
Run Code Online (Sandbox Code Playgroud)
因此,我正在寻找对Iris进行类似操作的方法Cube,或者将netCDF4数据集读入多维数据集,而无需通过磁盘上的临时文件。我曾希望Iris功能中可以存在一些东西,但是我(尚未)无法在参考文档中找到它。
要读取.nc文件,Iris 在内部使用您提到的相同netcdf4-python库。
这意味着理论上您可以:
子类CFReader重写它的__init__方法,唯一更改为行self._dataset = netCDF4.Dataset(self._filename, mode='r')
要么编写您自己的load_cube函数(基于此代码)来使用您的自定义 CFReader,要么您可以iris使用您的自定义 CFReader 进行 Monkeypatch。
猴子补丁的总体思路:
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')
Run Code Online (Sandbox Code Playgroud)
警告!根据项目中导入的方式,猴子修补可能并不总是像您首先想象的那样起作用。所以也许你应该更喜欢使用一些专门为猴子补丁设计的库,例如 gorilla:
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)
Run Code Online (Sandbox Code Playgroud)