def RequestData(self, request, inInfoVec, outInfoVec): from vtkmodules.vtkCommonDataModel import vtkTable, vtkDataSet, vtkPolyData input0 = vtkDataSet.GetData(inInfoVec[0], 0) input1 = vtkDataSet.GetData(inInfoVec[1], 0) output = vtkPolyData.GetData(outInfoVec, 0) # do work print("Pretend work done!") return 1
def RequestData(self, request, inInfoVec, outInfoVec): from vtkmodules.vtkCommonDataModel import vtkTable, vtkDataSet, vtkPolyData from vtkmodules.vtkIOExodus import vtkExodusIIReader as e2r from vtkmodules.vtkFiltersVerdict import vtkMeshQuality as mq from vtkmodules.util.numpy_support import vtk_to_numpy as vton from vtkmodules.util.numpy_support import numpy_to_vtk as ntov import numpy as np ## Fetch the filter inputs and outputs: input0 = vtkDataSet.GetData(inInfoVec[0], 0) output = vtkDataSet.GetData(outInfoVec, 0) output.ShallowCopy(input0) ## Fetch input arrays cd = output.GetCellData() vmask = cd.GetArray('mask') vsaturation = cd.GetArray('saturation') vporosity = cd.GetArray('porosity') vpressure = cd.GetArray('pressure') vspecstor = cd.GetArray('specific storage') if vmask == None or vsaturation == None or \ vporosity == None or vpressure == None or \ vspecstor == None: print('Error: A required array was not present.') return 0 ## Compute the volume of each cell: mqf = mq() mqf.SetHexQualityMeasureToVolume() mqf.SetInputDataObject(0, output) mqf.Update() volume = vton( mqf.GetOutputDataObject(0).GetCellData().GetArray('Quality')) ## Get NumPy versions of each array so we can do arithmetic: mask = vton(vmask) saturation = vton(vsaturation) porosity = vton(vporosity) pressure = vton(vpressure) specstor = vton(vspecstor) ## Compute the subsurface storage as sbs ## and store it as field data. sbs = np.sum(pftools.computeSubsurfaceStorage( \ saturation, pressure, volume, porosity, specstor)) arr = ntov(sbs) arr.SetName('subsurface storage') arr.GetInformation().Set(e2r.GLOBAL_TEMPORAL_VARIABLE(), 1) output.GetFieldData().AddArray(arr) return 1
def RequestInformation(self, request, inInfoVec, outInfoVec): from vtkmodules.vtkCommonDataModel import vtkDataSet executive = self.GetExecutive() input0 = vtkDataSet.GetData(inInfoVec[0], 0) self.vars = [] for i in range(input0.GetCellData().GetNumberOfArrays()): self.vars.append(input0.GetCellData().GetArray(i).GetName()) return 1
def RequestData(self, request, inInfoVec, outInfoVec): from vtkmodules.vtkCommonDataModel import vtkDataSet vtk_data = vtkDataSet.GetData(inInfoVec[0], 0) gdf = _NCubeDataSetToGeoDataFrame(vtk_data) gdf.to_file(filename=self._filename) #gdf.to_file(filename=self._filename, layer="vtk", driver="GPKG") return 1
def RequestData(self, request, inInfoVec, outInfoVec): from vtkmodules.vtkCommonDataModel import vtkTable, vtkDataSet, vtkPolyData from vtkmodules.vtkIOExodus import vtkExodusIIReader as e2r from vtkmodules.vtkFiltersVerdict import vtkMeshQuality as mq from vtkmodules.util.numpy_support import vtk_to_numpy as vton from vtkmodules.util.numpy_support import numpy_to_vtk as ntov import numpy as np ## Fetch the filter inputs and outputs: input0 = vtkDataSet.GetData(inInfoVec[0], 0) input1 = vtkDataSet.GetData(inInfoVec[1], 0) output = vtkDataSet.GetData(outInfoVec, 0) output.ShallowCopy(input1) ## Fetch input arrays cd = output.GetCellData() scd = input0.GetCellData() vmask = scd.GetArray('mask') vsaturation = scd.GetArray('saturation') if vmask == None or vsaturation == None: print('Error: A required array was not present.') return 0 ## Get NumPy versions of each array so we can do arithmetic: mask = vton(vmask) saturation = vton(vsaturation) ## Find the top surface ext = pftools.dataCellExtent(input0) top = pftools.computeTopSurface(ext, mask) ## Compute the water table depth storage as wtd ## and store it as cell data. ps = pftools.dataPointExtent(input0) + (3, ) xx = np.reshape(vton(input0.GetPoints().GetData()), ps) wtd = pftools.computeWaterTableDepth(top, saturation, xx) arr = ntov(wtd) arr.SetName('water table depth') output.GetCellData().AddArray(arr) return 1
def RequestDataObject(self, request, inInfoVec, outInfoVec): """Create a data object of the same type as the input.""" from vtkmodules.vtkCommonDataModel import vtkTable, vtkDataSet, vtkPolyData from vtkmodules import vtkCommonDataModel as dm input0 = vtkDataSet.GetData(inInfoVec[0], 0) opt = dm.vtkDataObject.GetData(outInfoVec) if opt and opt.IsA(input0.GetClassName()): return 1 opt = input0.NewInstance() outInfoVec.GetInformationObject(0).Set(dm.vtkDataObject.DATA_OBJECT(), opt) return 1
def RequestInformation(self, request, inInfoVec, outInfoVec): from vtkmodules.vtkCommonDataModel import vtkDataSet executive = self.GetExecutive() input0 = vtkDataSet.GetData(inInfoVec[0], 0) vars = [] for i in range(input0.GetPointData().GetNumberOfArrays()): vars.append(input0.GetPointData().GetArray(i).GetName()) if 'pdf' in vars: self.vars = ['pdf'] else: self.vars = [] for i in vars: if i != 'pdf': self.vars.append(i) return 1
def RequestData(self, request, inInfoVec, outInfoVec): print("NCubeNetCDF2DWriter") vtk_data = vtkDataSet.GetData(inInfoVec[0], 0) ds = _NCubeDataSetToNetCDF2D(vtk_data) ds.to_netcdf(self._filename) return 1
def RequestData(self, request, inInfo, outInfo): logger.debug("Requesting data...") info = outInfo.GetInformationObject(0) logger.debug(f"Information object: {info}") update_extents = info.Get(self.GetExecutive().UPDATE_EXTENT()) logger.debug( f"Responsible for updating these extents: {update_extents}") output = dsa.WrapDataObject(vtkDataSet.GetData(outInfo)) logger.info("Computing SWSH grid...") start_time = time.time() # Setup grid # TODO: Take the `update_extents` into account to support rendering # in parallel N = self.num_points_per_dim N_y = N // 2 if self.clip_y_normal else N size = self.size spacing = 2.0 * size / N output.SetDimensions(N, N_y, N) output.SetOrigin(*(3 * (-size, ))) output.SetSpacing(*(3 * (spacing, ))) # Compute the SWSHs on the grid swsh_grid, r = swsh_cache.cached_swsh_grid( size=size, num_points=N, spin_weight=self.spin_weight, ell_max=self.ell_max, clip_y_normal=self.clip_y_normal, clip_z_normal=False, cache_dir=self.swsh_cache_dir, ) # Expose radial coordinate to VTK r_vtk = vtknp.numpy_to_vtk(r, deep=False) r_vtk.SetName("RadialCoordinate") output.GetPointData().AddArray(r_vtk) for l in range(abs(self.spin_weight), self.ell_max + 1): for m in range(1, l + 1): mode_profile = (swsh_grid[:, LM_index(l, m, 0)] + swsh_grid[:, LM_index(l, -m, 0)]) mode_name = "Y_l{}_m{}".format(l, m) # Expose complex field to VTK as two arrays of floats mode_real_vtk = vtknp.numpy_to_vtk(np.real(mode_profile), deep=True) mode_imag_vtk = vtknp.numpy_to_vtk(np.imag(mode_profile), deep=True) mode_abs_vtk = vtknp.numpy_to_vtk(np.abs(mode_profile), deep=True) mode_real_vtk.SetName(mode_name + " Real") mode_imag_vtk.SetName(mode_name + " Imag") mode_abs_vtk.SetName(mode_name + " Abs") output.GetPointData().AddArray(mode_real_vtk) output.GetPointData().AddArray(mode_imag_vtk) output.GetPointData().AddArray(mode_abs_vtk) logger.info(f"SWSH grid computed in {time.time() - start_time:.3f}s.") return 1
def RequestData(self, request, inInfoVec, outInfoVec): from vtkmodules.vtkCommonDataModel import vtkTable, vtkDataSet, vtkPolyData from vtkmodules.vtkIOExodus import vtkExodusIIReader as e2r from vtkmodules.vtkFiltersVerdict import vtkMeshQuality as mq from vtkmodules.util.numpy_support import vtk_to_numpy as vton from vtkmodules.util.numpy_support import numpy_to_vtk as ntov import numpy as np ## Fetch the filter inputs and outputs: input0 = vtkDataSet.GetData(inInfoVec[0], 0) input1 = vtkDataSet.GetData(inInfoVec[1], 0) output = vtkDataSet.GetData(outInfoVec, 0) output.ShallowCopy(input0) ## Fetch input arrays cd = output.GetCellData() scd = input1.GetCellData() vmask = cd.GetArray('mask') vsaturation = cd.GetArray('saturation') vporosity = cd.GetArray('porosity') vpressure = cd.GetArray('pressure') vspecstor = cd.GetArray('specific storage') vslope = scd.GetArray('slope') vmannings = scd.GetArray('mannings') if vmask == None or vsaturation == None or \ vporosity == None or vpressure == None or \ vspecstor == None or vslope == None or \ vmannings == None: print('Error: A required array was not present.') return 0 ## Compute the volume of each cell: mqf = mq() mqf.SetHexQualityMeasureToVolume() mqf.SetInputDataObject(0, output) mqf.Update() volume = vton( mqf.GetOutputDataObject(0).GetCellData().GetArray('Quality')) ## Get NumPy versions of each array so we can do arithmetic: mask = vton(vmask) saturation = vton(vsaturation) porosity = vton(vporosity) pressure = vton(vpressure) specstor = vton(vspecstor) slope = vton(vslope) mannings = vton(vmannings) ## Compute the subsurface storage as sbs ## and store it as field data. sbs = np.sum(pftools.computeSubsurfaceStorage( \ saturation, pressure, volume, porosity, specstor)) arr = ntov(sbs) arr.SetName('subsurface storage') arr.GetInformation().Set(e2r.GLOBAL_TEMPORAL_VARIABLE(), 1) output.GetFieldData().AddArray(arr) ## Find the top surface ext = pftools.dataCellExtent(input0) top = pftools.computeTopSurface(ext, mask) itop = pftools.computeTopSurfaceIndices(top) ## Compute the surface storage ps = pftools.dataPointExtent(input0) + (3, ) xx = np.reshape(vton(output.GetPoints().GetData()), ps) sus = np.sum(pftools.computeSurfaceStorage(ext, itop, xx, pressure)) arr = ntov(sus) arr.SetName('surface storage') arr.GetInformation().Set(e2r.GLOBAL_TEMPORAL_VARIABLE(), 1) output.GetFieldData().AddArray(arr) ## Compute the surface runoff slope = np.reshape(slope, top.shape + (2, )) mannings = np.reshape(mannings, top.shape) sro = np.sum( pftools.computeSurfaceRunoff(top, xx, pressure, slope, mannings)) arr = ntov(sro) arr.SetName('surface runoff') arr.GetInformation().Set(e2r.GLOBAL_TEMPORAL_VARIABLE(), 1) output.GetFieldData().AddArray(arr) return 1
def RequestData(self, request, inInfoVec, outInfoVec): import sys, os import vtk from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid, vtkImageData, vtkDataSet from vtk.numpy_interface import dataset_adapter as dsa import ctypes as C import numpy as np if 'HOME' in os.environ: ccode_so = os.environ['HOME'] + '/python/_sample.so' elif 'HOMEPATH' in os.environ: ccode_so = os.environ['HOMEPATH'] + '/python/_sample.so' else: code_so = '_sample.so' if not os.path.isfile(ccode_so): print('can\'t find so:', ccode_so) return ccode = C.CDLL(ccode_so) if not ccode: print('failed to load ', ccode.so) ccode.SampleTetrahedra.argtypes = [ C.c_int, C.c_int, C.c_int, C.c_int, np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"), np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"), np.ctypeslib.ndpointer(C.c_int, flags="C_CONTIGUOUS") ] ccode.SampleRectilinear.argtypes = [ C.c_int, C.c_int, np.ctypeslib.ndpointer(C.c_int, flags="C_CONTIGUOUS"), np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"), np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"), np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"), np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS") ] ccode.SampleVTI.argtypes = [ C.c_int, C.c_int, np.ctypeslib.ndpointer(C.c_int, flags="C_CONTIGUOUS"), np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"), np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"), np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS") ] ccode.GetNumberOfSamples.restype = C.c_int ccode.GetSamples.restype = C.c_void_p obj = dsa.WrapDataObject(vtkDataSet.GetData(inInfoVec[0], 0)) pdf = self.inputArray nSamples = self.nSamples if obj.GetClassName() == 'vtkImageData': a = vtkImageData.GetData(inInfoVec[0], 0) vti = dsa.WrapDataObject(vtkImageData.GetData(inInfoVec[0], 0)) e = vti.VTKObject.GetExtent() o = vti.VTKObject.GetOrigin() s = vti.VTKObject.GetSpacing() dimensions = np.array([(e[2 * i + 1] - e[2 * i]) + 1 for i in range(3)]).astype('i4') origin = np.array([o[i] + e[2 * i] * s[i] for i in range(3)]).astype('f4') spacing = np.array(s).astype('f4') pdf = np.ascontiguousarray(vti.CellData[pdf]).astype('f4') ccode.SampleVTI(0, nSamples, dimensions, origin, spacing, pdf) elif obj.GetClassName() == 'vtkUnstructuredGrid': vtu = dsa.WrapDataObject( vtkUnstructuredGrid.GetData(inInfoVec[0], 0)) points = np.ascontiguousarray(vtu.Points).astype('f4') pdf = np.ascontiguousarray(vtu.CellData[pdf]).astype('f4') tets = np.ascontiguousarray(vtu.Cells).astype('i4') ccode.SampleTetrahedra(0, nSamples, vtu.GetNumberOfPoints(), vtu.GetNumberOfCells(), points, pdf, tets) elif obj.GetClassName() == 'vtkRectilinearGrid': vtr = vtkRectilinearGrid.GetData(inInfoVec[0], 0) x_array = dsa.numpy_support.vtk_to_numpy( vtr.GetXCoordinates()).astype('f4') y_array = dsa.numpy_support.vtk_to_numpy( vtr.GetYCoordinates()).astype('f4') z_array = dsa.numpy_support.vtk_to_numpy( vtr.GetZCoordinates()).astype('f4') pdf = dsa.numpy_support.vtk_to_numpy( vtr.GetCellData().GetArray(pdf)).astype('f4') dim = (len(x_array), len(y_array), len(z_array)) ccode.SampleRectilinear(0, dim, x_array, y_array, z_array, pdf) else: print( "SampleCellDensity requires vtkImageData or vtkUnstructuredGrid" ) return 0 n = ccode.GetNumberOfSamples() s = np.ctypeslib.as_array(C.cast(ccode.GetSamples(), C.POINTER(C.c_float)), shape=(n, 3)) samples = dsa.WrapDataObject(vtk.vtkUnstructuredGrid()) co = dsa.numpy_support.numpy_to_vtkIdTypeArray( np.arange(n).astype('i8') * 2) ca = vtk.vtkCellArray() ca.SetCells( n, dsa.numpy_support.numpy_to_vtkIdTypeArray( np.column_stack(([1] * n, range(n))).reshape( (2 * n, )).astype('i8'))) ct = dsa.numpyTovtkDataArray( np.array([vtk.VTK_VERTEX] * n).astype('u1')) samples.VTKObject.SetCells(ct, co, ca) samples.Points = dsa.numpy_support.numpy_to_vtk(s, deep=1) outpt = vtkUnstructuredGrid.GetData(outInfoVec, 0) outpt.ShallowCopy(samples.VTKObject) ccode.Cleanup() return 1
def RequestData(self, request, inInfoVec, outInfoVec): print ("NCubeGeoPackageWriter") vtk_data = vtkDataSet.GetData(inInfoVec[0], 0) gdf = _NCubeDataSetToGeoDataFrame(vtk_data, extent_only=True) gdf.to_file(filename=self._filename, layer="vtk", driver="GPKG") return 1
def RequestData(self, request, inInfoVec, outInfoVec): print ("NCubeShapefileWriter") vtk_data = vtkDataSet.GetData(inInfoVec[0], 0) gdf = _NCubeDataSetToGeoDataFrame(vtk_data) gdf.to_file(filename=self._filename) return 1