-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.py
334 lines (261 loc) · 9.29 KB
/
calc.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import csv
from collections import OrderedDict
from decimal import Decimal
import datetime
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
output_filename = 'output/combined.csv'
trendline_both_output_filename = 'output/trendline_both.png'
trendline_north_output_filename = 'output/trendline_north.png'
trendline_south_output_filename = 'output/trendline_south.png'
input_north_filename = 'data/NH_seaice_extent_final_v2.csv'
input_north_2016_filename = 'data/NH_seaice_extent_nrt_v2.csv'
input_south_filename = 'data/SH_seaice_extent_final_v2.csv'
input_south_2016_filename = 'data/SH_seaice_extent_nrt_v2.csv'
data = {}
list_of_years = []
# Pull data from 1978-2015
with open(input_north_filename) as csvfile:
north_reader = csv.DictReader(csvfile, skipinitialspace=True)
for row in north_reader:
# Skip the example formatting line
if row['Year'] == 'YYYY':
continue
date = (row['Year'], row['Month'], row['Day'])
extent = row['Extent']
if date not in data:
data[date] = {}
data[date]['north'] = extent
if row['Year'] not in list_of_years:
list_of_years.append(row['Year'])
# Pull data from 2016
with open(input_north_2016_filename) as csvfile:
north_reader = csv.DictReader(csvfile, skipinitialspace=True)
for row in north_reader:
# Skip the example formatting line
if row['Year'] == 'YYYY':
continue
date = (row['Year'], row['Month'], row['Day'])
extent = row['Extent']
if date not in data:
data[date] = {}
data[date]['north'] = extent
if row['Year'] not in list_of_years:
list_of_years.append(row['Year'])
# Pull data from 1978-2015
with open(input_south_filename) as csvfile:
south_reader = csv.DictReader(csvfile, skipinitialspace=True)
for row in south_reader:
# Skip the example formatting line
if row['Year'] == 'YYYY':
continue
date = (row['Year'], row['Month'], row['Day'])
extent = row['Extent']
if date not in data:
data[date] = {}
data[date]['south'] = extent
if row['Year'] not in list_of_years:
list_of_years.append(row['Year'])
# Pull data from 2016
with open(input_south_2016_filename) as csvfile:
south_reader = csv.DictReader(csvfile, skipinitialspace=True)
for row in south_reader:
# Skip the example formatting line
if row['Year'] == 'YYYY':
continue
date = (row['Year'], row['Month'], row['Day'])
extent = row['Extent']
if date not in data:
data[date] = {}
data[date]['south'] = extent
if row['Year'] not in list_of_years:
list_of_years.append(row['Year'])
for date in data.keys():
north_extent = data[date]['north'] if 'north' in data[date] else 0
south_extent = data[date]['south'] if 'south' in data[date] else 0
total = Decimal(north_extent) + Decimal(south_extent)
data[date]['total'] = str(total)
ordered = OrderedDict(sorted(data.items(), key=lambda t: t[0]))
'''
with open(output_filename, 'w') as outfile:
header = "Year,Month,Day,NorthExtent,SouthExtent,TotalExtent\n"
outfile.write(header)
for date, info_dict in ordered.items():
row = ','.join([date[0], date[1], date[2], info_dict['north'], info_dict['south'], info_dict['total']])
outfile.write(row)
outfile.write("\n")
'''
# Setup the trendline data in the structure we need for plotting
trendline_data = {}
for y in list_of_years:
trendline_data[y] = {'date': [], 'total_extent': [], 'north_extent': [], 'south_extent': []}
for date, data in ordered.items():
year = date[0]
trendline_data[year]['date'].append(date)
trendline_data[year]['total_extent'].append(data['total'])
trendline_data[year]['north_extent'].append(data['north'])
trendline_data[year]['south_extent'].append(data['south'])
# Setup the formatting for the axes
months = mdates.MonthLocator() # every month
monthsFmt = mdates.DateFormatter('%b')
x_axis_label = 'Date of Measurement'
y_axis_label = 'Ice Extent (10^6 sq km)'
# Format the trendline style
trendline_style = 'solid'
default_trendline_size = 1
special_year_trendline_size = 3
marker_style = None
# Generate the combined ice coverage plot
fig, ax = plt.subplots()
ax.set_title('Global (North && South) Ice Extent')
for y in list_of_years:
# Pull the aggregate data for the current year
year_data = trendline_data[y]
# We finagle the year for the date of each datapoint so that we can get them to all display overlaid on top of each other
dates_list = [datetime.date(2016, int(x[1]), int(x[2])) for x in year_data['date']]
# We want to plot the total extent data here
total_extent_list = year_data['total_extent']
# Set the line color
if y == '2016':
line_color = 'red'
legend_label = y
trendline_size = special_year_trendline_size
elif y == '2015':
line_color = 'orange'
legend_label = y
trendline_size = special_year_trendline_size
elif y == '2014':
line_color = 'black'
legend_label = 'All Other Years'
trendline_size = default_trendline_size
else:
line_color = 'black'
legend_label = ''
trendline_size = default_trendline_size
# Plot the datapoints for the current year
ax.plot_date(dates_list, total_extent_list, color=line_color, linestyle=trendline_style, linewidth=trendline_size, marker=marker_style, label=legend_label)
# Format the ticks
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsFmt)
datemin = datetime.date(2016, 1, 1)
datemax = datetime.date(2016 + 1, 1, 1)
ax.set_xlim(datemin, datemax)
# Format the coords message box
def area(x):
return '{}'.format(x)
ax.format_xdata = mdates.DateFormatter('%b')
ax.format_ydata = area
ax.grid(True)
# Rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them
fig.autofmt_xdate()
# Add a legend for the trendlines
ax.legend(loc='best')
# Add labels on the axes
ax.set_xlabel(x_axis_label)
ax.set_ylabel(y_axis_label)
# Save the plot
plt.savefig(trendline_both_output_filename)
# Generate the north ice coverage plot
fig, ax = plt.subplots()
ax.set_title('North Ice Extent')
for y in list_of_years:
# Pull the aggregate data for the current year
year_data = trendline_data[y]
# We finagle the year for the date of each datapoint so that we can get them to all display overlaid on top of each other
dates_list = [datetime.date(2016, int(x[1]), int(x[2])) for x in year_data['date']]
# We want to plot the total extent data here
north_extent_list = year_data['north_extent']
# Set the line color
if y == '2016':
line_color = 'red'
legend_label = y
trendline_size = special_year_trendline_size
elif y == '2015':
line_color = 'orange'
legend_label = y
trendline_size = special_year_trendline_size
elif y == '2014':
line_color = 'black'
legend_label = 'All Other Years'
trendline_size = default_trendline_size
else:
line_color = 'black'
legend_label = ''
trendline_size = default_trendline_size
# Plot the datapoints for the current year
ax.plot_date(dates_list, north_extent_list, color=line_color, linestyle=trendline_style, linewidth=trendline_size, marker=marker_style, label=legend_label)
# Format the ticks
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsFmt)
datemin = datetime.date(2016, 1, 1)
datemax = datetime.date(2016 + 1, 1, 1)
ax.set_xlim(datemin, datemax)
# Format the coords message box
def area(x):
return '{}'.format(x)
ax.format_xdata = mdates.DateFormatter('%b')
ax.format_ydata = area
ax.grid(True)
# Rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them
fig.autofmt_xdate()
# Add a legend for the trendlines
ax.legend(loc='best')
# Add labels on the axes
ax.set_xlabel(x_axis_label)
ax.set_ylabel(y_axis_label)
# Save the plot
plt.savefig(trendline_north_output_filename)
# Generate the south ice coverage plot
fig, ax = plt.subplots()
ax.set_title('South Ice Extent')
for y in list_of_years:
# Pull the aggregate data for the current year
year_data = trendline_data[y]
# We finagle the year for the date of each datapoint so that we can get them to all display overlaid on top of each other
dates_list = [datetime.date(2016, int(x[1]), int(x[2])) for x in year_data['date']]
# We want to plot the total extent data here
south_extent_list = year_data['south_extent']
# Set the line color
if y == '2016':
line_color = 'red'
legend_label = y
trendline_size = special_year_trendline_size
elif y == '2015':
line_color = 'orange'
legend_label = y
trendline_size = special_year_trendline_size
elif y == '2014':
line_color = 'black'
legend_label = 'All Other Years'
trendline_size = default_trendline_size
else:
line_color = 'black'
legend_label = ''
trendline_size = default_trendline_size
# Plot the datapoints for the current year
ax.plot_date(dates_list, south_extent_list, color=line_color, linestyle=trendline_style, linewidth=trendline_size, marker=marker_style, label=legend_label)
# Format the ticks
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsFmt)
datemin = datetime.date(2016, 1, 1)
datemax = datetime.date(2016 + 1, 1, 1)
ax.set_xlim(datemin, datemax)
# Format the coords message box
def area(x):
return '{}'.format(x)
ax.format_xdata = mdates.DateFormatter('%b')
ax.format_ydata = area
ax.grid(True)
# Rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them
fig.autofmt_xdate()
# Add a legend for the trendlines
ax.legend(loc='best')
# Add labels on the axes
ax.set_xlabel(x_axis_label)
ax.set_ylabel(y_axis_label)
# Save the plot
plt.savefig(trendline_south_output_filename)