Mat*_*ira 5 python concatenation netcdf python-xarray
I'm trying to open multiple netCDF files with xarray in Python. The files have data with same shape and I want to join them, creating a new dimension.
I tried to use concat_dim argument for xarray.open_mfdataset(), but it doesn't work as expected. An example is given below, which open two files with temperature data for 124 times, 241 latitudes and 480 longitudes:
DS = xr.open_mfdataset( 'eraINTERIM_t2m_*.nc', concat_dim='cases' )
da_t2m = DS.t2m
print( da_t2m )
Run Code Online (Sandbox Code Playgroud)
有了这段代码,我希望结果数据数组的形状像(情况:2,时间:124,纬度:241,经度:480)。但是,其形状为(案例:2,时间:248,纬度:241,经度:480)。它创建了一个新维度,但也将最左边的维度相加:两个数据集的“时间”维度。我想知道这是来自“ xarray.open_mfdateset”的错误还是预期的行为,因为两个数据集的“时间”维度都是无限的。
有没有一种方法可以直接使用xarray从这些文件中联接数据并获得上述预期收益?
谢谢。
马泰斯
小智 5
从我的评论延伸,我会尝试这个:
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)
Run Code Online (Sandbox Code Playgroud)
这里的好处是,您可以stime在重命名原始维度(time-> ntime)时保留原始时间坐标。
如果一切正常,您应该得到结果尺寸为 ( cases, ntime, latitude, longitude)。
免责声明:我在最后的 concat 循环中做了类似的事情(效果很好),但没有测试preprocess- 方法。