def USVS_from_Chi(self,Chi): s_Chi=self.spharm.grdtospec(g_Chi) g_U, g_V = self.spharm.getgrad(s_Chi) slice_lon=slice(1,self.spharm_regrid.nlon+1,2) slice_lat=slice(0,self.spharm_regrid.nlat+1,2) shift_offset=1 US = np.flipud(np.roll( spharm.regrid(self.spharm,self.spharm_regrid,g_U)[slice_lat,slice_lon], self.shift_index+shift_offset,axis=1)) slice_lon=slice(0,self.spharm_regrid.nlon+1,2) slice_lat=slice(1,self.spharm_regrid.nlat,2) shift_offset=0 VS = np.flipud(np.roll( spharm.regrid(self.spharm,self.spharm_regrid,g_V)[slice_lat,slice_lon], self.shift_index+shift_offset,axis=1)) return US, VS
def regrid_sphere(nlat,nlon,Nens,X): """ Truncate lat,lon grid to another resolution in spherical harmonic space. Triangular truncation Inputs: nlat : number of latitudes nlon : number of longitudes Nens : number of ensemble members X : data array of shape (nlat*nlon,Nens) ntrunc : triangular truncation (e.g., use 42 for T42) Outputs : lat_new : 2D latitude array on the new grid (nlat_new,nlon_new) lon_new : 2D longitude array on the new grid (nlat_new,nlon_new) X_new : truncated data array of shape (nlat_new*nlon_new, Nens) """ # Originator: Greg Hakim # University of Washington # May 2015 # create the spectral object on the original grid specob_lmr = Spharmt(nlon,nlat,gridtype='regular',legfunc='computed') # truncate to a lower resolution grid (triangular truncation) # ifix = np.remainder(ntrunc,2.0).astype(int) # nlat_new = ntrunc + ifix # nlon_new = int(nlat_new*1.5) ntrunc = 42 nlat_new = 64 nlon_new = 128 # create the spectral object on the new grid specob_new = Spharmt(nlon_new,nlat_new,gridtype='regular',legfunc='computed') # create new lat,lon grid arrays dlat = 90./((nlat_new-1)/2.) dlon = 360./nlon_new veclat = np.arange(-90.,90.+dlat,dlat) veclon = np.arange(0.,360.,dlon) blank = np.zeros([nlat_new,nlon_new]) lat_new = (veclat + blank.T).T lon_new = (veclon + blank) # transform each ensemble member, one at a time X_new = np.zeros([nlat_new*nlon_new,Nens]) for k in range(Nens): X_lalo = np.reshape(X[:,k],(nlat,nlon)) Xbtrunc = regrid(specob_lmr, specob_new, X_lalo, ntrunc=nlat_new-1, smooth=None) vectmp = Xbtrunc.flatten() X_new[:,k] = vectmp return X_new,lat_new,lon_new
def regrid_field(field, lat, lon, lat_new, lon_new): nlat_old, nlon_old = np.size(lat), np.size(lon) nlat_new, nlon_new = np.size(lat_new), np.size(lon_new) spec_old = Spharmt(nlon_old, nlat_old, gridtype='regular', legfunc='computed') spec_new = Spharmt(nlon_new, nlat_new, gridtype='regular', legfunc='computed') #remove nans field[np.isnan(field)] = 0 field_new = [] for field_old in field: regridded_field = regrid(spec_old, spec_new, field_old, ntrunc=None, smooth=None) field_new.append(regridded_field) field_new = np.array(field_new) return field_new
def regrid(self, ntrunc, inplace=False): old_spec = Spharmt(self.nlon, self.nlat, gridtype='regular', legfunc='computed') ifix = ntrunc % 2 new_nlat = ntrunc + ifix new_nlon = int(new_nlat * 1.5) new_spec = Spharmt(new_nlon, new_nlat, gridtype='regular', legfunc='computed') include_poles = False if new_nlat % 2 == 0 else True new_lat_2d, new_lon_2d, _, _ = generate_latlon( new_nlat, new_nlon, include_endpts=include_poles) new_lat = new_lat_2d[:, 0] new_lon = new_lon_2d[0, :] # new_value = [] # for old_value in self.value: old_value = np.moveaxis(self.value, 0, -1) regridded_value = regrid(old_spec, new_spec, old_value, ntrunc=new_nlat - 1, smooth=None) new_value = np.moveaxis(regridded_value, -1, 0) # new_value.append(regridded_value) new_value = np.array(new_value) if inplace: self.value = new_value self.lat = new_lat self.lon = new_lon self.nlat = np.size(new_lat) self.nlon = np.size(new_lon) self.ntrunc = ntrunc else: new_field = self.copy() new_field.value = new_value new_field.lat = new_lat new_field.lon = new_lon new_field.nlat = np.size(new_lat) new_field.nlon = np.size(new_lon) new_field.ntrunc = ntrunc return new_field
def regrid(lon_in,lat_in,data,nlon_o,nlat_o): ''' Given data(nlat,nlon), regrid the data into (nlat_o,nlon_o) using the spharm.regrid routine If the input data is not on a complete sphere, copy the input onto a complete sphere before regridding Input: lon_in - the longitude the input data is on lat_in - the latitude the input data is on data - numpy.ndarray nlon_o - integer nlat_o - integer Return: numpy.ndarray of shape (nlat_o,nlon_o) ''' if not _SPHARM_INSTALLED: raise _import_error assert data.ndim <= 3 # determine if the domain is a complete sphere dlon = numpy.diff(lon_in[0:2]) AllLon = dlon*(len(lon_in)+1) > 360. dlat = numpy.diff(lat_in[0:2]) AllLat = dlat*(len(lat_in)+1) > 180. sliceobj = [slice(None),]*data.ndim if AllLon and AllLat: # The data covers the whole sphere inputdata = data nlat_i = len(lat_in) nlon_i = len(lon_in) lat = lat_in lon = lon_in else: inputdata,lon,lat = regional2global(data,lon_in,lat_in) nlat_i = len(lat) nlon_i = len(lon) gridin = spharm.Spharmt(nlon_i,nlat_i) gridout = spharm.Spharmt(nlon_o,nlat_o) return spharm.regrid(gridin,gridout,inputdata)
lats = (0.5*math.pi-delat*numpy.indices(lats_reg.shape)[0,:,:]) lons = (delat*numpy.indices(lons_reg.shape)[1,:,:]) psi_reg_exact = rhwave(wavenum,omega,re,lats,lons) delat = 2.*math.pi/nlons_gau lats = (math.pi/180.)*numpy.transpose(gaulats*numpy.ones((nlons_gau,nlats_gau),'d')) lons = (delat*numpy.indices(lons_gau.shape)[1,:,:]) psi_gau = rhwave(wavenum,omega,re,lats,lons) # create Spharmt instances for regular and gaussian grids. reggrid = Spharmt(nlons_reg,nlats_reg,gridtype='regular') gaugrid = Spharmt(nlons_gau,nlats_gau,gridtype='gaussian') # regrid from gaussian to regular grid. psi_reg = regrid(gaugrid,reggrid,psi_gau) print('reggrid error (should be less than 1.e-6):') print(numpy.fabs(psi_reg-psi_reg_exact).max()/numpy.fabs(psi_reg_exact).max()) # spectrally interpolate to geodesic grid. ntrunc = nlats_reg-1 latpts,lonpts = getgeodesicpts(7) # compute geodesic points dataspec = reggrid.grdtospec(psi_reg_exact,ntrunc) # compute spectral coeffs nlat = 0 dg2rad = math.pi/180. # degrees to radians factor. err = [] for lat, lon in zip(latpts,lonpts): legfuncs = legendre(lat,ntrunc) # compute legendre functions intrp = specintrp(lon,dataspec,legfuncs) # do spectral interpolation
os.path.join(os.path.join(datapath2,date),runname2+'.t'+date[8:10]+'z.pgrbf'+fhr) f2 = nio.open_file(grbfile_2+'.grib') levels = f2.variables['lv_ISBL3'][:].tolist() nlev = levels.index(level) dat2u = f2.variables[varnameu][nlev] dat2v = f2.variables[varnamev][nlev] f2.close() f1 = nio.open_file(grbfile_1+'.grib') levels = f1.variables['lv_ISBL3'][:].tolist() nlev = levels.index(level) dat1u = f1.variables[varnameu][nlev] dat1v = f1.variables[varnamev][nlev] f1.close() datainu = varinu[nt,nlevec,:,:] datainv = varinv[nt,nlevec,:,:] datverifu = regrid(sin,sout,datainu,ntrunc=ntrunc) datverifv = regrid(sin,sout,datainv,ntrunc=ntrunc) if varshort == 'psi': datverif,chi =\ sout.getpsichi(datverifu,datverifv,ntrunc=ntrunc) dat2,chi = sout.getpsichi(dat2u,dat2v,ntrunc=ntrunc) dat1,chi = sout.getpsichi(dat1u,dat1v,ntrunc=ntrunc) else: psi,datverif = sout.getpsichi(datverifu,datverifv,ntrunc=ntrunc) psi,dat2 = sout.getpsichi(dat2u,dat2v,ntrunc=ntrunc) psi,dat1 = sout.getpsichi(dat1u,dat1v,ntrunc=ntrunc) else: if fhr=='0': grbfile_1 =\ os.path.join(os.path.join(datapath1,date),runname1+'.t'+date[8:10]+'z.pgrbanl') grbfile_2 =\
era20c_zm = np.zeros([len(cyears),nlat_new]) k = -1 for yr in cyears: k = k + 1 LMR_smatch, LMR_ematch = find_date_indices(LMR_time,yr-iw,yr+iw+1) TCR_smatch, TCR_ematch = find_date_indices(TCR_time,yr-iw,yr+iw+1) ERA20C_smatch, ERA20C_ematch = find_date_indices(ERA20C_time,yr-iw,yr+iw+1) print('------------------------------------------------------------------------') print('working on year... %5s' % str(yr)) print(' %5s LMR index= %5s : LMR year= %5s' % (str(yr), str(LMR_smatch),str(LMR_time[LMR_smatch]))) # LMR pdata_lmr = np.mean(LMR[LMR_smatch:LMR_ematch,:,:],0) lmr_trunc = regrid(specob_lmr, specob_new, pdata_lmr, ntrunc=nlat_new-1, smooth=None) # TCR if TCR_smatch and TCR_ematch: pdata_tcr = np.mean(TCR[TCR_smatch:TCR_ematch,:,:],0) else: pdata_tcr = np.zeros(shape=[nlat_TCR,nlon_TCR]) pdata_tcr.fill(np.nan) # regrid on LMR grid if np.isnan(pdata_tcr).all(): tcr_trunc = np.zeros(shape=[nlat_new,nlon_new]) tcr_trunc.fill(np.nan) else: tcr_trunc = regrid(specob_tcr, specob_new, pdata_tcr, ntrunc=nlat_new-1, smooth=None)