Skip to content

Instantly share code, notes, and snippets.

@swarbhanu
Created November 29, 2011 14:47
Show Gist options
  • Save swarbhanu/1405042 to your computer and use it in GitHub Desktop.
Save swarbhanu/1405042 to your computer and use it in GitHub Desktop.
This program creates a CArray using PyTables. A numpy array is initialized and used to fill a hyperslab of a CArray. Slices of the array are then printed.
import sys
import numpy as np
import tables as tb
filename='carrayEx1.h5'
fileh=tb.openFile(filename, 'w')
carray = fileh.createCArray(fileh.root, 'carray', tb.UInt32Atom(), shape=(200,300))
a=np.array(np.arange(5*3)).reshape((5,3))
carray[10:15,10:15:2] =a
print ""
print "The array, ", a[:,:]
print ""
print "is written into a hyperslab in CArray."
print ""
print "The portion of the CArray,"
print "carray[10:15,10:15:2] = "
print ""
print carray[10:15,10:15]
print ""
print "type of a:", type(a)
print "dtype of a: ",a.dtype
print "type of carray:", type(carray)
#print "type of carray:", carray.dtype
print""
print "Converting the pytables array into a numpy array:"
#Method for fast conversion from a pytables array to a numpy array
def fast_conversion(hdfarray):
a=np.empty(shape=hdfarray.shape,dtype=hdfarray.dtype)
a[:]=hdfarray[:]
return a
#Conversion from a pytables array to a numpy array
b = fast_conversion(carray)
print "type of b:", type(b)
print "dtype of b:", b.dtype
print ""
carray[1,1]=10**9
#print "picking a very high value for carray[1,1]: ", carray[1,1]
#print ""
fileh.close()
#Reopen and read another hyperslab from the hdf file
h5f=tb.openFile(filename)
print h5f
print ""
print h5f.root.carray[5:15,5:15]
h5f.close()
@yeshbhosale
Copy link

How can i append new data into same group and node?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment