我正在尝试在 Python 中使用 xarray 打开多个 netCDF 文件。这些文件具有相同形状的数据,我想加入它们,创建一个新的维度。
我尝试对 xarray.open_mfdataset() 使用 concat_dim 参数,但它没有按预期工作。下面给出一个例子,打开两个温度数据文件124次,241个纬度和480个经度:
DS = xr.open_mfdataset( 'eraINTERIM_t2m_*.nc', concat_dim='cases' )
da_t2m = DS.t2m
print( da_t2m )
使用此代码,我希望结果数据数组的形状类似于 (cases: 2, time: 124, latitude: 241, longitude: 480)。但是,它的形状是(案例:2,时间:248,纬度:241,经度:480)。 它创建了一个新维度,但也对最左边的维度求和:两个数据集的“时间”维度。 我想知道这是“xarray.open_mfdateset”的错误还是预期的行为,因为两个数据集的“时间”维度都是无限的。
有没有办法直接使用 xarray 连接这些文件中的数据并获得上述预期返回?
谢谢。
马特乌斯
最佳答案
从我的评论延伸我会试试这个:
def preproc(ds):
ds = ds.assign({'stime': (['time'], ds.time)}).drop('time').rename({'time': 'ntime'})
# we might need to tweak this a bit further, depending on the actual data layout
return ds
DS = xr.open_mfdataset( 'eraINTERIM_t2m_*.nc', concat_dim='cases', preprocess=preproc)
这里的好处是,在重命名原始维度 (time
-> ntime
) 的同时,您将原始时间坐标保留在 stime
中.
如果一切正常,您应该得到结果维度为 (cases
, ntime
, latitude
, longitude
).
免责声明:我在带有最终连接的循环中做了类似的事情(效果很好),但没有测试 preprocess
方法。
关于python - 如何在 Python 中使用 xarray 连接来自多个 netCDF 文件的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55457872/