-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplot_mesh.py
executable file
·70 lines (59 loc) · 2.05 KB
/
plot_mesh.py
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
70
#! /usr/bin/env python
# Script for plotting mesh structure in mesh_structure.dat (default name) file
# produced by running Athena++ with "-m" argument.
# Can optionally specify "-i <input_file>" and/or "-o <output_file>". If -o
# argument is omitted, output defaults to display on screen rather than
# saving to file.
# Run "plot_mesh.py -h" for help.
# Python modules
import argparse
# Main function
def main(**kwargs):
# Extract inputs
input_file = kwargs['input']
output_file = kwargs['output']
# Load Python plotting modules
if output_file != 'show':
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
# not used explicitly, but required for 3D projections
from mpl_toolkits.mplot3d import Axes3D # noqa
# Read and plot block edges
fig = plt.figure()
ax = fig.gca(projection='3d')
x = []
y = []
z = []
with open(input_file) as f:
for line in f:
if line[0] != '\n' and line[0] != '#':
numbers_str = line.split()
x.append(float(numbers_str[0]))
y.append(float(numbers_str[1]))
# append zero if 2D
if (len(numbers_str) > 2):
z.append(float(numbers_str[2]))
else:
z.append(0.0)
if line[0] == '\n' and len(x) != 0:
ax.plot(x, y, z, 'k-')
x = []
y = []
z = []
if output_file == 'show':
plt.show()
else:
plt.savefig(output_file, bbox_inches='tight')
# Execute main function
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input',
default='mesh_structure.dat',
help='name of input (mesh structure) file')
parser.add_argument('-o',
'--output',
default='show',
help='image filename; omit to display to screen')
args = parser.parse_args()
main(**vars(args))