Reputation: 4007
With the python netcdf4 library I want to write a test dataset for my netcdf4 read script. However, I am not able to generate the desired output.
This is currently my write script:
# write file
varlist = ['ABC123', 'DEF456', 'GHI789']
varlist = np.array([[i for i in k] for k in varlist], dtype='S1')
with Dataset(indexfile, 'w', format='NETCDF4') as file:
file.createDimension('vars', [3,6])
vars_list = file.createVariable('vars', 'S1', (u'vars',))
vars_list[:] = varlist
But this returns a TypeError
:
TypeError: an integer is required
How should I change my input or write a script to get the desired result?
Upvotes: 3
Views: 4488
Reputation: 85432
You need to create one dimension at a time. For example, call your dimensions x
and y
:
import numpy as np
from netCDF4 import Dataset
indexfile = 'data.nc'
varlist = ['ABC123', 'DEF456', 'GHI789']
varlist = np.array([[i for i in k] for k in varlist], dtype='S1')
with Dataset(indexfile, 'w', format='NETCDF4') as nc_file:
nc_file.createDimension('x', 3)
nc_file.createDimension('y', 6)
vars_list = nc_file.createVariable('vars', 'S1', ('x', 'y'))
vars_list[:] = varlist
This produces this file:
$ ncdump data.nc
netcdf data {
dimensions:
x = 3 ;
y = 6 ;
variables:
char vars(x, y) ;
data:
vars =
"ABC123",
"DEF456",
"GHI789" ;
}
Reading it back with Python works too:
with Dataset(indexfile, 'r', format='NETCDF4') as nc_file:
print(nc_file.variables['vars'][:])
[[b'A' b'B' b'C' b'1' b'2' b'3']
[b'D' b'E' b'F' b'4' b'5' b'6']
[b'G' b'H' b'I' b'7' b'8' b'9']]
Upvotes: 2