import numpy as np
import matplotlib.pyplot as plt
import os
import pandas as pd
import netCDF4 as nc
import datetime as dt
from salishsea_tools import evaltools as et, viz_tools
import gsw
import matplotlib.gridspec as gridspec
import matplotlib as mpl
import matplotlib.dates as mdates
import cmocean as cmo
import scipy.interpolate as sinterp
import pickle
import cmocean
import json
import f90nml
from collections import OrderedDict
import warnings
warnings.filterwarnings('ignore')
from IPython.display import Markdown, display
%matplotlib inline
# prep and load dictionary to save stats in
if os.path.isfile('vET-HC1905-DFO-NutChlPhys-stats.json'):
with open('vET-HC1905-DFO-NutChlPhys-stats.json', 'r') as fstat:
statsDict = json.load(fstat);
else:
print('Error: No stats file')
statsDict['2007']['NO3']['all']
{'N': 1060, 'Bias': -0.6905845237509283, 'RMSE': 3.515278044470087, 'WSS': 0.89470388601169}
statsDict.keys()
dict_keys(['2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018'])
varlist=('NO3', 'dSi', 'Chl', 'Chl log10', 'Temperature', 'Salinity', 'Density')
sublist=('all',)#'z < 15 m')
yearlist=[str(ii) for ii in range(2007,2019)]
statsDict['2007'].keys()
width=6
prec=3
trVar={'NO3': (r'NO\ :sub:`3` (\ :math:`{\mu}`\ M )',(' ')),
'dSi': (r'Dissolved Silica (\ :math:`{\mu}`\ M )',(' ')),
'Chl': (r' Chl a (µg L\ :sup:`-1`\ )',(' ')),
'Chl log10': (r'log10[Chl a (µg L\ :sup:`-1`\ )]',(' ')),
'Temperature': (r'Conservative',('Temperature (°C)')),
'Salinity': (r'Reference',('Salinity (g kg\ :sup:`-1`\ )')),
'Density': (r' Density (kg m\ :sup:`-3`\ )',(' '))}
def makeTable2():
lines=list()
lines.append(r'+----------------------------------------+-------+--------+--------+--------+------+'+'\n')
lines.append(r'| Variable | Year | Bias | RMSE | WSS | N |'+'\n')
for var in varlist:
var1=True
var2=False
for sub in sublist:
for year in yearlist:
if var1:
vartxt=r'| '+'{:>38}'.format(trVar[var][0])
varsep=r'+----------------------------------------'
elif var2:
vartxt=r'| '+'{:>38}'.format(trVar[var][1])
varsep='+ '
else:
vartxt=r'| '+'{:38}'.format(' ')
varsep='+ '
yeartxt=r' | '+'{:5}'.format(str(year))
st=statsDict[year][var][sub]
statlist=('{:{w}.{p}f}'.format(st['Bias'], w=width, p=prec),
'{:{w}.{p}f}'.format(st['RMSE'], w=width, p=prec),
'{:{w}.{p}f}'.format(st['WSS'], w=width, p=prec),
'{:4d}'.format(st['N']))
sepline=(varsep+'+-------+--------+--------+--------+------+'+'\n')
lines.append(sepline)
lines.append(vartxt+yeartxt+' | '+' | '.join(statlist)+' |\n')
var2=True if var1 else False
var1=False
lines.append(r'+----------------------------------------+-------+--------+--------+--------+------+'+'\n')
for el in lines:
print(el)
return lines
if len(sublist)>1:
lines=makeTable1()
elif len(sublist)==1:
lines=makeTable2()
else:
print('Error, sublist:',sublist)
+----------------------------------------+-------+--------+--------+--------+------+ | Variable | Year | Bias | RMSE | WSS | N | +----------------------------------------+-------+--------+--------+--------+------+ | NO\ :sub:`3` (\ :math:`{\mu}`\ M ) | 2007 | -0.691 | 3.515 | 0.895 | 1060 | + +-------+--------+--------+--------+------+ | | 2008 | -0.572 | 4.201 | 0.904 | 1096 | + +-------+--------+--------+--------+------+ | | 2009 | 0.371 | 3.711 | 0.915 | 1097 | + +-------+--------+--------+--------+------+ | | 2010 | -0.391 | 3.599 | 0.940 | 1461 | + +-------+--------+--------+--------+------+ | | 2011 | 0.209 | 4.586 | 0.899 | 1345 | + +-------+--------+--------+--------+------+ | | 2012 | -0.298 | 3.552 | 0.944 | 1410 | + +-------+--------+--------+--------+------+ | | 2013 | -2.013 | 4.248 | 0.923 | 1031 | + +-------+--------+--------+--------+------+ | | 2014 | -2.833 | 4.456 | 0.921 | 1493 | + +-------+--------+--------+--------+------+ | | 2015 | -1.676 | 3.488 | 0.934 | 533 | + +-------+--------+--------+--------+------+ | | 2016 | -1.200 | 3.333 | 0.962 | 1675 | + +-------+--------+--------+--------+------+ | | 2017 | -0.071 | 4.144 | 0.923 | 1590 | + +-------+--------+--------+--------+------+ | | 2018 | -0.931 | 3.110 | 0.925 | 634 | +----------------------------------------+-------+--------+--------+--------+------+ | Dissolved Silica (\ :math:`{\mu}`\ M ) | 2007 | -6.007 | 10.098 | 0.766 | 1060 | + +-------+--------+--------+--------+------+ | | 2008 | -6.411 | 11.323 | 0.762 | 1096 | + +-------+--------+--------+--------+------+ | | 2009 | -3.807 | 8.101 | 0.866 | 1099 | + +-------+--------+--------+--------+------+ | | 2010 | -3.587 | 10.418 | 0.826 | 1442 | + +-------+--------+--------+--------+------+ | | 2011 | -3.162 | 12.121 | 0.653 | 1345 | + +-------+--------+--------+--------+------+ | | 2012 | -2.909 | 9.399 | 0.792 | 1410 | + +-------+--------+--------+--------+------+ | | 2013 | -5.516 | 9.546 | 0.829 | 1031 | + +-------+--------+--------+--------+------+ | | 2014 | -6.799 | 10.635 | 0.811 | 1493 | + +-------+--------+--------+--------+------+ | | 2015 | -5.418 | 8.228 | 0.860 | 533 | + +-------+--------+--------+--------+------+ | | 2016 | -6.279 | 9.404 | 0.902 | 1675 | + +-------+--------+--------+--------+------+ | | 2017 | -3.035 | 10.567 | 0.782 | 1589 | + +-------+--------+--------+--------+------+ | | 2018 | -6.465 | 9.174 | 0.841 | 634 | +----------------------------------------+-------+--------+--------+--------+------+ | Chl a (µg L\ :sup:`-1`\ ) | 2007 | -0.834 | 2.928 | 0.520 | 249 | + +-------+--------+--------+--------+------+ | | 2008 | 0.412 | 4.729 | 0.488 | 222 | + +-------+--------+--------+--------+------+ | | 2009 | -2.129 | 7.556 | 0.352 | 240 | + +-------+--------+--------+--------+------+ | | 2010 | -0.062 | 3.234 | 0.609 | 268 | + +-------+--------+--------+--------+------+ | | 2011 | -2.984 | 8.912 | 0.334 | 224 | + +-------+--------+--------+--------+------+ | | 2012 | -0.856 | 5.715 | 0.222 | 245 | + +-------+--------+--------+--------+------+ | | 2013 | -2.051 | 5.066 | 0.422 | 179 | + +-------+--------+--------+--------+------+ | | 2014 | -1.544 | 4.895 | 0.633 | 259 | + +-------+--------+--------+--------+------+ | | 2015 | -0.186 | 3.461 | 0.581 | 125 | + +-------+--------+--------+--------+------+ | | 2016 | -0.364 | 2.473 | 0.661 | 387 | + +-------+--------+--------+--------+------+ | | 2017 | -1.266 | 4.872 | 0.453 | 428 | + +-------+--------+--------+--------+------+ | | 2018 | -1.264 | 3.952 | 0.493 | 191 | +----------------------------------------+-------+--------+--------+--------+------+ | log10[Chl a (µg L\ :sup:`-1`\ )] | 2007 | -0.161 | 0.423 | 0.742 | 249 | + +-------+--------+--------+--------+------+ | | 2008 | 0.014 | 0.494 | 0.702 | 222 | + +-------+--------+--------+--------+------+ | | 2009 | -0.014 | 0.497 | 0.737 | 240 | + +-------+--------+--------+--------+------+ | | 2010 | -0.033 | 0.521 | 0.683 | 268 | + +-------+--------+--------+--------+------+ | | 2011 | -0.149 | 0.694 | 0.630 | 224 | + +-------+--------+--------+--------+------+ | | 2012 | -0.010 | 0.476 | 0.602 | 245 | + +-------+--------+--------+--------+------+ | | 2013 | -0.233 | 0.532 | 0.595 | 179 | + +-------+--------+--------+--------+------+ | | 2014 | -0.177 | 0.465 | 0.772 | 259 | + +-------+--------+--------+--------+------+ | | 2015 | -0.015 | 0.429 | 0.677 | 125 | + +-------+--------+--------+--------+------+ | | 2016 | -0.090 | 0.393 | 0.783 | 387 | + +-------+--------+--------+--------+------+ | | 2017 | -0.108 | 0.544 | 0.618 | 428 | + +-------+--------+--------+--------+------+ | | 2018 | -0.100 | 0.420 | 0.739 | 191 | +----------------------------------------+-------+--------+--------+--------+------+ | Conservative | 2007 | -0.135 | 0.460 | 0.965 | 1103 | + +-------+--------+--------+--------+------+ | Temperature (°C) | 2008 | 0.078 | 0.656 | 0.953 | 1107 | + +-------+--------+--------+--------+------+ | | 2009 | -0.054 | 0.796 | 0.953 | 1103 | + +-------+--------+--------+--------+------+ | | 2010 | -0.095 | 0.517 | 0.970 | 1941 | + +-------+--------+--------+--------+------+ | | 2011 | 0.050 | 0.773 | 0.972 | 1465 | + +-------+--------+--------+--------+------+ | | 2012 | 0.014 | 0.558 | 0.987 | 1569 | + +-------+--------+--------+--------+------+ | | 2013 | 0.003 | 0.839 | 0.956 | 1035 | + +-------+--------+--------+--------+------+ | | 2014 | 0.047 | 0.632 | 0.976 | 1505 | + +-------+--------+--------+--------+------+ | | 2015 | -0.005 | 0.551 | 0.978 | 566 | + +-------+--------+--------+--------+------+ | | 2016 | -0.102 | 0.652 | 0.974 | 1801 | + +-------+--------+--------+--------+------+ | | 2017 | -0.005 | 0.731 | 0.975 | 1835 | + +-------+--------+--------+--------+------+ | | 2018 | -0.100 | 0.480 | 0.958 | 685 | +----------------------------------------+-------+--------+--------+--------+------+ | Reference | 2007 | 0.044 | 0.766 | 0.960 | 1103 | + +-------+--------+--------+--------+------+ | Salinity (g kg\ :sup:`-1`\ ) | 2008 | -0.119 | 0.839 | 0.951 | 1107 | + +-------+--------+--------+--------+------+ | | 2009 | 0.019 | 0.935 | 0.944 | 1103 | + +-------+--------+--------+--------+------+ | | 2010 | 0.037 | 1.059 | 0.942 | 1943 | + +-------+--------+--------+--------+------+ | | 2011 | 0.143 | 1.229 | 0.968 | 1465 | + +-------+--------+--------+--------+------+ | | 2012 | 0.039 | 0.884 | 0.980 | 1569 | + +-------+--------+--------+--------+------+ | | 2013 | -0.003 | 1.270 | 0.932 | 1036 | + +-------+--------+--------+--------+------+ | | 2014 | -0.168 | 0.821 | 0.954 | 1506 | + +-------+--------+--------+--------+------+ | | 2015 | 0.022 | 0.679 | 0.983 | 566 | + +-------+--------+--------+--------+------+ | | 2016 | 0.042 | 0.925 | 0.961 | 1801 | + +-------+--------+--------+--------+------+ | | 2017 | 0.047 | 0.952 | 0.967 | 1835 | + +-------+--------+--------+--------+------+ | | 2018 | -0.057 | 0.795 | 0.956 | 685 | +----------------------------------------+-------+--------+--------+--------+------+ | Density (kg m\ :sup:`-3`\ ) | 2007 | 0.054 | 0.616 | 0.971 | 1103 | + +-------+--------+--------+--------+------+ | | 2008 | -0.104 | 0.697 | 0.965 | 1107 | + +-------+--------+--------+--------+------+ | | 2009 | 0.025 | 0.798 | 0.958 | 1103 | + +-------+--------+--------+--------+------+ | | 2010 | 0.041 | 0.817 | 0.961 | 1941 | + +-------+--------+--------+--------+------+ | | 2011 | 0.103 | 0.982 | 0.975 | 1465 | + +-------+--------+--------+--------+------+ | | 2012 | 0.028 | 0.712 | 0.985 | 1569 | + +-------+--------+--------+--------+------+ | | 2013 | -0.000 | 1.041 | 0.950 | 1035 | + +-------+--------+--------+--------+------+ | | 2014 | -0.137 | 0.690 | 0.968 | 1505 | + +-------+--------+--------+--------+------+ | | 2015 | 0.020 | 0.576 | 0.987 | 566 | + +-------+--------+--------+--------+------+ | | 2016 | 0.050 | 0.765 | 0.973 | 1801 | + +-------+--------+--------+--------+------+ | | 2017 | 0.033 | 0.791 | 0.975 | 1835 | + +-------+--------+--------+--------+------+ | | 2018 | -0.031 | 0.632 | 0.967 | 685 | +----------------------------------------+-------+--------+--------+--------+------+
def makeTable1():
lines=list()
lines.append(r'+----------------------------------------+----------+-------+--------+--------+--------+------+'+'\n')
lines.append(r'| Variable | Subset | Year | Bias | RMSE | WSS | N |'+'\n')
#lines.append(r'+--------------------------------------------+------------+----------+---------+---------+---------+------+'+'\n')
#sepline=r'+--------------------------------------------+------------+----------+---------+---------+---------+------+'+'\n'
for var in varlist:
var1=True
var2=False
for sub in sublist:
sub1=True
for year in yearlist:
if var1:
vartxt=r'| '+'{:>38}'.format(trVar[var][0])
varsep=r'+----------------------------------------'
elif var2:
vartxt=r'| '+'{:>38}'.format(trVar[var][1])
varsep='+ '
else:
vartxt=r'| '+'{:38}'.format(' ')
varsep='+ '
subtxt=r' | '+'{:8}'.format(sub) if sub1 else r' | '+'{:8}'.format(' ')
subsep=r'+----------' if sub1 else '+ '
yeartxt=r' | '+'{:5}'.format(str(year))
st=statsDict[year][var][sub]
statlist=('{:{w}.{p}f}'.format(st['Bias'], w=width, p=prec),
'{:{w}.{p}f}'.format(st['RMSE'], w=width, p=prec),
'{:{w}.{p}f}'.format(st['WSS'], w=width, p=prec),
'{:4d}'.format(st['N']))
sepline=(varsep+subsep+'+-------+--------+--------+--------+------+'+'\n')
lines.append(sepline)
lines.append(vartxt+subtxt+yeartxt+' | '+' | '.join(statlist)+' |\n')
var2=True if var1 else False
var1=False
sub1=False
lines.append(r'+----------------------------------------+----------+-------+--------+--------+--------+------+'+'\n')
for el in lines:
print(el)
return lines
f = open('/data/eolson/results/MEOPAR/docs/NEMO36ModelEvaluation201905/HC1905statsTBL.txt', 'w')
f.writelines(lines)
f.close()