【发布时间】:2019-05-31 14:21:51
【问题描述】:
我最近从 netcdf4 切换到 iris 以在 Python 中读取 netcdf 文件(我使用的是 Python 2.7)。在许多方面,这改进了我的代码,但我在某些数据集上遇到了一些性能问题。使用 netcdf4 读取一些文件(不是全部)过去只需几秒钟,而使用 iris 则需要一分钟或更多分钟。
这是我使用的一个简单测试。使用 netcdf4 第一次读取需要 4 秒,使用 iris 大约需要 90 秒!这会显着降低我的代码性能,因为我通常会在一次运行中读取很多文件。
from datetime import datetime
import iris
import netCDF4 as nc
nr = 3
ifile = 'myfile.nc'
print('IRIS read\n')
for i in range(nr):
t1 = datetime.now()
fh = iris.load(ifile)
data = fh[0].data
t2 = datetime.now()
diff = (t2-t1).total_seconds()
print('Data loaded in {:8.3f} s\n'.format(diff))
print('NetCDF read\n')
for i in range(nr):
t1 = datetime.now()
fh = nc.Dataset(ifile, mode='r')
data = fh.variables.values()[-1][:]
t2 = datetime.now()
diff = (t2-t1).total_seconds()
print('Data loaded in {:8.3f} s\n'.format(diff))
有人发现了同样的行为吗?是不是我对 iris 做错了什么?
【问题讨论】:
-
据我了解,在 iris 立方体上调用“.data”会立即将数据加载到一个 numpy 数组中。对于 netcdf,您只需从 variables-dict 中获取 netcdf“变量”对象。您需要通过附加“[:]”将数据复制到 numpy 数组。
-
你说得对,我编辑了上面的例子。无论如何,如果我添加 [:],netcdf4 需要相同的时间,所以问题就在那里..
-
那么我只能推测,您使用 iris(索引 0)声明的数据与使用 netcdf4(索引 -1)检索到的变量不同。
-
我可以向你保证它是同一个变量。示例中的文件只有一个变量,但 netcdf4 还将坐标读取为变量(最后一个是真正的变量)。真正的重点是 iris 那段时间在做什么?
标签: performance netcdf python-iris