|
5 | 5 | import matplotlib as mpl |
6 | 6 | from mpl_toolkits.mplot3d import art3d |
7 | 7 | from matplotlib import pyplot as plt |
| 8 | +from matplotlib import use |
8 | 9 | import numpy as np |
9 | 10 | import json |
| 11 | +import sys |
| 12 | +import os |
10 | 13 |
|
11 | 14 | from util import (parse_multiline, rotate, rotate_points, draw_circle, get_line, |
12 | 15 | draw_box, draw_sphere, draw_cylinder, draw_disc, rotate_xyz, draw_cone, draw_hollow_box, draw_annulus, draw_new_circle) |
|
47 | 50 | x_max_polygon = y_max_polygon = z_max_polygon = float('-inf') |
48 | 51 | x_min_polygon = y_min_polygon = z_min_polygon = float('inf') |
49 | 52 |
|
| 53 | +def dumpfile(frmat, filename = None): |
| 54 | + """ save current fig to softcopy """ |
| 55 | + from pylab import savefig |
50 | 56 |
|
51 | | -def parse_trace(): |
| 57 | + if filename is None: |
| 58 | + filename = "display_matplotlib" |
| 59 | + |
| 60 | + # get directory, basename and extension |
| 61 | + if isinstance(filename, list): |
| 62 | + filename = filename[0] |
| 63 | + basename = os.path.splitext(filename)[0] # contains full path and base filename |
| 64 | + if frmat is None: |
| 65 | + ext = os.path.splitext(filename)[1] # extension with the dot |
| 66 | + else: |
| 67 | + ext = frmat |
| 68 | + if ext[0] != '.': |
| 69 | + ext = '.'+ext |
| 70 | + # assemble target name |
| 71 | + saveto = basename + ext |
| 72 | + |
| 73 | + # Check for existance of earlier exports |
| 74 | + if os.path.isfile(saveto): |
| 75 | + index=1 |
| 76 | + saveto = f"{basename}_{index}{frmat}" |
| 77 | + while os.path.isfile(saveto): |
| 78 | + index += 1 |
| 79 | + saveto = f"{basename}_{index}{frmat}" |
| 80 | + |
| 81 | + savefig(saveto) |
| 82 | + print("Saved " + saveto) |
| 83 | + |
| 84 | +def parse_trace(backend): |
52 | 85 | ''' Parse McStas trace output from stdin and write results |
53 | 86 | to file objects csv_comps and csv_lines ''' |
54 | 87 |
|
| 88 | + if backend is not None: |
| 89 | + use(backend) |
| 90 | + if backend in ('pdf', 'pgf', 'ps', 'svg'): |
| 91 | + use('template') |
| 92 | + |
55 | 93 | mpl.rcParams['legend.fontsize'] = 10 |
56 | 94 |
|
57 | 95 | ax = plt.figure(figsize=plt.figaspect(0.5) * 1.5).add_subplot(projection='3d') |
58 | | - ax.set(xlabel='z', ylabel='x', zlabel='y') |
59 | | - try: |
60 | | - ax.set_aspect('equal') |
61 | | - except: |
62 | | - print("manual aspect not supported") |
| 96 | + ax.set(xlabel='z / [m]', ylabel='x / [m]', zlabel='y / [m]') |
63 | 97 |
|
64 | 98 | color = 0 |
65 | 99 |
|
@@ -174,9 +208,15 @@ def parse_trace(): |
174 | 208 | else: |
175 | 209 | print(line) |
176 | 210 |
|
| 211 | + ax.set_box_aspect(None) |
177 | 212 | set_axis_limits(ax) |
178 | 213 |
|
179 | | - plt.show() |
| 214 | + try: |
| 215 | + plt.show() |
| 216 | + if backend in ('pdf', 'pgf', 'ps', 'svg'): |
| 217 | + dumpfile(backend) |
| 218 | + except KeyboardInterrupt: |
| 219 | + pass |
180 | 220 |
|
181 | 221 |
|
182 | 222 | def process_circle(ax, line, color, comp, transparency): |
@@ -427,4 +467,7 @@ def set_axis_limits(ax): |
427 | 467 |
|
428 | 468 |
|
429 | 469 | if __name__ == '__main__': |
430 | | - parse_trace() |
| 470 | + backend=None |
| 471 | + if len(sys.argv) > 1: |
| 472 | + backend = sys.argv[1] |
| 473 | + parse_trace(backend) |
0 commit comments