From: Alain F. <ala...@fr...> - 2005-11-29 19:01:18
|
"""Small but quite comprehensive example showing the use of PyTables.=0A= =0A= The program creates an output file, 'tutorial1.h5'. You can view it=0A= with any HDF5 generic utility.=0A= =0A= """=0A= =0A= =0A= import sys=0A= from numarray import *=0A= from tables import *=0A= =0A= # Define a user record to characterize some kind of particles=0A= class Particle(IsDescription):=0A= name =3D StringCol(16) # 16-character String=0A= idnumber =3D Int64Col() # Signed 64-bit integer=0A= ADCcount =3D UInt16Col() # Unsigned short integer=0A= TDCcount =3D UInt8Col() # unsigned byte=0A= grid_i =3D Int32Col() # integer=0A= grid_j =3D IntCol() # integer (equivalent to Int32Col)=0A= pressure =3D Float32Col() # float (single-precision)=0A= energy =3D FloatCol() # double (double-precision)=0A= =0A= def create_file():=0A= =0A= print=0A= print '-**-**-**-**-**-**- file creation -**-**-**-**-**-**-**-'=0A= =0A= # The name of our HDF5 filename=0A= filename =3D "tutorial1.h5"=0A= =0A= print "Creating file:", filename=0A= =0A= # Open a file in "w"rite mode=0A= h5file =3D openFile(filename, mode =3D "w", title =3D "Test file")=0A= =0A= print=0A= print '-**-**-**-**-**- group and table creation = -**-**-**-**-**-**-**-'=0A= =0A= # Create a new group under "/" (root)=0A= group =3D h5file.createGroup("/", 'detector', 'Detector information')=0A= print "Group '/detector' created"=0A= =0A= # Create one table on it=0A= table =3D h5file.createTable(group, 'readout', Particle, "Readout = example")=0A= print "Table '/detector/readout' created"=0A= =0A= return h5file=0A= =0A= def fill_10(h5file):=0A= for group in h5file.walkGroups("/detector"):=0A= entity_g =3D group=0A= =0A= table =3D entity_g.readout=0A= # Get a shortcut to the record object in table=0A= particle =3D table.row=0A= =0A= # Fill the table with 10 particles=0A= for i in xrange(10):=0A= particle['name'] =3D 'Particle: %6d' % (i)=0A= particle['TDCcount'] =3D i % 256=0A= particle['ADCcount'] =3D (i * 256) % (1 << 16)=0A= particle['grid_i'] =3D i=0A= particle['grid_j'] =3D 10 - i=0A= particle['pressure'] =3D float(i*i)=0A= particle['energy'] =3D float(particle['pressure'] ** 4)=0A= particle['idnumber'] =3D i * (2 ** 34)=0A= # Insert a new particle record=0A= particle.append()=0A= =0A= =0A= def _unittest():=0A= h5file =3D create_file()=0A= =0A= fill_10(h5file)=0A= =0A= # Flush the buffers for table=0A= for group in h5file.walkGroups("/detector"):=0A= entity_g =3D group=0A= =0A= table =3D entity_g.readout=0A= table.flush()# Close the file=0A= h5file.close()=0A= print "File '"+filename+"' created"=0A= =0A= if __name__ =3D=3D '__main__':=0A= _unittest()=0A= =0A= |