# Notes on the data: # data.keys() => dict_keys(['tef_q', 'tef_qs', 'sbins', 'ot', 'qnet', 'fnet', 'ssh']) # data['tef_q'].shape => (8761, 1000), so packed [hour, salinity bin] # sbins are packed low to high # ot is time in seconds from 1/1/1970 sbins = tef_ex['sbins'] ot = tef_ex['ot'] tef_q = tef_ex['tef_q'] tef_qs = tef_ex['tef_qs'] qnet = tef_ex['qnet'] fnet = tef_ex['fnet'] ssh = tef_ex['ssh'] # low-pass # tidal averaging tef_q_lp = zfun.filt_godin_mat(tef_q) tef_qs_lp = zfun.filt_godin_mat(tef_qs) qnet_lp = zfun.filt_godin(qnet) fnet_lp = zfun.filt_godin(fnet) ssh_lp = zfun.filt_godin(ssh) pad = 36 # subsample and cut off nans tef_q_lp = tef_q_lp[pad + dd_offset:-(pad+1):24, :] tef_qs_lp = tef_qs_lp[pad + dd_offset:-(pad+1):24, :] ot = ot[pad + dd_offset:-(pad+1):24] qnet_lp = qnet_lp[pad + dd_offset:-(pad+1):24] fnet_lp = fnet_lp[pad + dd_offset:-(pad+1):24] ssh_lp = ssh_lp[pad + dd_offset:-(pad+1):24] if counter == 0:
indir0 = indir0 + 'cas6_v3_lo8b_'+year_str+'.01.01_'+year_str+'.12.31' + '/' indir = indir0 + 'processed/' sect_list = list(sect_df.index) if testing: sect_list = ['ai2','tn2'] plt.close('all') for sn in sect_list: bulk = pickle.load(open(indir + sn + '.p', 'rb')) # bulk is a dict with keys ['tef_q', 'tef_qs', 'sbins', 'ot', 'qnet', 'fnet', 'ssh'] ot = bulk['ot'] # model time in seconds from 1/1/1970 (hourly) sbins = bulk['sbins'] q = bulk['tef_q'] # packed (time, salinity bin) qf = zfun.filt_godin_mat(q) qnet = bulk['qnet'] day = (ot - ot[0])/86400 # plotting fig = plt.figure(figsize=(14,10)) t0 = 75 t1 = 95 slo = 26 shi = 34 islo = (np.abs(sbins-slo)).argmin() ishi = (np.abs(sbins-shi)).argmin() it0 = (np.abs(day - t0)).argmin() it1 = (np.abs(day - t1)).argmin() sbins = sbins[islo:ishi]
if to_test == 'hanning': # filter each one individually aa1 = zfun.filt_hanning(a1) aa2 = zfun.filt_hanning(a2) aa3 = zfun.filt_hanning(a3) aa4 = zfun.filt_hanning(a4) # and filter this using the function we are testing AA = zfun.filt_hanning_mat(A) elif to_test == 'godin': # filter each one individually aa1 = zfun.filt_godin(a1) aa2 = zfun.filt_godin(a2) aa3 = zfun.filt_godin(a3) aa4 = zfun.filt_godin(a4) # and filter this using the function we are testing AA = zfun.filt_godin_mat(A) # PLOTTING plt.close('all') fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(111) ax.plot(t, aa1, '-r', t, aa2, '-b', t, aa3, '-g',
ii = 0 for Nvn in Nvn_list: if ii == 0: v = ds[Nvn][:].data else: v = v + ds[Nvn][:].data ii += 1 Ntotal = v.copy() units = 'millimole_nitrogen meter-3' else: v = ds[vn][:].data if vn == 'salt': units = 'g/kg' else: units = ds[vn].units V = zfun.filt_godin_mat(v)[12::24,:].T cmap = cmap_dict[vn] ax = fig.add_subplot(NR, 1, rr) cs = ax.pcolormesh(Days, Zr, V, cmap=cmap) if vn == 'NO3': Nlow, Nhi = cs.get_clim() if vn == 'Ntotal': cs.set_clim(vmin=Nlow, vmax=Nhi) plt.vlines([Days[365],Days[2*365]],Zr[0],Zr[-1]) if True: cb = fig.colorbar(cs, aspect=20/NR) else: # Inset colorbar cbaxes = inset_axes(ax, width="25%", height="8%", loc='lower right', borderpad=3) cb = fig.colorbar(cs, cax=cbaxes, orientation='horizontal') cb.ax.tick_params(labelsize=fs-2)
A[:,0,1] = a3 A[:,1,1] = a4 to_test = 'godin' if to_test == 'hanning': # filter each one individually aa1 = zfun.filt_hanning(a1) aa2 = zfun.filt_hanning(a2) aa3 = zfun.filt_hanning(a3) aa4 = zfun.filt_hanning(a4) # and filter this using the function we are testing AA = zfun.filt_hanning_mat(A) elif to_test == 'godin': # filter each one individually aa1 = zfun.filt_godin(a1) aa2 = zfun.filt_godin(a2) aa3 = zfun.filt_godin(a3) aa4 = zfun.filt_godin(a4) # and filter this using the function we are testing AA = zfun.filt_godin_mat(A) # PLOTTING plt.close('all') fig = plt.figure(figsize=(12,8)) ax = fig.add_subplot(111) ax.plot(t,aa1,'-r',t,aa2, '-b',t,aa3, '-g',t,aa4, '-m', linewidth=10, alpha=.2) ax.plot(t,AA[:,0,0],'-r',t,AA[:,1,0],'-b',t,AA[:,0,1],'-g',t,AA[:,1,1],'-m') ax.set_title(to_test.title()) plt.show()
else: dia_list = [] tv_list = ['u', 'v', 'zeta', 'lon', 'salt', 'z', 'cs'] ntv = len(tv_list) for ii in range(ntv): tv = tv_list[ii] NC = 2 ax = fig.add_subplot(ntv,NC, (ii+1)*NC) if True: if tv in dia_list: ax.plot(td, 100*dsr[tv][:,mask].sum(axis=1)/mask.sum()) ax.text(.05, .05, tv + ' %', fontweight='bold', transform=ax.transAxes) ax.set_ylim(0,100) else: ax.plot(td, dsr[tv][:,mask],linewidth=.5) ax.text(.05, .05, tv, fontweight='bold', transform=ax.transAxes) else: ax.plot(td, zfun.filt_godin_mat(dsr[tv][:,mask])) ax.set_xlim(td[0], td[-1]) ax.text(.05, .05, tv, fontweight='bold', transform=ax.transAxes) if ii == ntv-1: ax.set_xlabel('Time (days)') else: ax.set_xticklabels(['']) plt.show() dsr.close() dsg.close()
def tef_details(fn): # this is much the same as tef_integrals() but it returns the raw fields # which I plan to use for making a TEF tutorial # choices tidal_average = False # which kind of time filtering nlay_max = 2 # maximum allowable number of layers to process # load results tef_dict = pickle.load(open(fn, 'rb')) tef_q = tef_dict['tef_q'] tef_qs = tef_dict['tef_qs'] sbins = tef_dict['sbins'] smax = sbins.max() qnet = tef_dict['qnet'] fnet = tef_dict['fnet'] ot = tef_dict['ot'] td = (ot - ot[0])/86400 NS = len(sbins) # low-pass if tidal_average: # tidal averaging tef_q_lp = zfun.filt_godin_mat(tef_q) tef_qs_lp = zfun.filt_godin_mat(tef_qs) qnet_lp = zfun.filt_godin(qnet) fnet_lp = zfun.filt_godin(fnet) pad = 36 else: # nday Hanning window nday = 5 nfilt = nday*24 tef_q_lp = zfun.filt_hanning_mat(tef_q, n=nfilt) tef_qs_lp = zfun.filt_hanning_mat(tef_qs, n=nfilt) qnet_lp = zfun.filt_hanning(qnet, n=nfilt) fnet_lp = zfun.filt_hanning(fnet, n=nfilt) pad = int(np.ceil(nfilt/2)) # subsample tef_q_lp = tef_q_lp[pad:-(pad+1):24, :] tef_qs_lp = tef_qs_lp[pad:-(pad+1):24, :] td = td[pad:-(pad+1):24] qnet_lp = qnet_lp[pad:-(pad+1):24] fnet_lp = fnet_lp[pad:-(pad+1):24] #find integrated TEF quantities # start by making the low-passed flux arrays sorted # from high to low salinity rq = np.fliplr(tef_q_lp) rqs = np.fliplr(tef_qs_lp) sbinsr = sbins[::-1] # then form the cumulative sum (the function Q(s)) Q = np.cumsum(rq, axis=1) nt = len(td) Qi = np.nan * np.zeros((nt, nlay_max)) Fi = np.nan * np.zeros((nt, nlay_max)) Qi_abs = np.nan * np.zeros((nt, nlay_max)) Fi_abs = np.nan * np.zeros((nt, nlay_max)) Sdiv = np.nan * np.zeros(nt) for tt in range(nt): imax = np.argmax(Q[tt,:]) imin = np.argmin(Q[tt,:]) # set the dividing salinity by the size of the transport Qin = rq[tt, 0:imax].sum() Qout = rq[tt, 0:imin].sum() if np.abs(Qin) > np.abs(Qout): idiv = imax else: idiv = imin # get the dividing salinity Sdiv[tt] = sbinsr[idiv] ivec = np.unique(np.array([0, idiv, NS+1])) nlay = len(ivec)-1 for ii in range(nlay): Qi[tt,ii] = rq[tt, ivec[ii]:ivec[ii+1]].sum() Qi_abs[tt,ii] = np.abs(rq[tt, ivec[ii]:ivec[ii+1]]).sum() Fi[tt,ii] = rqs[tt, ivec[ii]:ivec[ii+1]].sum() Fi_abs[tt,ii] = np.abs(rqs[tt, ivec[ii]:ivec[ii+1]]).sum() # form derived quantities Qcrit = np.abs(Qi[:,0]).mean()/5 Qi[np.abs(Qi)==0] = np.nan Si = Fi_abs/Qi_abs return Qi, Si, Fi, qnet_lp, fnet_lp, td, sbinsr, Q, rq, Sdiv, tef_q
# data.keys() => dict_keys(['tef_q', 'tef_qs', 'sbins', 'ot', 'qnet', 'fnet', 'ssh']) # data['tef_q'].shape => (8761, 1000), so packed [hour, salinity bin] # sbins are packed low to high # ot is time in seconds from 1/1/1970 sbins = tef_ex['sbins'] ot = tef_ex['ot'] tef_q = tef_ex['tef_q'] tef_qs = tef_ex['tef_qs'] qnet = tef_ex['qnet'] fnet = tef_ex['fnet'] ssh = tef_ex['ssh'] # low-pass if True: # tidal averaging tef_q_lp = zfun.filt_godin_mat(tef_q) tef_qs_lp = zfun.filt_godin_mat(tef_qs) qnet_lp = zfun.filt_godin(qnet) fnet_lp = zfun.filt_godin(fnet) ssh_lp = zfun.filt_godin(ssh) pad = 36 else: # nday Hanning window nday = 5 nfilt = nday*24 tef_q_lp = zfun.filt_hanning_mat(tef_q, n=nfilt) tef_qs_lp = zfun.filt_hanning_mat(tef_qs, n=nfilt) qnet_lp = zfun.filt_hanning(qnet, n=nfilt) fnet_lp = zfun.filt_hanning(fnet, n=nfilt) ssh_lp = zfun.filt_hanning(ssh, n=nfilt) pad = int(np.ceil(nfilt/2))
def OBSOLETE_tef_details(fn): # this is much the same as tef_integrals() but it returns the raw fields # which I plan to use for making a TEF tutorial # choices tidal_average = False # which kind of time filtering nlay_max = 2 # maximum allowable number of layers to process # load results tef_dict = pickle.load(open(fn, 'rb')) tef_q = tef_dict['tef_q'] tef_qs = tef_dict['tef_qs'] sbins = tef_dict['sbins'] smax = sbins.max() qnet = tef_dict['qnet'] fnet = tef_dict['fnet'] ot = tef_dict['ot'] td = (ot - ot[0])/86400 NS = len(sbins) # low-pass if tidal_average: # tidal averaging tef_q_lp = zfun.filt_godin_mat(tef_q) tef_qs_lp = zfun.filt_godin_mat(tef_qs) qnet_lp = zfun.filt_godin(qnet) fnet_lp = zfun.filt_godin(fnet) pad = 36 else: # nday Hanning window nday = 5 nfilt = nday*24 tef_q_lp = zfun.filt_hanning_mat(tef_q, n=nfilt) tef_qs_lp = zfun.filt_hanning_mat(tef_qs, n=nfilt) qnet_lp = zfun.filt_hanning(qnet, n=nfilt) fnet_lp = zfun.filt_hanning(fnet, n=nfilt) pad = int(np.ceil(nfilt/2)) # subsample tef_q_lp = tef_q_lp[pad:-(pad+1):24, :] tef_qs_lp = tef_qs_lp[pad:-(pad+1):24, :] td = td[pad:-(pad+1):24] qnet_lp = qnet_lp[pad:-(pad+1):24] fnet_lp = fnet_lp[pad:-(pad+1):24] #find integrated TEF quantities # start by making the low-passed flux arrays sorted # from high to low salinity rq = np.fliplr(tef_q_lp) rqs = np.fliplr(tef_qs_lp) sbinsr = sbins[::-1] # then form the cumulative sum (the function Q(s)) Q = np.cumsum(rq, axis=1) nt = len(td) Qi = np.nan * np.zeros((nt, nlay_max)) Fi = np.nan * np.zeros((nt, nlay_max)) Qi_abs = np.nan * np.zeros((nt, nlay_max)) Fi_abs = np.nan * np.zeros((nt, nlay_max)) Sdiv = np.nan * np.zeros(nt) for tt in range(nt): imax = np.argmax(Q[tt,:]) imin = np.argmin(Q[tt,:]) # set the dividing salinity by the size of the transport Qin = rq[tt, 0:imax].sum() Qout = rq[tt, 0:imin].sum() if np.abs(Qin) > np.abs(Qout): idiv = imax else: idiv = imin # get the dividing salinity Sdiv[tt] = sbinsr[idiv] ivec = np.unique(np.array([0, idiv, NS+1])) nlay = len(ivec)-1 for ii in range(nlay): Qi[tt,ii] = rq[tt, ivec[ii]:ivec[ii+1]].sum() Qi_abs[tt,ii] = np.abs(rq[tt, ivec[ii]:ivec[ii+1]]).sum() Fi[tt,ii] = rqs[tt, ivec[ii]:ivec[ii+1]].sum() Fi_abs[tt,ii] = np.abs(rqs[tt, ivec[ii]:ivec[ii+1]]).sum() # form derived quantities Qcrit = np.abs(Qi[:,0]).mean()/5 Qi[np.abs(Qi)==0] = np.nan Si = Fi_abs/Qi_abs return Qi, Si, Fi, qnet_lp, fnet_lp, td, sbinsr, Q, rq, Sdiv, tef_q