import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def reg_r2(driver,bloomdate):
A = np.vstack([driver, np.ones(len(driver))]).T
m, c = np.linalg.lstsq(A, bloomdate,rcond=None)[0]
m=round(m,3)
c=round(c,2)
y = m*driver + c
model, resid = np.linalg.lstsq(A, bloomdate,rcond=None)[:2]
r2 = 1 - resid / (len(bloomdate) * np.var(bloomdate))
return y, r2, m, c
x=2*np.random.rand(50)
y=3*np.random.rand(50)
# z=Ax+By+Cr+noise
# RES = z - (B*y+Cb) = Ax+C+noise
# res2= z- (B*y) - (A*x) = Cr+noise
z=1*x+3*y+.1*np.random.rand(50)
plt.plot(x,z,'r.')
plt.xlabel('Fraser Flow')
plt.ylabel('Bloom Date')
Text(0, 0.5, 'Bloom Date')
yy, r2, m, c = reg_r2(y,z)
plt.plot(y,z,'b.')
y2=np.arange(0,4)
plt.plot(y,m*y+c,'c-')
plt.xlabel('Wind')
plt.ylabel('Bloom Date')
Text(0, 0.5, 'Bloom Date')
plt.plot(x,z-(m*y+c),'k.')
plt.xlabel('Fraser Flow')
plt.ylabel('residual (z- m*y-cy)')
Text(0, 0.5, 'residual (z- m*y-cy)')
yy, r2, mx, cx = reg_r2(x,z-(m*y+c))
mx,cx
(0.96, -0.97)
import netCDF4 as nc
f=nc.Dataset('/results2/SalishSea/nowcast-green.201905/01jan15/SalishSea_1d_20150101_20150101_grid_T.nc')
f.variables.keys()
dict_keys(['time_counter', 'nav_lat', 'nav_lon', 'deptht', 'sossheig', 'votemper', 'vosaline'])
import gsw
sigma=gsw.sigma0(f.variables['vosaline'][:,:,:,:],f.variables['votemper'][:,:,:,:])
plt.plot(sigma[0,:,450,258])
[<matplotlib.lines.Line2D at 0x7ff9fa0669a0>]
plt.plot(f.variables['vosaline'][0,:,450,258])
[<matplotlib.lines.Line2D at 0x7ff9f9210580>]
plt.plot(f.variables['votemper'][0,:,450,258])
[<matplotlib.lines.Line2D at 0x7ff9f91ef1c0>]
from salishsea_tools import places
places.PLACES['S3']
{'lon lat': (-123.558, 49.125), 'NEMO grid ji': (450, 258), 'GEM2.5 grid ji': (138, 144)}