Shows how to perform arbitrary rotations of spherical harmonic fields; optionally displays the fields as 3D plots.
Shows how to perform arbitrary rotations of spherical harmonic fields; optionally displays the fields as 3D plots.
    1 
    2from pylab import *
    3import shtns
    4 
    5Lmax = 13  
    6 
    7sh = shtns.sht(Lmax)         
    8rot = shtns.rotation(Lmax)   
    9 
   10 
   11
   12alpha = pi/2
   13beta = pi/2
   14gamma = 0
   15rot.set_angles_ZYZ(alpha, beta, gamma)   
   16
   17
   18
   19 
   20 
   22 
   23 
   24Qlm = sh.spec_array()   
   25Qlm[1] = 1.  
   26 
   27Rlm = rot.apply_real(Qlm)   
   28print('rotated by (%g,%g,%g) degrees (ZYZ)' % (rot.alpha*180/pi, rot.beta*180/pi, rot.gamma*180/pi))
   29 
   30sh.set_grid(nlat=64, nphi=128)
   31q = sh.synth(Qlm)
   32r = sh.synth(Rlm)
   33 
   34
   35
   36 
   37try:
   38        from matplotlib import cm, colors
   39        from mpl_toolkits.mplot3d import Axes3D
   40except:
   41        print("matplotlib required to plot.")
   42        exit()
   43 
   44 
   45phi = arange(sh.nphi)*2*pi/sh.nphi
   46theta = arccos(sh.cos_theta)
   47phi, theta = meshgrid(phi, theta)
   48
   49x = sin(theta) * cos(phi)
   50y = sin(theta) * sin(phi)
   51z = cos(theta)
   52 
   53
   54fmin, fmax = q.min(), q.max()
   55q = (q-fmin)/(fmax-fmin)
   56r = (r-fmin)/(fmax-fmin)
   57 
   58fig = plt.figure(figsize=plt.figaspect(0.5))
   59ax1 = fig.add_subplot(121, projection='3d')
   60ax1.plot_surface(x, y, z,  rstride=1, cstride=1, facecolors=cm.seismic(q))
   61ax1.set_title('original')
   62ax2 = fig.add_subplot(122, projection='3d')
   63ax2.plot_surface(x, y, z,  rstride=1, cstride=1, facecolors=cm.seismic(r))
   64ax2.set_title('rotated')
   65for ax in (ax1, ax2):
   66        ax.set_xlabel('X')
   67        ax.set_ylabel('Y')
   68        ax.set_zlabel('Z')
   69show()