#ntrunc = 64 #dt = 1800. # time step in seconds nlons = 96 nlats = nlons / 2 # number of longitudes/latitudes ntrunc = 32 dt = 3600. # time step in seconds fhout = 12. # output interval (hours) gridtype = 'gaussian' # 'regular' or 'gaussian' output_file = 'truth_twolevel_t%s_%sh_tst.nc' % (ntrunc, int(fhout)) # create spherical harmonic instance. rsphere = 6.37122e6 # earth radius sp = Spharmt(nlons, nlats, ntrunc, rsphere, gridtype=gridtype) nstart = int((200. * 86400.) / dt) # end of spinup period nmax = int((1300. * 86400.) / dt) # total duration of run # create model instance model = TwoLevel(sp, dt) print 'pole/equator temp diff = ', model.thetaref.max() - model.thetaref.min() # vort, div initial conditions psipert = np.zeros((2, model.nlat, model.nlon), np.float) psipert[1, :, :] = 5.e6 * np.sin( (model.lons - np.pi))**12 * np.sin(2. * model.lats)**12 psipert = np.where(model.lons[np.newaxis, :, :] > 0., 0, psipert) psipert[1, :, :] += np.random.normal(scale=1.e6, size=(sp.nlats, sp.nlons)) ug = np.zeros((2, model.nlat, model.nlon), np.float)
gridtype = 'gaussian' dt = 3600. # time step in seconds rsphere = 6.37122e6 # earth radius # fix random seed for reproducibility. np.random.seed(42) # model nature run to sample initial ensemble and draw additive noise. modelclimo_file = 'truth_twolevel_t%s_12h.nc' % ntrunc # 'truth' nature run to sample obs # (these two files can be the same for perfect model expts) # file to sample additive noise. truth_file = 'truth_twolevel_t32_12h.nc' # create spherical harmonic transform instance sp = Spharmt(nlons,nlats,ntrunc,rsphere,gridtype=gridtype) spout = sp models = [] for nanal in range(nanals): models.append(TwoLevel(sp,dt)) # weights for computing global means. globalmeanwts = models[0].globalmeanwts # read nature run, create obs. nct = Dataset(truth_file) lats = nct.variables['lat'][:] lons = nct.variables['lon'][:] spin = Spharmt(len(lons),len(lats),int(nct.ntrunc),rsphere,gridtype=gridtype) samegrid = spin.nlons == spout.nlons and spin.nlats == spout.nlats
vrtmag = (vrtspec[n]*np.conj(vrtspec[n])).real divmag = (divspec[n]*np.conj(divspec[n])).real if indxm[n] == 0: varspect[indxn[n]] += norm[n]*vrtmag varspect[indxn[n]] += norm[n]*divmag else: varspect[indxn[n]] += 2.*norm[n]*vrtmag varspect[indxn[n]] += 2.*norm[n]*divmag return varspect f = Dataset('/Volumes/Drobo/truth_twolevel_t63_12h.nc') lats = f.variables['lat'][:]; lons = f.variables['lon'][:] u = f.variables['u']; v = f.variables['v'] ntimes, nlevs, nlats, nlons = u.shape print nlons, nlats, f.ntrunc, f.rsphere sp = Spharmt(nlons,nlats,int(f.ntrunc),f.rsphere,gridtype='gaussian') kenorm = (-0.25*sp.invlap).astype(np.float) kespecmean = None nout = 0 for ntime in range(ntimes): print ntime vrtspec, divspec = sp.getvrtdivspec(u[ntime,1,...],v[ntime,1,...]) kespec = getvarspectrum(vrtspec,divspec,kenorm,sp.order,sp.degree,sp.ntrunc) if kespecmean is None: kespecmean = kespec else: kespecmean += kespec nout += 1 kespecmean = kespecmean/nout plt.loglog(np.arange(f.ntrunc+1),kespec,linewidth=2,\ label='t63')
ntrunc = 32 # spectral truncation (for alias-free computations) gridtype = 'gaussian' dt = 3600. # time step in seconds rsphere = 6.37122e6 # earth radius # set random states rs1 = np.random.RandomState(seed=42) # reproduce obs rs2 = np.random.RandomState(seed=None) # different ensemble each run # model nature run to sample initial ensemble and draw additive noise. modelclimo_file = 'truth_twolevel_t%s_12h.nc' % ntrunc # 'truth' nature run to sample obs truth_file = 'truth_twolevel_t32_12h.nc' # create spherical harmonic transform instance sp = Spharmt(nlons, nlats, ntrunc, rsphere, gridtype=gridtype) spout = sp models = [] for nanal in range(nanals): models.append(TwoLevel(sp, dt)) # weights for computing global means. globalmeanwts = models[0].globalmeanwts # read nature run, create obs. nct = Dataset(truth_file) lats = nct.variables['lat'][:] lons = nct.variables['lon'][:] spin = Spharmt(len(lons), len(lats),
gridtype = "gaussian" dt = 3600.0 # time step in seconds rsphere = 6.37122e6 # earth radius # fix random seed for reproducibility. np.random.seed(42) # model nature run to sample initial ensemble and draw additive noise. modelclimo_file = "truth_twolevel_t%s_12h.nc" % ntrunc # 'truth' nature run to sample obs # (these two files can be the same for perfect model expts) # file to sample additive noise. truth_file = "truth_twolevel_t32_12h.nc" # create spherical harmonic transform instance sp = Spharmt(nlons, nlats, ntrunc, rsphere, gridtype=gridtype) spout = sp models = [] for nanal in range(nanals): models.append(TwoLevel(sp, dt)) # weights for computing global means. globalmeanwts = models[0].globalmeanwts # read nature run, create obs. nct = Dataset(truth_file) lats = nct.variables["lat"][:] lons = nct.variables["lon"][:] spin = Spharmt(len(lons), len(lats), int(nct.ntrunc), rsphere, gridtype=gridtype) samegrid = spin.nlons == spout.nlons and spin.nlats == spout.nlats
return vrtspec,divspec,thetaspec if __name__ == "__main__": import matplotlib.pyplot as plt import matplotlib.animation as animation # grid, time step info nlons = 128 # number of longitudes ntrunc = 42 # spectral truncation (for alias-free computations) dt = 2700 # time step in seconds nlats = nlons/2 # for regular grid. gridtype = 'gaussian' # create spherical harmonic instance. rsphere = 6.37122e6 # earth radius sp = Spharmt(nlons,nlats,ntrunc,rsphere,gridtype=gridtype) # create model instance using default parameters. model = TwoLevel(sp,dt) # vort, div initial conditions psipert = np.zeros((2,model.nlat,model.nlon),np.float) psipert[1,:,:] = 5.e6*np.sin((model.lons-np.pi))**12*np.sin(2.*model.lats)**12 psipert = np.where(model.lons[np.newaxis,:,:] > 0., 0, psipert) psipert[1,:,:] += np.random.normal(scale=1.e6,size=(sp.nlats,sp.nlons)) ug = np.zeros((2,model.nlat,model.nlon),np.float) vg = np.zeros((2,model.nlat,model.nlon),np.float) ug[1,:,:] = model.umax*np.sin(2.*model.lats)**model.jetexp vrtspec, divspec = sp.getvrtdivspec(ug,vg) vrtspec = vrtspec + model.lap*sp.grdtospec(psipert) thetaspec = model.nlbalance(vrtspec)
ntrunc = 32 # spectral truncation (for alias-free computations) gridtype = 'gaussian' dt = 3600. # time step in seconds rsphere = 6.37122e6 # earth radius # set random states rs1 = np.random.RandomState(seed=42) # reproduce obs rs2 = np.random.RandomState(seed=None) # different ensemble each run # model nature run to sample initial ensemble and draw additive noise. modelclimo_file = 'truth_twolevel_t%s_12h.nc' % ntrunc # 'truth' nature run to sample obs truth_file = 'truth_twolevel_t32_12h.nc' # create spherical harmonic transform instance sp = Spharmt(nlons,nlats,ntrunc,rsphere,gridtype=gridtype) spout = sp models = [] for nanal in range(nanals): models.append(TwoLevel(sp,dt)) # weights for computing global means. globalmeanwts = models[0].globalmeanwts # read nature run, create obs. nct = Dataset(truth_file) lats = nct.variables['lat'][:] lons = nct.variables['lon'][:] spin = Spharmt(len(lons),len(lats),int(nct.ntrunc),rsphere,gridtype=gridtype) samegrid = spin.nlons == spout.nlons and spin.nlats == spout.nlats
varspect[indxn[n]] += norm[n] * vrtmag varspect[indxn[n]] += norm[n] * divmag else: varspect[indxn[n]] += 2. * norm[n] * vrtmag varspect[indxn[n]] += 2. * norm[n] * divmag return varspect f = Dataset('/Volumes/Drobo/truth_twolevel_t63_12h.nc') lats = f.variables['lat'][:] lons = f.variables['lon'][:] u = f.variables['u'] v = f.variables['v'] ntimes, nlevs, nlats, nlons = u.shape print nlons, nlats, f.ntrunc, f.rsphere sp = Spharmt(nlons, nlats, int(f.ntrunc), f.rsphere, gridtype='gaussian') kenorm = (-0.25 * sp.invlap).astype(np.float) kespecmean = None nout = 0 for ntime in range(ntimes): print ntime vrtspec, divspec = sp.getvrtdivspec(u[ntime, 1, ...], v[ntime, 1, ...]) kespec = getvarspectrum(vrtspec, divspec, kenorm, sp.order, sp.degree, sp.ntrunc) if kespecmean is None: kespecmean = kespec else: kespecmean += kespec nout += 1 kespecmean = kespecmean / nout plt.loglog(np.arange(f.ntrunc+1),kespec,linewidth=2,\