def example(): """ example that plots the power spectrum of Mars topography data """ # --- input data filename --- infile = '../../ExampleDataFiles/MarsTopo719.shape' coeffs, lmax = shtools.SHRead(infile, 719) lmax = coeffs.shape[1] - 1 # --- plot grid --- grid = shtools.MakeGridDH(coeffs, lmax, csphase=-1) fig_map = plt.figure() plt.imshow(grid) # ---- compute spectrum ---- ls = np.arange(lmax + 1) pspectrum = shtools.SHPowerSpectrum(coeffs) pdensity = shtools.SHPowerSpectrumDensity(coeffs) # ---- 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('SHCtopography_mars.png') fig_spectrum.savefig('SHCspectrum_mars.png') print('mars topography and spectrum saved')
def test_RealSpectralAnalysis(): # ---- input parameters ---- lmax = 5 ls = np.arange(lmax + 1) mask = np.zeros((2, lmax + 1, lmax + 1), dtype=np.bool) for l in np.arange(lmax + 1): mask[:, l, :l + 1] = True mask[1, :, 0] = False print('\n---- testing SHPower/DensityL, SHPowerSpectrum/Density ----') print('generating normal distributed coefficients with variance 1...') coeffs1 = np.random.normal(size=(2, lmax + 1, lmax + 1)) coeffs1[np.invert(mask)] = 0. spec1 = np.array([shtools.SHPowerL(coeffs1, l) for l in ls]) spec2 = shtools.SHPowerSpectrum(coeffs1) print('tot power computed with SHPowerL={:2.2f}'.format(np.sum(spec1))) print('tot power computed with SHPowerSpectrum={:2.2f}'.format( np.sum(spec2))) spec1 = np.array([shtools.SHPowerDensityL(coeffs1, l) for l in ls]) spec2 = shtools.SHPowerSpectrumDensity(coeffs1) print('tot power computed with SHPowerDensityL={:2.2f}'.format( np.sum(spec1 * (2 * ls + 1)))) print('tot power computed with SHPowerSpectrumDensity={:2.2f}'.format( np.sum(spec2 * (2 * ls + 1)))) print('\n---- testing SHCrossCrossPower/DensityL, ' + 'SHCrossCrossPowerSpectrum/Density ----') print('generating two sets of normal distributed coefficients ' + 'with variance 1...') coeffs2 = np.random.normal(size=(2, lmax + 1, lmax + 1)) coeffs2[np.invert(mask)] = 0. spec1 = np.array([shtools.SHCrossPowerL(coeffs1, coeffs2, l) for l in ls]) spec2 = shtools.SHCrossPowerSpectrum(coeffs1, coeffs2) print('tot cpower computed with SHCrossPowerL={:2.2f}'.format( np.sum(spec1))) print('tot cpower computed with SHCrossPowerSpectrum={:2.2f}'.format( np.sum(spec2))) spec1 = np.array( [shtools.SHCrossPowerDensityL(coeffs1, coeffs2, l) for l in ls]) spec2 = shtools.SHCrossPowerSpectrumDensity(coeffs1, coeffs2) print('tot cpower computed with SHCrossPowerDensityL={:2.2f}'.format( np.sum(spec1 * (2 * ls + 1)))) print( 'tot cpower computed with SHCrossPowerSpectrumDensity={:2.2f}'.format( np.sum(spec2 * (2 * ls + 1)))) print('\n---- testing SHAdmitCorr and SHConfidence ----') admit, dadmit, corr = shtools.SHAdmitCorr(coeffs1, coeffs2) confidence = np.array([shtools.SHConfidence(l, corr[l]) for l in ls]) print('admittance:', admit) print('admittance error:', dadmit) print('correlation:', corr) print('confidence:', confidence)
def TimingAccuracyDH(sampling=1): # ---- input parameters ---- maxdeg = 2800 ls = np.arange(maxdeg + 1) beta = 1.5 print('Driscoll-Healy (real) 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.random.normal(loc=0., scale=1., size=(2, maxdeg + 1, maxdeg + 1)) old_power = shtools.SHPowerSpectrum(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 = shtools.MakeGridDH(cilm_trim, sampling=sampling) tend = time.time() tinverse = tend - tstart # analysis / forward tstart = time.time() cilm2_trim = shtools.SHExpandDH(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