Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paraview python scripting equivalent of File->save Data

I would like to automate exporting csv files from vtk/vtu files.

Right now, the steps I take are:

  • open paraview
  • load in the pvd file that stores the information about all vtu files (one for each time steps in my PDE simulation)
  • goto Properties tab on the left, hit 'apply'
  • File->save Data... provide a base file name, select 'points' and 'write all timesteps'

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?

like image 986
Hans Avatar asked Jul 25 '13 09:07

Hans


People also ask

How do I save a file in ParaView?

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.

How do I run a Python script in ParaView?

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.

Is ParaView written in Python?

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.

How does ParaView work?

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.


2 Answers

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()
like image 131
Utkarsh Avatar answered Oct 22 '22 09:10

Utkarsh


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. """"
like image 24
Reinhard Avatar answered Oct 22 '22 08:10

Reinhard