PythonCompressVTUFiles

From FEAP Wiki
Jump to navigation Jump to search

The following Python script goes through all the non-compressed VTU files in the current working directory and compresses them. The script (vtkcomp.py) can be executed using pvpython or pvbatch included in the Paraview installation in the bin folder. You may want to add this folder to your operating system's path to be able to directly issue pvbatch vtkcomp.py.

Moreover, by default, the compressed files have a Prefix Comp_. This can be changed in the code. Using no prefix means that the non-compressed file will be replaced by the compressed one.

from paraview.simple import *
import os
import fnmatch

# get the current directory path
cwd = os.getcwd()
 
# find all the vtu files in the current directory
files = fnmatch.filter(os.listdir(cwd), "*.vtu")
files.sort()
# print the names of the files (to be converted) on the screen
print("Found:")
print(files)
 
for fileno, entry in enumerate(files):
  print("==========")
  print("Processing File", fileno + 1, "of", len(files))
  # check the file to see if it is already compressed or not
  print("Checking: ", entry)
  fline = open(entry).readline().rstrip()
  if ("compressor" in fline or os.path.isfile("Comp_"+entry)):
    print(entry, "is already compressed! skipping to the next file...")
    # if already compressed, skip the file
    continue
  print("Converting: ", entry)
  vtu_file = XMLUnstructuredGridReader(FileName=[entry])
  # Compressor types:
  # 1: vtkZLibDataCompressor, 2: vtkLZ4DataCompressor , 3: vtkLZMADataCompressor
  writer = XMLUnstructuredGridWriter \
  (FileName=["Comp_"+entry],CompressorType=3,CompressionLevel=9)
  writer.DataMode="Binary" # appended, ascii
  writer.UpdatePipeline()
  print("Done.")
 
print("==========")
print("***FINISHED***")