Visualization for Spherical Harmonics

This example transforms a gridded field to packed triangular spectral coefficients, visualizes the spectral coefficients, and transforms the field back to latitude-longitude space. The standard backend and Rust-backed functions are compared with the same input data.

import numpy as np
import matplotlib.pyplot as plt
import easyclimate as ecl

Open a tutorial specific-humidity field. The spectral transform expects latitude and longitude dimensions on a regular or Gaussian global grid.

<xarray.DataArray 'shum' (time: 5, level: 8, lat: 73, lon: 144)> Size: 2MB
[420480 values with dtype=float32]
Coordinates:
  * time     (time) datetime64[ns] 40B 2022-01-01 2022-01-02 ... 2022-01-05
  * level    (level) float32 32B 1e+03 925.0 850.0 700.0 600.0 500.0 400.0 300.0
  * lat      (lat) float32 292B 90.0 87.5 85.0 82.5 ... -82.5 -85.0 -87.5 -90.0
  * lon      (lon) float32 576B 0.0 2.5 5.0 7.5 10.0 ... 350.0 352.5 355.0 357.5
Attributes:
    long_name:     mean Daily specific humidity
    units:         kg/kg
    precision:     6
    GRIB_id:       51
    GRIB_name:     SPFH
    var_desc:      Specific humidity
    level_desc:    Pressure Levels
    statistic:     Mean
    parent_stat:   Individual Obs
    dataset:       NCEP Reanalysis Daily Averages
    actual_range:  [0.        0.0463525]


Transform grid-space data to packed triangular spherical-harmonic coefficients. The output has a spec_dim dimension whose length is determined by the triangular truncation.

q_spec_data = ecl.spec.transfer_grid2spectral_transform(
    q_grid_data,
    grid_data_type = "regular"
)
q_spec_rs_data = ecl.spec.transfer_grid2spectral_transform_rs(
    q_grid_data,
    grid_data_type = "regular"
)
q_spec_data
<xarray.DataArray 'shum' (time: 5, level: 8, spec_dim: 2701)> Size: 864kB
array([[[ 1.43180564e-02+0.0000000e+00j, -1.68855651e-03+0.0000000e+00j,
         -7.43684545e-03+0.0000000e+00j, ...,
         -1.24628752e-06-7.9038671e-07j,  0.00000000e+00+0.0000000e+00j,
         -2.28514841e-06+0.0000000e+00j],
        [ 1.10343983e-02+0.0000000e+00j, -1.21083658e-03+0.0000000e+00j,
         -5.63617935e-03+0.0000000e+00j, ...,
         -2.66562847e-06-2.3535765e-06j,  0.00000000e+00+0.0000000e+00j,
         -5.35154754e-07+0.0000000e+00j],
        [ 7.96954799e-03+0.0000000e+00j, -8.13533959e-04+0.0000000e+00j,
         -4.06459998e-03+0.0000000e+00j, ...,
         -3.43809256e-06-2.0546768e-06j,  0.00000000e+00+0.0000000e+00j,
         -2.73745968e-06+0.0000000e+00j],
        ...,
        [ 1.59483729e-03+0.0000000e+00j, -2.02749274e-04+0.0000000e+00j,
         -7.34775560e-04+0.0000000e+00j, ...,
          5.86281374e-07+3.9806136e-07j,  0.00000000e+00+0.0000000e+00j,
         -3.23434790e-07+0.0000000e+00j],
        [ 8.69618554e-04+0.0000000e+00j, -1.16985939e-04+0.0000000e+00j,
         -4.49327781e-04+0.0000000e+00j, ...,
          3.12762836e-07-7.4018331e-08j,  0.00000000e+00+0.0000000e+00j,
...
         -1.49719824e-06-4.9508944e-06j,  0.00000000e+00+0.0000000e+00j,
         -1.24850033e-07+0.0000000e+00j],
        [ 7.95069057e-03+0.0000000e+00j, -9.10700648e-04+0.0000000e+00j,
         -4.07394581e-03+0.0000000e+00j, ...,
         -1.75999412e-06-3.1492207e-06j,  0.00000000e+00+0.0000000e+00j,
         -9.99167469e-07+0.0000000e+00j],
        ...,
        [ 1.64485443e-03+0.0000000e+00j, -2.15131455e-04+0.0000000e+00j,
         -7.26255530e-04+0.0000000e+00j, ...,
          3.99673922e-07-7.7032553e-07j,  0.00000000e+00+0.0000000e+00j,
          3.52751016e-07+0.0000000e+00j],
        [ 8.87441682e-04+0.0000000e+00j, -1.20008503e-04+0.0000000e+00j,
         -4.50855616e-04+0.0000000e+00j, ...,
          1.72734730e-07+3.6744453e-08j,  0.00000000e+00+0.0000000e+00j,
         -1.93136472e-07+0.0000000e+00j],
        [ 3.74470750e-04+0.0000000e+00j, -5.92384858e-05+0.0000000e+00j,
         -2.20764821e-04+0.0000000e+00j, ...,
         -7.83763880e-08+5.9142561e-08j,  0.00000000e+00+0.0000000e+00j,
         -9.26337265e-08+0.0000000e+00j]]],
      shape=(5, 8, 2701), dtype=complex64)
