I would like to automate exporting csv files from vtk/vtu files.
Right now, the steps I take are:
this writes a csv file for each timesteps with the name basefilename#timestepno#.csv
Is there a way to do this from the commandline (there's no X server on the computer that's doing the computations), eg using the python interface?
To save the render image from a view in paraview , use the File > Save Screenshot menu option. When selected, a file dialog will appear where you can select the file path and format to which the screenshot should be saved. After selecting the image file, the Save Screenshot Options dialog (Fig. 8.3) will be shown.
To get started with ParaView from python we recommend that you: use Tools->Start Trace in the desktop application to record a python script. try a few scripts in the Tools->Python Shell of the desktop application. use the pvpython command line executable instead of the desktop application.
Scripting and extensibility ParaView is fully scriptable using the simple but powerful Python language. ParaView's data engine, called server manager, is fully accessible through the Python interface.
The underlying visualization pipeline that ParaView uses comes from VTK. The visualization pipeline is responsible for constructing a geometric representation of the data set that is then rendered by the graphics hardware. The visualization pipeline transforms informational data into graphical data.
Try the following in either the Python Shell in the UI or using the pvpython or pvbatch Python executables.
from paraview import simple
reader = simple.OpenDataFile("..../foo.pvd")
writer = simple.CreateWriter("..../foo.csv", reader)
writer.WriteAllTimeSteps = 1
writer.FieldAssociation = "Points"
writer.UpdatePipeline()
I had a similar problem with pvtu files and solved it with the script below. I run the script using execfile("SCRIPTNAME") in Paraview -> Tools -> Python Shell. I hope this helps.
-- Reinhard
from paraview.simple import *
import os
"""Function that counts number of files
with specific extension in directory """
def directory(path,extension):
list_dir = []
list_dir = os.listdir(path)
count = 0
for file in list_dir:
if file.endswith(extension): # eg: '.txt'
count += 1
return count
"""Choose input/output directory and filenames"""
pvtu_input_directory = "thin_1000_0.4/mesh/"
csv_output_directory = "thin_1000_0.4/csv/"
input_filename_root = "output"
output_filename_root = "output"
""" Create output directory """
os.system('mkdir '+csv_output_directory)
"""Start Loop over all files """
number_of_pvtu = directory(pvtu_input_directory,'.pvtu')
for index in range(1,number_of_pvtu):
in_filename = input_filename_root + "%0.4d" % index+".pvtu"
out_filename = output_filename_root + "%0.4d" % index +".csv"
loadfile = pvtu_input_directory + in_filename
writefile = csv_output_directory + out_filename
r = XMLPartitionedUnstructuredGridReader( FileName=loadfile)
writer = CreateWriter(writefile,r)
writer.FieldAssociation = "Points"
writer.UpdatePipeline()
""" That's it. """"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With