-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnw-retreat-animation.py
executable file
·279 lines (215 loc) · 8.6 KB
/
nw-retreat-animation.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#/usr/bin/env python
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import PIL
from PIL import ImageDraw
from PIL import ImageFont
import tempfile
import os
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.description = "Generating scripts for warming experiments."
parser.add_argument(
"--rcp", dest="rcp", help="""RCP scenario. default=45.""", default=45
)
options = parser.parse_args()
rcp = options.rcp
rcp_dict = {"26": "RCP 2.6", "45": "RCP 4.5", "85": "RCP 8.5", "CTRL": "CTRL"}
print(rcp)
# max index to process
N = 335
# HD resolution: 1920x1080
hd_res = [1920, 1080]
font = ImageFont.truetype("/Library/Fonts/Arial Bold.ttf", 40)
pism_logo = PIL.Image.open("data/colorbars/pism-logo.png")
speed_colorbar = PIL.Image.open("data/colorbars/speed_blue_red_nonlin_0_1500_horizontal.png")
time_colorbar = PIL.Image.open("data/colorbars/bath_112_horizontal.png")
overview_map = PIL.Image.open("data/colorbars/overview.png")
# Start Year 2015
offset = 7
def text(draw, text, x, y, color):
"Draw text 'text', centered at (x,y), using color 'color' (R,G,B)."
size = draw.textsize(text, font=font)
draw.text((x - size[0] / 2, y - size[1] / 2),
text,
color, font=font)
def box(draw, pos, color):
"Draw text 'rectangle', spanning pos = [(x0,y0), (x1, y1)], using color 'color' (R,G,B)."
draw.rectangle(pos, color)
def size(old_size, desired_width):
"Compute the new size of an image given its old size and the desired width. Preserves the aspect ratio."
ratio = float(desired_width) / old_size[0]
# and the actual panel size
return int(old_size[0] * ratio), int(old_size[1] * ratio)
def size_height(old_size, desired_height):
"Compute the new size of an image given its old size and the desired height. Preserves the aspect ratio."
ratio = float(desired_height) / old_size[1]
# and the actual panel size
return int(old_size[0] * ratio), int(old_size[1] * ratio)
def generate_frame(rcp, index, output_filename):
"generate one frame and save it to the file output_filename"
# load panels
rcp_filename = "data/nw-600m/frame{:04d}.png".format(index + offset)
ts_filename = "data/profiles/rcp{}_Upernavik_Isstrom_S_{:04d}.png".format(rcp, index + offset)
rcp_img = PIL.Image.open(rcp_filename)
# set the panel width
# and the actual panel size
panel_height = hd_res[1]
panel_size = size_height(rcp_img.size, panel_height)
panel_width = panel_size[0]
# height of the header (white strip above the panels), in pixels
header = 50
# size of the border around the panels, in pixels
border = 5
# resize input images
rcp_img = rcp_img.resize(panel_size, resample=1)
# open the ts plot
ts = PIL.Image.open(ts_filename)
ts = ts.resize(size(ts.size, hd_res[0] - panel_width - border), resample=1)
ts_width = ts.size[0]
ts_height = ts.size[1]
# resize colorbars
overview = overview_map.resize(size(overview_map.size, 150), resample=1)
pism = pism_logo.resize(size(pism_logo.size, 280), resample=1)
speed = speed_colorbar.resize(size(time_colorbar.size, 500), resample=1)
time = time_colorbar.resize(size(time_colorbar.size, 500), resample=1)
bar_height = time.size[1]
# set the size of the resulting image
canvas_size = (hd_res[0], hd_res[1])
img_width = canvas_size[0]
# create the output image
img = PIL.Image.new("RGB", canvas_size, color=(255, 255, 255))
# add text and box
draw = PIL.ImageDraw.Draw(img)
# paste individual panels into the output image
img.paste(rcp, (hd_res[0] - panel_size[0], 0))
img.paste(ts, (0, 220))
img.paste(overview, (hd_res[0] - overview.size[0], 0))
img.paste(pism, (10, hd_res[1] - 100))
img.paste(time, (400, hd_res[1] - 100), mask=time.split()[3])
box(draw, [(1150, hd_res[1] - 100), (1150 + speed.size[0], hd_res[1] - 100 + speed.size[1])], (255, 255, 255))
img.paste(speed, (1150, hd_res[1] - 100), mask=speed.split()[3])
text(draw,
"Year {:04d} CE".format(2008 + index + offset),
200,
40,
(0, 0, 0))
text(draw,
"{}".format(rcp_dict[str(rcp)]),
800,
40,
"#990002")
text(draw,
u"Upernavik Isstr\u00F8m S".format(2008 + index + offset),
600,
220,
(0, 0, 0))
img.save(output_filename)
def generate_nw_frame(rcp, index, output_filename):
"generate one frame and save it to the file output_filename"
# load panels
rcp_filename = "data/nw-600m-rcp{}/frame{:04d}.png".format(rcp, index + offset)
rcp_img = PIL.Image.open(rcp_filename)
ts_filename = "data/discharge/d_contrib_discharge_contrib_rcp{}_{:04d}.png".format(rcp, index + offset)
# set the panel width
# and the actual panel size
panel_height = hd_res[1]
panel_size = size_height(rcp_img.size, panel_height)
panel_width = panel_size[0]
# height of the header (white strip above the panels), in pixels
header = 50
# size of the border around the panels, in pixels
border = 5
# resize input images
rcp_img = rcp_img.resize(panel_size, resample=1)
# open the ts plot
ts = PIL.Image.open(ts_filename)
ts = ts.resize(size(ts.size, hd_res[0] - panel_width - border), resample=1)
ts_width = ts.size[0]
ts_height = ts.size[1]
# resize colorbars
overview = overview_map.resize(size(overview_map.size, 150), resample=1)
pism = pism_logo.resize(size(pism_logo.size, 240), resample=1)
speed = speed_colorbar.resize(size(speed_colorbar.size, 440), resample=1)
bar_height = speed.size[1]
# set the size of the resulting image
canvas_size = (hd_res[0], hd_res[1])
img_width = canvas_size[0]
# create the output image
img = PIL.Image.new("RGB", canvas_size, color=(255, 255, 255))
# add text and box
draw = PIL.ImageDraw.Draw(img)
# paste individual panels into the output image
img.paste(rcp_img, (hd_res[0] - panel_width, 0))
img.paste(ts, (0, 400))
img.paste(overview, (hd_res[0] - overview.size[0], 0))
img.paste(pism, (10, hd_res[1] - 100))
box(draw, [(400, hd_res[1] - 110), (400 + speed.size[0], hd_res[1] - 110 + speed.size[1])], (255, 255, 255))
img.paste(speed, (400, hd_res[1] - 110), mask=speed.split()[3])
text(draw,
"Year {:04d} CE".format(2008 + index + offset),
500,
40,
(0, 0, 0))
text(draw,
"{}".format(rcp_dict[str(rcp)]),
100,
40,
"#990002")
text(draw,
"Contribution of outlet glaciers to discharge",
430,
380,
"#000000")
img.save(output_filename)
def generate_upernavik_frame(rcp, index, output_filename):
"generate one frame and save it to the file output_filename"
# height of the header (white strip above the panels), in pixels
header = 50
# size of the border around the panels, in pixels
border = 5
# load panels
ts_filename = "data/profiles/rcp{}_Upernavik_Isstrom_S_{:04d}.png".format(rcp, index + offset)
# open the ts plot
ts = PIL.Image.open(ts_filename)
# set the panel width
# and the actual panel size
panel_height = hd_res[1]
panel_size = size_height(ts.size, panel_height)
panel_width = panel_size[0]
ts = ts.resize(size_height(ts.size, hd_res[1] - 200), resample=1)
ts_width = ts.size[0]
ts_height = ts.size[1]
# resize colorbars
overview = overview_map.resize(size(overview_map.size, 150), resample=1)
pism = pism_logo.resize(size(pism_logo.size, 280), resample=1)
# set the size of the resulting image
canvas_size = (hd_res[0], hd_res[1])
img_width = canvas_size[0]
# create the output image
img = PIL.Image.new("RGB", canvas_size, color=(255, 255, 255))
# add text and box
draw = PIL.ImageDraw.Draw(img)
# paste individual panels into the output image
img.paste(ts, (200, 0))
img.paste(pism, (10, hd_res[1]))
text(draw,
"Year {:04d} CE".format(2008 + index + offset),
200,
40,
(0, 0, 0))
text(draw,
"{}".format(rcp_dict[str(rcp)]),
1700,
40,
"#990002")
text(draw,
u"Upernavik Isstr\u00F8m S".format(2008 + index + offset),
900,
100,
(0, 0, 0))
img.save(output_filename)
for k in range(401):
print('Generating frame {}'.format(k))
generate_nw_frame(rcp, k, "output/nw_g600m_rcp{}_{:04d}.png".format(rcp, k))
generate_upernavik_frame(rcp, k, "output/upernavik_g600m_rcp{}_{:04d}.png".format(rcp, k))
#generate_frame(rcp, k, "output/nw_g600m_rcp_%04d.png" % k)
print("")