Coordinates:
  * time      (time) datetime64[ns] 40B 2022-01-01 2022-01-02 ... 2022-01-05
  * level     (level) float32 32B 1e+03 925.0 850.0 700.0 ... 500.0 400.0 300.0
  * spec_dim  (spec_dim) int64 22kB 0 1 2 3 4 5 ... 2696 2697 2698 2699 2700
Attributes: (12/14)
    long_name:     mean Daily specific humidity
    units:         kg/kg
    precision:     6
    GRIB_id:       51
    GRIB_name:     SPFH
    var_desc:      Specific humidity
    ...            ...
    parent_stat:   Individual Obs
    dataset:       NCEP Reanalysis Daily Averages
    actual_range:  [0.        0.0463525]
    Description:   Spherical harmonic coefficients
    Truncation:    Triangular truncation at wavenumber 72
    Order:         Complex coefficients in triangular order


The plotting helpers expect a one-dimensional spectral coefficient array, so select one time and level before visualizing the spectrum.

<xarray.DataArray 'shum' (spec_dim: 2701)> Size: 22kB
array([ 7.9695480e-03+0.0000000e+00j, -8.1353396e-04+0.0000000e+00j,
       -4.0646000e-03+0.0000000e+00j, ..., -3.4380926e-06-2.0546768e-06j,
        0.0000000e+00+0.0000000e+00j, -2.7374597e-06+0.0000000e+00j],
      shape=(2701,), dtype=complex64)
Coordinates:
  * spec_dim  (spec_dim) int64 22kB 0 1 2 3 4 5 ... 2696 2697 2698 2699 2700
    time      datetime64[ns] 8B 2022-01-01
    level     float32 4B 850.0
Attributes: (12/14)
    long_name:     mean Daily specific humidity
    units:         kg/kg
    precision:     6
    GRIB_id:       51
    GRIB_name:     SPFH
    var_desc:      Specific humidity
    ...            ...
    parent_stat:   Individual Obs
    dataset:       NCEP Reanalysis Daily Averages
    actual_range:  [0.        0.0463525]
    Description:   Spherical harmonic coefficients
    Truncation:    Triangular truncation at wavenumber 72
    Order:         Complex coefficients in triangular order


Plot the total-wavenumber power spectrum, computed as the sum of squared coefficient magnitude over zonal wavenumber.

ecl.spec.plot_spectral_power(q_spec_data_sample)
shum power
(<Figure size 600x400 with 1 Axes>, <Axes: title={'center': 'shum power'}, xlabel='Total wavenumber n', ylabel='$\\sum_m |\\hat{a}_{n,m}|^2$'>)

