我有两个netCDF文件:file1.nc和file2.nc,唯一的区别是file1.nc包含一个变量'rho‘,我想通过修改变量将其附加到file2.nc中。原始file2.nc中不包含'rho‘。我使用的是Python模块netCDF4。
import netCDF4 as ncd
file1data=ncd.Dataset('file1.nc')
file1data.variables['rho']
<class 'netCDF4._netCDF4.Variable'> float64 rho(ocean_time, s_rho, eta_rho, xi_rho)
long_name: density anomaly
units: kilogram meter-3
time: ocean_time
grid: grid
location: face
coordinates: lon_rho lat_rho s_rho ocean_time
field: density, scalar, series
_FillValue: 1e+37
unlimited dimensions: ocean_time
current shape = (2, 15, 1100, 1000)
filling on所以rho的形状是2,15,1100,1000,但在添加到file2.nc时,我只想添加ro1,15,1100,1000,即只添加第二个时间步长的数据。这将导致file2.nc中的'rho‘的形状为15,1100,1000。但我一直无法做到这一点。
我一直在尝试这样的代码:
file1data=ncd.Dataset('file1.nc')
rho2=file1data.variables['rho']
file2data=ncd.Dataset('file2.nc','r+') # I also tried with 'w' option; it does not work
file2data.createVariable('rho','float64')
file2data.variables['rho']=rho2 # to copy rho2's attributes
file2data.variables['rho'][:]=rho2[-1,15,1100,1000] # to modify rho's shape in file2.nc
file2data.close()这里我漏掉了什么?
发布于 2019-01-15 22:15:09
您尚未在第二个netCDF文件中指定变量rho的大小。
您正在做的是:
file2data.createVariable('rho','float64')当它被认为是
file2data.createVariable('rho','float64',('ocean_time', 's_rho', 'eta_rho', 'xi_rho'))https://stackoverflow.com/questions/54190315
复制相似问题