Beispiel #1
0
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
Beispiel #2
0
def example():
    print('---- SHrtoc example ----')
    # --- input data filename ---
    infile = '../../ExampleDataFiles/MarsTopo719.shape'
    coeffs1, lmax = shio.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 = shio.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))[np.newaxis, :])

    # --- compute and plot grid ---
    grid1 = expand.MakeGridDH(coeffs1, lmax, csphase=-1)
    grid2 = expand.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')
Beispiel #3
0
 def spharm_to_surface(self, lmax=None):
     """
     Inverse transform the SPHARM spectrum to surface using the given number of components.
     
     Parameters
     ----------
     lmax : int, optional
         The maximum spherical harmonic degree to be used in the inverse transform.
         If None, all degrees will be used.
         Default is None.
         
     Returns
     -------
     ndarray : reconstructed surface grid.
     """
     grid = shtools.MakeGridDHC(self.harmonics_shtools, lmax_calc=lmax).real
     return grid