-
Notifications
You must be signed in to change notification settings - Fork 0
/
anomaly.py
279 lines (263 loc) · 10.7 KB
/
anomaly.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
__filename__ = "anomaly.py"
__author__ = "Bob Mottram"
__license__ = "GPL3+"
__version__ = "2.0.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Commandline Interface"
import os
def _stations_anomaly(year: int, stations_data: {}, stations_locations: {},
baseline: [], station_ids: set,
min_latitude: float, max_latitude: float,
month_number: int) -> float:
"""Returns anomaly for the given stations for the given year
"""
anomaly = 0.0
hits = 0
for sid in station_ids:
if sid not in stations_data:
continue
if year not in stations_data[sid]:
continue
if stations_locations[sid]['latitude'] < min_latitude or \
stations_locations[sid]['latitude'] > max_latitude:
continue
month_data = stations_data[sid][year]['month']
if month_number > -1:
month_index = month_number
if baseline[month_index]:
if month_data[month_index]['av'] > -80 and \
month_data[month_index]['av'] < 80:
anomaly += \
month_data[month_index]['av'] - baseline[month_index]
hits += 1
else:
for month_index in range(12):
if not baseline[month_index]:
continue
if month_data[month_index]['av'] > -80 and \
month_data[month_index]['av'] < 80:
anomaly += \
month_data[month_index]['av'] - baseline[month_index]
hits += 1
if hits > 0:
return anomaly / float(hits)
return None
def update_grid_anomalies(grid: [], stations_data: {}, stations_locations: {},
start_year: int, end_year: int,
min_latitude: float, max_latitude: float) -> int:
"""Calculates anomalies for each grid cell within a range of years
"""
ctr = 0
year_ctr = 0
for grid_cell in grid:
grid_cell['anomalies'] = {}
grid_cell['anomalies_monthly'] = {}
if not grid_cell['station_ids']:
continue
baseline = grid_cell['baseline']
station_ids = grid_cell['station_ids']
for year in range(start_year, end_year + 1, 1):
grid_cell['anomalies'][year] = \
_stations_anomaly(year, stations_data, stations_locations,
baseline, station_ids,
min_latitude, max_latitude, -1)
if grid_cell['anomalies'][year] is not None:
year_ctr += 1
grid_cell['anomalies_monthly'][year] = []
for month_index in range(12):
mnth_anom = \
_stations_anomaly(year, stations_data, stations_locations,
baseline, station_ids, min_latitude,
max_latitude, month_index)
grid_cell['anomalies_monthly'][year].append(mnth_anom)
ctr += 1
if year_ctr > 0:
return int(year_ctr * 100 / float(ctr))
return 0
def get_global_anomalies(grid: [],
start_year: int, end_year: int) -> {}:
"""Returns global anomalies in the given year range
"""
result = {}
for year in range(start_year, end_year + 1, 1):
year_anomaly = 0.0
ctr = 0
for grid_cell in grid:
if grid_cell['anomalies'].get(year):
year_anomaly += grid_cell['anomalies'][year]
ctr += 1
if ctr > 0:
result[year] = year_anomaly / float(ctr)
else:
result[year] = None
return result
def get_monthly_anomalies(grid: [],
start_year: int, end_year: int) -> {}:
"""Returns monthly anomalies in the given year range
"""
result = {}
for year in range(start_year, end_year + 1, 1):
months_anomaly = [0,0,0,0,0,0,0,0,0,0,0,0]
months_ctr = [0,0,0,0,0,0,0,0,0,0,0,0]
ctr = 0
for grid_cell in grid:
if grid_cell['anomalies_monthly'].get(year):
months_list = grid_cell['anomalies_monthly'][year]
idx = 0
for month_anom in months_list:
if month_anom is None:
idx += 1
continue
months_anomaly[idx] += month_anom
months_ctr[idx] += 1
idx += 1
ctr += 1
for month_index in range(12):
if months_ctr[month_index] > 0:
months_anomaly[month_index] /= months_ctr[month_index]
else:
months_anomaly[month_index] = None
if ctr > 0:
result[year] = months_anomaly
else:
result[year] = None
return result
def plot_global_anomalies(grid: [],
start_year: int, end_year: int,
baseline_start: int, baseline_end: int,
min_latitude: float, max_latitude: float) -> None:
"""Plot yearly anomalies graph
"""
anomalies = get_global_anomalies(grid, start_year, end_year)
series = []
minimum_temp = 99999999
maximum_temp = -99999999
for year in range(start_year, end_year + 1, 1):
series.append(anomalies[year])
if anomalies[year] > maximum_temp:
maximum_temp = anomalies[year]
if anomalies[year] < minimum_temp:
minimum_temp = anomalies[year]
title = \
"Global Temperature Anomalies " + \
str(start_year) + ' - ' + str(end_year) + \
' for latitude range ' + str(min_latitude) + ' - ' + str(max_latitude)
subtitle = "Source https://www.ncei.noaa.gov/pub/data/ghcn/v4"
x_label = 'Year'
y_label = 'Average Temperature Anomaly (Celcius) ' + \
'relative to ' + str(baseline_start) + '-' + str(baseline_end)
indent = 0.34
vpos = 0.94
image_width = 1000
image_height = 1000
plot_name = 'global_anomalies'
image_format = 'jpg'
image_format2 = 'jpeg'
filename = plot_name + '.' + image_format
script_filename = plot_name + '.gnuplot'
data_filename = plot_name + '.data'
with open(data_filename, 'w+', encoding='utf-8') as fp_data:
year = start_year
for temperature_anomaly in series:
fp_data.write(str(year) + " " + str(temperature_anomaly) + '\n')
year += 1
script = \
"reset\n" + \
"set title \"" + title + "\"\n" + \
"set label \"" + subtitle + "\" at screen " + \
str(indent) + ", screen " + str(vpos) + "\n" + \
"set yrange [" + str(minimum_temp) + ":" + \
str(maximum_temp) + "]\n" + \
"set xrange [" + str(start_year) + ":" + str(end_year) + "]\n" + \
"set lmargin 9\n" + \
"set rmargin 2\n" + \
"set xlabel \"" + x_label + "\"\n" + \
"set ylabel \"" + y_label + "\"\n" + \
"set grid\n" + \
"set key right bottom\n" + \
"set terminal " + image_format2 + \
" size " + str(image_width) + "," + str(image_height) + "\n" + \
"set output \"" + filename + "\"\n" + \
"plot \"" + data_filename + "\" using 1:2 notitle with lines\n"
with open(script_filename, 'w+', encoding='utf-8') as fp_scr:
fp_scr.write(script)
os.system('gnuplot ' + script_filename)
def plot_monthly_anomalies(grid: [],
start_year: int, end_year: int,
baseline_start: int, baseline_end: int,
min_latitude: float, max_latitude: float) -> None:
"""Plot monthly anomalies graph
"""
anomalies = get_monthly_anomalies(grid, start_year, end_year)
series = []
minimum_temp = 99999999
maximum_temp = -99999999
for year in range(start_year, end_year + 1, 1):
series.append(anomalies[year])
for month_index in range(12):
if anomalies[year][month_index] is None:
continue
if anomalies[year][month_index] > maximum_temp:
maximum_temp = anomalies[year][month_index]
if anomalies[year][month_index] < minimum_temp:
minimum_temp = anomalies[year][month_index]
title = \
"Monthly Temperature Anomalies " + \
str(start_year) + ' - ' + str(end_year) + \
' for latitude range ' + str(min_latitude) + ' - ' + str(max_latitude)
subtitle = "Source https://www.ncei.noaa.gov/pub/data/ghcn/v4"
x_label = 'Month'
y_label = 'Average Monthly Temperature Anomaly (Celcius) ' + \
'relative to ' + str(baseline_start) + '-' + str(baseline_end)
indent = 0.34
vpos = 0.94
image_width = 1000
image_height = 1000
plot_name = 'monthly_anomalies'
image_format = 'jpg'
image_format2 = 'jpeg'
filename = plot_name + '.' + image_format
script_filename = plot_name + '.gnuplot'
data_filename = plot_name + '.data'
with open(data_filename, 'w+', encoding='utf-8') as fp_data:
for month_index in range(12):
line = str(month_index + 1)
for temperature_anomaly in series:
if temperature_anomaly[month_index] is not None:
line += " " + str(temperature_anomaly[month_index])
else:
line += " 0"
fp_data.write(line + '\n')
script = \
"reset\n" + \
"set title \"" + title + "\"\n" + \
"set label \"" + subtitle + "\" at screen " + \
str(indent) + ", screen " + str(vpos) + "\n" + \
"set yrange [" + str(minimum_temp) + ":" + \
str(maximum_temp) + "]\n" + \
"set xrange [1:12]\n" + \
"set lmargin 9\n" + \
"set rmargin 2\n" + \
"set xlabel \"" + x_label + "\"\n" + \
"set ylabel \"" + y_label + "\"\n" + \
"set xtics (\"Jan\" 1, \"Feb\" 2, \"Mar\" 3, \"Apr\" 4, " + \
"\"May\" 5, \"Jun\" 6, \"Jul\" 7, \"Aug\" 8, \"Sep\" 9, " + \
"\"Oct\" 10, \"Nov\" 11, \"Dec\" 12)\n" + \
"set grid\n" + \
"set key right bottom\n" + \
"set terminal " + image_format2 + \
" size " + str(image_width) + "," + str(image_height) + "\n" + \
"set output \"" + filename + "\"\n"
script += "plot "
for year in range(start_year, end_year+1, 20):
script += \
"\"" + data_filename + "\" using 1:" + str(2+year-start_year) + \
" title \"" + str(year) + "\" with lines"
if year < end_year+1:
script += ', '
script += '\n'
with open(script_filename, 'w+', encoding='utf-8') as fp_scr:
fp_scr.write(script)
os.system('gnuplot ' + script_filename)