from fluiddyn.util.serieofarrays import SeriesOfArrays

This class works with a serie of files (or a file containing a serie of arrays) so we first need to create files. For this demo, we just create emtpy files.

import tempfile
from pathlib import Path
from shutil import rmtree
from pprint import pprint
path_dir = Path(tempfile.mkdtemp('_singleframe'))

for i0 in range(6):
    with open(path_dir / f'image{i0}.png', 'w') as f:
print([ for p in path_dir.rglob("*")])
['image2.png', 'image4.png', 'image3.png', 'image5.png', 'image0.png', 'image1.png']

We write a simple function to print the subsets of files that we are going to create…

def print_subsets(series):
    for serie in series:
        print('(', end='')
        for name in serie.iter_name_files():
            print(name, end=', ')

We show that we can create many different subsets quite easily:

series = SeriesOfArrays(path_dir / 'im*', 'i:i+2')
(image0.png, image1.png, )
(image1.png, image2.png, )
(image2.png, image3.png, )
(image3.png, image4.png, )
(image4.png, image5.png, )
series = SeriesOfArrays(path_dir / 'im*', 'i:i+2', ind_step=2)
(image0.png, image1.png, )
(image2.png, image3.png, )
(image4.png, image5.png, )
series = SeriesOfArrays(path_dir / 'im*', 'i:i+3', ind_stop=3)
(image0.png, image1.png, image2.png, )
(image1.png, image2.png, image3.png, )
(image2.png, image3.png, image4.png, )
series = SeriesOfArrays(path_dir / 'im*', 'i:i+3:2')
(image0.png, image2.png, )
(image1.png, image3.png, )
(image2.png, image4.png, )
(image3.png, image5.png, )
rmtree(path_dir, ignore_errors=True)

Let’s consider another serie of files this time with two indices:

path_dir = Path(tempfile.mkdtemp('_doubleframe'))

for i0 in range(3):
    with open(path_dir / f'im_{i0}a.png', 'w') as f:
    with open(path_dir / f'im_{i0}b.png', 'w') as f:
print([ for p in path_dir.rglob("*")])
['im_0a.png', 'im_2a.png', 'im_2b.png', 'im_1a.png', 'im_1b.png', 'im_0b.png']

Creating subsets of files is still very simple:

series = SeriesOfArrays(path_dir / 'im*', 'i, 0:2')
(im_0a.png, im_0b.png, )
(im_1a.png, im_1b.png, )
(im_2a.png, im_2b.png, )
series = SeriesOfArrays(path_dir / 'im*', '0:2, i')
(im_0a.png, im_1a.png, )
(im_0b.png, im_1b.png, )

Of course we can do many more things with these objects:

pprint([name for name in dir(series) if not name.startswith('__')])
pprint([name for name in dir(series.serie) if not name.startswith('__')])
rmtree(path_dir, ignore_errors=True)