def _write_legacy_vtu(x, fname): """ Write a legacy VTK unstructured grid file. """ # Lower bound value used for pixel/voxel culling, any value below this # value won't be plotted. Should be same as VOID's value in 'topology.py'. THRESHOLD = 0.001 # Voxel local points relative to its centre of geometry: voxel_local_points = asarray([[-1,-1,-1],[ 1,-1,-1],[-1, 1,-1],[ 1, 1,-1], [-1,-1, 1],[ 1,-1, 1],[-1, 1, 1],[ 1, 1, 1]])\ * 0.5 # scaling # Voxel world points: points = [] # Culled input array -- as list: xculled = [] try: depth, rows, columns = x.shape except ValueError: sys.exit('Array dimensions not equal to 3, possibly 2-dimensional.\n') for i in xrange(depth): for j in xrange(rows): for k in xrange(columns): if x[i, j, k] > THRESHOLD: xculled.append(x[i, j, k]) points += (voxel_local_points + [i, j, k]).tolist() voxels = arange(len(points)).reshape(len(xculled), 8).tolist() topology = UnstructuredGrid(points, voxel=voxels) file_header = \ 'ToPy data, created '\ + str(datetime.now()).rsplit('.')[0] scalars = CellData(Scalars(xculled, name='Densities', lookup_table =\ 'default')) vtk = VtkData(topology, file_header, scalars) vtk.tofile(fname, 'binary')
def toVTK(self, fName, dx, dy, mask=False, clip=False, force=False, method='ct'): """ Convert a 3D volume of interpolated values to vtk for visualization in Paraview """ print('toVTK') self.getAttribute(xy=True, elevation=True, force=force) self.getMean3D(dx=dx, dy=dy, mask=mask, clip=clip, force=force, method=method) self.getZGrid() self.points.getBounds() x,y,intPoints = interpolation.getGridLocations2D(self.points.bounds, dx, dy) z=self.zGrid from pyvtk import VtkData, UnstructuredGrid, PointData, CellData, Scalars # Get the 3D dimensions mx = x.size my = y.size mz = z.size nPoints = mx*my*mz nCells = (mx-1)*(my-1)*(mz-1) # Interpolate the elevation to the grid nodes if (method == 'ct'): tx,ty, vals = self.points.interpCloughTocher(self.elevation, dx = dx,dy=dy, mask = mask, clip = clip, extrapolate='nearest') elif (method == 'mc'): tx,ty, vals = self.points.interpMinimumCurvature(self.elevation, dx = dx, dy=dy, mask = mask, clip = clip) vals = vals[:my,:mx] vals = vals.reshape(mx*my) # Set up the nodes and voxel indices points = np.zeros([nPoints,3], order='F') points[:,0] = np.tile(x,my*mz) points[:,1] = np.tile(y.repeat(mx),mz) points[:,2] = np.tile(vals,mz)-z.repeat(mx*my) # Create the cell indices into the points p = np.arange(nPoints).reshape((mz,my,mx)) voxels = np.zeros([nCells,8],dtype=np.int) iCell = 0 for k in range(mz-1): k1 = k + 1 for j in range(my-1): j1 = j + 1 for i in range(mx-1): i1 = i + 1 voxels[iCell,:] = [p[k1,j,i],p[k1,j,i1],p[k1,j1,i1],p[k1,j1,i], p[k,j,i],p[k,j,i1],p[k,j1,i1],p[k,j1,i]] iCell += 1 # Create the various point data pointID = Scalars(np.arange(nPoints),name='Point iD') pointElev = Scalars(points[:,2],name='Point Elevation (m)') tmp=self.mean3D.reshape(np.size(self.mean3D)) tmp1 = np.log10(1.0/tmp) pointRes = Scalars(tmp1, name = 'log10(Resistivity) (Ohm m)') tmp1 = np.log10(tmp) pointCon = Scalars(tmp1, name = 'log10(Conductivity) (S/m)') PData = PointData(pointID, pointElev, pointRes, pointCon) CData = CellData(Scalars(np.arange(nCells),name='Cell iD')) vtk = VtkData( UnstructuredGrid(points, hexahedron=voxels), # ), PData, CData, 'Some Name' ) vtk.tofile(fName, 'ascii')