[Paraview] Paraview-Python Looping over multiple state files

Neal,Christopher R chrisneal at ufl.edu
Mon Oct 6 14:14:55 EDT 2014


Hello,


This is my first time using the Paraview listserv.


I am working on a python script to automate the production of images(for purposes to making short videos from a data set).  I am using Paraview version 4.1.0-64bit.  My data is in an Ensight Gold format, which Paraview seems to read in just fine.


Here is my thought process:

1.) Open a file that has a list of state file names and store the list of state files. (rflu_states.inp)

2.) Load my Ensight data using the Paraview EnsightReader()

3.) Load a state file.

(http://www.paraview.org/Wiki/ParaView/Python_Scripting#Loading_State_and_Manipulating_It)<http://www.paraview.org/Wiki/ParaView/Python_Scripting#Loading_State_and_Manipulating_It>

4.) Loop over all times in the data and export an image for each time step in the data from Step 2.

5.) Close the state file and delete any data that may cause a memory leak(Basically to reset paraview for the next state file to be read in)

6.) Return to Step 3.


Issues that I am having:

1.) I can't seem to delete all of the data from each state file once I am done using it. (I see the memory usage keep creeping up when each state file loads)

2.) When I run the script I notice that Paraview keeps opening up new layout windows, but I never explicitly tell it to do that.

Does anyone have any experience with using Paraview like this?

I have been using the Paraview User's Manual, the Paraview Wiki, and the Paraview Trace feature to make my script.


Here is an old post from someone with a similar issue as me, but I don't think their problem ever got resolved:

http://public.kitware.com/pipermail/paraview/2012-November/026756.html

Here is my script so far:


#import os options
import os
import errno

#Load Paraview Python libraries
#from paraview.simple import *

#Image Extension
extension = '.png'

#Max Image Number Indicator
ImNum='000' # for padding image numbers with 0s

#Filename where state files are to be read from
fname = "rflu_states.inp"


#Count the number of lines in the rflu_states.inp file(gives the number of states to load)
num_lines = sum(1 for line in open(fname))

#Error Check: Print the number of lines that were read from file
print("\nNumber of lines read from input file, %s , is: %d " %(fname,num_lines) )

#Store list of state files to be loaded
with open(fname) as f:
    state_names = f.read().splitlines()

#Strip newline characters from the end of each line
state_names = [x.strip('\n') for x in state_names]


#Error Check: Print stored state names to screen
print("\nFollowing state files will be processed: " )
for x in range(0,num_lines):
        print("%s" %(state_names[x]) )

print("\n")


#Loop over all state files in rflu_states.inp
for bigLoop in range(0,num_lines):

   #Print state number and state name
   print( "Loading State %d: %s" %(bigLoop+1,state_names[bigLoop]) )

   #Call Paraview Ensight Data Reader on case file in current directory
   reader = EnSightReader(CaseFileName='cylds.case.00001')

   #Get all timesteps that are in data file
   timesteps = reader.TimestepValues

   #Load the State
   servermanager.LoadState(state_names[bigLoop])

   #Store directory name from state file name
   dirname = state_names[bigLoop] #Store loop state name
   start = dirname.find('_') + 1 #Find location of first _
   end = dirname.find('.',start) #Find location of . after first _
   dirname = dirname[start:end] #Take region between _ and .

   #Error Check: Display directory name that will be created
   print("Images for State, %s, are output to directory: %s" %(state_names[bigLoop],dirname))

   #Check if output directory exists. Create one if it does not
   try:
       os.makedirs(dirname)
   except OSError:
       if os.path.isdir(dirname):
           # We are nearly safe
           pass
       else:
           # There was an error on creation, so make sure we know about it
           raise

   #Loop over all time steps and output images
   for TimeStepNum in range(0,len(timesteps)):

      #Determine padding zeros for image
      if TimeStepNum+1 > 999:
         zero_pad = ''
      elif TimeStepNum+1 > 99:
         zero_pad = ImNum[:1]
      elif TimeStepNum+1 > 9:
         zero_pad = ImNum[:2]
      else:
         zero_pad = ImNum

      #Store active view
      view = GetActiveView()

      view.ViewTime = timesteps[TimeStepNum]

      #Render current view
      Render()


      #Adjust image name be of the of the form <name>_<number>.extension(png, jpg, etc)
      ImageName = dirname + '_' + zero_pad + str(TimeStepNum+1) + extension

      print("Printing Image %d of %d " %(TimeStepNum+1,len(timesteps)) )

      #Append directory path to image name to prepare for output
      outputDir = dirname + '/' +ImageName

      #Write image of current view to file
      WriteImage(outputDir)

   #Delete all sources(To clean up memory)
   for f in GetSources().values():
      Delete(f)

   #Delete the reader data(To clean up memory)
   Delete(reader)
   del(reader)





Thank you,


Christopher R. Neal
Graduate Student
Center for Compressible Multiphase Turbulence
Mechanical and Aerospace Engineering Department
University of Florida
Cell: (863)-697-1958
E-mail:  chrisneal at ufl.edu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/paraview/attachments/20141006/d907b24f/attachment.html>


More information about the ParaView mailing list