Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. from tables import *
  2.  
  3. class Particle(IsDescription):
  4. identity = StringCol(itemsize=22, dflt=" ", pos=0) # character String
  5. idnumber = Int16Col(dflt=1, pos = 1) # short integer
  6. speed = Float32Col(dflt=1, pos = 2) # single-precision
  7.  
  8. # Open a file in "w"rite mode
  9. fileh = open_file("objecttree.h5", mode = "w")
  10.  
  11. # Get the HDF5 root group
  12. root = fileh.root
  13.  
  14. # Create the groups
  15. group1 = fileh.create_group(root, "group1")
  16. group2 = fileh.create_group(root, "group2")
  17.  
  18. # Now, create an array in root group
  19. array1 = fileh.create_array(root, "array1", ["string", "array"], "String array")
  20.  
  21. # Create 2 new tables in group1
  22. table1 = fileh.create_table(group1, "table1", Particle)
  23. table2 = fileh.create_table("/group2", "table2", Particle)
  24.  
  25. # Create the last table in group2
  26. array2 = fileh.create_array("/group1", "array2", [1,2,3,4])
  27.  
  28. # Now, fill the tables
  29. for table in (table1, table2):
  30. # Get the record object associated with the table:
  31. row = table.row
  32.  
  33. # Fill the table with 10 records
  34. for i in xrange(10):
  35. # First, assign the values to the Particle record
  36. row['identity'] = 'This is particle: %2d' % (i)
  37. row['idnumber'] = i
  38. row['speed'] = i * 2.
  39.  
  40. # This injects the Record values
  41. row.append()
  42.  
  43. # Flush the table buffers
  44. table.flush()
  45.  
  46. # Finally, close the file (this also will flush all the remaining buffers!)
  47. fileh.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement