Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 0.99 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Column xyz data to grid for plotting
  2. x y z
  3.  
  4. 1 1 10
  5.  
  6. 1 2 12
  7.  
  8. 2 1 14
  9.  
  10. 2 2 16
  11.        
  12. 10 12
  13.  
  14. 14 16
  15.        
  16. import numpy
  17.  
  18. idx1 = numpy.array([0, 0, 1, 1])
  19. idx2 = numpy.array([0, 1, 0, 1])
  20. data = numpy.array([10, 12, 14, 16])
  21.  
  22. grid = numpy.zeros(len(data)/2, 2)
  23. grid[idx1, idx2] = data
  24.  
  25. >>>grid
  26. array([[ 10.,  12.],
  27.       [ 14.,  16.]])
  28.        
  29. data.txt
  30. x y z
  31. 1 1 10
  32. 1 2 12
  33. 2 1 14
  34. 2 2 16
  35.  
  36. def extract(filepath):
  37.     f = open(filepath)
  38.     f.readline() # to read out the first line that says "x y z"
  39.     while 1:
  40.         x = f.readline().strip().split()[-1]
  41.         y = f.readline().strip().split()[-1]
  42.         print x, y
  43.        
  44. data.txt
  45. x y z
  46. 1 1 10
  47. 2 1 14
  48. 1 2 12
  49. 2 2 16
  50.  
  51. from collections import defaultdict
  52. def extract(filepath):
  53.     coords = defaultdict(dict)
  54.     f = open(filepath)
  55.     f.readline() # to read out the first line that says "x y z"
  56.     for line in f:
  57.         id, axis, val = map(int, line.strip().split())
  58.         coords[id][axis] = val
  59.  
  60.     for i in sorted(coords):
  61.         print coords[i][1], coords[i][2]