Write/Read NetCDF4 VLEN data

import library

In [14]:
import numpy
import Nio
import time, os

Option to create a NetCDF4 file

In [15]:
opt = Nio.options()
opt.Format = 'NetCDF4'

print opt.Format
NetCDF4

Create the file

In [16]:
#create a file
hatt = "Created at " + time.ctime(time.time())
fn = "pynio_vlen.nc"
if(os.path.isfile(fn)):
    os.remove(fn)
file = Nio.open_file(fn, options=opt, history=hatt, mode="w")

File global attributes

In [17]:
#create global attributes
file.source   = "Nio created NetCDF4 vlen file"
#setattr(file, 'source', "Nio test NetCDF file")
file.history = "Created " + time.ctime(time.time())

print "file after add attributes:"
print file
file after add attributes:
Nio file:	pynio_vlen.nc
      history : Created Mon May 12 07:22:11 2014
      source : Nio created NetCDF4 vlen file
   dimensions:
   variables:


Define dimensions

In [18]:
nx = 3
ny = 4

x = file.create_dimension('x', nx)
y = file.create_dimension('y', ny)

print "file after add dimensions:"
print file
file after add dimensions:
Nio file:	pynio_vlen.nc
      history : Created Mon May 12 07:22:11 2014
      source : Nio created NetCDF4 vlen file
   dimensions:
      x = 3
      y = 4
   variables:


Define VLEN data with numpy

In [19]:
data = numpy.empty(ny*nx, object)
m = 0
for n in range(ny*nx):
    m += 1
    data[n] = numpy.arange(m, dtype='int32')+1
data = numpy.reshape(data,(ny, nx))

print "data", data
data [[array([1], dtype=int32) array([1, 2], dtype=int32)
  array([1, 2, 3], dtype=int32)]
 [array([1, 2, 3, 4], dtype=int32) array([1, 2, 3, 4, 5], dtype=int32)
  array([1, 2, 3, 4, 5, 6], dtype=int32)]
 [array([1, 2, 3, 4, 5, 6, 7], dtype=int32)
  array([1, 2, 3, 4, 5, 6, 7, 8], dtype=int32)
  array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)]
 [array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10], dtype=int32)
  array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11], dtype=int32)
  array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)]]

Define VLEN in NetCDF4 file, and write it

In [20]:
vlvar = file.create_vlen('vlen_var', 'i', ('y','x'))
#vlvar[:, :] = data
vlvar[:] = data

print "file after add vlen:"
print file

#print vlvar
#print 'vlen variable =\n',vlvar[:]
#print file
#print file.variables['phony_vlen_var']
#print file.vltypes['phony_vlen']
file after add vlen:
Nio file:	pynio_vlen.nc
      history : Created Mon May 12 07:22:11 2014
      source : Nio created NetCDF4 vlen file
   dimensions:
      x = 3
      y = 4
   variables:
   list vlen_var [ y|4, x|3 ]	 



Close the file

In [21]:
file.close()
In [22]:
(Re)Open the file for reading
  File "<ipython-input-22-5f7cf9db6ed0>", line 1
    (Re)Open the file for reading
           ^
SyntaxError: invalid syntax
In [23]:
#open a file
#fn = "examples/vlen.nc"
fn = "pynio_vlen.nc"
file = Nio.open_file(fn, "r")

print "file:"
print file
file:
Nio file:	pynio_vlen.nc
      history : Created Mon May 12 07:22:11 2014
      source : Nio created NetCDF4 vlen file
   dimensions:
      x = 3
      y = 4
   variables:
   list vlen_var [ y|4, x|3 ]	 



Read and print the VLEN variable

In [24]:
var = file.variables['vlen_var'][:]

print "var:"
print var
var:
[[array([1], dtype=int32) array([1, 2], dtype=int32)
  array([1, 2, 3], dtype=int32)]
 [array([1, 2, 3, 4], dtype=int32) array([1, 2, 3, 4, 5], dtype=int32)
  array([1, 2, 3, 4, 5, 6], dtype=int32)]
 [array([1, 2, 3, 4, 5, 6, 7], dtype=int32)
  array([1, 2, 3, 4, 5, 6, 7, 8], dtype=int32)
  array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)]
 [array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10], dtype=int32)
  array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11], dtype=int32)
  array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)]]

Close file

In [25]:
file.close()
In []: