SHTns  2.6.5
SHT_example.py

A Python example using NumPy and SHTns that performs backward and forward Spherical Harmonic Transforms.

1 #!/usr/bin/python
2 #
3 # Copyright (c) 2010-2015 Centre National de la Recherche Scientifique.
4 # written by Nathanael Schaeffer (CNRS, ISTerre, Grenoble, France).
5 #
6 # nathanael.schaeffer@ujf-grenoble.fr
7 #
8 # This software is governed by the CeCILL license under French law and
9 # abiding by the rules of distribution of free software. You can use,
10 # modify and/or redistribute the software under the terms of the CeCILL
11 # license as circulated by CEA, CNRS and INRIA at the following URL
12 # "http://www.cecill.info".
13 #
14 # The fact that you are presently reading this means that you have had
15 # knowledge of the CeCILL license and that you accept its terms.
16 #
17 
18 ###################################
19 # SHTns Python interface example #
20 ###################################
21 
22 import numpy # numpy for arrays
23 import shtns # shtns module
24  # compiled and installe using ./configure --enable-python && make && make install
25 
26 lmax = 7 # maximum degree of spherical harmonic representation.
27 mmax = 3 # maximum order of spherical harmonic representation.
28 
29 sh = shtns.sht(lmax, mmax) # create sht object with given lmax and mmax (orthonormalized)
30 # sh = shtns.sht(lmax, mmax, mres=2, norm=shtns.sht_schmidt | shtns.SHT_NO_CS_PHASE) # use schmidt semi-normalized harmonics
31 
32 nlat, nphi = sh.set_grid() # build default grid (gauss grid, phi-contiguous)
33 print(sh.nlat, sh.nphi) # displays the latitudinal and longitudinal grid sizes.
34 
35 cost = sh.cos_theta # latitudinal coordinates of the grid as cos(theta)
36 el = sh.l # array of size sh.nlm giving the spherical harmonic degree l for any sh coefficient
37 l2 = el*(el+1) # array l(l+1) that is useful for computing laplacian
38 
39 ### use advanced options to create a regular grid, theta-contiguous, and with south-pole comming first.
40 # nlat = lmax*2
41 # nphi = mmax*3
42 # grid_typ = shtns.sht_reg_fast | shtns.SHT_THETA_CONTIGUOUS | shtns.SHT_SOUTH_POLE_FIRST
43 # polar_opt_threshold = 1.0e-10
44 # sh.set_grid(nlat, nphi, flags=grid_typ, polar_opt=polar_opt_threshold)
45 
46 ylm = sh.spec_array() # a spherical harmonic spectral array, same as numpy.zeros(sh.nlm, dtype=complex)
47 vr = sh.spat_array() # a spatial array, same as numpy.zeros(sh.spat_shape)
48 
49 ylm[sh.idx(1,0)] = 1.0 # set sh coefficient l=1, m=0 to value 1
50 
51 print(ylm[sh.l == 1]) # print all l=1 coefficients
52 print(ylm[sh.m == 1]) # print all m=1 coefficients
53 
54 ylm = ylm * l2 # multiply by l(l+1)
55 
56 y = sh.synth(ylm) # transform sh description ylm into spatial representation y (scalar transform)
57 
58 print(y) # display spatial field
59 
60 i_theta = sh.nlat/2
61 i_phi = 1
62 print(y[i_theta, i_phi]) # spatial element of coordinate i_theta, i_phi
63 
64 
65 zlm = sh.analys(y) # transform the spatial field back to spectral
66 print(zlm)
67 
68 ### compute gradients :
69 dy_dt, dy_dp = sh.synth_grad(ylm) # compute gradients of ylm on a spatial grid.
70 
71 ### vector transforms :
72 #slm, tlm = sh.analys(v_theta,v_phi) # with two arguments, sh.analys() performs a 2D vector transform (spherical coordinates without radial)
73 #qlm, slm, tlm = sh.analys(v_r,v_theta,v_phi) # with three arguments, sh.alanys() performs a 3D vector tansform (spherical coordinates)
74 #
75 #v_theta, v_phi = sh.synth(slm, tlm) # same hold for synthesis, using spheroidal/toroidal vector spherical harmonics.
76 #v_r = sh.synth(qlm)
77