def example(): """ example that plots the power spectrum of Mars topography data """ # --- input data filename --- infile = os.path.join(os.path.dirname(__file__), '../../ExampleDataFiles/MarsTopo719.shape') coeffs, lmax = shio.shread(infile) # --- plot grid --- grid = expand.MakeGridDH(coeffs, csphase=-1) fig_map = plt.figure() plt.imshow(grid) # ---- compute spectrum ---- ls = np.arange(lmax + 1) pspectrum = spectralanalysis.spectrum(coeffs, unit='per_l') pdensity = spectralanalysis.spectrum(coeffs, unit='per_lm') # ---- plot spectrum ---- fig_spectrum, ax = plt.subplots(1, 1) ax.set_xscale('log') ax.set_yscale('log') ax.set_xlabel('degree l') ax.grid(True, which='both') ax.plot(ls[1:], pspectrum[1:], label='power per degree l') ax.plot(ls[1:], pdensity[1:], label='power per degree l and order m') ax.legend() fig_map.savefig('SHRtopography_mars.png') fig_spectrum.savefig('SHRspectrum_mars.png') print('mars topography and spectrum saved')
def example(): """ example that plots the power spectrum of Mars topography data """ # --- input data filename --- infile = os.path.join(os.path.dirname(__file__), '../../ExampleDataFiles/MarsTopo719.shape') coeffs, lmax = shio.shread(infile) # --- plot grid --- grid = expand.MakeGridDH(coeffs, csphase=-1) fig_map = plt.figure() plt.imshow(grid) # ---- compute spectrum ---- ls = np.arange(lmax + 1) pspectrum = spectralanalysis.spectrum(coeffs, unit='per_l') pdensity = spectralanalysis.spectrum(coeffs, unit='per_lm') # ---- plot spectrum ---- fig_spectrum, ax = plt.subplots(1, 1) ax.set_xscale('log') ax.set_yscale('log') ax.set_xlabel('degree l') ax.grid(True, which='both') ax.plot(ls[1:], pspectrum[1:], label='power per degree l') ax.plot(ls[1:], pdensity[1:], label='power per degree l and order m') ax.legend() fig_map.savefig('SHRtopography_mars.png') fig_spectrum.savefig('SHRspectrum_mars.png') print('mars topography and spectrum saved')
def TimingAccuracyDHC(sampling=1): # ---- input parameters ---- maxdeg = 2800 ls = np.arange(maxdeg + 1) beta = 1.5 print('Driscoll-Healy (complex), sampling =', sampling) # ---- create mask to filter out m<=l ---- mask = np.zeros((2, maxdeg + 1, maxdeg + 1), dtype=np.bool) mask[0, 0, 0] = True for l in ls: mask[:, l, :l + 1] = True mask[1, :, 0] = False # ---- create Gaussian powerlaw coefficients ---- print('creating {:d} random coefficients'.format(2 * (maxdeg + 1) * (maxdeg + 1))) np.random.seed(0) cilm = np.zeros((2, (maxdeg + 1), (maxdeg + 1)), dtype=np.complex) cilm.imag = np.random.normal(loc=0., scale=1., size=(2, maxdeg + 1, maxdeg + 1)) cilm.real = np.random.normal(loc=0., scale=1., size=(2, maxdeg + 1, maxdeg + 1)) old_power = spectralanalysis.spectrum(cilm) new_power = 1. / (1. + ls)**beta # initialize degrees > 0 to power-law cilm[:, :, :] *= np.sqrt(new_power / old_power)[None, :, None] cilm[~mask] = 0. # ---- time spherical harmonics transform for lmax set to increasing # ---- powers of 2 lmax = 2 print('lmax maxerror rms tinverse tforward') while lmax <= maxdeg: # trim coefficients to lmax cilm_trim = cilm[:, :lmax + 1, :lmax + 1] mask_trim = mask[:, :lmax + 1, :lmax + 1] # synthesis / inverse tstart = time.time() grid = expand.MakeGridDHC(cilm_trim, sampling=sampling) tend = time.time() tinverse = tend - tstart # analysis / forward tstart = time.time() cilm2_trim = expand.SHExpandDHC(grid, sampling=sampling) tend = time.time() tforward = tend - tstart # compute error err = np.abs(cilm_trim[mask_trim] - cilm2_trim[mask_trim]) / \ np.abs(cilm_trim[mask_trim]) maxerr = err.max() rmserr = np.mean(err**2) print('{:4d} {:1.2e} {:1.2e} {:1.1e}s {:1.1e}s'.format( lmax, maxerr, rmserr, tinverse, tforward)) lmax = lmax * 2
def TimingAccuracyDHC(sampling=1): # ---- input parameters ---- maxdeg = 2800 ls = np.arange(maxdeg + 1) beta = 1.5 print('Driscoll-Healy (complex), sampling =', sampling) # ---- create mask to filter out m<=l ---- mask = np.zeros((2, maxdeg + 1, maxdeg + 1), dtype=np.bool) mask[0, 0, 0] = True for l in ls: mask[:, l, :l + 1] = True mask[1, :, 0] = False # ---- create Gaussian powerlaw coefficients ---- print('creating {:d} random coefficients'.format(2 * (maxdeg + 1) * (maxdeg + 1))) np.random.seed(0) cilm = np.zeros((2, (maxdeg + 1), (maxdeg + 1)), dtype=np.complex) cilm.imag = np.random.normal(loc=0., scale=1., size=(2, maxdeg + 1, maxdeg + 1)) cilm.real = np.random.normal(loc=0., scale=1., size=(2, maxdeg + 1, maxdeg + 1)) old_power = spectralanalysis.spectrum(cilm) new_power = 1. / (1. + ls)**beta # initialize degrees > 0 to power-law cilm[:, :, :] *= np.sqrt(new_power / old_power)[None, :, None] cilm[~mask] = 0. # ---- time spherical harmonics transform for lmax set to increasing # ---- powers of 2 lmax = 2 print('lmax maxerror rms tinverse tforward') while lmax <= maxdeg: # trim coefficients to lmax cilm_trim = cilm[:, :lmax + 1, :lmax + 1] mask_trim = mask[:, :lmax + 1, :lmax + 1] # synthesis / inverse tstart = time.time() grid = expand.MakeGridDHC(cilm_trim, sampling=sampling) tend = time.time() tinverse = tend - tstart # analysis / forward tstart = time.time() cilm2_trim = expand.SHExpandDHC(grid, sampling=sampling) tend = time.time() tforward = tend - tstart # compute error err = np.abs(cilm_trim[mask_trim] - cilm2_trim[mask_trim]) / \ np.abs(cilm_trim[mask_trim]) maxerr = err.max() rmserr = np.mean(err**2) print('{:4d} {:1.2e} {:1.2e} {:1.1e}s {:1.1e}s'.format( lmax, maxerr, rmserr, tinverse, tforward)) lmax = lmax * 2
#plt.plot((1.0/np.pi)*lon,(1.0/np.pi)*lat,'.') #plt.xlim([-1.0,+1.0]) #plt.ylim([-0.5,+0.5]) #plt.show() levs = np.logspace(-8, 4, 200) print levs xx = np.linspace(-1.0 * np.pi, +1.0 * np.pi, nLon) yy = np.linspace(-0.5 * np.pi, +0.5 * np.pi, nLat) XX, YY = np.meshgrid(xx, yy) #plt.contourf(XX, YY, ke2, 100, locator=ticker.LogLocator()) plt.contourf(XX, YY, ke2, norm=LogNorm(), levels=levs) plt.colorbar(orientation='horizontal', ticks=[1.0e-8, 1.0e-6, 1.0e-4, 1.0e-2, 1.0e+0, 1.0e+2, 1.0e+4]) plt.savefig('kinetic_energy_galewsky_7days.png') plt.show() coeffs = SHExpandDH(ke2, sampling=2) power = spectrum(coeffs, unit='per_l') nl = coeffs.shape[1] / 2 plt.loglog(np.arange(nl), power[:nl]) #plt.loglog(np.arange(nl-6)+6, 1.0e+7*np.power(np.arange(nl-6)+6,-3.0)) plt.loglog( np.arange(nl - 6) + 6, 8.0e+6 * np.power(np.arange(nl - 6) + 6, -3.0)) #plt.loglog([50.0,50.0],[1.0e+5,1.0e-1]) plt.xlabel('$k$') plt.ylabel('$KE$') plt.savefig('ke_spectra_galewsky_7days.png') plt.show()
def TimingAccuracyGLQ(): # ---- input parameters ---- maxdeg = 2800 ls = np.arange(maxdeg + 1) beta = 1.5 print('Driscoll-Healy (real)') # ---- create mask to filter out m<=l ---- mask = np.zeros((2, maxdeg + 1, maxdeg + 1), dtype=np.bool) mask[0, 0, 0] = True for l in ls: mask[:, l, :l + 1] = True mask[1, :, 0] = False # ---- create Gaussian powerlaw coefficients ---- print('creating {:d} random coefficients'.format(2 * (maxdeg + 1) * (maxdeg + 1))) cilm = np.random.normal(loc=0., scale=1., size=(2, maxdeg + 1, maxdeg + 1)) cilm[:, 1:, :] *= np.sqrt((ls[1:]**beta) / (2. * ls[1:] + 1.))[None, :, None] old_power = spectralanalysis.spectrum(cilm) new_power = 1. / (1. + ls)**beta # initialize degrees > 0 to power-law cilm[:, :, :] *= np.sqrt(new_power / old_power)[None, :, None] cilm[~mask] = 0. # ---- time spherical harmonics transform for lmax set to increasing # ---- powers of 2 lmax = 2 print('lmax maxerror rms tprecompute tinverse tforward') while lmax <= maxdeg: # trim coefficients to lmax cilm_trim = cilm[:, :lmax + 1, :lmax + 1] mask_trim = mask[:, :lmax + 1, :lmax + 1] # precompute grid nodes and associated Legendre functions tstart = time.time() zeros, weights = expand.SHGLQ(lmax) tend = time.time() tprecompute = tend - tstart # synthesis / inverse tstart = time.time() grid = expand.MakeGridGLQ(cilm_trim, zeros) tend = time.time() tinverse = tend - tstart # analysis / forward tstart = time.time() cilm2_trim = expand.SHExpandGLQ(grid, weights, zeros) tend = time.time() tforward = tend - tstart # compute error err = np.abs(cilm_trim[mask_trim] - cilm2_trim[mask_trim]) / \ np.abs(cilm_trim[mask_trim]) maxerr = err.max() rmserr = np.mean(err**2) print('{:4d} {:1.2e} {:1.2e} {:1.1e}s {:1.1e}s ' '{:1.1e}s'.format(lmax, maxerr, rmserr, tprecompute, tinverse, tforward)) if maxerr > 100.: raise RuntimeError('Tests Failed. Maximum relative error = ', maxerr) lmax = lmax * 2
def TimingAccuracyGLQ(): # ---- input parameters ---- maxdeg = 2800 ls = np.arange(maxdeg + 1) beta = 1.5 print('Driscoll-Healy (real)') # ---- create mask to filter out m<=l ---- mask = np.zeros((2, maxdeg + 1, maxdeg + 1), dtype=np.bool) mask[0, 0, 0] = True for l in ls: mask[:, l, :l + 1] = True mask[1, :, 0] = False # ---- create Gaussian powerlaw coefficients ---- print('creating {:d} random coefficients'.format(2 * (maxdeg + 1) * (maxdeg + 1))) cilm = np.random.normal(loc=0., scale=1., size=(2, maxdeg + 1, maxdeg + 1)) cilm[:, 1:, :] *= np.sqrt((ls[1:]**beta) / (2. * ls[1:] + 1.))[None, :, None] old_power = spectralanalysis.spectrum(cilm) new_power = 1. / (1. + ls)**beta # initialize degrees > 0 to power-law cilm[:, :, :] *= np.sqrt(new_power / old_power)[None, :, None] cilm[~mask] = 0. # ---- time spherical harmonics transform for lmax set to increasing # ---- powers of 2 lmax = 2 print('lmax maxerror rms tprecompute tinverse tforward') while lmax <= maxdeg: # trim coefficients to lmax cilm_trim = cilm[:, :lmax + 1, :lmax + 1] mask_trim = mask[:, :lmax + 1, :lmax + 1] # precompute grid nodes and associated Legendre functions tstart = time.time() zeros, weights = expand.SHGLQ(lmax) tend = time.time() tprecompute = tend - tstart # synthesis / inverse tstart = time.time() grid = expand.MakeGridGLQ(cilm_trim, zeros) tend = time.time() tinverse = tend - tstart # analysis / forward tstart = time.time() cilm2_trim = expand.SHExpandGLQ(grid, weights, zeros) tend = time.time() tforward = tend - tstart # compute error err = np.abs(cilm_trim[mask_trim] - cilm2_trim[mask_trim]) / \ np.abs(cilm_trim[mask_trim]) maxerr = err.max() rmserr = np.mean(err**2) print('{:4d} {:1.2e} {:1.2e} {:1.1e}s {:1.1e}s ' '{:1.1e}s'.format(lmax, maxerr, rmserr, tprecompute, tinverse, tforward)) lmax = lmax * 2