Selecting multiple pvtu files

Hello!

I have a question about the best way to load a large number of time series .pvtu files into paraview using a python script.

My files are of the format:

data_0.pvtu
data_1.pvtu…

There are 200 of them in this particular situation.

I used the python trace tool to capture the events as I selected the series in the interface. In the gui it is as simple as just selecting the “data…pvtu” series. I was surprised that in the python trace each individual file was specified:

data_ = XMLPartitionedUnstructuredGridReader(FileName=[‘data_0.pvtu’, ‘data_1.pvtu’… ‘data_200.pvtu’])

Yikes.

So my questions are:

  1. Is there a way to wildcard the file names so that each does not need to be individually specified

or

  1. Can I pass a list of strings to the reader instead? Then I could just use a loop to build up a list from a base file name.

or

  1. Is there a completely better way of doing this that I am not seeing?

My end goal is to do some complicated analysis on a large amount of files.

Best,

-A

I forgot to add I am using Paraview 5.5.2

Just use python glob module

import glob
data_ = XMLPartitionedUnstructuredGridReader(FileName=glob.glob('data_*.pvtu’'))

Thank you, I think that is definitely a great approach to start with.

The python docs state that glob returns value in an arbitrary order. On my system the certainly don’t seem to be ordered how you would expect - the resulting list is neither in alpha order nor sequential order. So it’s not useful directly for forming a time series. But glob does return a list, and that list can then be sorted prior to feeding to XMLPartitionedUnstructuredGridReader()

Thanks again,

1 Like

Thanks for the precision about sorting.

Just a final followup in case anybody else has a similar need:

Ned Bachelder has a blog which explains a nifty “natural sorting” method that works very well for this particular problem. It seems to work very well in sorting strings of the form (alpha)_(number) in to the order that you would expect them to be. Below is a link to his blog as well as the code:

https://nedbatchelder.com/blog/200712/human_sorting.html

import re

def tryint(s):
    try:
        return int(s)
    except ValueError:
        return s
    
def alphanum_key(s):
    """ Turn a string into a list of string and number chunks.
        "z23a" -> ["z", 23, "a"]
    """
    return [ tryint(c) for c in re.split('([0-9]+)', s) ]

def sort_nicely(l):
    """ Sort the given list in the way that humans expect.
    """
    l.sort(key=alphanum_key)

For my code I assigned the glob statement to a variable called file_list:

file_list = glob.glob(...)

and then sorted that file_list:

sort_nicely(file_list)

Hopefully this will help others in the future.

Best,

1 Like