-
Notifications
You must be signed in to change notification settings - Fork 3
/
hadcrut5-stripe.py
executable file
·162 lines (139 loc) · 4.25 KB
/
hadcrut5-stripe.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
#!/usr/bin/python3
# Copyright (c) 2023 Davide Madrisan <d.madrisan@proton.me>
# SPDX-License-Identifier: GPL-3.0-or-later
"""
Display a stripe image of the HadCRUT5 Global temperature dataset.
"""
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.colors import ListedColormap
from matplotlib.patches import Rectangle
from hadcrut5lib import argparser, HadCRUT5
def parse_args():
"""This function parses and return arguments passed in"""
descr = "Parse and plot a stripe image of the HadCRUT5 temperature datasets"
examples = [
"%(prog)s",
"%(prog)s --no-labels --region northern",
"%(prog)s --region global --outfile HadCRUT5-stripe-global.png",
]
parser = argparser(descr, examples)
parser.add_argument(
"-f",
"--outfile",
action="store",
dest="outfile",
help="name of the output PNG file",
)
parser.add_argument(
"-r",
"--region",
choices=["global", "northern", "southern"],
action="store",
dest="region",
default="global",
help="select between Global (default), Northern, or Southern Temperatures",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
dest="verbose",
help="make the operation more talkative",
)
parser.add_argument(
"-l",
"--no-labels",
action="store_false",
dest="labels",
help="do not disply the header and footer labels",
)
parser.set_defaults(labels=True)
return parser.parse_args()
def plotstripe(region, outfile, labels, verbose):
"""
Create a stripe plot for the specified period and diplay it or save it to
file if outfile is set
"""
hc5 = HadCRUT5(
regions=(region == "global", region == "northern", region == "southern"),
verbose=verbose,
)
hc5.datasets_download()
hc5.datasets_load()
hc5.datasets_normalize()
years = hc5.dataset_years()
yfirst, ylast = years[0], years[-1]
yrange = ylast - yfirst
regions_switch = {
"global": hc5.GLOBAL_REGION,
"northern": hc5.NORTHERN_REGION,
"southern": hc5.SOUTHERN_REGION,
}
_, mean, _ = hc5.dataset_normalized_data(regions_switch[region])
plt.style.use("dark_background")
_, ax = plt.subplots()
# the colors in this colormap come from http://colorbrewer2.org
# (the 8 more saturated colors from the 9 blues / 9 reds)
cmap = ListedColormap(
[
"#08306b",
"#08519c",
"#2171b5",
"#4292c6",
"#6baed6",
"#9ecae1",
"#c6dbef",
"#deebf7",
"#fee0d2",
"#fcbba1",
"#fc9272",
"#fb6a4a",
"#ef3b2c",
"#cb181d",
"#a50f15",
"#67000d",
]
)
# create a collection with a rectangle for each year
collection = PatchCollection(
[Rectangle((x / yrange, 0), x + 1 / yrange, 1) for x in range(1 + yrange)]
)
# set data and colormap
collection.set_array(mean)
collection.set_cmap(cmap)
# collection.set_clim(,)
ax.add_collection(collection)
ax.get_yaxis().set_visible(False)
ax.set_frame_on(False)
if labels:
ax.tick_params(axis="both", which="both", length=0)
# pylint: disable=consider-using-f-string
plt.title(
(
"{} Temperature Change ({}-{})".format(
regions_switch[region], yfirst, ylast
)
),
fontsize=16,
loc="left",
fontweight="bold",
family="DejaVu Sans",
)
ticks = [0, 0.2, 0.4, 0.6, 0.8, 1]
xlabels = [round(yfirst + x * yrange) for x in ticks]
plt.xticks(ticks, xlabels, fontweight="bold", fontsize=12)
else:
ax.get_xaxis().set_visible(False)
fig = plt.gcf()
fig.set_size_inches(10, 4) # 1 inch equal to 80pt
if outfile:
fig.savefig(outfile, dpi=80, bbox_inches="tight")
plt.close(fig)
else:
plt.show()
# pylint: disable=C0116
def main():
args = parse_args()
plotstripe(args.region, args.outfile, args.labels, args.verbose)
main()