Plot the packed coefficients as an m by n triangular matrix. By default this displays the base-10 logarithm of coefficient magnitude.

ecl.spec.plot_spectral_matrix(q_spec_data_sample)
shum coefficients: logabs
<Axes: title={'center': 'shum coefficients: logabs'}, xlabel='Total wavenumber n', ylabel='Zonal wavenumber m'>

Plot the real and imaginary parts of the complex spectral coefficients in two side-by-side panels.

ecl.spec.plot_spectral_real_imag(q_spec_data_sample)
shum coefficients: real, shum coefficients: imag
(<Figure size 1100x400 with 4 Axes>, array([<Axes: title={'center': 'shum coefficients: real'}, xlabel='Total wavenumber n', ylabel='Zonal wavenumber m'>,
       <Axes: title={'center': 'shum coefficients: imag'}, xlabel='Total wavenumber n', ylabel='Zonal wavenumber m'>],
      dtype=object))

Transform the spectral coefficients back to grid space. The output grid size and grid type are supplied explicitly, matching the original regular grid.

q_grid_spectrans = ecl.spec.transfer_spectral_transform2grid(
    q_spec_data,
    nlon = 144, nlat = 73,
    grid_data_type = "regular"
)
q_grid_rs_spectrans = ecl.spec.transfer_spectral_transform2grid_rs(
    q_spec_rs_data,
    nlon = 144, nlat = 73,
    grid_data_type = "regular"
)
q_grid_spectrans
<xarray.DataArray 'shum' (time: 5, level: 8, lat: 73, lon: 144)> Size: 2MB
array([[[[ 3.75000411e-04,  3.75000527e-04,  3.75000411e-04, ...,
           3.75000411e-04,  3.75000527e-04,  3.75000527e-04],
         [ 1.54082547e-04,  1.49952015e-04,  1.46468054e-04, ...,
           1.70220621e-04,  1.64234196e-04,  1.58848008e-04],
         [ 2.86632916e-04,  3.15813697e-04,  3.41449748e-04, ...,
           1.95693341e-04,  2.24731863e-04,  2.55653518e-04],
         ...,
         [ 2.83719017e-03,  2.96073779e-03,  3.07870004e-03, ...,
           2.48415396e-03,  2.59390520e-03,  2.71320017e-03],
         [ 2.84030894e-03,  2.85256119e-03,  2.86575872e-03, ...,
           2.81347451e-03,  2.82038096e-03,  2.82944483e-03],
         [ 2.44500209e-03,  2.44500209e-03,  2.44500209e-03, ...,
           2.44500209e-03,  2.44500209e-03,  2.44500209e-03]],

        [[ 6.92499569e-04,  6.92499394e-04,  6.92499510e-04, ...,
           6.92499336e-04,  6.92499336e-04,  6.92499452e-04],
         [ 6.55074255e-04,  6.54641888e-04,  6.53357769e-04, ...,
           6.51238486e-04,  6.53358991e-04,  6.54644216e-04],
         [ 9.31357441e-04,  9.56826727e-04,  9.75402887e-04, ...,
           8.25621828e-04,  8.64319853e-04,  9.00072046e-04],
...
           4.24807877e-05,  4.16826260e-05,  4.25300241e-05],
         [ 2.81359080e-05,  2.90355711e-05,  3.01476357e-05, ...,
           2.67630421e-05,  2.70009114e-05,  2.74569575e-05],
         [ 4.75002525e-06,  4.75001434e-06,  4.75001070e-06, ...,
           4.75001798e-06,  4.75001070e-06,  4.75002889e-06]],

        [[ 3.02499975e-05,  3.02499939e-05,  3.02500030e-05, ...,
           3.02500302e-05,  3.02500193e-05,  3.02500084e-05],
         [ 2.97073293e-05,  2.89857180e-05,  2.82413821e-05, ...,
           3.17230515e-05,  3.10768955e-05,  3.04047353e-05],
         [ 1.77943984e-05,  1.81317737e-05,  1.82613803e-05, ...,
           1.59907086e-05,  1.66669488e-05,  1.72887085e-05],
         ...,
         [ 3.89103025e-06,  5.13460873e-06,  6.50465290e-06, ...,
           1.33785625e-06,  1.96142264e-06,  2.82108567e-06],
         [-8.32538717e-07, -1.10457586e-06, -1.36704239e-06, ...,
           4.55929694e-08, -2.57845386e-07, -5.50461664e-07],
         [ 7.73070497e-11,  6.63931132e-11,  6.18456397e-11, ...,
           7.09405867e-11,  7.54880602e-11,  7.82165444e-11]]]],
      shape=(5, 8, 73, 144), dtype=float32)
