-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_images.py
219 lines (189 loc) · 5.74 KB
/
make_images.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import argparse
import sys
import warnings
from pathlib import Path
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from utils import Params, make_dataclass_from_args
from pyminiweather.mesh import MeshData
plt.set_cmap("Spectral")
def get_parser():
parser = argparse.ArgumentParser(
description="This is a post-processing tool that makes images "
"from the file written by PyMiniWeather. The images "
"can then be concatenated to make a gif using ImageMagick. "
"Make sure to set output-freq > 0 in PyMiniWeather "
"and that a sub-directory named images exists within "
"the directory passed to the script."
)
parser.add_argument(
"--nx",
type=int,
default=200,
dest="nx",
help="Number of points in x-direction (default: 200)",
)
parser.add_argument(
"--nz",
type=int,
default=100,
dest="nz",
help="Number of points in z-direction (default: 100)",
)
parser.add_argument(
"--xlen",
type=float,
default=2e4,
dest="xlen",
help="Length of domain in x-direction (default: 2e4)",
)
parser.add_argument(
"--zlen",
type=float,
default=1e4,
dest="zlen",
help="Length of domain in Z-direction (default: 1e4)",
)
parser.add_argument(
"--nvariables",
type=int,
default=4,
dest="nvariables",
help="Number of variables in the simulation" " (default: 4)",
)
parser.add_argument(
"--ntimesteps",
type=int,
default=10,
dest="ntimesteps",
help="Number of timesteps in the simulation" " (default: 10)",
)
parser.add_argument(
"--variable-index",
type=int,
default=3,
dest="variable_index",
help="Variable index must be less than number of variables" " (default: 3)",
)
parser.add_argument(
"--directory",
type=str,
default="./",
dest="directory",
help="Directory where the input file is located (default: ./)",
)
parser.add_argument(
"--plot-nlevels",
type=str,
default=128,
dest="plot_nlevels",
help="Number of levels to be used in the plotter" " (default: 128)",
)
parser.add_argument(
"--plot-nticks",
type=str,
default=10,
dest="plot_nticks",
help="Number of ticks to be used in the plotter" " (default: 10)",
)
parser.add_argument(
"--plot-no-colorbar",
action="store_true",
default=False,
dest="plot_no_colorbar",
help="Use this flag to disable plotting the colorbar in the "
"contour plots (default: False)",
)
parser.add_argument(
"--plot-vmin-vmax",
type=float,
nargs=2,
default=None,
dest="plot_vmin_vmax",
help="Provide the vmin and vmax for the plot",
)
parser.add_argument(
"--filename",
type=str,
default="PyMiniWeatherData.txt",
dest="filename",
help="Name of the output file the solution variables"
" will be written to. (default: PyMiniWeatherData.txt)",
)
return parser
# Get a dataclass from Arguments
parser = get_parser()
args, _ = parser.parse_known_args()
params = make_dataclass_from_args(args)
for k, v in params.__dict__.items():
print(f"{k:25s} {v}")
# Create a mesh
mesh_params = {
"nx": params.nx,
"nz": params.nz,
"xlen": params.xlen,
"zlen": params.zlen,
"dx": params.xlen / params.nx,
"dz": params.zlen / params.nz,
"hs": 2,
}
mesh = MeshData(mesh_params)
# sanity checks
image_directory = Path(params.directory) / "images"
if not image_directory.exists():
warnings.warn(
f"Make sure the images sub-directory inside"
f" {params.directory} exists AND writeable"
)
sys.exit()
assert params.nvariables == 4
assert params.variable_index < params.nvariables
# Read the data file
base_filename = params.filename.split(".")[0]
py = np.loadtxt(Path(params.directory) / f"{base_filename}_svars.txt", delimiter=",").reshape(
params.ntimesteps, params.nvariables, params.nz, params.nx
)
# create mesh
mesh_x, mesh_y = mesh.get_mesh_cell_centers()
# setup plotters
padding = 0.10
round_to_decimals = 2
if args.plot_vmin_vmax is None:
vmin = py[0 : params.ntimesteps, params.variable_index].min()
vmax = py[0 : params.ntimesteps, params.variable_index].max()
else:
vmin, vmax = args.plot_vmin_vmax
print(f"Contour vmin/vmax: {vmin}, {vmax}")
levels = np.linspace(vmin, vmax, params.plot_nlevels)
ticks = np.linspace(vmin, vmax, params.plot_nticks).round(round_to_decimals)
step_start = 0
step_end = params.ntimesteps
step_skip = 1
# Loop through the timesteps and plot; this part can be accelerated using
# multiprocessing
for timestep in range(step_start, step_end, step_skip):
fig = plt.figure()
ax = plt.gca()
plt.set_cmap("jet")
quantity = py[timestep, params.variable_index]
h = plt.contourf(mesh_x, mesh_y, quantity, levels=levels)
plt.axis("scaled")
# Make adjustments to fit the colorbar
if not params.plot_no_colorbar:
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
cbar = plt.colorbar(h, cax=cax)
# Adjust colorbar such that the ticks and ticklabels are not displayed
cbar.ax.tick_params(size=0)
# No ticks or ticklabels
ax.set_xticks([])
ax.set_xticklabels([])
ax.set_yticks([])
ax.set_yticklabels([])
plt.tight_layout()
fname = f"{image_directory}/{timestep:0>3}.png"
plt.savefig(fname)
plt.close(fig)
# Make animation if requested
# TODO