import numpy as np
import xarray as xr
import pandas as pd
from salishsea_tools import viz_tools, places, visualisations
from matplotlib import pyplot as plt, dates
from datetime import datetime, timedelta
from calendar import month_name
from scipy.io import loadmat
from tqdm.notebook import tqdm
from salishsea_tools import nc_tools
from dask.diagnostics import ProgressBar
import cmocean
from scipy.stats import sem
import scipy.stats as stats
%matplotlib inline
plt.rcParams.update({'font.size': 12, 'axes.titlesize': 'medium'})
#years, months, data
monthly_array_nitrate_depthint_slice = np.zeros([14,12,50,50])
# Load monthly averages
mask = xr.open_dataset('/data/eolson/results/MEOPAR/NEMO-forcing-new/grid/mesh_mask201702.nc')
slc = {'y': slice(450,500), 'x': slice(250,300)}
e3t, tmask = [mask[var].isel(z=slice(None, 10),**slc).values for var in ('e3t_0', 'tmask')]
years, variables = range(2007, 2021), ['nitrate']
# nitrate_depthintorary list dict
data = {}
# Permanent aggregate dict
aggregates = {var: {} for var in variables}
monthlydat = {var: {} for var in variables}
# Loop through years
for year in [2007,2008,2009,2010,2011,2012,2016,2017,2018,2019,2020]:
# Initialize lists
for var in variables: data[var] = []
# Load monthly averages
for month in range(1, 13):
datestr = f'{year}{month:02d}'
prefix = f'/data/sallen/results/MEOPAR/v201905r/SalishSea_1m_{datestr}_{datestr}'
# Load grazing variables
with xr.open_dataset(prefix + '_ptrc_T.nc') as ds:
q = np.ma.masked_where(tmask == 0, ds[var].isel(deptht=slice(None, 10),**slc)*e3t*tmask).sum(axis=1)/((e3t*tmask).sum(axis=1)).data
q2 = q[0,:,:]
monthly_array_nitrate_depthint_slice[year-2007,month-1,:,:] = q2 #year2015 is index 0 along 1st dimension
for var in ['nitrate']:
data[var].append((ds[var].isel(deptht=slice(None, 10),**slc)*e3t*tmask).sum(axis=1)/((e3t*tmask).sum(axis=1)).data)
# Concatenate months
for var in variables: aggregates[var][year] = np.concatenate(data[var]).mean(axis=0)
# Loop through years for wrap files
for year in [2013,2014,2015]:
# Initialize lists
for var in variables: data[var] = []
# Load monthly averages
for month in range(1, 13):
datestr = f'{year}{month:02d}' #SalishSea_1m_201606_2016_06_ptrc_T.nc e.g., file name
prefix = f'/data/sallen/results/MEOPAR/v201905r_wrap/SalishSea_1m_{datestr}_{datestr}'
# Load grazing variables
with xr.open_dataset(prefix + '_ptrc_T.nc') as ds:
q = np.ma.masked_where(tmask == 0, ds[var].isel(deptht=slice(None, 10),**slc)*e3t*tmask).sum(axis=1)/((e3t*tmask).sum(axis=1)).data
q2 = q[0,:,:]
monthly_array_nitrate_depthint_slice[year-2007,month-1,:,:] = q2 #year2015 is index 0 along 1st dimension
for var in ['nitrate']:
data[var].append((ds[var].isel(deptht=slice(None, 10),**slc)*e3t*tmask).sum(axis=1)/((e3t*tmask).sum(axis=1)).data)
# Concatenate months
for var in variables: aggregates[var][year] = np.concatenate(data[var]).mean(axis=0)
# # Calculate 5 year mean and anomalies
# for var in variables:
# aggregates[var][‘mean’] = np.concatenate([aggregates[var][year][None, ...] for year in years]).mean(axis=0)
# for year in years: aggregates[var][year] = aggregates[var][year] - aggregates[var][‘mean’]
monthly_array_nitrate_depthint_slice[monthly_array_nitrate_depthint_slice == 0 ] = np.nan
monthly_array_nitrate_depthint_slicemean = \
np.nanmean(np.nanmean(monthly_array_nitrate_depthint_slice, axis = 2),axis = 2)
print(np.shape(monthly_array_nitrate_depthint_slicemean))
(14, 12)
annualnitrate=np.array([monthly_array_nitrate_depthint_slicemean[:,11]])
annualmean=annualnitrate.mean(axis=0).flatten()
annualmean
array([24.00553418, 23.24806845, 24.41816855, 23.53940048, 23.23904027, 24.25458263, 22.24560435, 21.43710567, 23.33864058, 21.57330813, 21.87513823, 23.055292 , 21.62759183, 21.96520988])
#plot monthly means for all years
fig, ax = plt.subplots(figsize=(15, 6))
bbox = {'boxstyle': 'round', 'facecolor': 'w', 'alpha': 0.9}
cmap = plt.get_cmap('tab10')
palette = [cmap(0), cmap(0.2), 'k', cmap(0.1), cmap(0.3)]
for i in range(0,7):
ax.plot(np.arange(1,13), monthly_array_nitrate_depthint_slicemean[i,:],label=2007+i)
ax.set_title('Central SoG Nitrate Seasonal Cycle',fontsize=18)
ax.legend(frameon=False)
ax.set_ylim(0,500)
ax.set_ylabel('\u03bcmol N')
for i in range(7,14):
ax.plot(np.arange(1,13), monthly_array_nitrate_depthint_slicemean[i,:],linestyle='--',label=2007+i)
ax.set_title('Central SoG 0-10 m Nitrate Seasonal Cycle',fontsize=18)
ax.legend(frameon=False,bbox_to_anchor=(1, 1))
ax.set_ylim(0,30)
ax.set_ylabel('\u03bcmol N m$^{-3}$')
#2008, 2010, 2011, 2012
NPGO_C=(((+monthly_array_nitrate_depthint_slicemean[1,:]+\
monthly_array_nitrate_depthint_slicemean[3,:]+\
monthly_array_nitrate_depthint_slicemean[4,:]+monthly_array_nitrate_depthint_slicemean[5,:])/4))
#2015, 2018, 2019, 2020
NPGO_W=(((monthly_array_nitrate_depthint_slicemean[8,:]+\
monthly_array_nitrate_depthint_slicemean[11,:]+monthly_array_nitrate_depthint_slicemean[12,:]+\
monthly_array_nitrate_depthint_slicemean[13,:])/4))
## Plot the coldest and warmest years only; Supp Fig. S6
fig, ax = plt.subplots(figsize=(14, 2))
bbox = {'boxstyle': 'round', 'facecolor': 'w', 'alpha': 0.9}
cmap = plt.get_cmap('tab10')
palette = [cmap(0), cmap(0.2), 'k', cmap(0.1), cmap(0.3)]
xticks=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov',"Dec"]
ax.plot(xticks, monthly_array_nitrate_depthint_slicemean[1,:],color='b',linestyle='-',label='2008')
ax.plot(xticks, monthly_array_nitrate_depthint_slicemean[3,:],color='b',linestyle='--',label='2010')
ax.plot(xticks, monthly_array_nitrate_depthint_slicemean[4,:],color='b',linestyle='-.',label='2011')
ax.plot(xticks, monthly_array_nitrate_depthint_slicemean[5,:],color='b',linestyle=':',label='2012')
ax.plot(xticks, monthly_array_nitrate_depthint_slicemean[8,:],color='r',linestyle='-',label='2015')
ax.plot(xticks, monthly_array_nitrate_depthint_slicemean[11,:],color='r',linestyle='--',label='2018')
ax.plot(xticks, monthly_array_nitrate_depthint_slicemean[12,:],color='r',linestyle='-.',label='2019')
ax.plot(xticks, monthly_array_nitrate_depthint_slicemean[13,:],color='r',linestyle=':',label='2020')
ax.set_title('Depth-Averaged Nitrate (0-10 m)',fontsize=14)
ax.legend(frameon=False,loc='center left', bbox_to_anchor=(1, 0.5),fontsize=10)
ax.set_ylim(0,30)
ax.set_ylabel('\u03bcmol N m$^{-3}$')
ax.xaxis.set_tick_params(labelsize=12)
ax.yaxis.set_tick_params(labelsize=12)
ax.set_xticklabels([])
[Text(0, 0, ''), Text(1, 0, ''), Text(2, 0, ''), Text(3, 0, ''), Text(4, 0, ''), Text(5, 0, ''), Text(6, 0, ''), Text(7, 0, ''), Text(8, 0, ''), Text(9, 0, ''), Text(10, 0, ''), Text(11, 0, '')]
NPGO_W_years=[monthly_array_nitrate_depthint_slicemean[8,:],monthly_array_nitrate_depthint_slicemean[11,:],monthly_array_nitrate_depthint_slicemean[12,:],monthly_array_nitrate_depthint_slicemean[13,:]]
sem(NPGO_W_years)
array([0.51579676, 0.37462136, 1.25781481, 0.71621388, 0.51148759, 0.26021288, 0.46033086, 1.01753056, 1.25218246, 0.58446005, 0.40358085, 0.41419799])
NPGO_W_SEM=[0.36677255, 0.25092139, 0.68853362, 0.50627303, 0.44464386,
0.24919049, 0.63251747, 1.03646634, 0.85953584, 0.45853951,
0.25025754, 0.12691501]
NPGO_C_years=[monthly_array_nitrate_depthint_slicemean[1,:],
monthly_array_nitrate_depthint_slicemean[3,:],
monthly_array_nitrate_depthint_slicemean[4,:],monthly_array_nitrate_depthint_slicemean[5,:]]
sem(NPGO_C_years)
array([0.15447266, 0.17800945, 0.42490985, 2.15860675, 0.36713257, 0.31158354, 0.34657936, 0.74214772, 1.03726756, 0.60475733, 0.11337984, 0.23853087])
NPGO_C_SEM=[0.11667523, 0.11190603, 0.20137107, 1.23414193, 0.51169805,
0.58081648, 0.51456234, 0.5983553 , 0.45166945, 0.24010748,
0.22360953, 0.1472202]
NPGO_C
array([24.3980628 , 23.67905457, 22.59146629, 14.48463374, 5.38950038, 2.51176774, 1.97124377, 3.54658716, 8.74831538, 17.18580439, 21.6594439 , 23.57027296])
## Preliminary Figure 4g
fig, ax = plt.subplots(figsize=(15, 3))
bbox = {'boxstyle': 'round', 'facecolor': 'w', 'alpha': 0.9}
cmap = plt.get_cmap('tab10')
palette = [cmap(0), cmap(0.2), 'k', cmap(0.1), cmap(0.3)]
xticks=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov',"Dec"]
border = 0.25
ax.errorbar(xticks, NPGO_C, yerr= NPGO_C_SEM, capsize=3,label='NPGO+ coldest',linewidth=2)
ax.errorbar(xticks, NPGO_W,yerr= NPGO_W_SEM, capsize=3,linestyle='--',label='NPGO- warmest',color='r',linewidth=2)
ax.set_title('Depth Averaged Nitrate (0-10 m)',fontsize=18)
ax.legend((),frameon=False)
ax.set_ylim(0,30)
ax.set_ylabel('\u03bcmol N m$^{-3}$',fontsize=14)
ax.xaxis.set_tick_params(labelsize=14)
ax.yaxis.set_tick_params(labelsize=14)
ax.set_xticklabels([])
a=2
b=5
c=5
d=8
ax.text(-.4, 25, '(g)', fontsize=15, color='k')
#plt.fill([a, a, b, b], [0, 25, 25, 0], color = 'lightgreen', alpha = 0.1)
#plt.fill([c, c, d, d], [0, 25, 25, 0], color = 'wheat', alpha = 0.2)
#plt.savefig('Fig3g_Nitrate.png', bbox_inches='tight',dpi=1000,transparent=False)
Text(-0.4, 25, '(g)')
Spring_C=(((monthly_array_nitrate_depthint_slicemean[(1,3,4,5),2]+
monthly_array_nitrate_depthint_slicemean[(1,3,4,5),3]+monthly_array_nitrate_depthint_slicemean[(1,3,4,5),4]))/3) #
Spring_W=(((monthly_array_nitrate_depthint_slicemean[(8,11,12,13),2]+
monthly_array_nitrate_depthint_slicemean[(8,11,12,13),3]+monthly_array_nitrate_depthint_slicemean[(8,11,12,13),4]))/3) #
Summer_C=(((monthly_array_nitrate_depthint_slicemean[(1,3,4,5),5]+
monthly_array_nitrate_depthint_slicemean[(1,3,4,5),6]+monthly_array_nitrate_depthint_slicemean[(1,3,4,5),7]))/3) #
Summer_W=(((monthly_array_nitrate_depthint_slicemean[(8,11,12,13),5]+
monthly_array_nitrate_depthint_slicemean[(8,11,12,13),6]+monthly_array_nitrate_depthint_slicemean[(8,11,12,13),7]))/3) #
Summer_C
array([3.48006453, 3.20434749, 1.67761007, 2.34410947])
Summer_W
array([3.69164181, 1.13548262, 1.43987888, 1.86403363])
## Preliminary figure 4g
def color_boxplot(data, color, pos=[0], ax=None):
ax = ax or plt.gca()
bp = ax.boxplot(data, patch_artist=True, showmeans=False, positions=pos,widths=0.4)
for item in ['boxes']:
plt.setp(bp[item], color=color)
for item in ['whiskers', 'fliers', 'medians', 'caps']:
plt.setp(bp[item], color='k')
data1 = [Spring_C]
data2 = [Spring_W]
data3 = [Summer_C]
data4 = [Summer_W]
fig, ax = plt.subplots(figsize=(3,3))
bp1 = color_boxplot(data1, 'royalblue', [1])
bp2 = color_boxplot(data2, 'r', [1.5])
bp3 = color_boxplot(data3, 'royalblue', [2.5])
bp4 = color_boxplot(data4, 'r', [3])
#ax.autoscale()
ax.set(xticks=[1.25,2.75], xticklabels=['Spring','Summer'])
ax.set_ylim(0,30)
ax.set_ylabel('\u03bcmol N m$^{-3}$')
#ax.legend([bp1["boxes"], bp2["boxes"], ['A', 'B'], loc='upper right')
plt.show()
Spring_C.mean()
14.155200137737731
Spring_W.mean()
9.039775058401927
Summer_C.mean()
2.676532890033181
Summer_W.mean()
2.0327592345167007
stats.ttest_ind(a=Spring_C, b=Spring_W, equal_var=True)
Ttest_indResult(statistic=5.614525894310908, pvalue=0.0013625490765860285)
stats.ttest_ind(a=Summer_C, b=Summer_W, equal_var=True)
Ttest_indResult(statistic=0.9127505297995592, pvalue=0.39656649233682384)