简体   繁体   English

netcdf创建变量:错误,需要整数

[英]netcdf create variable: error an integer is required

My code: 我的代码:

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')

Some additional Information: 一些其他信息:

shape(nx) = 288 ; shape(ny) = 288 ; shape(nz) = 67
shape(N) = (67,288,288)

nx, ny an nz have to be the dimension vectors nx,ny和nz必须是尺寸向量

I get the following error: 我收到以下错误:

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

I am using python 2.7 我正在使用python 2.7

When you set dimension of a variable you have to use tuple not just a sequence: 设置变量的维数时,不仅要使用序列,还必须使用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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM