1

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

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

1 Answer 1

2

When you set dimension of a variable you have to use tuple not just a sequence:

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'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.