我的代码:
data = Dataset('/scratch/uni/ifmto/janniklas/data.nc', 'w', format='NETCDF4_CLASSIC')
data.createDimension('z', len(zn))
data.createDimension('x', len(xn))
data.createDimension('y', len(yn))
nz = data.createVariable('zn', np.float32, 'z')
nx = data.createVariable('xn', np.float32, 'x')
nx = data.createVariable('yn', np.float32, 'y')
N = data.createVariable('N', np.float32, 'z','x','y')
其他一些信息:
shape(nx) = 288 ; shape(ny) = 288 ; shape(nz) = 67
shape(N) = (67,288,288)
nx,ny和nz必须是维度向量
我收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "Aufgabe.py", line 97, in <module>
N = data.createVariable('N', np.float32, 'z','x','y')
File "netCDF4/_netCDF4.pyx", line 2222, in netCDF4._netCDF4.Dataset.createVariable (netCDF4/_netCDF4.c:16460)
File "netCDF4/_netCDF4.pyx", line 3141, in netCDF4._netCDF4.Variable.__init__ (netCDF4/_netCDF4.c:27788)
TypeError: an integer is required
我正在使用python 2.7
答案 0 :(得分:2)
设置变量的维度时,必须使用tuple
而不仅仅是序列:
from netCDF4 import Dataset
import numpy as np
data = Dataset('data.nc', 'w', format='NETCDF4_CLASSIC')
data.createDimension('z', 67)
data.createDimension('x', 288)
data.createDimension('y', 288)
# 'z' has to be a tuple
nz = data.createVariable('zn', np.float32, ('z',))
nx = data.createVariable('xn', np.float32, ('x',))
nx = data.createVariable('yn', np.float32, ('y',))
N = data.createVariable('N', np.float32, ('z','x','y'))