# End i loop #%% Examine sea surface height values during that period #%% Load in NAISST to see what is happening # Left off at trying to plot AMV Index, but am having trouble matching the actual index... ld3 = np.load("/Users/gliu/Downloads/02_Research/01_Projects/01_AMV/02_stochmod/01_Data/CESM-PIC_NASSTI.npz") nassti = ld3['nassti_full'] # Calculate AMV Index nassti_ann = proc.ann_avg(nassti,0) # Apply Low Pass Filter cutofftime = 10 order = 5 aa_sst = nassti_ann # Design Butterworth Lowpass Filter filtfreq = len(aa_sst)/cutofftime nyquist = len(aa_sst)/2 cutoff = filtfreq/nyquist b,a = butter(order,cutoff,btype="lowpass") amv = filtfilt(b,a,aa_sst)
model = 3 # Select Model( 0:hfix || 1:hmax || 2:hvar || 3: entrain) mon = 6 xl = [0,12000] #yl = [-1e200,1e200] sstpt = sst[model] if model == 3: Fpt = F[2] else: Fpt = F[model] # Calculate Annual Averages sstptann = proc.ann_avg(sstpt,0) Fptann = proc.ann_avg(Fpt,0) # Set plotting parameters and text tper = np.arange(0,t_end) yper = np.arange(0,t_end,12) fstats = viz.quickstatslabel(Fpt) tstats = viz.quickstatslabel(sstpt) # Start plot fig,ax = plt.subplots(2,1,figsize=(8,6)) plt.style.use('ggplot') plt.subplot(2,1,1) plt.plot(tper,Fpt) plt.plot(yper,Fptann,color='k',label='Ann. Avg')
#%% Plot Timeseries # ----------------------------------------------------------------- nmean = 10 # ----------------------- # CESM time plot Model Output Time Plot # ----------------------- fig,ax=plt.subplots(1,1,figsize=(8,3)) csst = [sstfullpt,sstslabpt] ccol = ['k','gray'] clab = ["CESM-Full","CESM-Slab"] for i in range(2): sstann = proc.ann_avg(csst[i],0) win = np.ones(nmean)/nmean sstann = np.convolve(sstann,win,mode='valid') yrs = np.arange(0,sstann.shape[0]) plabel = clab[i] + r", 1$\sigma=%.2f$" % np.std(sstann) ax.plot(sstann,label=plabel,lw=0.5,color=ccol[i]) print("Std for %s is %.2f"%(labels[i],np.std(sst[i]))) ax.legend(fontsize=8,ncol=3) ax.set_xlabel("Years") ax.set_ylabel("degC") ax.set_title("CESM SST (Annual) at %s (%i-year Running Mean) "% (loctitle,nmean)) ax.grid(True,ls='dotted')
#ax.plot(sstq[klon,klat,:],label="Flux Integration with Q Correction",color='orange',lw=lws,alpha=0.90) #ax.set_xlim([10276,10776]) ax.set_xlabel("Time (Months)") ax.set_ylabel("SST (degC)") ax.grid(True,ls='dotted') #ax.set_ylim([-2,2]) #ax.set_xlim([0,100]) ax.legend(fontsize=8,ncol=1) #ax.set_title("SST Integration Comparison \n Location: %s" % (loctitle)) plt.savefig(outfigpath + "CESM_SLAB_FluxIntegrationComp_Point%s_Model_Output_detrend_Quad_all.png" % (locfn),dpi=200) #%% Do the same as above, but for annual averages lws = 1 sstqann = proc.ann_avg(sstq[klon,klat,:],0) sstnann = proc.ann_avg(sstn[klon,klat,:],0) sstsann = proc.ann_avg(ssts[klon,klat,:],0) sstslab = proc.ann_avg(tsenso0[klon360,klat,:],0) fig,ax = plt.subplots(1,1,figsize=(12,3)) ax.plot(sstslab,label="SLAB SST",color='k',lw=lws) ax.plot(sstnann,label="Flux Integration ",color='b',lw=lws,alpha=0.90) ax.plot(sstsann,label="Flux Integration, Shifted Forcing",color='g',alpha=0.9) #ax.plot(sstqann,label="Flux Integration with Q Correction",color='orange',lw=lws,alpha=0.90) ax.set_xlabel("Time (Years)") ax.set_xticks(np.arange(0,1000,50)) ax.set_ylabel("SST (degC)") #ax.set_xlim([800,900]) ax.grid(True,ls='dotted') #ax.legend(fontsize=8,ncol=3)
#%% Calculate AMV Index amv = {} aa = {} annsst = {} # Load in dat for each mode and compute amv index for mode in hvarmode: print(mode) #sst[mode] = np.load(datpath+"stoch_output_1000yr_funiform%i_entrain0_hvar%i.npy" % (funiform,mode)) annsst[mode] = proc.ann_avg(sst[mode],2) # Calculate AMV Index amv[mode],aa[mode] = proc.calc_AMV(lon,lat,annsst[mode],bbox,order,cutofftime,1) # # Repeat, but for forcing # if funiform == 2: # lpforcing = {} # aaforcing = {} # for mode in hvarmode: # annforcing = ann_avg(F[mode],2) # lpforcing[mode],aaforcing[mode] = calc_AMV(lon,lat,annforcing,bbox,order,cutofftime,1) # else: # annforcing = ann_avg(F,2)
plot_AMV_spatial(dtr.T, hlon, hlatnew, bbox, cmap) plt.title("Detrended Monthly SST") #%% AMV <Raw> dtamv, dtaa = calc_AMV(hlon, hlatnew, hsstnew, bbox, order, cutofftime, 1) dtr = proc.regress2ts(hsstnew, dtamv, 0, 1) plot_AMV_spatial(dtr.T, hlon, hlatnew, bbox, cmap) plt.title("Raw Monthly SST") #%% Trying annually averaged data aaraw = proc.ann_avg(hsstnew, 2) #dtraw = ann_avg(dt_hsst,2) #adtraw = ann_avg(ahsst,2) # Detrend annual data invar = ahsst cutofftime = 120 dtamv, dtaa = calc_AMV(hlon, hlatnew, invar, bbox, order, cutofftime, 1) dtr = proc.regress2ts(invar, dtamv / np.std(dtamv), 0, 1) xtk = np.arange(1, 101, 10) xlbs = np.arange(1920, 2020, 10) #plt.subplots(211) plot_AMV_spatial(dtr.T, hlon, hlatnew, bbox, cmap, cint=cint, clab=clab) #plot_AMV_spatial(dtr.T,hlon,hlatnew,bbox,cmap) #plt.title("Detrended, Deanomalized, Monthly SST")
h_amv,h_regr = proc.calc_AMVquick(hsst,hlon,hlat,bbox) #%% Try Another Calculation Method #Methods # 0) Regress anomaly onto global mean # 1...N) Remove N-degree polynomial # Get timedim nyr = int(hsst.shape[2]/12) x = np.arange(0,nyr,1) # First get annual averaged data hsstann = proc.ann_avg(hsstnew,2) hsstann = hsstann - hsstann.mean(2)[:,:,None] # Get nan points hsstann = hsstann.reshape(360*180,nyr) okdata,knan,okpts = proc.find_nan(hsstann,1) if method == 0: # Calculate global mean SST glomean = okdata.mean(0) # Regress back to the original data to get the global component beta,b=proc.regress_2d(glomean,okdata) # Subtract this from the original data okdt = okdata - beta[:,None]
ax.grid(True, which='both', ls='dotted') #ax.set_xlabel("Frequency (cycles/sec)",fontsize=13) ax.set_ylabel(r"Frequency x Power $(^{\circ}C)^{2}$", fontsize=13) ax.set_title("Power Spectrum at %s \n" % (locstringtitle) + "nsmooth=%i, taper=%.2f" % (nsmooth, pct)) plt.tight_layout() plt.savefig("%sPowerSpectra_%s_nsmooth%i_pct%03d_axopt%i_%s.png" % (outpath, 'COMPARISON', nsmooth, pct * 100, axopt, locstring), dpi=200) # -------------------------------------------------- #%% Spectral Analysis, but using annual averaged data # -------------------------------------------------- slabann = proc.ann_avg(cesmslab, 0) fullann = proc.ann_avg(cesmfull, 0) sstann = [] for i in range(4): sstann.append(proc.ann_avg(sst[i], 0)) nyr = sstann[0].shape[0] simtime = np.arange(0, sst[0].shape[0]) years = np.arange(0, nyr) # Check Annual Averaging i = 3 fig, ax = plt.subplots(1, 1) ax.plot(simtime, sst[i], color='g', lw=0.5) ax.plot(simtime[::12], sstann[i], color='k') ax.set_xlim([0, 120])