NetCDF table
Jump to navigation
Jump to search
Storing tables in NetCDF4
Writting a table using Python
The following example shows how to store a table of 10 records with 8 members :
| name | ADCcount | grid_i | grid_j | pressure | energy | idnumber | pressure2 |
|---|---|---|---|---|---|---|---|
| 16-character String | Unsigned short integer | 32-bit integer | 32-bit integer | float (single-precision) | double (double-precision) | Signed 64-bit integer | 2-dim table of float (2*3) |
The script has been run on gpc with the following modules :
module load gcc/4.8.1 hdf5/187-v18-serial-gcc netcdf/4.1.3_hdf5_serial-gcc intel/14.0.0 python/2.7.2
from netCDF4 import Dataset
from netCDF4 import chartostring, stringtoarr
import numpy
f = Dataset('particles.nc','w',format='NETCDF4')
size = 10
Particle = numpy.dtype([('name', 'S1', 16), # 16-character String
('ADCcount',numpy.uint16), # Unsigned short integer
('grid_i',numpy.int32), # 32-bit integer
('grid_j',numpy.int32), # 32-bit integer
('pressure',numpy.float32), # float (single-precision)
('energy',numpy.float64), # double (double-precision)
('idnumber',numpy.int64), # Signed 64-bit integer
('pressure2' , numpy.float32 , (2,3) ) # array of floats (single-precision) table 2 lines * 3 columns
])
Particle_t = f.createCompoundType(Particle,'Particle')
f.createDimension('NRecords',None)
v = f.createVariable('Data',Particle_t,'NRecords')
data = numpy.empty(size,Particle)
for i in xrange(10):
data['name'][i] = stringtoarr('Particle: %6d' % (i),16)
data['ADCcount'][i] = (i * 256) % (1 << 16)
data['grid_i'][i] = i
data['grid_j'][i] = 10 - i
data['pressure'][i] = float(i*i)
data['energy'][i] = float(data['pressure'][i] ** 4)
data['idnumber'][i] = i * (2 ** 34)
data['pressure2'][i] = [
[0.5+float(i),1.5+float(i),2.5+float(i)],
[-1.5+float(i),-2.5+float(i),-3.5+float(i)]]
#Fill data in File
v[:] = data
f.close()