Read vtk binary file, modify the data and write

Dear all,

I am trying to read a .vts file in the attachment using python.

test.vts (2.5 KB)

It is a 10x5 2D mesh with 1 degree of freedom on each point.
Since it is indeed a periodic domain, I want to add another row and column to the original mesh and the corresponding degree of freedom.
So I read and modify the data like this:

import numpy as np
from paraview.simple import *
from paraview import numpy_support as ns

# create a new 'XML Structured Grid Reader'
testvts = XMLStructuredGridReader(registrationName='test.vts', FileName=['test.vts'])
data = paraview.servermanager.Fetch(testvts)
nx, ny, nz = data.GetDimensions()
coords = ns.vtk_to_numpy(data.GetPoints().GetData())
u = ns.vtk_to_numpy(data.GetPointData().GetArray('Unnamed.u'))

dx = 2
dy = 1
dz = 0

# deal with coordinates
coords = coords.T.reshape(3, ny, nx)
# x-direction
x_final = coords[:,:,None,0].copy()
x_final[0,:,0] += dx
coords = np.concatenate((coords, x_final), axis=2)
# y-direction
y_final = coords[:,None,0,:].copy()
y_final[1,0,:] += dy
coords = np.concatenate((coords, y_final), axis=1)
coords = coords.reshape(3, -1).T

newPoints = paraview.vtk.vtkPoints()
for i, ci in enumerate(coords):
    newPoints.InsertPoint(i, ci[0], ci[1], ci[2])
# print(ns.vtk_to_numpy(newPoints.GetData()))

# deal with point data
u = u.reshape(ny, nx)
# x-direction
u = np.concatenate((u, u[:,None,0]), axis=1)
# y-direction
u = np.concatenate((u, u[None,0,:]), axis=0)
u = u.reshape(1, -1)

Now I have searched around but have no idea how to write the .vts file.
I tried the following but don’t know how to proceed.

data.SetDimensions(nx+1, ny+1, nz)
data.SetPoints(newPoints)
writer = XMLStructuredGridWriter(FileName=['post_test.vts'])

Can you tell me the direction?
Thank you in advance!

Junming

You can create a new vtkStructuredGrid and use this with the writer. See here how to populate a new vtkStructuredGrid