-
Notifications
You must be signed in to change notification settings - Fork 1
/
azmp_composite_index.py
473 lines (415 loc) · 21.5 KB
/
azmp_composite_index.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# -*- coding: utf-8 -*-
'''
To generate Colbourne's and STACFIS composite anomalies
Uses this pickled DataFrame:
/home/cyrf0006/AZMP/state_reports/SSTs/SSTs_merged_monthly.pkl
generated by from azmp_sst_scorecards.py
'''
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import os
import unicodedata
from matplotlib.colors import from_levels_and_colors
import cmocean as cmo
clim_year = [1991, 2020]
years = [1980, 2023]
width = 0.5
year0 = 1985
yearf = 2023
n=5
#### ---- LOAD THE DATA ---- ####
# 1. CIL [years: vol_itp, core_itp, core_depth_itp] [DONE 2024]
#df_CIL_SI = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/sections_plots/CIL/df_CIL_SI_summer.pkl')
#df_CIL_BB = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/sections_plots/CIL/df_CIL_BB_summer.pkl')
#df_CIL_FC = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/sections_plots/CIL/df_CIL_FC_summer.pkl')
df_CIL_SI = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/df_CIL_SI_summer_climfill.pkl')
df_CIL_BB = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/df_CIL_BB_summer_climfill.pkl')
df_CIL_FC = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/df_CIL_FC_summer_climfill.pkl')
# 2. NAO & AO [years: Value] [DONE 2024]
nao_winter = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/NAO_winter.pkl')
nao_winter = nao_winter[nao_winter.index<=yearf]
ao = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/AO_annual.pkl')
# 3. Air Temperature [DONE 2024]
df_air = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/airT_monthly.pkl')
df_air = df_air.resample('YS').mean()
df_air.index = df_air.index.year
# 4. SSTs (problem: NS data missing prior 1997...) [DONE 2024]
#df_sst_boxes = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/SSTs/SSTs_merged_monthly.pkl')
#df_sst_boxes = df_sst_boxes.resample('As').mean()
#df_sst_boxes.index = df_sst_boxes.index.year
## df_sst_1997 = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/SSTs/SSTs_bometrics_annual.pkl')
## df_sst_1997 = df_sst_1997.resample('As').mean()
## df_sst_1997.index = df_sst_1997.index.year
# Peter's SST
df_sst = pd.read_csv('/home/cyrf0006/github/AZMP-NL/external_data/Galbraith_data/AZMP_SST_Seasonal.dat', delimiter=r"\s+", index_col='#Yr', header=27)
df_sst.index.name = 'year'
df_sst = df_sst.replace(-99.00, np.nan)
df_sst = df_sst[df_sst.index<=yearf]
# 5. Bottom temperature [DONE 2024]
# [TO DO: remove bad years from .pkl file]
# 3LNO - Spring
df = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/stats_3LNO_spring.pkl')
df.index = df.index.astype('int')
bad_years = np.array([2020, 2021])
for i in bad_years:
df[df.index==i]=np.nan
df_3LNO_spring = df.Tmean
del df
# 3Ps - Spring
df = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/stats_3Ps_spring.pkl')
df.index = df.index.astype('int')
bad_years = np.array([1980, 1981, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 2006])
for i in bad_years:
df[df.index==i]=np.nan
df_3Ps_spring = df.Tmean
del df
# 2H - Fall
df = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/stats_2H_fall.pkl')
df.index = df.index.astype('int')
bad_years = np.array([1980, 1982, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1992, 1993, 1994, 1995, 1996, 2000, 2002, 2003, 2005, 2007, 2009])
for i in bad_years:
df[df.index==i]=np.nan
df_2H_fall = df.Tmean
del df
# 2J - Fall
df = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/stats_2J_fall.pkl')
df.index = df.index.astype('int')
df_2J_fall = df.Tmean
del df
# 3K - Fall
df = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/stats_3K_fall.pkl')
df.index = df.index.astype('int')
df_3K_fall = df.Tmean
del df
# 3LNO - Fall
df = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/stats_3LNO_fall.pkl')
df.index = df.index.astype('int')
bad_years = np.array([2021])
for i in bad_years:
df[df.index==i]=np.nan
df_3LNO_fall = df.Tmean
del df
# 3M - Summer now using CABOTS
df = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/stats_3M_summer.pkl')
df.index = df.index.astype('int')
bad_years = np.array([1983, 1984, 1994, 2007])
for i in bad_years:
df[df.index==i]=np.nan
df_3M_summer = df.Tmean
del df
# 4VWX - Summer now using CABOTS
df = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/stats_4VWX_summer.pkl')
df.index = df.index.astype('int')
bad_years = np.array([])
for i in bad_years:
df[df.index==i]=np.nan
df_4VWX_summer = df.Tmean
del df
# 4VWX from Layton
df_4v = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/summerGroundfishBottomTemperature_4V.dat', delimiter=r",", index_col='year', header=10)
df_4vn = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/summerGroundfishBottomTemperature_4Vn.dat', delimiter=r",", index_col='year', header=10)
df_4vs = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/summerGroundfishBottomTemperature_4Vs.dat', delimiter=r",", index_col='year', header=10)
df_4w = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/summerGroundfishBottomTemperature_4W.dat', delimiter=r",", index_col='year', header=10)
df_4x = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/summerGroundfishBottomTemperature_4X.dat', delimiter=r",", index_col='year', header=10)
# Merge weird formatting.
df_4vwx = pd.concat([df_4v['temperature'],df_4w['temperature'],df_4x['temperature']], axis=1)
df_4vwx = df_4vwx.replace(r'\s+', '', regex=True)
df_4vwx = df_4vwx.replace('NA', np.nan)
df_4vwx = df_4vwx.astype('float')
df_4vwx = df_4vwx.mean(axis=1)
# comparision
df_4VWX_summer.plot()
df_4vwx.plot()
plt.legend(['from CASTS', 'from BIO'])
plt.title('Bottom temperature - NAFO 4VWX')
plt.ylabel(r'$\rm T(^{\circ}C)$')
# 6. Fixed stations [CHECK!]
# S27
df_s27 = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/S27_temperature_monthly.pkl')
df_s27 = df_s27.resample('YS').mean()
df_s27.index = df_s27.index.year
df_s27_mean = df_s27.mean(axis=1)
# HFX-2 0-50m
#df_hfx2_surf = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/HFX2_Integrated_0-50m.csv', index_col='Year')
#df_hfx2_surf = df_hfx2_surf.iloc[:,0]
#df_hfx2_surf.index = np.array(df_hfx2_surf.index, dtype=int)
#df_hfx2_surf = pd.to_numeric(df_hfx2_surf, errors='coerce').astype('Float64')
df_hfx2_surf = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/H2_0-50m_integrated.dat', sep=' ', skiprows=[1], index_col='Year')
df_hfx2_surf = df_hfx2_surf['T'].astype('float')
df_hfx2_surf = df_hfx2_surf.replace(-99.00, np.nan)
df_hfx2_surf = df_hfx2_surf[df_hfx2_surf.index<=yearf]
# HFX-2 150m
#df_hfx2_150 = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/HFX2_150m_Temperature.csv', header=2, index_col='Year')
#df_hfx2_150 = df_hfx2_150.iloc[:,2]
df_hfx2_150 = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/HFX2_150m_temp.dat', sep=' ')
df_hfx2_150.columns=['year', 'temp']
df_hfx2_150.set_index('year', inplace=True)
df_hfx2_150 = df_hfx2_150.replace(-99.00, np.nan)
df_hfx2_150 = df_hfx2_150[df_hfx2_150.index<=yearf]
# Prince-5 0-50m
#df_p5_surf = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/P5_Annual_Series_0-50m.csv', header=1, index_col='Year')
df_p5_surf = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/P5_0-50m_integrated.dat', sep=' ', skiprows=[1], index_col='Year')
df_p5_surf = df_p5_surf['T'].astype('float')
df_p5_surf = df_p5_surf.replace(-99.00, np.nan)
df_p5_surf = df_p5_surf[df_p5_surf.index<=yearf]
# Prince-5 0-90m (Have to manually tweak it)
#df_p5_90 = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/P5_Integrated_0-90m.csv', header=0, index_col='Year')
#df_p5_90 = df_p5_90.iloc[:,1]
df_p5_90 = pd.read_csv('/home/cyrf0006/data/Hebert_timeseries/prince5IntegratedTemperature0-90m.dat', index_col='year', header=10)
df_p5_90 = df_p5_90.replace(-99.00, np.nan)
df_p5_90 = df_p5_90.replace(r'\s+', '', regex=True)
df_p5_90 = df_p5_90.replace('NA', np.nan)
df_p5_90 = df_p5_90['temperature'].astype('float')
df_p5_90 = df_p5_90[df_p5_90.index<=yearf]
# 7. Section average Temperature (should eventually add salinity in these dataFrame, see azmp_CIL_stats.py) [DONE 2024]
# New from 2024:
# SI
df_SI = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/df_SI_meanT_summer.pkl')
df_SI = df_SI['stn_meanT']
# BB
df_BB = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/df_BB_meanT_summer.pkl')
df_BB = df_BB['stn_meanT']
# FC
df_FC = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/df_FC_meanT_summer.pkl')
df_FC = df_FC['stn_meanT']
# FC shelf
df_FC_shelf = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/df_FC_meanT_shelf_summer.pkl')
df_FC_shelf = df_FC_shelf['stn_meanT_shelf']
# FC cap
df_FC_cap = pd.read_pickle('/home/cyrf0006/AZMP/state_reports/2023_report/operation_files/df_FC_meanT_cap_summer.pkl')
df_FC_cap = df_FC_cap['stn_meanT_cap']
# 8. Greenland Fylla and Cape Desolation (from IROC) [NOT UPDATE SINCE 2019] [DONE 2024]
df_FB4 = pd.read_csv('/home/cyrf0006/data/IROC_timeseries/Greenland_Fylla_0-50_Annual.csv', header=15, index_col='Year', encoding = "ISO-8859-1")
df_CD3_2000 = pd.read_csv('/home/cyrf0006/data/IROC_timeseries/Greenland_Desolation_2000_Annual.csv', header=14, index_col='Year', encoding = "ISO-8859-1")
df_CD3_200 = pd.read_csv('/home/cyrf0006/data/IROC_timeseries/Greenland_Desolation_75-200_Annual.csv', header=15, index_col='Year', encoding = "ISO-8859-1")
# Keep only tempeprature
df_FB4 = df_FB4.iloc[:,0]
df_CD3_2000 = df_CD3_2000.iloc[:,0]
df_CD3_200 = df_CD3_200.iloc[:,0]
# 9. Scotian shelf and GoM timeseries (from IROC) [DONE 2024]
df_emeral = pd.read_csv('/home/cyrf0006/data/IROC_timeseries/Scotian_Emerald_Annual.csv', header=15, index_col='Year', encoding = "ISO-8859-1")
df_misaine = pd.read_csv('/home/cyrf0006/data/IROC_timeseries/Scotian_Misaine_Annual.csv', header=15, index_col='Year', encoding = "ISO-8859-1")
df_egom = pd.read_csv('/home/cyrf0006/data/IROC_timeseries/USA_EGOM_Annual.csv', header=19, index_col='Year', encoding = "ISO-8859-1")
df_nec = pd.read_csv('/home/cyrf0006/data/IROC_timeseries/USA_NEC_Annual.csv', header=19, index_col='Year', encoding = "ISO-8859-1")
## Keep only temperature
df_emeral = df_emeral.iloc[:,1] # Already stn anom!!
df_misaine = df_misaine.iloc[:,1] # already std anom!!
df_egom = df_egom.iloc[:,0]
df_nec = df_nec.iloc[:,0]
# 10. Central Lab Sea (from IROC) [DONE 2024]
# Legacy
## df_cls = pd.read_csv('/home/cyrf0006/data/IROC_timeseries/LabradorSea_0020-1800_Annual.csv', header=15, index_col='Year', encoding = "ISO-8859-1")
## df_cls = df_cls.iloc[:,0]
## df_cls.index = df_cls.index.astype('int')
## df_cls = df_cls[df_cls.index<=yearf]
# Shallow Lab Sea
df_cls_shal = pd.read_csv('/home/cyrf0006/data/IROC_timeseries/LabradorSea_0050-0200_Annual.csv', header=15, index_col='Year', encoding = "ISO-8859-1")
df_cls_shal = df_cls_shal.iloc[:,0]
df_cls_shal.index = df_cls_shal.index.astype('int')
df_cls_shal = df_cls_shal[df_cls_shal.index<=yearf]
# Deep Lab Sea
df_cls_deep = pd.read_csv('/home/cyrf0006/data/IROC_timeseries/LabradorSea_1000-1800_Annual.csv', header=15, index_col='Year', encoding = "ISO-8859-1")
df_cls_deep = df_cls_deep.iloc[:,0]
df_cls_deep.index = df_cls_deep.index.astype('int')
df_cls_deep = df_cls_deep[df_cls_deep.index<=yearf]
#### ---- STACFIS - 3LNO ---- ####
df_comp_3LNO = pd.concat([df_s27_mean,
df_3LNO_spring, df_3LNO_fall,
df_SI, df_BB, df_FC_shelf,
df_CIL_SI.vol_itp, df_CIL_BB.vol_itp, df_CIL_FC.vol_itp,
df_sst['3L'], df_sst['3N'], df_sst['3O']
], axis=1)
df_3LNO_clim = df_comp_3LNO[(df_comp_3LNO.index>=clim_year[0]) & (df_comp_3LNO.index<=clim_year[1])]
std_anom_3LNO = (df_comp_3LNO-df_3LNO_clim.mean(axis=0))/df_3LNO_clim.std(axis=0)
# revert CIL volume
std_anom_3LNO['vol_itp'] = std_anom_3LNO['vol_itp']*-1
# mean anomaly
composite_3LNO = std_anom_3LNO.mean(axis=1)
composite_3LNO.to_csv('composite_3LNO.csv', float_format='%.2f')
# Plot
composite_3LNO = composite_3LNO[composite_3LNO.index>=year0]
fig, ax = plt.subplots(nrows=1, ncols=1)
sign=composite_3LNO>0
composite_3LNO.plot(kind='bar', color=sign.map({True: 'indianred', False: 'steelblue'}), width = width)
ticks = ax.xaxis.get_ticklocs()
ticklabels = [l.get_text() for l in ax.xaxis.get_ticklabels()]
ax.xaxis.set_ticks(ticks[::n])
ax.xaxis.set_ticklabels(ticklabels[::n])
plt.fill_between([ticks[0]-1, ticks[-1]+1], [-.5, -.5], [.5, .5], facecolor='gray', alpha=.2)
plt.ylabel('Standardized Anomaly', weight='bold', fontsize=14)
plt.title('Composite anomaly 3LNO', weight='bold', fontsize=14)
plt.grid()
plt.ylim([-2,2])
#fig.set_size_inches(w=15,h=7)
fig_name = 'composite_3LNO.png'
fig.savefig(fig_name, dpi=200)
os.system('convert -trim composite_3LNO.png composite_3LNO.png')
#### ---- STACFIS - 3M ---- ####
df_comp_3M = pd.concat([df_FC_cap,
df_sst['3M'],
df_3M_summer
], axis=1)
df_3M_clim = df_comp_3M[(df_comp_3M.index>=clim_year[0]) & (df_comp_3M.index<=clim_year[1])]
std_anom_3M = (df_comp_3M-df_3M_clim.mean(axis=0))/df_3M_clim.std(axis=0)
composite_3M = std_anom_3M.mean(axis=1)
composite_3M.to_csv('composite_3M.csv', float_format='%.2f')
# Plot
composite_3M = composite_3M[composite_3M.index>=year0]
fig, ax = plt.subplots(nrows=1, ncols=1)
sign=composite_3M>0
composite_3M.plot(kind='bar', color=sign.map({True: 'indianred', False: 'steelblue'}), width = width)
ticks = ax.xaxis.get_ticklocs()
ticklabels = [l.get_text() for l in ax.xaxis.get_ticklabels()]
ax.xaxis.set_ticks(ticks[::n])
ax.xaxis.set_ticklabels(ticklabels[::n])
plt.fill_between([ticks[0]-1, ticks[-1]+1], [-.5, -.5], [.5, .5], facecolor='gray', alpha=.2)
plt.ylabel('Standardized Anomaly', weight='bold', fontsize=14)
plt.title('Composite anomaly 3M', weight='bold', fontsize=14)
plt.grid()
plt.ylim([-2,2])
#fig.set_size_inches(w=15,h=7)
fig_name = 'composite_3M.png'
fig.savefig(fig_name, dpi=200)
os.system('convert -trim composite_3M.png composite_3M.png')
#### ---- STACFIS - SA01 ---- #### (0B1CDEF)
# New from 2024 (no SST boxes): (TO-DO add SFA bottom T)
df_comp_SA01 = pd.concat([df_FB4, df_CD3_200, df_CD3_2000,
df_air.Nuuk, df_air.Iqaluit, df_cls_shal, df_cls_deep
], axis=1)
## df_comp_SA01 = pd.concat([df_sst_boxes.Central_Labrador_Sea,
## df_sst_boxes.North_Central_Labrador_Sea,
## df_sst_boxes.Greenland_Shelf,
## df_sst_boxes.Hudson_Strait,
## df_FB4, df_CD3_200, df_CD3_2000,
## df_air.Nuuk, df_air.Iqaluit, df_cls_shal, df_cls_deep
## ], axis=1)
df_SA01_clim = df_comp_SA01[(df_comp_SA01.index>=clim_year[0]) & (df_comp_SA01.index<=clim_year[1])]
std_anom_SA01 = (df_comp_SA01-df_SA01_clim.mean(axis=0))/df_SA01_clim.std(axis=0)
composite_SA01 = std_anom_SA01.mean(axis=1)
composite_SA01.to_csv('composite_SA01.csv', float_format='%.2f')
# Plot
composite_SA01 = composite_SA01[composite_SA01.index>=year0]
fig, ax = plt.subplots(nrows=1, ncols=1)
sign=composite_SA01>0
composite_SA01.plot(kind='bar', color=sign.map({True: 'indianred', False: 'steelblue'}), width = width, zorder=10)
ticks = ax.xaxis.get_ticklocs()
ticklabels = [l.get_text() for l in ax.xaxis.get_ticklabels()]
ax.xaxis.set_ticks(ticks[::n])
ax.xaxis.set_ticklabels(ticklabels[::n])
plt.fill_between([ticks[0]-1, ticks[-1]+1], [-.5, -.5], [.5, .5], facecolor='gray', alpha=.2)
plt.ylabel('Normalized Anomaly', weight='bold', fontsize=14)
plt.title('Composite anomaly SA01', weight='bold', fontsize=14)
plt.grid()
plt.ylim([-2,2])
#fig.set_size_inches(w=15,h=7)
fig_name = 'composite_SA01.png'
fig.savefig(fig_name, dpi=200)
os.system('convert -trim composite_SA01.png composite_SA01.png')
#### ---- STACFIS - SA234 ---- ####
# ** HEre I ignored SSTs, because not available for NS region prior to 1997
# df_sst.Hudson_Strait, df_sst.Hamilton_Bank, df_sst['St.Anthony_Basin'], df_sst.Orphan_Knoll
# df_sst.Avalon_Channel, df_sst.Hybernia, df_sst.Flemish_Pass, df_sst.Flemish_Cap, ...
## df_sst_SA3 = pd.concat([df_sst['St.Anthony_Basin'], df_sst['Northeast_Nfld_Shelf'],
## df_sst['Orphan_Knoll'],df_sst['Avalon_Channel'],
## df_sst['Hybernia'],df_sst['Flemish_Cap'],
## df_sst['Flemish_Pass'],df_sst['Green-St._Pierre_Bank']
## ], axis=1)
# Here df_p5_0-90 is missing because anomaly was provided rather than T
df_comp_SA234 = pd.concat([df_s27_mean, df_p5_90, df_hfx2_surf, df_hfx2_150,
df_2H_fall, df_2J_fall,
df_3LNO_spring, df_3LNO_fall, df_3M_summer, df_4VWX_summer,
df_SI, df_BB, df_FC_shelf,
df_CIL_SI.vol_itp, df_CIL_BB.vol_itp, df_CIL_FC.vol_itp,
df_egom, df_nec
], axis=1)
df_comp_SA2 = pd.concat([df_2H_fall, df_2J_fall,
df_air.Cartwright,
df_sst['2G'], df_sst['2H'], df_sst['2J'],
df_SI,
df_CIL_SI.vol_itp
], axis=1)
df_comp_SA3 = pd.concat([df_s27_mean,
df_air.Bonavista, df_air.StJohns,
df_sst['3K'], df_sst['3L'], df_sst['3M'],
df_sst['3N'], df_sst['3O'], df_sst['3P'],
df_3LNO_spring, df_3LNO_fall, df_3M_summer,
df_BB, df_FC,
df_CIL_BB.vol_itp, df_CIL_FC.vol_itp
], axis=1)
df_comp_SA4 = pd.concat([df_p5_90, df_hfx2_surf, df_hfx2_150,
df_sst['4Vn'], df_sst['4Vs'], df_sst['4W'], df_sst['4XSS'],
df_4vwx,
df_egom, df_nec
], axis=1)
# anomaly calculations
df_SA234_clim = df_comp_SA234[(df_comp_SA234.index>=clim_year[0]) & (df_comp_SA234.index<=clim_year[1])]
df_SA2_clim = df_comp_SA2[(df_comp_SA2.index>=clim_year[0]) & (df_comp_SA2.index<=clim_year[1])]
df_SA3_clim = df_comp_SA3[(df_comp_SA3.index>=clim_year[0]) & (df_comp_SA3.index<=clim_year[1])]
df_SA4_clim = df_comp_SA4[(df_comp_SA4.index>=clim_year[0]) & (df_comp_SA4.index<=clim_year[1])]
std_anom_SA234 = (df_comp_SA234-df_SA234_clim.mean(axis=0))/df_SA234_clim.std(axis=0)
std_anom_SA2 = (df_comp_SA2-df_SA2_clim.mean(axis=0))/df_SA2_clim.std(axis=0)
std_anom_SA3 = (df_comp_SA3-df_SA3_clim.mean(axis=0))/df_SA3_clim.std(axis=0)
std_anom_SA4 = (df_comp_SA4-df_SA4_clim.mean(axis=0))/df_SA4_clim.std(axis=0)
# revert CIL volume
std_anom_SA234['vol_itp'] = std_anom_SA234['vol_itp']*-1
std_anom_SA2['vol_itp'] = std_anom_SA2['vol_itp']*-1
std_anom_SA3['vol_itp'] = std_anom_SA3['vol_itp']*-1
# add misaine and Emerald (already std anom)
std_anom_SA234['misaine'] = df_misaine
std_anom_SA234['emerald'] = df_emeral
std_anom_SA4['misaine'] = df_misaine
std_anom_SA4['emerald'] = df_emeral
# Composite as average and save
composite_SA234 = std_anom_SA234.mean(axis=1)
composite_SA234.to_csv('composite_SA234.csv', float_format='%.2f')
composite_SA2 = std_anom_SA2.mean(axis=1)
composite_SA3 = std_anom_SA3.mean(axis=1)
composite_SA4 = std_anom_SA4.mean(axis=1)
# Plot
composite_SA234 = composite_SA234[composite_SA234.index>=year0]
fig, ax = plt.subplots(nrows=1, ncols=1)
sign=composite_SA234>0
composite_SA234.plot(kind='bar', color=sign.map({True: 'indianred', False: 'steelblue'}), width = width)
ticks = ax.xaxis.get_ticklocs()
ticklabels = [l.get_text() for l in ax.xaxis.get_ticklabels()]
ax.xaxis.set_ticks(ticks[::n])
ax.xaxis.set_ticklabels(ticklabels[::n])
plt.fill_between([ticks[0]-1, ticks[-1]+1], [-.5, -.5], [.5, .5], facecolor='gray', alpha=.2)
plt.ylabel('Standardized Anomaly', weight='bold', fontsize=14)
plt.title('Composite anomaly SA234', weight='bold', fontsize=14)
plt.grid()
plt.ylim([-2,2])
#fig.set_size_inches(w=15,h=7)
fig_name = 'composite_SA234.png'
fig.savefig(fig_name, dpi=200)
os.system('convert -trim composite_SA234.png composite_SA234.png')
# Plot SA 2-3-4 stacked HERE!!!!!!!
df_comp_SA234_stack = pd.concat([composite_SA2,
composite_SA3,
composite_SA4,
], keys = ['SA2', 'SA3', 'SA4'], axis=1)
df_comp_SA234_stack = df_comp_SA234_stack.sort_index()
df_comp_SA234_stack = df_comp_SA234_stack[df_comp_SA234_stack.index>=1950]
df_comp_SA234_stack.to_csv('composite_SA234_stacked.csv', float_format='%.2f')
# Normalize sub indices
df_comp_SA234_stack_norm = df_comp_SA234_stack.divide((df_comp_SA234_stack.shape[1] - df_comp_SA234_stack.isna().sum(axis=1)).values, axis=0)
## ---- plot annual ---- ##
#fig, ax = plt.subplots(nrows=1, ncols=1)
n = 5 # xtick every n years
ax = df_comp_SA234_stack_norm.plot(kind='bar', stacked=True, cmap=cmo.cm.haline, alpha=.9, zorder=10)
ticks = ax.xaxis.get_ticklocs()
ticklabels = [l.get_text() for l in ax.xaxis.get_ticklabels()]
ax.xaxis.set_ticks(ticks[::n])
ax.xaxis.set_ticklabels(ticklabels[::n])
plt.grid('on')
ax.set_ylabel(r'Standardized anomaly')
ax.set_title('NAFO sub-areas 2, 3 & 4')
plt.legend(['SA-2', 'SA-3', 'SA-4'])
plt.fill_between([ticks[0]-1, ticks[-1]+1], [-.5, -.5], [.5, .5], facecolor='gray', alpha=.2)
#plt.xlim([ticks[0]-1, ticks[0]+1])
fig = ax.get_figure()
#fig.set_size_inches(w=12,h=8)
fig_name = 'composite_SA234_stacked.png'
fig.savefig(fig_name, dpi=300)
os.system('convert -trim ' + fig_name + ' ' + fig_name)