-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathazmp_bergs.py
More file actions
229 lines (187 loc) · 7.95 KB
/
azmp_bergs.py
File metadata and controls
229 lines (187 loc) · 7.95 KB
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
'''
AZMP reporting - Iceberg counts from :
Iceberg data from USCG International Ice Patrol
Contact Michael Hicks USCG
Hicks, Michael R CIV <Michael.R.Hicks@uscg.mil>
The Iceberg Season runs fron Oct 1 to Sept 30
data in /home/cyrf0006/data/AZMP/ColbourneStuff/NL_ICE_BERG_NUMBERS_DATA_1900_2017.xlsx
(script ran in /home/cyrf0006/AZMP/state_report/bergs)
Frederic.Cyr@dfo-mpo.gc.ca - June 2019
Modifications:
- Jan 2021 to now take data from NSIDC
'''
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os
#import seaborn as sns
# Adjust fontsize/weight
font = {'family' : 'sans-serif',
'weight' : 'normal',
'size' : 10}
plt.rc('font', **font)
clim_year = [1991, 2020]
current_year = 2025
## ---- Prepare the data ---- ##
# Legacy - Load from Excel sheets
#df = pd.read_excel('/home/cyrf0006/data/AZMP/ColbourneStuff/NL_ICE_BERG_NUMBERS_DATA_1900_2019.xlsx', header=5, index_col='YEAR')
#df = df.drop(columns = 'TOT SEASON')
# Since 2021, load from NSIDC (https://nsidc.org/data/G10028/versions/1)
# This file is updated manually using the IIP 202* North Atlantic Iceberg Summary (Contact Alexis Denton)
df = pd.read_csv('~/github/AZMP-NL/external_data/NSIDC/Icebergs/G10028_Icebergs_South_of_48N.csv', index_col='YEAR')
df = df.drop(columns = 'TOTAL')
# Rename columns
df.rename(columns={'OCT':'Oct', 'NOV':'Nov', 'DEC':'Dec', 'JAN':'Jan', 'FEB':'Feb', 'MAR':'Mar', 'APR':'Apr', 'MAY':'May', 'JUN':'Jun', 'JUL':'Jul', 'AUG':'Aug', 'SEP':'Sep'}, inplace=True)
# Stack months under Years (pretty cool!)
#df = df.stack()
# Transform to a series with values based the 15th of each month (had to convert years to string)
#df_BB.index = pd.to_datetime('15-' + df_BB.index.get_level_values(1) + '-' + df_BB.index.get_level_values(0).values.astype(np.str))
# Annual mean
# We now get the annual number from Section 2.2 of the Ice and Environmental Conditions in Ice Year 202* report
df_annual = df.sum(axis=1)
# Temporary:
df_annual.loc[2022] = 58
df_annual.loc[2023] = 385
df_annual.loc[2024] = 22
df_annual.loc[2025] = 250
df_annual.to_pickle('bergs_annual.pkl')
df_annual_clim = df_annual[(df_annual.index>=clim_year[0]) & (df_annual.index<=clim_year[1])]
df_annual_anom = df_annual - df_annual_clim.mean()
df_annual_std_anom = df_annual_anom/df_annual_clim.std()
df_annual_std_anom.to_pickle('bergs_std_anom.pkl')
# Monthly mean
df_monthly = df[df.index==current_year]
df_monthly_clim = df[(df.index>=clim_year[0]) & (df.index<=clim_year[1])]
df_monthly_std = df_monthly_clim.std(axis=0)
df_monthly_clim = df_monthly_clim.mean(axis=0)
## ---- plot monthly ---- ##
ind = np.arange(len(df_monthly.keys())) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind - width/2, df_monthly_clim.values, width, yerr=df_monthly_std.values*.5,
label='1991-2020')
rects2 = ax.bar(ind + width/2, np.squeeze(df_monthly.values), width, yerr=None,
label=str(current_year))
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Counts')
#ax.set_title('Number of icebergs')
ax.set_xticks(ind)
ax.set_xticklabels(df_monthly_clim.index)
ax.legend()
ax.yaxis.grid() # horizontal lines
plt.ylim([0, 400])
fig.set_size_inches(w=6,h=3)
fig_name = 'bergs_monthly.png'
#plt.annotate('data source: www.ncdc.noaa.gov/teleconnections/', xy=(.58, .01), xycoords='figure fraction', annotation_clip=False, FontSize=12)
fig.savefig(fig_name, dpi=300)
os.system('convert -trim ' + fig_name + ' ' + fig_name)
# Save French Figure
french_months = ['oct', 'nov', 'déc', 'jan', 'fev', 'mar', 'avr', 'mai', 'juin', 'juil', 'aou', 'sep']
ax.set_ylabel('Nombre d\'icebergs')
ax.set_xticklabels(french_months, rotation='horizontal')
fig_name = 'bergs_monthly_FR.png'
fig.savefig(fig_name, dpi=300)
os.system('convert -trim ' + fig_name + ' ' + fig_name)
## ---- plot annual ---- ##
width = 0.75 # the width of the bars
fig, ax = plt.subplots()
ax.bar(df_annual.index, df_annual.values, width)
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Counts')
#ax.set_title('Number of icebergs')
#ax.set_xticks(ind)
#ax.set_xticklabels(df_annual.index)
#ax.legend()
#ax.yaxis.grid() # horizontal lines
#plt.ylim([0, 330])
plt.grid()
ax.axhspan(df_annual_clim.mean()-df_annual_clim.std()/2, df_annual_clim.mean()+df_annual_clim.std()/2, alpha=0.25, color='gray')
fig.set_size_inches(w=6,h=3)
fig_name = 'bergs_annual.png'
#plt.annotate('data source: www.ncdc.noaa.gov/teleconnections/', xy=(.58, .01), xycoords='figure fraction', annotation_clip=False, FontSize=12)
fig.savefig(fig_name, dpi=300)
os.system('convert -trim ' + fig_name + ' ' + fig_name)
# Save French Figure
french_months = ['oct', 'nov', 'déc', 'jan', 'fev', 'mar', 'avr', 'mai', 'juin', 'juil', 'aou', 'sep']
ax.set_ylabel('Nombre d\'icebergs')
fig_name = 'bergs_annual_FR.png'
fig.savefig(fig_name, dpi=300)
os.system('convert -trim ' + fig_name + ' ' + fig_name)
## ---- plot annual normalized (with scorecards) ---- ##
# preamble
from matplotlib.colors import from_levels_and_colors
# Build the colormap
vmin = -3.49
vmax = 3.49
midpoint = 0
levels = np.linspace(vmin, vmax, 15)
midp = np.mean(np.c_[levels[:-1], levels[1:]], axis=1)
colvals = np.interp(midp, [vmin, midpoint, vmax], [-1, 0., 1])
normal = plt.Normalize(-3.49, 3.49)
reds = plt.cm.Reds(np.linspace(0,1, num=7))
blues = plt.cm.Blues_r(np.linspace(0,1, num=7))
whites = [(1,1,1,1)]*2
colors = np.vstack((blues[0:-1,:], whites, reds[1:,:]))
colors = np.concatenate([[colors[0,:]], colors, [colors[-1,:]]], 0)
cmap, norm = from_levels_and_colors(levels, colors, extend='both')
cmap_r, norm_r = from_levels_and_colors(levels, np.flipud(colors), extend='both')
# Common parameters
hcell, wcell = 0.5, 0.6
hpad, wpad = 0, 0
fig, ax = plt.subplots()
ax.bar(df_annual.index, df_annual.values, width)
ax.set_ylabel('Counts')
plt.xlim([1899.5, current_year+0.5])
plt.grid()
ax.axhspan(df_annual_clim.mean()-df_annual_clim.std()/2, df_annual_clim.mean()+df_annual_clim.std()/2, alpha=0.25, color='gray')
std_anom = (df_annual - df_annual_clim.mean()) / df_annual_clim.std()
ax.set_title('Annual icebergs count')
colors = cmap(normal(std_anom.values*-1))
cell_text = [std_anom.values.round(1)]
the_table = ax.table(cellText=cell_text,
rowLabels=['Icebergs subindex'],
colLabels=None,
cellColours = [colors],
cellLoc = 'center', rowLoc = 'center',
loc='bottom', bbox=[0, -0.1, 1, 0.05])
the_table.auto_set_font_size (False)
the_table.set_fontsize(6)
for key, cell in the_table.get_celld().items():
if key[1] == -1:
cell.set_linewidth(0)
cell.set_fontsize(10)
else:
cell._text.set_rotation(90)
fig.set_size_inches(w=13,h=8)
fig_name = 'icebergs_climate_index.png'
fig.savefig(fig_name, dpi=300)
os.system('convert -trim -bordercolor White -border 10x10 ' + fig_name + ' ' + fig_name)
## In French ##
fig, ax = plt.subplots()
ax.bar(df_annual.index, df_annual.values, width)
ax.set_ylabel("Nombre d'icebergs")
plt.xlim([1899.5, 2021.5])
plt.grid()
ax.axhspan(df_annual_clim.mean()-df_annual_clim.std()/2, df_annual_clim.mean()+df_annual_clim.std()/2, alpha=0.25, color='gray')
std_anom = (df_annual - df_annual_clim.mean()) / df_annual_clim.std()
ax.set_title("Décompte annuel d'icebergs")
colors = cmap(normal(std_anom.values*-1))
cell_text = [std_anom.values.round(1)]
the_table = ax.table(cellText=cell_text,
rowLabels=['Sous-indice Icebergs'],
colLabels=None,
cellColours = [colors],
cellLoc = 'center', rowLoc = 'center',
loc='bottom', bbox=[0, -0.1, 1, 0.05])
the_table.auto_set_font_size (False)
the_table.set_fontsize(6)
for key, cell in the_table.get_celld().items():
if key[1] == -1:
cell.set_linewidth(0)
cell.set_fontsize(10)
else:
cell._text.set_rotation(90)
fig.set_size_inches(w=13,h=8)
fig_name = 'icebergs_climate_index_FR.png'
fig.savefig(fig_name, dpi=300)
os.system('convert -trim -bordercolor White -border 10x10 ' + fig_name + ' ' + fig_name)