5Create DurhamLogo.h and DurhamLogo.cpp for the tutorial 11_tsunami.ipynb
7The script downloads the shield of Durham University and writes it as a C++
8array of strings into the directory of this script: one string per row of
9pixels, starting with the top row, with a 1 for each purple pixel and a 0
10for each white one. The tutorial uses the array as depth map. The two
11generated files are part of the repository, so the tutorial runs without
12internet access. Rerun the script only if you want to change the depth map:
14 python3 tutorials/exahype2/bathymetry/create-durham-logo.py
20import matplotlib.image
22Here = os.path.dirname(os.path.abspath(__file__))
24LogoURL =
"https://www.topuniversities.com/sites/default/files/profiles/logos/durham-university_170_large.jpg"
26Header =
"""// This file is part of the Peano project. For conditions of distribution and
27// use, please see the copyright notice at www.peano-framework.org
29// Generated by create-durham-logo.py. Do not edit.
32namespace tutorials::exahype2::tsunami {{
34 * Number of pixels per axis of DurhamLogo
36 constexpr int DurhamLogoSize = {size};
39 * The shield of Durham University as depth map
41 * One string per row of pixels, starting with the top row of the image,
42 * i.e. DurhamLogo[row][column]. A '1' marks a purple pixel, a '0' a white
43 * one. The tutorial 11_tsunami.ipynb maps the image onto its domain and
44 * uses it as depth map: the water is deeper where the logo is purple.
50 * by create-durham-logo.py.
52 extern const char* const DurhamLogo[DurhamLogoSize];
53}} // namespace tutorials::exahype2::tsunami
56Implementation =
"""// This file is part of the Peano project. For conditions of distribution and
57// use, please see the copyright notice at www.peano-framework.org
59// Generated by create-durham-logo.py. Do not edit.
60#include "DurhamLogo.h"
62const char* const tutorials::exahype2::tsunami::DurhamLogo[DurhamLogoSize] = {{
68if __name__ ==
"__main__":
70 request = urllib.request.Request(LogoURL, headers={
"User-Agent":
"Mozilla/5.0"})
71 logo = matplotlib.image.imread(urllib.request.urlopen(request), format=
"jpg") / 255.0
72 purple = logo.mean(axis=2) < 0.7
73 assert purple.shape[0] == purple.shape[1],
"We expect a square logo"
76 ' "' +
"".join(
"1" if pixel
else "0" for pixel
in row) +
'"' for row
in purple
78 with open(os.path.join(Here,
"DurhamLogo.h"),
"w")
as f:
79 f.write(Header.format(size=purple.shape[0], url=LogoURL))
80 with open(os.path.join(Here,
"DurhamLogo.cpp"),
"w")
as f:
81 f.write(Implementation.format(rows=rows))
82 print(
"wrote DurhamLogo.h and DurhamLogo.cpp into {}".format(Here))