Python vtk.vtkXMLUnstructuredGridReader() Examples

The following are 2 code examples of vtk.vtkXMLUnstructuredGridReader(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module vtk , or try the search function .
Example #1
Source File: vtk_file_converter.py    From handyscripts with MIT License 6 votes vote down vote up
def convert_vtk_file(vtk_file, plt_file, strand=None, solution_time=None):
    reader = None
    if vtk_file.endswith(".vtu"):
        reader = vtk.vtkXMLUnstructuredGridReader()
    elif vtk_file.endswith(".vtp"):
        reader = vtk.vtkXMLPolyDataReader()
    elif vtk_file.endswith(".vts"):
        reader = vtk.vtkXMLStructuredGridReader()
    elif vtk_file.endswith(".vti"):
        reader = vtk.vtkXMLImageDataReader()
    reader.SetFileName(vtk_file)
    reader.Update()
    vtk_dataset = reader.GetOutput()
    tp.new_layout()
    tecplot_dataset = tp.active_frame().dataset
    add_vtk_dataset(vtk_dataset, tecplot_dataset)
    for z in tecplot_dataset.zones():
        z.name = os.path.basename(vtk_file)
        if strand and solution_time:
            z.strand = strand
            z.solution_time = solution_time
    tp.data.save_tecplot_plt(plt_file, dataset=tecplot_dataset) 
Example #2
Source File: plot_cosipy_fields_vtk.py    From cosipy with GNU General Public License v3.0 4 votes vote down vote up
def add_scalar(var, timestamp):

    vtkFile = vtk.vtkXMLUnstructuredGridReader()
    vtkFile.SetFileName('cosipy.vtu')
    vtkFile.Update()
    
    # Find cellId by coordinates
    pointLocator =  vtk.vtkPointLocator()
    pointLocator.SetDataSet(vtkFile.GetOutput())
    pointLocator.BuildLocator()
    
    ds = xr.open_dataset('../data/output/Peru_20160601-20180530_comp4.nc')
    ds = ds.sel(time=timestamp)
    
    ds_sub = ds[var].stack(x=['south_north','west_east']) 
    ds_sub = ds_sub.dropna(dim='x')
    lats = ds_sub.x.lat.values
    lons = ds_sub.x.lon.values
    data = ds_sub.values
    print(lats)

    numPoints = vtkFile.GetOutput().GetNumberOfPoints()
    scalar = np.empty(numPoints)
    scalar[:] = np.nan

    interpField = numpy_support.numpy_to_vtk(scalar)
    interpField.SetName(var)
    vtkFile.GetOutput().GetPointData().AddArray(interpField)
    vtkFile.Update()

    print('Write points \n')
    for i in np.arange(len(data)):
        # Get height
        alt = ds.HGT.sel(lat=lats[i],lon=lons[i]).values/6370000.0
        
        pointId = vtk.mutable(0) 
        Id = vtk.vtkIdList()
        pointId = pointLocator.FindClosestPoint([lons[i],lats[i],alt])
        vtkFile.GetOutput().GetPointData().GetArray(var).InsertTuple1(pointId,data[i])

    writer = vtk.vtkXMLUnstructuredGridWriter()
    writer.SetFileName('cosipy.vtu')
    writer.SetInputData(vtkFile.GetOutput())
    writer.Write()

    #plotSurface(vtkFile)