def specsmooth(self, datagrid, smooth): """ isotropic spectral smoothing on a sphere. @param datagrid: rank 2 or 3 numpy float32 array with shape (nlat,nlon) or (nlat,nlon,nt), where nt is the number of grids to be smoothed. If datagrid is rank 2, nt is assumed to be 1. @param smooth: rank 1 array of length nlat containing smoothing factors as a function of total wavenumber. @return: C{B{datagrid}} - rank 2 or 3 numpy float32 array with shape (nlat,nlon) or (nlat,nlon,nt) containing the smoothed grids. """ # check that datagrid is rank 2 or 3 with size (self.nlat, self.nlon) or # (self.nlat, self.nlon, nt) where nt is number of grids to transform. if len(datagrid.shape) > 3: msg = 'specsmooth needs a rank two or three array, got %d' % (len( datagrid.shape), ) raise ValueError(msg) if datagrid.shape[0] != self.nlat or datagrid.shape[1] != self.nlon: msg = 'specsmooth needs an array of size %d by %d, got %d by %d' % ( self.nlat, self.nlon, datagrid.shape[0], datagrid.shape[1], ) raise ValueError(msg) # make sure smooth is rank 1, same size as datagrid.shape[0] if len(smooth.shape) != 1 or smooth.shape[0] != datagrid.shape[0]: msg = 'smooth must be rank 1 and same size as datagrid.shape[0] in specsmooth!' raise ValueError(msg) # grid to spectral transform. nlat = self.nlat dataspec = self.grdtospec(datagrid, nlat - 1) # multiply spectral coeffs. by smoothing factor. dataspec = _spherepack.multsmoothfact(dataspec, smooth) # spectral to grid transform. datagrid = self.spectogrd(dataspec) return datagrid
def regrid(grdin, grdout, datagrid, ntrunc=None, smooth=None): """ regrid data using spectral interpolation, while performing optional spectral smoothing and/or truncation. @param grdin: Spharmt class instance describing input grid. @param grdout: Spharmt class instance describing output grid. @param datagrid: data on input grid (grdin.nlat x grdin.nlon). If datagrid is rank 3, last dimension is the number of grids to interpolate. @keyword ntrunc: optional spectral truncation limit for datagrid (default min(grdin.nlat-1,grdout.nlat-1)). @keyword smooth: rank 1 array of length grdout.nlat containing smoothing factors as a function of total wavenumber (default is no smoothing). @return: C{B{datagrid}} - interpolated (and optionally smoothed) array(s) on grdout.nlon x grdout.nlat grid. """ # check that datagrid is rank 2 or 3 with size (grdin.nlat, grdin.nlon) or # (grdin.nlat, grdin.nlon, nt) where nt is number of grids to transform. if len(datagrid.shape) > 3: msg = 'regrid needs a rank two or three array, got %d' % (len( datagrid.shape), ) raise ValueError(msg) if datagrid.shape[0] != grdin.nlat or datagrid.shape[1] != grdin.nlon: msg = 'grdtospec needs an array of size %d by %d, got %d by %d' % ( grdin.nlat, grdin.nlon, datagrid.shape[0], datagrid.shape[1], ) raise ValueError(msg) if smooth is not None and (len(smooth.shape) != 1 or smooth.shape[0] != grdout.nlat): msg = 'smooth must be rank 1 size grdout.nlat in regrid!' raise ValueError(msg) if ntrunc is None: ntrunc = min(grdout.nlat - 1, grdin.nlat - 1) dataspec = grdin.grdtospec(datagrid, ntrunc) if smooth is not None: dataspec = _spherepack.multsmoothfact(dataspec, smooth) return grdout.spectogrd(dataspec)
def specsmooth(self, datagrid, smooth): """ isotropic spectral smoothing on a sphere. @param datagrid: rank 2 or 3 numpy float32 array with shape (nlat,nlon) or (nlat,nlon,nt), where nt is the number of grids to be smoothed. If datagrid is rank 2, nt is assumed to be 1. @param smooth: rank 1 array of length nlat containing smoothing factors as a function of total wavenumber. @return: C{B{datagrid}} - rank 2 or 3 numpy float32 array with shape (nlat,nlon) or (nlat,nlon,nt) containing the smoothed grids. """ # check that datagrid is rank 2 or 3 with size (self.nlat, self.nlon) or # (self.nlat, self.nlon, nt) where nt is number of grids to transform. if len(datagrid.shape) > 3: msg = 'specsmooth needs a rank two or three array, got %d' % (len(datagrid.shape),) raise ValueError(msg) if datagrid.shape[0] != self.nlat or datagrid.shape[1] != self.nlon: msg = 'specsmooth needs an array of size %d by %d, got %d by %d' % (self.nlat, self.nlon, datagrid.shape[0], datagrid.shape[1],) raise ValueError(msg) # make sure smooth is rank 1, same size as datagrid.shape[0] if len(smooth.shape) !=1 or smooth.shape[0] != datagrid.shape[0]: msg = 'smooth must be rank 1 and same size as datagrid.shape[0] in specsmooth!' raise ValueError(msg) # grid to spectral transform. nlat = self.nlat dataspec = self.grdtospec(datagrid, nlat-1) # multiply spectral coeffs. by smoothing factor. dataspec = _spherepack.multsmoothfact(dataspec, smooth) # spectral to grid transform. datagrid = self.spectogrd(dataspec) return datagrid
def regrid(grdin, grdout, datagrid, ntrunc=None, smooth=None): """ regrid data using spectral interpolation, while performing optional spectral smoothing and/or truncation. @param grdin: Spharmt class instance describing input grid. @param grdout: Spharmt class instance describing output grid. @param datagrid: data on input grid (grdin.nlat x grdin.nlon). If datagrid is rank 3, last dimension is the number of grids to interpolate. @keyword ntrunc: optional spectral truncation limit for datagrid (default min(grdin.nlat-1,grdout.nlat-1)). @keyword smooth: rank 1 array of length grdout.nlat containing smoothing factors as a function of total wavenumber (default is no smoothing). @return: C{B{datagrid}} - interpolated (and optionally smoothed) array(s) on grdout.nlon x grdout.nlat grid. """ # check that datagrid is rank 2 or 3 with size (grdin.nlat, grdin.nlon) or # (grdin.nlat, grdin.nlon, nt) where nt is number of grids to transform. if len(datagrid.shape) > 3: msg = 'regrid needs a rank two or three array, got %d' % (len(datagrid.shape),) raise ValueError(msg) if datagrid.shape[0] != grdin.nlat or datagrid.shape[1] != grdin.nlon: msg = 'grdtospec needs an array of size %d by %d, got %d by %d' % (grdin.nlat, grdin.nlon, datagrid.shape[0], datagrid.shape[1],) raise ValueError(msg) if smooth is not None and (len(smooth.shape) !=1 or smooth.shape[0] != grdout.nlat): msg = 'smooth must be rank 1 size grdout.nlat in regrid!' raise ValueError(msg) if ntrunc is None: ntrunc = min(grdout.nlat-1,grdin.nlat-1) dataspec = grdin.grdtospec(datagrid,ntrunc) if smooth is not None: dataspec = _spherepack.multsmoothfact(dataspec, smooth) return grdout.spectogrd(dataspec)