Adding data array to existing .vtu file

Hi,

I’m trying to add a simple array of data to an existing .vtu file. I was trying to do this with the following code in Python prior to importing the data to ParaView:

import vtk
from vtk.numpy_interface import dataset_adapter as dsa

# Read in the existing VTU file
fileName = os.path.join(full_model_output_path, "dis.vtu")
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName(fileName)
reader.Update()
mesh = reader.GetOutput()

# Add data array and write the new VTK file
meshNew = dsa.WrapDataObject(mesh)
hsuArray = np.zeros((5,40,40))
meshNew.CellData.append(hsuArray, "HSU")
writer = vtk.vtkXMLUnstructuredGridWriter()
writer.SetFileName("New.vtu")
writer.SetInputData(meshNew.VTKObject)
writer.Write()

…but the resulting file seems to have appended the entire array to every single cell:
image

What I was hoping for was for each cell to take the value ‘0’ (and once I had that working I was going to change the values to reflect the layer in the unstructured grid.

Can anyone tell what is wrong such that the entire array is being appended to each cell? Whilst the grid is technically unstructured, it’s actually a well-defined row/col/layer situation with node numbers…so this shouldn’t be too hard?

Hello,

It seems to work well if you convert hsuArray to a 1-D array using the ravel or flatten function.

hsuArray = np.zeros((5,40,40)).ravel()
1 Like

Thanks! This worked perfectly. I just need to be sure the length of the arrays matches exactly.