Author Topic: A python script to compress Paraview VTU files  (Read 5290 times)

resammc

  • Full Member
  • ***
  • Posts: 95
A python script to compress Paraview VTU files
« on: July 14, 2020, 11:49:30 AM »
Dear FEAP Users,

I have prepared a simple Python script which helps in compressing the VTU files created by FEAP for Paraview. This is already included in the latest version of our NPVI add-on for IGA post-processing (http://feap.berkeley.edu/forum/index.php?topic=1542.0), but since it could be of use for all the other classical FE users, here is the script:

Code: [Select]
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***")

The above Python script goes through all the non-compressed VTU files and compresses them. This usually reduces the final file-size by around 80%. The generated Python script (npvi_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 npvi_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.

Edit: added the possible compressor types
« Last Edit: July 15, 2020, 10:40:34 AM by resammc »

Prof. S. Govindjee

  • Administrator
  • FEAP Guru
  • *****
  • Posts: 1164
Re: A python script to compress Paraview VTU files
« Reply #1 on: July 14, 2020, 01:55:49 PM »
Maybe you should also make a Wiki entry for this routine.

resammc

  • Full Member
  • ***
  • Posts: 95
Re: A python script to compress Paraview VTU files
« Reply #2 on: July 15, 2020, 10:39:05 AM »
Thanks a lot for the suggestion. I added it to the tutorials section of the Wiki:
http://feap.berkeley.edu/wiki/index.php?title=Tutorials

I hope that I did everything correctly...