import netCDF4 as nc
import datetime as dt
import subprocess
import requests
import matplotlib.pyplot as plt
import cmocean
import numpy as np
import os
import glob
import dateutil as dutil
from salishsea_tools import viz_tools
%matplotlib inline
with nc.Dataset('/ocean/eolson/MEOPAR/NEMO-forcing/grid/mesh_mask201702_noLPE.nc') as fm:
tmask=np.copy(fm.variables['tmask'])
umask=np.copy(fm.variables['umask'])
vmask=np.copy(fm.variables['vmask'])
navlon=np.copy(fm.variables['nav_lon'])
navlat=np.copy(fm.variables['nav_lat'])
dept=np.copy(fm.variables['gdept_1d'])
e3t_0=np.copy(fm.variables['e3t_0'])
e3u_0=np.copy(fm.variables['e3u_0'])
e3v_0=np.copy(fm.variables['e3v_0'])
e1t=np.copy(fm.variables['e1t'])
e2t=np.copy(fm.variables['e2t'])
e1v=np.copy(fm.variables['e1v'])
e2u=np.copy(fm.variables['e2u'])
A=fm.variables['e1t'][0,:,:]*fm.variables['e2t'][0,:,:]*tmask[0,0,:,:]
t0=dt.datetime(2014,12,1) # 1st start date of run
#te=dt.datetime(2016,12,1)# last start date of runfnum=18
stm=np.shape(tmask)
SiN=2.0
#nlen=36*2
nlen=53
dlist=[t0+dt.timedelta(days=ii*10) for ii in range(0,nlen)]
#sdir0='/results/SalishSea/nowcast-green/'
sdir1='/results/SalishSea/hindcast/'
#sdir3='/data/eolson/MEOPAR/SS36runs/CedarRuns/spring2015_HCMZ/'
tmaskC=np.copy(tmask)
tmaskC[:,:,370:490,:12]=0
tmaskC[:,:,887:,30:70]=0
tlist=dlist
SiGlobalTot=dict()
SiTot=dict()
BSiTot=dict()
DiatTot=dict()
changeSiGlobalTot=dict()
for idir in (sdir1,):
fformat1='%d%b%y/'
if idir.startswith('/data/eolson/MEOPAR/SS36runs/CedarRuns/'):
fformatT='SalishSea_1h_*_ptrc_T_%Y%m%d-*.nc'
fformatP='SalishSea_1h_*_ptrc_T_%Y%m%d-*.nc'
#elif idir==sdir0:
# fformatT='SalishSea_1h_%Y%m%d_%Y%m%d_ptrc_T.nc'
# fformatP='SalishSea_1h_%Y%m%d_%Y%m%d_grid_T.nc'
elif idir==sdir1:
fformatT='SalishSea_1h_%Y%m%d_%Y%m%d_ptrc_T.nc'
fformatP='SalishSea_1h_%Y%m%d_%Y%m%d_carp_T.nc'
sumSi=np.zeros((len(tlist),stm[1]))
sumBSi=np.zeros((len(tlist),stm[1]))
sumDiat=np.zeros((len(tlist),stm[1]))
ind=-1
for idt0 in tlist:
ind=ind+1
cdir=idt0.strftime(fformat1).lower()
iffT=idt0.strftime(fformatT)
iffP=idt0.strftime(fformatP)
if idir.startswith('/data/eolson/MEOPAR/SS36runs/CedarRuns/'):
sffT=idir+iffT
sffP=idir+iffP
elif idir.startswith('/results/'):
sffT=idir+cdir+iffT
sffP=idir+cdir+iffP
f=nc.Dataset(glob.glob(sffT)[0])
print(sffT)
fP=nc.Dataset(glob.glob(sffP)[0])
#if idir==sdir0:
# e3t=np.expand_dims((1+fP.variables['sossheig'][0,:,:]/np.sum(e3t_0*tmask,1)),0)*e3t_0
if idir==sdir1:
e3t=fP.variables['e3t'][:2,:,:,:]
Vol=A*np.ones(np.shape(e3t))
sumSi[ind,:]=1e-3*np.sum(np.sum(tmaskC[0,:,:,:]*Vol[0,:,:,:]*f.variables['silicon'][0,:,:,:],2),1) #mmol/m3*m3*10^-3=mol
sumBSi[ind,:]=1e-3*np.sum(np.sum(tmaskC[0,:,:,:]*Vol[0,:,:,:]*f.variables['biogenic_silicon'][0,:,:,:],2),1) #mmol/m3*m3*10^-3=mol
sumDiat[ind,:]=SiN*1e-3*np.sum(np.sum(tmaskC[0,:,:,:]*Vol[0,:,:,:]*f.variables['diatoms'][0,:,:,:],2),1) #mmol/m3*m3*10^-3=mol
f.close()
fP.close()
SiGlobalTot[idir]=sumSi+sumBSi+sumDiat
/results/SalishSea/hindcast/01dec14/SalishSea_1h_20141201_20141201_ptrc_T.nc /results/SalishSea/hindcast/11dec14/SalishSea_1h_20141211_20141211_ptrc_T.nc /results/SalishSea/hindcast/21dec14/SalishSea_1h_20141221_20141221_ptrc_T.nc /results/SalishSea/hindcast/31dec14/SalishSea_1h_20141231_20141231_ptrc_T.nc /results/SalishSea/hindcast/10jan15/SalishSea_1h_20150110_20150110_ptrc_T.nc /results/SalishSea/hindcast/20jan15/SalishSea_1h_20150120_20150120_ptrc_T.nc /results/SalishSea/hindcast/30jan15/SalishSea_1h_20150130_20150130_ptrc_T.nc /results/SalishSea/hindcast/09feb15/SalishSea_1h_20150209_20150209_ptrc_T.nc /results/SalishSea/hindcast/19feb15/SalishSea_1h_20150219_20150219_ptrc_T.nc /results/SalishSea/hindcast/01mar15/SalishSea_1h_20150301_20150301_ptrc_T.nc /results/SalishSea/hindcast/11mar15/SalishSea_1h_20150311_20150311_ptrc_T.nc /results/SalishSea/hindcast/21mar15/SalishSea_1h_20150321_20150321_ptrc_T.nc /results/SalishSea/hindcast/31mar15/SalishSea_1h_20150331_20150331_ptrc_T.nc /results/SalishSea/hindcast/10apr15/SalishSea_1h_20150410_20150410_ptrc_T.nc /results/SalishSea/hindcast/20apr15/SalishSea_1h_20150420_20150420_ptrc_T.nc /results/SalishSea/hindcast/30apr15/SalishSea_1h_20150430_20150430_ptrc_T.nc /results/SalishSea/hindcast/10may15/SalishSea_1h_20150510_20150510_ptrc_T.nc /results/SalishSea/hindcast/20may15/SalishSea_1h_20150520_20150520_ptrc_T.nc /results/SalishSea/hindcast/30may15/SalishSea_1h_20150530_20150530_ptrc_T.nc /results/SalishSea/hindcast/09jun15/SalishSea_1h_20150609_20150609_ptrc_T.nc /results/SalishSea/hindcast/19jun15/SalishSea_1h_20150619_20150619_ptrc_T.nc /results/SalishSea/hindcast/29jun15/SalishSea_1h_20150629_20150629_ptrc_T.nc /results/SalishSea/hindcast/09jul15/SalishSea_1h_20150709_20150709_ptrc_T.nc /results/SalishSea/hindcast/19jul15/SalishSea_1h_20150719_20150719_ptrc_T.nc /results/SalishSea/hindcast/29jul15/SalishSea_1h_20150729_20150729_ptrc_T.nc /results/SalishSea/hindcast/08aug15/SalishSea_1h_20150808_20150808_ptrc_T.nc /results/SalishSea/hindcast/18aug15/SalishSea_1h_20150818_20150818_ptrc_T.nc /results/SalishSea/hindcast/28aug15/SalishSea_1h_20150828_20150828_ptrc_T.nc /results/SalishSea/hindcast/07sep15/SalishSea_1h_20150907_20150907_ptrc_T.nc /results/SalishSea/hindcast/17sep15/SalishSea_1h_20150917_20150917_ptrc_T.nc /results/SalishSea/hindcast/27sep15/SalishSea_1h_20150927_20150927_ptrc_T.nc /results/SalishSea/hindcast/07oct15/SalishSea_1h_20151007_20151007_ptrc_T.nc /results/SalishSea/hindcast/17oct15/SalishSea_1h_20151017_20151017_ptrc_T.nc /results/SalishSea/hindcast/27oct15/SalishSea_1h_20151027_20151027_ptrc_T.nc /results/SalishSea/hindcast/06nov15/SalishSea_1h_20151106_20151106_ptrc_T.nc /results/SalishSea/hindcast/16nov15/SalishSea_1h_20151116_20151116_ptrc_T.nc /results/SalishSea/hindcast/26nov15/SalishSea_1h_20151126_20151126_ptrc_T.nc /results/SalishSea/hindcast/06dec15/SalishSea_1h_20151206_20151206_ptrc_T.nc /results/SalishSea/hindcast/16dec15/SalishSea_1h_20151216_20151216_ptrc_T.nc /results/SalishSea/hindcast/26dec15/SalishSea_1h_20151226_20151226_ptrc_T.nc /results/SalishSea/hindcast/05jan16/SalishSea_1h_20160105_20160105_ptrc_T.nc /results/SalishSea/hindcast/15jan16/SalishSea_1h_20160115_20160115_ptrc_T.nc /results/SalishSea/hindcast/25jan16/SalishSea_1h_20160125_20160125_ptrc_T.nc /results/SalishSea/hindcast/04feb16/SalishSea_1h_20160204_20160204_ptrc_T.nc /results/SalishSea/hindcast/14feb16/SalishSea_1h_20160214_20160214_ptrc_T.nc /results/SalishSea/hindcast/24feb16/SalishSea_1h_20160224_20160224_ptrc_T.nc /results/SalishSea/hindcast/05mar16/SalishSea_1h_20160305_20160305_ptrc_T.nc /results/SalishSea/hindcast/15mar16/SalishSea_1h_20160315_20160315_ptrc_T.nc /results/SalishSea/hindcast/25mar16/SalishSea_1h_20160325_20160325_ptrc_T.nc /results/SalishSea/hindcast/04apr16/SalishSea_1h_20160404_20160404_ptrc_T.nc /results/SalishSea/hindcast/14apr16/SalishSea_1h_20160414_20160414_ptrc_T.nc /results/SalishSea/hindcast/24apr16/SalishSea_1h_20160424_20160424_ptrc_T.nc /results/SalishSea/hindcast/04may16/SalishSea_1h_20160504_20160504_ptrc_T.nc
siplot=SiGlobalTot[idir]-SiGlobalTot[idir][0,:]
cm1=cmocean.cm.balance
#plt.pcolormesh([t0+dt.timedelta(10*ii) for ii in range(0,30)],dept[0,:],np.transpose(siplot[:30,:]),cmap=cm1,vmin=-1e9,vmax=1e9)
plt.pcolormesh([t0+dt.timedelta(10*ii) for ii in range(0,nlen)],dept[0,:],np.transpose(siplot),cmap=cm1,vmin=-1e8,vmax=1e8)
plt.ylim(400,0)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x7f61ea81d9e8>
tlist=dlist
NGlobalTot=dict()
VolTot=dict()
NO3Tot=dict()
NH4Tot=dict()
PONTot=dict()
DONTot=dict()
DiatTot=dict()
MyriTot=dict()
NanoTot=dict()
MiZoTot=dict()
changeNGlobalTot=dict()
for idir in (sdir1,):
fformat1='%d%b%y/'
if idir.startswith('/data/eolson/MEOPAR/SS36runs/CedarRuns/'):
fformatT='SalishSea_1h_*_ptrc_T_%Y%m%d-*.nc'
fformatP='SalishSea_1h_*_ptrc_T_%Y%m%d-*.nc'
#elif idir==sdir0:
# fformatT='SalishSea_1h_%Y%m%d_%Y%m%d_ptrc_T.nc'
# fformatP='SalishSea_1h_%Y%m%d_%Y%m%d_grid_T.nc'
elif idir==sdir1:
fformatT='SalishSea_1h_%Y%m%d_%Y%m%d_ptrc_T.nc'
fformatP='SalishSea_1h_%Y%m%d_%Y%m%d_carp_T.nc'
sumNO3=np.zeros((len(tlist),stm[1]))
sumVol=np.zeros((len(tlist),stm[1]))
sumNH4=np.zeros((len(tlist),stm[1]))
sumPON=np.zeros((len(tlist),stm[1]))
sumDON=np.zeros((len(tlist),stm[1]))
sumDiat=np.zeros((len(tlist),stm[1]))
sumMyri=np.zeros((len(tlist),stm[1]))
sumNano=np.zeros((len(tlist),stm[1]))
sumMiZo=np.zeros((len(tlist),stm[1]))
ind=-1
for idt0 in tlist:
ind=ind+1
cdir=idt0.strftime(fformat1).lower()
iffT=idt0.strftime(fformatT)
iffP=idt0.strftime(fformatP)
if idir.startswith('/data/eolson/MEOPAR/SS36runs/CedarRuns/'):
sffT=idir+iffT
sffP=idir+iffP
elif idir.startswith('/results/'):
sffT=idir+cdir+iffT
sffP=idir+cdir+iffP
f=nc.Dataset(glob.glob(sffT)[0])
print(sffT)
fP=nc.Dataset(glob.glob(sffP)[0])
#if idir==sdir0:
# e3t=np.expand_dims((1+fP.variables['sossheig'][0,:,:]/np.sum(e3t_0*tmask,1)),0)*e3t_0
if idir==sdir1:
e3t=fP.variables['e3t'][:2,:,:,:]
Vol=A*np.ones(np.shape(e3t))
sumVol[ind,:]=1e-3*np.sum(np.sum(tmaskC[0,:,:,:]*Vol[0,:,:,:],2),1) #mmol/m3*m3*10^-3=mol
sumNO3[ind,:]=1e-3*np.sum(np.sum(tmaskC[0,:,:,:]*Vol[0,:,:,:]*f.variables['nitrate'][0,:,:,:],2),1) #mmol/m3*m3*10^-3=mol
sumNH4[ind,:]=1e-3*np.sum(np.sum(tmaskC[0,:,:,:]*Vol[0,:,:,:]*f.variables['ammonium'][0,:,:,:],2),1) #mmol/m3*m3*10^-3=mol
sumPON[ind,:]=1e-3*np.sum(np.sum(tmaskC[0,:,:,:]*Vol[0,:,:,:]*f.variables['particulate_organic_nitrogen'][0,:,:,:],2),1) #mmol/m3*m3*10^-3=mol
sumDON[ind,:]=1e-3*np.sum(np.sum(tmaskC[0,:,:,:]*Vol[0,:,:,:]*f.variables['dissolved_organic_nitrogen'][0,:,:,:],2),1) #mmol/m3*m3*10^-3=mol
sumDiat[ind,:]=1e-3*np.sum(np.sum(tmaskC[0,:,:,:]*Vol[0,:,:,:]*f.variables['diatoms'][0,:,:,:],2),1) #mmol/m3*m3*10^-3=mol
sumMyri[ind,:]=1e-3*np.sum(np.sum(tmaskC[0,:,:,:]*Vol[0,:,:,:]*f.variables['ciliates'][0,:,:,:],2),1) #mmol/m3*m3*10^-3=mol
sumMiZo[ind,:]=1e-3*np.sum(np.sum(tmaskC[0,:,:,:]*Vol[0,:,:,:]*f.variables['microzooplankton'][0,:,:,:],2),1) #mmol/m3*m3*10^-3=mol
f.close()
fP.close()
NGlobalTot[idir]=sumNO3+sumNH4+sumPON+sumDON+sumDiat+sumMyri+sumNano+sumMiZo
/results/SalishSea/hindcast/01dec14/SalishSea_1h_20141201_20141201_ptrc_T.nc /results/SalishSea/hindcast/11dec14/SalishSea_1h_20141211_20141211_ptrc_T.nc /results/SalishSea/hindcast/21dec14/SalishSea_1h_20141221_20141221_ptrc_T.nc /results/SalishSea/hindcast/31dec14/SalishSea_1h_20141231_20141231_ptrc_T.nc /results/SalishSea/hindcast/10jan15/SalishSea_1h_20150110_20150110_ptrc_T.nc /results/SalishSea/hindcast/20jan15/SalishSea_1h_20150120_20150120_ptrc_T.nc /results/SalishSea/hindcast/30jan15/SalishSea_1h_20150130_20150130_ptrc_T.nc /results/SalishSea/hindcast/09feb15/SalishSea_1h_20150209_20150209_ptrc_T.nc /results/SalishSea/hindcast/19feb15/SalishSea_1h_20150219_20150219_ptrc_T.nc /results/SalishSea/hindcast/01mar15/SalishSea_1h_20150301_20150301_ptrc_T.nc /results/SalishSea/hindcast/11mar15/SalishSea_1h_20150311_20150311_ptrc_T.nc /results/SalishSea/hindcast/21mar15/SalishSea_1h_20150321_20150321_ptrc_T.nc /results/SalishSea/hindcast/31mar15/SalishSea_1h_20150331_20150331_ptrc_T.nc /results/SalishSea/hindcast/10apr15/SalishSea_1h_20150410_20150410_ptrc_T.nc /results/SalishSea/hindcast/20apr15/SalishSea_1h_20150420_20150420_ptrc_T.nc /results/SalishSea/hindcast/30apr15/SalishSea_1h_20150430_20150430_ptrc_T.nc /results/SalishSea/hindcast/10may15/SalishSea_1h_20150510_20150510_ptrc_T.nc /results/SalishSea/hindcast/20may15/SalishSea_1h_20150520_20150520_ptrc_T.nc /results/SalishSea/hindcast/30may15/SalishSea_1h_20150530_20150530_ptrc_T.nc /results/SalishSea/hindcast/09jun15/SalishSea_1h_20150609_20150609_ptrc_T.nc /results/SalishSea/hindcast/19jun15/SalishSea_1h_20150619_20150619_ptrc_T.nc /results/SalishSea/hindcast/29jun15/SalishSea_1h_20150629_20150629_ptrc_T.nc /results/SalishSea/hindcast/09jul15/SalishSea_1h_20150709_20150709_ptrc_T.nc /results/SalishSea/hindcast/19jul15/SalishSea_1h_20150719_20150719_ptrc_T.nc /results/SalishSea/hindcast/29jul15/SalishSea_1h_20150729_20150729_ptrc_T.nc /results/SalishSea/hindcast/08aug15/SalishSea_1h_20150808_20150808_ptrc_T.nc /results/SalishSea/hindcast/18aug15/SalishSea_1h_20150818_20150818_ptrc_T.nc /results/SalishSea/hindcast/28aug15/SalishSea_1h_20150828_20150828_ptrc_T.nc /results/SalishSea/hindcast/07sep15/SalishSea_1h_20150907_20150907_ptrc_T.nc /results/SalishSea/hindcast/17sep15/SalishSea_1h_20150917_20150917_ptrc_T.nc /results/SalishSea/hindcast/27sep15/SalishSea_1h_20150927_20150927_ptrc_T.nc /results/SalishSea/hindcast/07oct15/SalishSea_1h_20151007_20151007_ptrc_T.nc /results/SalishSea/hindcast/17oct15/SalishSea_1h_20151017_20151017_ptrc_T.nc /results/SalishSea/hindcast/27oct15/SalishSea_1h_20151027_20151027_ptrc_T.nc /results/SalishSea/hindcast/06nov15/SalishSea_1h_20151106_20151106_ptrc_T.nc /results/SalishSea/hindcast/16nov15/SalishSea_1h_20151116_20151116_ptrc_T.nc /results/SalishSea/hindcast/26nov15/SalishSea_1h_20151126_20151126_ptrc_T.nc /results/SalishSea/hindcast/06dec15/SalishSea_1h_20151206_20151206_ptrc_T.nc /results/SalishSea/hindcast/16dec15/SalishSea_1h_20151216_20151216_ptrc_T.nc /results/SalishSea/hindcast/26dec15/SalishSea_1h_20151226_20151226_ptrc_T.nc /results/SalishSea/hindcast/05jan16/SalishSea_1h_20160105_20160105_ptrc_T.nc /results/SalishSea/hindcast/15jan16/SalishSea_1h_20160115_20160115_ptrc_T.nc /results/SalishSea/hindcast/25jan16/SalishSea_1h_20160125_20160125_ptrc_T.nc /results/SalishSea/hindcast/04feb16/SalishSea_1h_20160204_20160204_ptrc_T.nc /results/SalishSea/hindcast/14feb16/SalishSea_1h_20160214_20160214_ptrc_T.nc /results/SalishSea/hindcast/24feb16/SalishSea_1h_20160224_20160224_ptrc_T.nc /results/SalishSea/hindcast/05mar16/SalishSea_1h_20160305_20160305_ptrc_T.nc /results/SalishSea/hindcast/15mar16/SalishSea_1h_20160315_20160315_ptrc_T.nc /results/SalishSea/hindcast/25mar16/SalishSea_1h_20160325_20160325_ptrc_T.nc /results/SalishSea/hindcast/04apr16/SalishSea_1h_20160404_20160404_ptrc_T.nc /results/SalishSea/hindcast/14apr16/SalishSea_1h_20160414_20160414_ptrc_T.nc /results/SalishSea/hindcast/24apr16/SalishSea_1h_20160424_20160424_ptrc_T.nc /results/SalishSea/hindcast/04may16/SalishSea_1h_20160504_20160504_ptrc_T.nc
Nplot=NGlobalTot[idir]-NGlobalTot[idir][0,:]
#plt.pcolormesh([t0+dt.timedelta(10*ii) for ii in range(0,30)],dept[0,:],np.transpose(siplot[:30,:]),cmap=cm1,vmin=-1e9,vmax=1e9)
plt.pcolormesh([t0+dt.timedelta(10*ii) for ii in range(0,nlen)],dept[0,:],np.transpose(Nplot),cmap=cm1,vmin=-1e8,vmax=1e8)
plt.ylim(400,0)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x7f61d2b81438>