paraview python scripting equivalent of File->save Data
Asked Answered
C

2

9

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?

Cornela answered 25/7, 2013 at 9:45 Comment(0)
D
10

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()
Deuterium answered 31/7, 2013 at 23:41 Comment(1)
Thanks! It just has to say "CreateWriter('foo.csv', reader)", then it works for meCornela
C
4

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. """"
Cleliaclellan answered 30/7, 2013 at 12:42 Comment(1)
For directory you could use glob.Morris

© 2022 - 2024 — McMap. All rights reserved.