-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathopen_multi_files_in_dir.py
More file actions
69 lines (58 loc) · 2.67 KB
/
open_multi_files_in_dir.py
File metadata and controls
69 lines (58 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
A script opening all files of a certain extension in a folder
General usage:
> python -O .\open_multi_files_in_dir.py
Example usage with Linux batch mode:
> /path/to/tecplot360/bin/tec360-env -- python -O open_multi_files_in_dir.py
Necessary modules
-----------------
sys
A module that provides variables and functions to manipulate the Python runtime environment
glob
A module that can capture patterns and supports recursive wildcards
os
A module that provides functions to interact with the operating system
Description
-----------
This script can be used to loop through opening, plotting, and closing all the data files of a
particular file extension. This script can also be altered to append all the files in a directory
to a single data set. Currently, it is written to open .szplt files (this can be updated by
updating the load_tecplot_szl() and glob() functions).
To run this script in connected mode, we must first enable PyTecplot Connections via the
Scripting menu (Scripting>PyTecplot Connections...). Then add a "-c" after the .py on the commandline.
Note, this script uses f-strings, so if you have a version of Python before 3.6,
you may need to use str.format() instead.
"""
import tecplot as tp
import glob
import sys
import os
# This script will run in batch mode by default. Use the -c commandline argument for connected mode:
if '-c' in sys.argv:
tp.session.connect()
# Load multiple files with the same extension from the following directory:
# directory = r"path/to/datafiles"
# Or you can cd to the directory in the terminal with the data files, run the script and use the
# following to get the path of the current working directory:
directory = os.getcwd()
# glob the files in that directory based on extension:
files = glob.glob(os.path.join(directory, "*.szplt"))
# Loop over the globbed files. These files can appended to one data set or
# opened individually with tp.new_layout()--the latter is demonstrated below:
for file in files:
tp.new_layout()
tp.data.load_tecplot_szl(file) #change this loader based on your needs
print(f"Loaded: {file}")
# ....
# do something with the plot (set style, write data, export images,...)
# ....
# for example, show contour/slice and export an image:
plot = tp.active_frame().plot()
plot.contour(0).variable_index=2 #setting contour group 1 variable
plot.show_contour=True
plot.show_slices=True
# ....
# export an image:
image_file = os.path.join(directory, os.path.basename(file)+".png") #.png path to the same dir as the data files
print(f"Saving an image to: {image_file}\n")
tp.export.save_png(f"{directory}/{os.path.basename(file)}.png") #saves the .png