import numpy as np
import matplotlib.pyplot as plt
from salishsea_tools import viz_tools, places, visualisations
import netCDF4 as nc # unless you prefer xarray
import datetime as dt
import os
import glob
import cmocean
from IPython.display import Markdown, display
%matplotlib inline
d0=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20071201_20071231.nc') #Use December 2007 for both 2007 and 2008 "winter values
d1=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20081201_20081231.nc')
d2=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20091201_20091231.nc')
d3=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20101201_20101231.nc')
d4=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20111201_20111231.nc')
d5=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20121201_20121231.nc')
d6=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20131201_20131231.nc')
d7=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20141201_20141231.nc')
d8=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20151201_20151231.nc')
d9=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20161201_20161231.nc')
d10=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20171201_20171231.nc')
d11=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20181201_20181231.nc')
d12=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20191201_20191231.nc')
d13=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20201201_20201231.nc')
print(d1.variables.keys())
dict_keys(['time', 'depth', 'gridY', 'gridX', 'nitrate', 'ammonium', 'silicon', 'diatoms', 'flagellates', 'ciliates', 'microzooplankton', 'dissolved_organic_nitrogen', 'particulate_organic_nitrogen', 'biogenic_silicon', 'mesozooplankton'])
AllYears_N_Dec=((d0.variables['nitrate'][0,...])+\
(d0.variables['nitrate'][0,...])+\
(d1.variables['nitrate'][0,...])+\
(d2.variables['nitrate'][0,...])+\
(d3.variables['nitrate'][0,...])+\
(d4.variables['nitrate'][0,...])+\
(d5.variables['nitrate'][0,...])+\
(d6.variables['nitrate'][0,...])+\
(d7.variables['nitrate'][0,...])+\
(d8.variables['nitrate'][0,...])+\
(d9.variables['nitrate'][0,...])+\
(d10.variables['nitrate'][0,...])+\
(d11.variables['nitrate'][0,...])+\
(d12.variables['nitrate'][0,...]))/14
### use 2007 values for both 2007 and 2008 because there are no December 2006 files
ColdYears_N_Dec=((d0.variables['nitrate'][0,...])+\
(d2.variables['nitrate'][0,...])+\
(d3.variables['nitrate'][0,...])+\
(d4.variables['nitrate'][0,...]))/4
WarmYears_N_Dec=((d8.variables['nitrate'][0,...])+\
(d11.variables['nitrate'][0,...])+\
(d12.variables['nitrate'][0,...])+\
(d13.variables['nitrate'][0,...]))/4
AllYears_Si_Dec=((d0.variables['silicon'][0,...])+\
(d0.variables['silicon'][0,...])+\
(d1.variables['silicon'][0,...])+\
(d2.variables['silicon'][0,...])+\
(d3.variables['silicon'][0,...])+\
(d4.variables['silicon'][0,...])+\
(d5.variables['silicon'][0,...])+\
(d6.variables['silicon'][0,...])+\
(d7.variables['silicon'][0,...])+\
(d8.variables['silicon'][0,...])+\
(d9.variables['silicon'][0,...])+\
(d10.variables['silicon'][0,...])+\
(d11.variables['silicon'][0,...])+\
(d12.variables['silicon'][0,...]))/14
ColdYears_Si_Dec=((d0.variables['silicon'][0,...])+\
(d2.variables['silicon'][0,...])+\
(d3.variables['silicon'][0,...])+\
(d4.variables['silicon'][0,...]))/4
WarmYears_Si_Dec=((d8.variables['silicon'][0,...])+\
(d11.variables['silicon'][0,...])+\
(d12.variables['silicon'][0,...])+\
(d13.variables['silicon'][0,...]))/4
j0=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20070101_20070131.nc')
j1=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20080101_20080131.nc')
j2=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20090101_20090131.nc')
j3=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20100101_20100131.nc')
j4=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20110101_20110131.nc')
j5=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20120101_20120131.nc')
j6=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20130101_20130131.nc')
j7=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20140101_20140131.nc')
j8=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20150101_20150131.nc')
j9=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20160101_20160131.nc')
j10=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20170101_20170131.nc')
j11=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20180101_20180131.nc')
j12=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20190101_20190131.nc')
j13=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20200101_20200131.nc')
AllYears_N_Jan=((j0.variables['nitrate'][0,...])+\
(j1.variables['nitrate'][0,...])+\
(j2.variables['nitrate'][0,...])+\
(j3.variables['nitrate'][0,...])+\
(j4.variables['nitrate'][0,...])+\
(j5.variables['nitrate'][0,...])+\
(j6.variables['nitrate'][0,...])+\
(j7.variables['nitrate'][0,...])+\
(j8.variables['nitrate'][0,...])+\
(j9.variables['nitrate'][0,...])+\
(j10.variables['nitrate'][0,...])+\
(j11.variables['nitrate'][0,...])+\
(j12.variables['nitrate'][0,...])+\
(j13.variables['nitrate'][0,...]))/14
ColdYears_N_Jan=((j1.variables['nitrate'][0,...])+\
(j3.variables['nitrate'][0,...])+\
(j4.variables['nitrate'][0,...])+\
(j5.variables['nitrate'][0,...]))/4
WarmYears_N_Jan=((j8.variables['nitrate'][0,...])+\
(j11.variables['nitrate'][0,...])+\
(j12.variables['nitrate'][0,...])+\
(j13.variables['nitrate'][0,...]))/4
AllYears_Si_Jan=((j0.variables['silicon'][0,...])+\
(j1.variables['silicon'][0,...])+\
(j2.variables['silicon'][0,...])+\
(j3.variables['silicon'][0,...])+\
(j4.variables['silicon'][0,...])+\
(j5.variables['silicon'][0,...])+\
(j6.variables['silicon'][0,...])+\
(j7.variables['silicon'][0,...])+\
(j8.variables['silicon'][0,...])+\
(j9.variables['silicon'][0,...])+\
(j10.variables['silicon'][0,...])+\
(j11.variables['silicon'][0,...])+\
(j12.variables['silicon'][0,...])+\
(j13.variables['silicon'][0,...]))/14
ColdYears_Si_Jan=((j1.variables['silicon'][0,...])+\
(j3.variables['silicon'][0,...])+\
(j4.variables['silicon'][0,...])+\
(j5.variables['silicon'][0,...]))/4
WarmYears_Si_Jan=((j8.variables['silicon'][0,...])+\
(j11.variables['silicon'][0,...])+\
(j12.variables['silicon'][0,...])+\
(j13.variables['silicon'][0,...]))/4
f0=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20070201_20070228.nc')
f1=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20080201_20080229.nc')
f2=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20090201_20090228.nc')
f3=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20100201_20100228.nc')
f4=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20110201_20110228.nc')
f5=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20120201_20120229.nc')
f6=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20130201_20130228.nc')
f7=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20140201_20140228.nc')
f8=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20150201_20150228.nc')
f9=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20160201_20160229.nc')
f10=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20170201_20170228.nc')
f11=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20180201_20180228.nc')
f12=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20190201_20190228.nc')
f13=nc.Dataset('/results2/SalishSea/month-avg.201905/SalishSeaCast_1m_ptrc_T_20200201_20200229.nc')
print(f1.variables.keys())
dict_keys(['time', 'depth', 'gridY', 'gridX', 'nitrate', 'ammonium', 'silicon', 'diatoms', 'flagellates', 'ciliates', 'microzooplankton', 'dissolved_organic_nitrogen', 'particulate_organic_nitrogen', 'biogenic_silicon', 'mesozooplankton'])
AllYears_N_Feb=((f0.variables['nitrate'][0,...])+\
(f1.variables['nitrate'][0,...])+\
(f2.variables['nitrate'][0,...])+\
(f3.variables['nitrate'][0,...])+\
(f4.variables['nitrate'][0,...])+\
(f5.variables['nitrate'][0,...])+\
(f6.variables['nitrate'][0,...])+\
(f7.variables['nitrate'][0,...])+\
(f8.variables['nitrate'][0,...])+\
(f9.variables['nitrate'][0,...])+\
(f10.variables['nitrate'][0,...])+\
(f11.variables['nitrate'][0,...])+\
(f12.variables['nitrate'][0,...])+\
(f13.variables['nitrate'][0,...]) )/14
### use 2007 values for both 2007 and 2008 because there are no December 2006 files
ColdYears_N_Feb=((f1.variables['nitrate'][0,...])+\
(f3.variables['nitrate'][0,...])+\
(f4.variables['nitrate'][0,...])+\
(f5.variables['nitrate'][0,...]))/4
WarmYears_N_Feb=((f8.variables['nitrate'][0,...])+\
(f11.variables['nitrate'][0,...])+\
(f12.variables['nitrate'][0,...])+\
(f13.variables['nitrate'][0,...]))/4
AllYears_Si_Feb=((f0.variables['silicon'][0,...])+\
(f1.variables['silicon'][0,...])+\
(f2.variables['silicon'][0,...])+\
(f3.variables['silicon'][0,...])+\
(f4.variables['silicon'][0,...])+\
(f5.variables['silicon'][0,...])+\
(f6.variables['silicon'][0,...])+\
(f7.variables['silicon'][0,...])+\
(f8.variables['silicon'][0,...])+\
(f9.variables['silicon'][0,...])+\
(f10.variables['silicon'][0,...])+\
(f11.variables['silicon'][0,...])+\
(f12.variables['silicon'][0,...])+\
(f13.variables['silicon'][0,...]))/14
ColdYears_Si_Feb=((f1.variables['silicon'][0,...])+\
(f3.variables['silicon'][0,...])+\
(f4.variables['silicon'][0,...])+\
(f5.variables['silicon'][0,...]))/4
WarmYears_Si_Feb=((f8.variables['silicon'][0,...])+\
(f11.variables['silicon'][0,...])+\
(f12.variables['silicon'][0,...])+\
(f13.variables['silicon'][0,...]))/4
AllYears_N_Winter=(AllYears_N_Dec+AllYears_N_Jan+AllYears_N_Feb)/3
AllYears_Si_Winter=(AllYears_Si_Dec+AllYears_Si_Jan+AllYears_Si_Feb)/3
ColdYears_N_Winter=(ColdYears_N_Dec+ColdYears_N_Jan+ColdYears_N_Feb)/3
WarmYears_N_Winter=(WarmYears_N_Dec+WarmYears_N_Jan+WarmYears_N_Feb)/3
ColdYears_Si_Winter=(ColdYears_Si_Dec+ColdYears_Si_Jan+ColdYears_Si_Feb)/3
WarmYears_Si_Winter=(WarmYears_Si_Dec+WarmYears_Si_Jan+WarmYears_Si_Feb)/3
method using contour_thalweg from visualisations.py in tools repo
#open bathy file and meshmask
fbathy=nc.Dataset('/data/eolson/results/MEOPAR/NEMO-forcing-new/grid/bathymetry_201702.nc')
fmesh=nc.Dataset('/data/eolson/results/MEOPAR/NEMO-forcing-new/grid/mesh_mask201702.nc')
#il=0
fig,ax=plt.subplots(1,1,figsize=(11,3))
#fig.title('Mesozooplankton Biomass')
levels = np.arange(-2, 2, .01)
cb=visualisations.contour_thalweg(ax,((ColdYears_N_Winter)-(AllYears_N_Winter)),fbathy,fmesh,clevels=levels,cmap=cmocean.cm.balance)
cb.set_label('$\mu$M', rotation=90,labelpad=6)
fig.suptitle('Winter Nitrate Anomalies - Cold Years', fontsize=16)
#ax.text(15, 100, '(d)', fontsize=15, color='k')
#plt.savefig('MesozoopThalweg_Fig6d.png', bbox_inches='tight',dpi=1000,transparent=False)
Text(0.5, 0.98, 'Winter Nitrate Anomalies - Cold Years')
#il=0
fig,ax=plt.subplots(1,1,figsize=(11,3))
#fig.title('Mesozooplankton Biomass')
levels = np.arange(-2.5, 2.5, .01)
cb=visualisations.contour_thalweg(ax,((WarmYears_N_Winter)-(AllYears_N_Winter)),fbathy,fmesh,clevels=levels,cmap=cmocean.cm.balance)
cb.set_label('$\mu$M', rotation=90,labelpad=6)
fig.suptitle('Winter Nitrate Anomalies - Warm Years', fontsize=16)
#ax.text(15, 100, '(d)', fontsize=15, color='k')
#plt.savefig('MesozoopThalweg_Fig6d.png', bbox_inches='tight',dpi=1000,transparent=False)
Text(0.5, 0.98, 'Winter Nitrate Anomalies - Warm Years')
#il=0
fig,ax=plt.subplots(1,1,figsize=(11,3))
#fig.title('Mesozooplankton Biomass')
levels = np.arange(-3.5, 3.5, .05)
cb=visualisations.contour_thalweg(ax,((ColdYears_Si_Winter)-(AllYears_Si_Winter)),fbathy,fmesh,clevels=levels,cmap=cmocean.cm.balance)
cb.set_label('$\mu$M', rotation=90,labelpad=6)
fig.suptitle('Winter Silicon Anomalies - Cold Years', fontsize=16)
#ax.text(15, 100, '(d)', fontsize=15, color='k')
#plt.savefig('MesozoopThalweg_Fig6d.png', bbox_inches='tight',dpi=1000,transparent=False)
Text(0.5, 0.98, 'Winter Silicon Anomalies - Cold Years')
#il=0
fig,ax=plt.subplots(1,1,figsize=(11,3))
#fig.title('Mesozooplankton Biomass')
levels = np.arange(-3.5, 3.5, .05)
cb=visualisations.contour_thalweg(ax,((WarmYears_Si_Winter)-(AllYears_Si_Winter)),fbathy,fmesh,clevels=levels,cmap=cmocean.cm.balance)
cb.set_label('$\mu$M', rotation=90,labelpad=6)
fig.suptitle('Winter Silicon Anomalies - Warm Years', fontsize=16)
#ax.text(15, 100, '(d)', fontsize=15, color='k')
#plt.savefig('MesozoopThalweg_Fig6d.png', bbox_inches='tight',dpi=1000,transparent=False)
Text(0.5, 0.98, 'Winter Silicon Anomalies - Warm Years')
fig,ax=plt.subplots(4,1,figsize=(11,15))
#fig.title('Mesozooplankton Biomass')
levels = np.arange(-2.5, 2.5, .01)
fig.tight_layout(pad=2.5)
cb=visualisations.contour_thalweg(ax[0],((ColdYears_N_Winter)-(AllYears_N_Winter)),fbathy,fmesh,clevels=levels,cmap=cmocean.cm.balance)
cb.set_label('Nitrate - Cold Years ($\mu$M)', rotation=90,labelpad=6)
#fig.suptitle('Winter Nitrate Anomalies - Cold Years', fontsize=16)
ax[0].text(15, -10, '(a)', fontsize=15, color='k')
#fig,ax=plt.subplots(1,1,figsize=(11,3))
levels = np.arange(-2.5, 2.5, .01)
cb=visualisations.contour_thalweg(ax[1],((WarmYears_N_Winter)-(AllYears_N_Winter)),fbathy,fmesh,clevels=levels,cmap=cmocean.cm.balance)
cb.set_label('Nitrate - Warm Years ($\mu$M)', rotation=90,labelpad=6)
#fig.suptitle('Winter Nitrate Anomalies - Warm Years', fontsize=16)
ax[1].text(15, -10, '(b)', fontsize=15, color='k')
#fig,ax=plt.subplots(1,1,figsize=(11,3))
levels = np.arange(-3.5, 3.5, .05)
cb=visualisations.contour_thalweg(ax[2],((ColdYears_Si_Winter)-(AllYears_Si_Winter)),fbathy,fmesh,clevels=levels,cmap=cmocean.cm.balance)
cb.set_label('Silicon - Cold Years ($\mu$M)', rotation=90,labelpad=6)
#fig.suptitle('Winter Silicon Anomalies - Cold Years', fontsize=16)
ax[2].text(15, -10, '(c)', fontsize=15, color='k')
#fig,ax=plt.subplots(1,1,figsize=(11,3))
levels = np.arange(-3.5, 3.5, .05)
cb=visualisations.contour_thalweg(ax[3],((WarmYears_Si_Winter)-(AllYears_Si_Winter)),fbathy,fmesh,clevels=levels,cmap=cmocean.cm.balance)
cb.set_label('Silicon - Warm Years ($\mu$M)', rotation=90,labelpad=6)
#ax[3].suptitle('Winter Silicon Anomalies - Warm Years', fontsize=16)
ax[3].text(15, -10, '(d)', fontsize=15, color='k')
#plt.savefig('SuppFigureS5_NutrientThalwegPlots.png', bbox_inches='tight',dpi=1000,transparent=False)
f0.close()
f1.close()
f2.close()
f3.close()
f4.close()
f5.close()
f6.close()
f7.close()
f8.close()
f9.close()
f10.close()
f11.close()
f12.close()
f13.close()