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
%matplotlib inline
from IPython.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
plt.rcParams.update({'font.size': 12, 'axes.titlesize': 'medium'})
df = pd.read_csv('/ocean/ksuchy/MOAD/observe/NPGO.csv', index_col=0,header=0)
df.index.name = "YEAR"
df = df.apply(pd.to_numeric) # convert all columns of DataFrame
#df = df[:-1]
df.loc[2007:2023]
JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
YEAR | ||||||||||||
2007 | -0.243309 | -0.729927 | -0.182149 | 0.384615 | 0.144928 | 1.230000 | 1.230000 | 1.430000 | 0.110011 | 1.270000 | 0.137363 | 0.218341 |
2008 | 0.179856 | 1.140000 | 1.370000 | 1.400000 | 1.740000 | 1.280000 | 1.430000 | 2.140000 | 2.230000 | 1.770000 | 1.970000 | 0.227273 |
2009 | 0.138313 | 0.110011 | 0.302115 | 0.240385 | 0.180180 | 0.270270 | 0.178571 | 0.139276 | 0.102669 | 1.150000 | 1.050000 | 1.060000 |
2010 | 2.060000 | 1.840000 | 1.650000 | 1.180000 | 1.870000 | 1.240000 | 1.150000 | 1.080000 | 1.000000 | 1.610000 | 0.101215 | 0.128370 |
2011 | 0.310559 | 0.181818 | 1.200000 | 0.167785 | 1.390000 | 1.110000 | 1.040000 | 1.040000 | 1.330000 | 1.200000 | 0.267380 | 0.757576 |
2012 | 0.119904 | 0.115875 | 1.120000 | 1.930000 | 1.630000 | 1.940000 | 1.860000 | 1.730000 | 1.360000 | 1.870000 | 1.400000 | 1.200000 |
2013 | 1.230000 | 1.200000 | 0.155521 | 0.210084 | 0.117371 | 0.137552 | 0.221729 | 0.195312 | 0.256410 | -0.680272 | -0.124224 | -1.450000 |
2014 | -0.256410 | -0.374532 | -0.220264 | -0.161812 | 0.440529 | -0.239808 | -0.746269 | -0.183486 | -0.128700 | 0.235849 | 0.320513 | -0.280899 |
2015 | -0.181488 | -1.270000 | -1.400000 | -1.380000 | -0.138889 | -1.250000 | -1.470000 | -1.930000 | -2.100000 | -1.310000 | -2.250000 | -1.110000 |
2016 | 0.106724 | 0.436681 | 0.211864 | -0.250000 | -0.146628 | -0.104384 | -0.205339 | -0.183824 | -0.129199 | 0.284091 | 1.160000 | -1.540000 |
2017 | 1.622642 | -0.149701 | -0.112108 | -0.884956 | -0.219298 | -0.341297 | -0.225225 | -0.188324 | -0.190114 | -1.410000 | -2.060000 | -2.690000 |
2018 | -1.500000 | -2.230000 | -2.030000 | -2.060000 | -1.920000 | -1.940000 | -1.570000 | -2.120000 | -2.320000 | -2.340000 | -1.770000 | -1.020000 |
2019 | -0.116293 | -2.560000 | -1.990000 | -2.160000 | -1.860000 | -2.350000 | -2.160000 | -2.060000 | -2.520000 | -2.580000 | -3.370000 | -2.540000 |
2020 | -1.520000 | -2.320000 | -1.830000 | -1.880000 | -1.360000 | -1.890000 | -2.040000 | -1.360000 | 1.250000 | -1.830000 | -2.590000 | -1.330000 |
2021 | -0.580000 | -1.200000 | -0.860000 | -1.890000 | -0.860000 | -0.550000 | -0.690000 | -0.960000 | -0.670000 | -0.060000 | -0.300000 | -1.990000 |
2022 | -1.350000 | -1.850000 | -1.220000 | -0.670000 | -0.290000 | -0.520000 | -0.320000 | -0.390000 | -0.670000 | 0.000000 | 0.000000 | 0.000000 |
NPGO = df.loc[2007:2020].to_numpy().flatten()
df2 = pd.read_csv('/ocean/ksuchy/MOAD/observe/CentralSoG_0-10mNitrateAnomalies.csv', index_col=0)
#df2 = pd.read_csv('/ocean/ksuchy/MOAD/observe/CentralSoG_NitrateAnomalies.csv', index_col=0)
df2.index.name = "YEAR"
df2 = df2.apply(pd.to_numeric) # convert all columns of DataFrame
df2
Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
YEAR | ||||||||||||
2007 | 0.568147 | 1.051579 | 2.377032 | 2.414698 | 0.237077 | 0.132123 | 1.528375 | -0.668343 | -0.697072 | 1.511975 | 0.743682 | 1.161057 |
2008 | 1.276618 | 0.479209 | 1.319208 | -1.097002 | 1.878009 | 0.518437 | 0.618601 | 2.267914 | 2.672396 | -1.678422 | 0.497572 | 0.403591 |
2009 | 0.779868 | 0.430057 | 2.087796 | 4.853228 | 1.664037 | 0.246908 | 0.905363 | 0.360774 | -1.045085 | 0.595109 | 2.490391 | 1.573691 |
2010 | 1.034209 | 1.255441 | 1.802995 | 3.074134 | 1.325453 | 0.726371 | 0.762110 | 1.089319 | 0.088487 | 1.137430 | 0.201821 | 0.694923 |
2011 | 0.560476 | 0.772615 | 2.546247 | 9.392093 | 0.847004 | -0.471765 | -0.582232 | -0.948415 | 0.565439 | -0.134362 | -0.006921 | 0.394563 |
2012 | 0.789796 | 1.147739 | 3.256987 | 4.216038 | 0.147361 | 0.936165 | -0.418391 | -0.520689 | -2.380251 | 0.515971 | 0.416087 | 1.410105 |
2013 | 0.216742 | 0.035722 | 0.997229 | -2.721693 | 0.434487 | 0.117015 | 0.132615 | 0.691013 | 1.132842 | 0.705337 | -0.054270 | -0.598873 |
2014 | -0.168980 | 0.088505 | -0.472435 | -2.550886 | -1.914227 | -0.344112 | -0.414754 | -0.995434 | -2.113837 | -0.514631 | -1.249320 | -1.407372 |
2015 | -2.075749 | -1.507267 | -6.402064 | -5.363631 | -0.790012 | 0.466801 | 1.025258 | 2.547623 | 4.371997 | 0.614517 | -0.078646 | 0.494163 |
2016 | -0.754900 | -1.356740 | 0.465542 | -4.617599 | 0.367258 | -0.221639 | -0.017881 | 1.801608 | 1.597828 | 1.803464 | -0.713561 | -1.271169 |
2017 | -1.682356 | -1.640867 | 0.514737 | 1.776615 | -1.944853 | -0.714686 | -1.145170 | -1.623345 | -2.310101 | -0.724774 | 0.381249 | -0.969339 |
2018 | -0.754947 | -0.677864 | -4.683838 | -2.127948 | -1.830622 | -0.717698 | -0.960148 | -1.950949 | -0.838416 | -2.117258 | -0.501826 | 0.210814 |
2019 | 0.165554 | -0.378386 | -3.366277 | -2.951009 | 0.622692 | -0.518994 | -0.645146 | -1.551466 | -0.555656 | -0.434903 | -1.859252 | -1.216886 |
2020 | 0.045522 | 0.300256 | -0.443162 | -4.297039 | -1.043665 | -0.154926 | -0.788602 | -0.499612 | -0.488570 | -1.279451 | -0.267004 | -0.879268 |
Nitrate_Anom=df2.to_numpy().flatten()
Nitrate_Anom
array([ 5.68146998e-01, 1.05157894e+00, 2.37703222e+00, 2.41469847e+00, 2.37076790e-01, 1.32123051e-01, 1.52837541e+00, -6.68342631e-01, -6.97071613e-01, 1.51197457e+00, 7.43681864e-01, 1.16105666e+00, 1.27661808e+00, 4.79209169e-01, 1.31920849e+00, -1.09700244e+00, 1.87800895e+00, 5.18436663e-01, 6.18600847e-01, 2.26791377e+00, 2.67239635e+00, -1.67842230e+00, 4.97571606e-01, 4.03590932e-01, 7.79868031e-01, 4.30057381e-01, 2.08779567e+00, 4.85322797e+00, 1.66403734e+00, 2.46908417e-01, 9.05363427e-01, 3.60774480e-01, -1.04508507e+00, 5.95108725e-01, 2.49039109e+00, 1.57369104e+00, 1.03420928e+00, 1.25544086e+00, 1.80299535e+00, 3.07413424e+00, 1.32545266e+00, 7.26370614e-01, 7.62110383e-01, 1.08931918e+00, 8.84867780e-02, 1.13742996e+00, 2.01821384e-01, 6.94922968e-01, 5.60475691e-01, 7.72615336e-01, 2.54624739e+00, 9.39209283e+00, 8.47004141e-01, -4.71765293e-01, -5.82231833e-01, -9.48414964e-01, 5.65439204e-01, -1.34361576e-01, -6.92094700e-03, 3.94562750e-01, 7.89796373e-01, 1.14773927e+00, 3.25698656e+00, 4.21603837e+00, 1.47361248e-01, 9.36165497e-01, -4.18390629e-01, -5.20688752e-01, -2.38025140e+00, 5.15971374e-01, 4.16086935e-01, 1.41010512e+00, 2.16742057e-01, 3.57217600e-02, 9.97228796e-01, -2.72169262e+00, 4.34486776e-01, 1.17014636e-01, 1.32615273e-01, 6.91012707e-01, 1.13284154e+00, 7.05336681e-01, -5.42703520e-02, -5.98873171e-01, -1.68980149e-01, 8.85050050e-02, -4.72434545e-01, -2.55088564e+00, -1.91422712e+00, -3.44111588e-01, -4.14753567e-01, -9.95433646e-01, -2.11383725e+00, -5.14630849e-01, -1.24932009e+00, -1.40737185e+00, -2.07574867e+00, -1.50726665e+00, -6.40206363e+00, -5.36363145e+00, -7.90011519e-01, 4.66801299e-01, 1.02525846e+00, 2.54762336e+00, 4.37199676e+00, 6.14516751e-01, -7.86461490e-02, 4.94163064e-01, -7.54899971e-01, -1.35674001e+00, 4.65542370e-01, -4.61759877e+00, 3.67257973e-01, -2.21639077e-01, -1.78812820e-02, 1.80160841e+00, 1.59782790e+00, 1.80346363e+00, -7.13561317e-01, -1.27116939e+00, -1.68235614e+00, -1.64086653e+00, 5.14737431e-01, 1.77661531e+00, -1.94485306e+00, -7.14686053e-01, -1.14516991e+00, -1.62334515e+00, -2.31010071e+00, -7.24774061e-01, 3.81248943e-01, -9.69339283e-01, -7.54947191e-01, -6.77863755e-01, -4.68383759e+00, -2.12794834e+00, -1.83062162e+00, -7.17697714e-01, -9.60148189e-01, -1.95094855e+00, -8.38416384e-01, -2.11725814e+00, -5.01826326e-01, 2.10814486e-01, 1.65554013e-01, -3.78386428e-01, -3.36627651e+00, -2.95100897e+00, 6.22692351e-01, -5.18994005e-01, -6.45145911e-01, -1.55146575e+00, -5.55655881e-01, -4.34903450e-01, -1.85925242e+00, -1.21688569e+00, 4.55215950e-02, 3.00255645e-01, -4.43161994e-01, -4.29703898e+00, -1.04366491e+00, -1.54926445e-01, -7.88602482e-01, -4.99612472e-01, -4.88570239e-01, -1.27945130e+00, -2.67004223e-01, -8.79267635e-01])
Nitrate_Anom.max()
9.392092825
date = []
for year in range(2007, 2021):
for month in range(1, 13):
index = df.index == str(year)
date.append(datetime(year, month, 1))
date = np.array(date)
#NPGO = df.loc[2007:2020].to_numpy().flatten()
#Nitrate_Anom = df2.to_numpy().flatten()
fig, ax1 = plt.subplots(figsize=(15, 5))
ax2 = ax1.twinx()
ax1.bar(date[NPGO>0], NPGO[NPGO>0], width=20, color='royalblue',label='NPGO+ (cold)')
ax1.bar(date[NPGO<0], NPGO[NPGO<0], width=20, color='r',label='NPGO- (warm)')
ax1.set_ylim(-5,5)
ax2.set_ylim(-10,10)
ax2.plot(date,Nitrate_Anom,color='k',linewidth=2.5,label='0-10 m Nitrate Anomalies')
ax1.set_title('',fontsize=18)
ax1.set_ylabel('NPGO Index',fontsize=14)
ax2.set_ylabel('Nitrate \n(umol N m$^{-3}$)',fontsize=14)
ax1.legend(frameon=False,loc=2,fontsize=12)
ax2.legend(frameon=False,loc=1,fontsize=12)
ax1.xaxis.set_tick_params(labelsize=14)
ax1.yaxis.set_tick_params(labelsize=14)