Advertisement
Guest User

RequestData

a guest
Dec 15th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. import numpy as np
  2. from vtk.util.numpy_support import numpy_to_vtk
  3. from vtk.util.numpy_support import vtk_to_numpy
  4.  
  5. # Parallel information, useful for debugging only
  6. controller = vtk.vtkMultiProcessController.GetGlobalController()
  7. rank = controller.GetLocalProcessId()
  8.  
  9. # Read the binary cartesian file
  10. def readcart(fname, dims):
  11.     dims = np.array(dims)  # Easier to use np array to get count of items
  12.     with open(fname, 'rb') as f:
  13.         # Fortran records have INTEGER*4 headers which are useless here
  14.         f.seek(4)
  15.  
  16.         data = np.fromfile(f, dtype=np.float32, count=np.prod(dims), sep="")
  17.         # Order="F" is the key here
  18.         # Bare in mind that if I need to save in F order I have to ravel
  19.         # first
  20.         return data.reshape(dims, order="F")
  21.  
  22.  
  23. xname = "/home/mfontana/Tesis/Codigo/Modes-Display/odir/field_br.R+1+1+0.out"
  24. yname = "/home/mfontana/Tesis/Codigo/Modes-Display/odir/field_bt.R+1+1+0.out"
  25. zname = "/home/mfontana/Tesis/Codigo/Modes-Display/odir/field_bp.R+1+1+0.out"
  26. dims = (dim_x, dim_y, dim_z)
  27.  
  28. # Construct grid
  29. output.SetDimensions(dims)
  30. output.SetXCoordinates(numpy_to_vtk(linspace(-1, 1, dims[0]), deep=True,
  31.                        array_type=vtk.VTK_FLOAT))
  32. output.SetYCoordinates(numpy_to_vtk(linspace(-1, 1, dims[1]), deep=True,
  33.                        array_type=vtk.VTK_FLOAT))
  34. output.SetZCoordinates(numpy_to_vtk(linspace(-1, 1, dims[2]), deep=True,
  35.                        array_type=vtk.VTK_FLOAT))
  36.  
  37. # Get data
  38. # Reshaping is because I need two axes to hstack and get (n, 3). If I ravel
  39. # I get (3n,) with hstack
  40. # Must use "F" order again as reshaping only changes access order, not
  41. # memory location (implying that every reshaping must use "F" order)
  42. bx = readcart(xname, dims).reshape(-1, 1, order="F")
  43. by = readcart(yname, dims).reshape(-1, 1, order="F")
  44. bz = readcart(zname, dims).reshape(-1, 1, order="F")
  45.  
  46. b = np.hstack((bx, by, bz))
  47.  
  48. # Convert to VTK and give names
  49. array = numpy_to_vtk(b, deep=True, array_type=vtk.VTK_FLOAT)
  50. array.SetName("B")
  51. array.SetComponentName(0, "r")
  52. array.SetComponentName(1, "t")
  53. array.SetComponentName(2, "p")
  54.  
  55. # Associate data to grid
  56. output.GetPointData().AddArray(array)
  57.  
  58. output.SetExtent(0, dims[0]-1 , 0, dims[1]-1 , 0, dims[2]-1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement