我从同事那里收到了一些数据文件(NetCDF),我试图将这些文件连接成一个文件,这样我就可以用以前的一些脚本运行它了。问题是我收到的NetCDF文件的尺寸为“高度”,填充值为1e20。当我在文件上尝试几乎任何xarray操作时,这会导致错误,因为维度坐标中有重复的值。
以下是其中一个文件的信息(您可以在“高度”坐标的最终值中看到重复值):
<xarray.Dataset> Dimensions: (height: 1061)
Coordinates: * height (height) float64 0.0 10.0 20.0 30.0 ... 1e+20 1e+20 1e+20 1e+20
Data variables:
pres (height) float32 ...
tdry (height) float32 ...
rh (height) float32 ...
u_wind (height) float32 ...
v_wind (height) float32 ...
mr (height) float32 ...
theta (height) float32 ...
theta_e (height) float32 ...
theta_v (height) float32 ...
lat (height) float32 ...
lon (height) float32 ...
alt (height) float32 ...
wdir (height) float32 ...
我认为可能重新索引到一个没有填充值的新索引可能会有所帮助,但我也无法做到这一点,因为错误再次弹出:
ValueError: cannot reindex or align along dimension 'height' because the index has duplicate values
如果有人可以帮助我,这将是一个很大的帮助!我一直在努力解决这个问题,但也许解决方案非常简单,我的初学者的状态在这里并没有真正的帮助。 :/
这不是一个python解决方案,但我想知道CDO或NCO是否可能无法帮助您更轻松/更快地解决这个问题?
如果文件是不同的时间你可以尝试
cdo mergetime input_t*.nc output.nc
(*名称的通配符)
更一般地说,你可以尝试cat文件:
cdo cat input_t*.nc output.nc
我不确定cdo将如何处理丢失的高度坐标
您还可以在nco中附加文件
ncks -A appended_file.nc target_file.nc
不确定这些解决方案中哪些(如果有的话)可行,但我希望有人可以提供帮助。
如何删除this post中建议的重复条目?
即使坐标有重复的条目,.isel
方法也应该有效。以下脚本可能有效
_, index = np.unique(ds['time'], return_index=True)
ds.isel(time=index)