Localization windows and spectral analysis

Now that we know how to work with spherical harmonic coefficient and grid classes, let's move on to the other major feature of pyshtools: localization windows and localied spectral analysis. pyshtools allows for localized spectral analyses using either spherical-cap localization windows or localization windows constructed from an arbitrary mask. The windows are constructed to solve the same optimization problem as the Slepian windows that are used in Cartesian multitaper spectral analysis problems. A single class, SHWindow, bundles the relevant shtools functions. Here, we will demonstrate how to construct optimal localization windows, and how to obtain localized spectral estimates for arbitrary regions on the sphere.

In [1]:
%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np
import pyshtools
In [2]:
pyshtools.utils.figstyle(rel_width=0.75)
%config InlineBackend.figure_format = 'retina'  # if you are not using a retina display, comment this line

Spherical cap localization windows

To demonstrate the localized spectral analysis routines, we will make use of a pathological data set. In particular, we will construct a function on the sphere whose power spectrum is different in the northern and southern hemispheres. We first create two grids using random coefficients that have power spectra following power laws with exponents of -2 and -3, up to and including degree 300:

In [3]:
ldata = 300
degrees = np.arange(ldata+1, dtype=float)
degrees[0] = np.inf

power_north = degrees**(-2)
power_south = degrees**(-3)

clm_north = pyshtools.SHCoeffs.from_random(power_north, seed=12345)
grid_north = clm_north.expand()

clm_south = pyshtools.SHCoeffs.from_random(power_south, seed=54321)
grid_south = clm_south.expand()

Next, create a new grid that contains data from the northern hemisphere of one grid and the southern hemisphere of the other. The SHGrid method from_array() returns a copy of the data, so you don't need to worry about modifying the original data set:

In [4]:
grid = pyshtools.SHGrid.from_array(grid_north.data)

nlat = grid_north.nlat
grid.data[nlat//2:nlat+1,:] = grid_south.data[nlat//2:nlat+1,:].copy()

clm = grid.expand()

Here, we made use of the grid attribute nlat, which is the number of rows of the gridded data, and we modified the raw gridded data for grid that is stored in the data class attribute.

Next, let's construct some spherical-cap localization windows. This is done using the constructor from_cap() that is associated with the SHWindow class. The other option, as we will demonstrate later, is from_mask() and takes as input a user defined mask. For spherical-cap windows, we need to specify both the angular radius of the cap and its spherical harmonic bandwidth, and for this example we will use an angular radius of 30 degrees and a bandwidth of 29.

In [5]:
lwin = 29
capwin = pyshtools.SHWindow.from_cap(theta=30., lwin=lwin)

This command calculates the spherical harmonic coefficients of the spherical cap windows along with their associated angular orders and concentration factors. If you ever need to access these directly, they are stored in the class attributes tapers, orders and eigenvalues. By default, theta should be provided in degrees, but this can be input in radians by specifying the optional parameter theta_degrees=False. If you are curious to see what these windows look like, you can plot them using the built in method plot_windows(). Let's plot the first 12 of them:

In [6]:
fig, axes = capwin.plot_windows(12, show=False)

As is seen, the windows are centered oven the north pole, 1 minus the concentration factor is noted above the window, and all of these windows are very well localized. If you want to know how many windows have concentration factors greater than a specified value, you can use the method number_concentrated():

In [7]:
capwin.number_concentrated(0.99)
Out[7]:
34

You can return an SHGrid class instance of an individual window using the method to_shgrid(), you can return the spherical harmonic coefficients of individual windows using to_array() and to_shcoeffs(), and you can also return as a numpy array the power spectra of one or more windows using the method spectra(). If you want to plot the power spectra of the windows, this can be done using the built in method plot_spectra():

In [8]:
fig, axes = capwin.plot_spectra(12, show=False)

In general, we will not perform localized spectral analyses at the north pole, so let's rotate the windows so that they are centered at 45 degrees north and 90 degrees east. This is easily accomplished using the rotate() method. To speed things up, we will rotate only the first 50 best localized windows, and not all 900 of them:

In [9]:
capwin.rotate(clat=45., clon=90., nwinrot=50)

fig, axes = capwin.plot_windows(12, show=False)

In rotating the windows, the spherical harmonic coefficients of the initial tapers centered at the north pole are not modified, but a second set of rotated coefficients are created and stored in the attribute coeffs. If you ever forget how you constructed your windows, you can always use the info() method to print out all the metadata:

In [10]:
capwin.info()
kind = 'cap'
theta = 30.000000 degrees
lwin = 29
nwin = 900
nwinrot = 50
shannon = 60.288568
area (radians) = 8.417872e-01
clat = 45.000000 degrees
clon = 90.000000 degrees
dj_matrix is stored
Taper weights are not set

Next, let's calculate the localized power spectrum at two different locations, one in the northern hemisphere and the other in the southern hemisphere. We will use all 34 windows that have concentration factors greater than 0.99:

In [11]:
k = capwin.number_concentrated(0.99)

mtse_north, sd_north = capwin.multitaper_spectrum(clm, k)
mtse_south, sd_south = capwin.multitaper_spectrum(clm, k, clat=-55., clon=90.)

If you don't provide the central latitude and longitude of the analysis with the parameters clat and clon, the coefficients that were created previously using the rotate() method will be employed. If these coefficients are not available, the analysis will be performed at the north pole.

multitaper_spectrum returns a power spectrum whose maximum degree is ldata - lwin. This is because localized degrees beyond this limit depend upon coefficients of the global data beyond ldata, and these are not known. If the global function was truly zero beyond ldata, you could simply construct the spherical harmonic coefficients of clm to have zeros beyond that degree.

When a function is multiplied by a window, the power spectrum of the localized function is modified. For the case of stationary and random data, the expectation of this biasing effect can be estimated using two methods. First, we could calculate the "coupling matrix" using the method coupling_matrix(). The expectation of the output windowed power spectrum is then simply the multiplication of this matrix by the input global spectrum. If you are interested, you can plot the coupling matrix using the built in method plot_coupling_matrix():

In [12]:
fig, ax = capwin.plot_coupling_matrix(ldata, k=k, show=False)