def sharpness(t): #h = cubicSymmetry(t) h = numpy.array(t) mass = getMass(h) h /= mass realSpace = pyshtools.MakeGridDH(h, sampling = 2) #print numpy.sum((realSpace * numpy.log2(realSpace)).flatten()) #if numpy.any(realSpace < 0): # print realSpace[numpy.where(realSpace < 0)] #realSpaces.append(realSpace) #plt.imshow(realSpace) #plt.colorbar() #plt.show() # return mean# / std#getMass(pyshtools.SHExpandDH(1.0 / (1 + numpy.exp(-(realSpace - thresh) / realSpace)), sampling = 2)) / (4 * numpy.pi) fm = getEnergies(h) fm /= max(fm) #plt.plot(fm) #plt.xlabel('Harmonic number L') #plt.ylabel('L2 norm across all M values for a given L (this is how you get rotation invariance)') #plt.title('Rotation invariant feature detectors for ideal microstructures') #plt.show() print '75', get75percentile(fm[1:]), sum(fm[2::2]) / sum(fm[1:]), fm[0] / sum(fm[1:]) inf = -4 * numpy.pi * sum(pyshtools.SHCrossPowerSpectrum(h, pyshtools.SHExpandDH(numpy.log2(realSpace + 1e-10), sampling = 2))) return inf
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, 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('SHRtopography_mars.png') fig_spectrum.savefig('SHRspectrum_mars.png') print 'mars topography and spectrum saved'
def example(): print '---- SHrtoc example ----' #--- input data filename --- infile = '../../ExampleDataFiles/MarsTopo719.shape' coeffs1, lmax = shtools.SHRead(infile, 719) coeffs1 = coeffs1[:, :lmax + 1, :lmax + 1] #--- convert to complex coefficients, fill negative order coefficients --- coeffs2 = np.empty((2, lmax + 1, lmax + 1), dtype=np.complex) coeffs2_buf = shtools.SHrtoc(coeffs1, convention=1, switchcs=0) coeffs2[0, :, :].real = coeffs2_buf[0, :, :] coeffs2[0, :, :].imag = coeffs2_buf[1, :, :] coeffs2[1] = coeffs2[0].conjugate() * ( (-1)**np.arange(lmax + 1)).reshape(1, 1, lmax + 1) #--- compute and plot grid --- grid1 = shtools.MakeGridDH(coeffs1, lmax, csphase=-1) grid2 = shtools.MakeGridDHC(coeffs2, lmax, csphase=-1) gridmin = min(grid1.min(), grid2.real.min()) gridmax = max(grid1.max(), grid2.real.max()) norm = plt.Normalize(gridmin, gridmax) fig, axes = plt.subplots(1, 2) im1 = axes[0].imshow(grid1, norm=norm) axes[0].set_title('from real coefficients') im2 = axes[1].imshow(grid2.real, norm=norm) axes[1].set_title('from complex coefficients') fig.tight_layout(pad=1) fig.savefig('topography_mars.png') print 'mars topography plotted and saved to file'
def TimingAccuracyDH(): #---- input parameters ---- maxdeg = 2800 ls = np.arange(maxdeg + 1) sampling = 1 beta = -1.5 #---- create mask to filter out m<=l ---- mask = np.zeros(2 * (maxdeg + 1) * (maxdeg + 1), dtype=np.bool).reshape(2, maxdeg + 1, maxdeg + 1) 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)) random_numbers = np.random.normal(loc=0., scale=1., size=2 * (maxdeg + 1) * (maxdeg + 1)) cilm = random_numbers.reshape(2, maxdeg + 1, maxdeg + 1) cilm[:, 1:, :] *= np.sqrt((ls[1:]**beta) / (2. * ls[1:] + 1.))[None, :, None] #---- 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 = ((cilm_trim[mask_trim] - cilm2_trim[mask_trim]) / cilm_trim[mask_trim])**2 maxerr = np.sqrt(err.max()) rmserr = np.mean(err) print '{:4d} {:1.2e} {:1.2e} {:1.1e}s {:1.1e}s'.\ format(lmax, maxerr, rmserr, tinverse, tforward) lmax = lmax * 2
def TestCrustalThickness(): """ Example routine that calculates the crustal thickness of Mars """ delta_max = 5.0 nmax = 6 degmax = 50 lmax = 200 rho_c = 2900.0 rho_m = 3500.0 filter_type = 0 half = 0 gravfile = '../../ExampleDataFiles/jgmro_110b_sha.tab' pot, lmaxp, header = shtools.SHReadH(gravfile, degmax, 2) gm = header[1] * 1.e9 mass = gm / shtools.constant.grav_constant r_grav = header[0] * 1.e3 print r_grav, gm, mass, lmaxp topofile = '../../ExampleDataFiles/MarsTopo719.shape' hlm, lmaxt = shtools.SHRead(topofile, 719) r0 = hlm[0, 0, 0] d = r0 - 45.217409924028445e3 print r0, lmaxt for l in range(2, lmaxp + 1): pot[:, l, :l + 1] = pot[:, l, :l + 1] * (r_grav / r0)**l topo_grid = shtools.MakeGridDH(hlm, lmax=lmax, sampling=2, lmax_calc=degmax) print "Maximum radius (km) = ", topo_grid.max() / 1.e3 print "Minimum radius (km) = ", topo_grid.min() / 1.e3 bc, r0 = shtools.CilmPlusDH(topo_grid, nmax, mass, rho_c, lmax=degmax) ba = pot - bc moho_c = np.zeros([2, degmax + 1, degmax + 1], dtype=float) moho_c[0, 0, 0] = d for l in range(1, degmax + 1): if filter_type == 0: moho_c[:, l, :l + 1] = ba[:, l, :l + 1] * mass * (2 * l + 1) * ((r0 / d)**l) \ / (4.0 * np.pi * (rho_m - rho_c) * d**2) elif filter_type == 1: moho_c[:, l, :l + 1] = DownContFilterMA(l, half, r0, d) * ba[:, l, :l + 1] * mass * (2 * l + 1) * \ ((r0 / d)**l) / (4.0 * np.pi * (rho_m - rho_c) * d**2) else: moho_c[:, l, :l + 1] = DownContFilterMC(l, half, r0, d) * ba[:, l, :l + 1] * mass * (2 * l + 1) *\ ((r0 / d)**l) / (4.0 * np.pi * (rho_m - rho_c) * d**2) moho_grid3 = shtools.MakeGridDH(moho_c, lmax=lmax, sampling=2, lmax_calc=degmax) print "Maximum Crustal thickness (km) = ", (topo_grid - moho_grid3).max() / 1.e3 print "Minimum Crustal thickness (km) = ", (topo_grid - moho_grid3).min() / 1.e3 moho_c = shtools.BAtoHilmDH(ba, moho_grid3, nmax, mass, r0, (rho_m - rho_c), lmax=lmax, filter_type=filter_type, filter_deg=half, lmax_calc=degmax) moho_grid2 = shtools.MakeGridDH(moho_c, lmax=lmax, sampling=2, lmax_calc=degmax) print "Delta (km) = ", abs(moho_grid3 - moho_grid2).max() / 1.e3 temp_grid = topo_grid - moho_grid2 print "Maximum Crustal thickness (km) = ", temp_grid.max() / 1.e3 print "Minimum Crustal thickness (km) = ", temp_grid.min() / 1.e3 iter = 0 delta = 1.0e9 while delta > delta_max: iter += 1 print "Iteration ", iter moho_grid = (moho_grid2 + moho_grid3) / 2.0 print "Delta (km) = ", abs(moho_grid - moho_grid2).max() / 1.e3 temp_grid = topo_grid - moho_grid print "Maximum Crustal thickness (km) = ", temp_grid.max() / 1.e3 print "Minimum Crustal thickness (km) = ", temp_grid.min() / 1.e3 moho_grid3 = moho_grid2 moho_grid2 = moho_grid iter += 1 print "Iteration ", iter moho_c = shtools.BAtoHilmDH(ba, moho_grid2, nmax, mass, r0, rho_m - rho_c, lmax=lmax, filter_type=filter_type, filter_deg=half, lmax_calc=degmax) moho_grid = shtools.MakeGridDH(moho_c, lmax=lmax, sampling=2, lmax_calc=degmax) delta = abs(moho_grid - moho_grid2).max() print "Delta (km) = ", delta / 1.e3 temp_grid = topo_grid - moho_grid print "Maximum Crustal thickness (km) = ", temp_grid.max() / 1.e3 print "Minimum Crustal thickness (km) = ", temp_grid.min() / 1.e3 moho_grid3 = moho_grid2 moho_grid2 = moho_grid if temp_grid.max() > 100.e3: print "Not converging" exit(1) fig_map = plt.figure() plt.imshow(temp_grid) fig_map.savefig('Mars_CrustalThicknes.png')
angles = ((0.0, 0.0), (0.0, 270.0), (0.0, 180.0), (0.0, 90.0), (90.0, 0.0), (90.0, 270.0), (90.0, 180.0), (90.0, 90.0), (180.0, 0.0), (180.0, 270.0), (180.0, 180.0), (180.0, 90.0)) totalSPH = numpy.zeros(o.shape) for thetaphi in angles: theta, phi = numpy.radians(thetaphi) totalSPH += pyshtools.SHRotateRealCoef(o, (phi, theta, 0.0), dj) return totalSPH def getMass(output): return 4 * numpy.pi * sum(pyshtools.SHCrossPowerSpectrum(output, pyshtools.SHExpandDH(numpy.ones(histogram.shape), sampling = 2))) #print thetas.shape #%% plt.imshow(pyshtools.MakeGridDH(cubicSymmetry(output), sampling = 2)) #%% if True: #for g in range(len(stacks[:])): #histogram = numpy.array(stacks[g]) output = pyshtools.SHExpandDH(histogram, sampling = 2) #if True: # output = numpy.array(lookup[0, 0]) output /= getMass(output) histogram = pyshtools.MakeGridDH(output, sampling = 2) #plt.imshow(histogram) #plt.colorbar()
def reconstruct_grid(C): return shtools.MakeGridDH(C, csphase=-1)
rpm_2D = np.zeros(shp, np.float64) rpm_2D[:, :] = rpm_1D[:, np.newaxis] return rpm_2D #defining our colatitudes co_lats = np.linspace(0., np.pi, num=T.shape[0], endpoint=True) #defining the coordinates of our shell of radius r_i rpm_2D = -make_Rpm(R_e=6371000., r_i=r_i, co_lats=co_lats, shp=T.shape) #expanding to find SH coefficients for shell of radius r_i rpm_2D_SH = sht.SHExpandDH(rpm_2D) kernel = rpm_2D_SH[0, :, 0] convolved = kernel[np.newaxis, :, np.newaxis] * T_SH convolved[:, 1, 1] = 0. #grid of gravitational source values on shell of radius r_i tomo_r1 = sht.MakeGridDH(convolved, sampling=2, csphase=1) #defining filter to be applied before backprojection ramp_filter = np.linspace(0., 1., num=convolved.shape[1], endpoint=True) cone_filter_2d = np.sqrt( np.outer(ramp_filter * ramp_filter, ramp_filter * ramp_filter)) #convolution of coefficients and shell after filterning filtered_convolved = cone_filter_2d[np.newaxis, :, :] * convolved tomo_r1_filtered = sht.MakeGridDH(filtered_convolved, sampling=2, csphase=1)