Coordinates:
  * time     (time) datetime64[ns] 40B 2022-01-01 2022-01-02 ... 2022-01-05
  * level    (level) float32 32B 1e+03 925.0 850.0 700.0 600.0 500.0 400.0 300.0
  * lat      (lat) float64 584B 90.0 87.5 85.0 82.5 ... -82.5 -85.0 -87.5 -90.0
  * lon      (lon) float64 1kB 0.0 2.5 5.0 7.5 10.0 ... 350.0 352.5 355.0 357.5
Attributes: (12/14)
    long_name:     mean Daily specific humidity
    units:         kg/kg
    precision:     6
    GRIB_id:       51
    GRIB_name:     SPFH
    var_desc:      Specific humidity
    ...            ...
    parent_stat:   Individual Obs
    dataset:       NCEP Reanalysis Daily Averages
    actual_range:  [0.        0.0463525]
    Description:   Spherical harmonic coefficients
    Truncation:    Triangular truncation at wavenumber 72
    Order:         Complex coefficients in triangular order


Compare the original field, the grid field reconstructed after one spectral transform cycle, and the backend difference between Fortran and Rust transforms.

draw_q_grid = q_grid_data.isel(time = 2).sel(level = 850)
draw_q_grid_spectrans = q_grid_spectrans.isel(time = 2).sel(level = 850)
draw_diff_grid_minus_grid_spectrans = draw_q_grid - draw_q_grid_spectrans
draw_diff_rust = (q_grid_spectrans - q_grid_rs_spectrans).isel(time = 2).sel(level = 850)

# ---------------------------
fig, ax = plt.subplots(2, 2, figsize = (10, 10))

axi = ax[0, 0]
draw_q_grid.plot.contourf(
    ax = axi,
    levels= np.linspace(0, 0.015, 11),
    cmap = "plasma",
    cbar_kwargs = {'location': 'bottom', 'aspect': 60},
)
axi.set_title("Orignal [O]")

axi = ax[0, 1]
draw_q_grid_spectrans.plot.contourf(
    ax = axi,
    levels= np.linspace(0, 0.015, 11),
    cmap = "plasma",
    cbar_kwargs = {'location': 'bottom', 'aspect': 60},
)
axi.set_title("Single spectral transformation [ST]")

axi = ax[1, 0]
draw_diff_grid_minus_grid_spectrans.plot(
    ax = axi,
    vmax = 5e-4,
    cmap = "RdBu_r",
    cbar_kwargs = {'location': 'bottom', 'aspect': 60},
)
axi.set_title("Diff: [O] - [ST]")

axi = ax[1, 1]
draw_diff_rust.plot(
    ax = axi,
    vmax = 1e-8,
    cmap = "RdBu_r",
    cbar_kwargs = {'location': 'bottom', 'aspect': 60},
)
axi.set_title("Diff: (Fortran - Rust) [ST]")
Orignal [O], Single spectral transformation [ST], Diff: [O] - [ST], Diff: (Fortran - Rust) [ST]
Text(0.5, 1.0, 'Diff: (Fortran - Rust) [ST]')

Total running time of the script: (0 minutes 5.693 seconds)