.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "content/user_guide/auto_examples/1single/plot_complex_eof.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_content_user_guide_auto_examples_1single_plot_complex_eof.py: Complex EOF analysis ============================================ In this tutorial, we'll walk through how to perform a Complex EOF analysis on the zonal and meridional wind components. Let's start by importing the necessary packages and loading the data: .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: default import matplotlib.pyplot as plt import xarray as xr import xeofs as xe xr.set_options(display_expand_attrs=False) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 20-21 For this example, we'll use the ERA-Interim tutorial dataset ``eraint_uvz``: .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: default uvz = xr.tutorial.open_dataset("eraint_uvz") uvz .. raw:: html
<xarray.Dataset>
    Dimensions:    (longitude: 480, latitude: 241, level: 3, month: 2)
    Coordinates:
      * longitude  (longitude) float32 -180.0 -179.2 -178.5 ... 177.8 178.5 179.2
      * latitude   (latitude) float32 90.0 89.25 88.5 87.75 ... -88.5 -89.25 -90.0
      * level      (level) int32 200 500 850
      * month      (month) int32 1 7
    Data variables:
        z          (month, level, latitude, longitude) float64 ...
        u          (month, level, latitude, longitude) float64 ...
        v          (month, level, latitude, longitude) float64 ...
    Attributes: (2)


.. GENERATED FROM PYTHON SOURCE LINES 26-32 This dataset contains the zonal, meridional, and vertical wind components at three different atmospheric levels. Note that the data only covers two months, so we have just two time steps (samples). While this isn't enough for a robust EOF analysis, we'll proceed for demonstration purposes. Now, let's combine the zonal (``u``) and meridional (``v``) wind components into a complex-valued dataset: .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: default Z = uvz["u"] + 1j * uvz["v"] .. GENERATED FROM PYTHON SOURCE LINES 36-42 Next, we'll initialize and fit the ``ComplexEOF`` model to our data. The ``xeofs`` package makes this easy by allowing us to specify the sample dimension (``month``), automatically performing the Complex EOF analysis across all three atmospheric levels. As a standard practice, we'll also weigh each grid cell by the square root of the cosine of the latitude (``use_coslat=True``). .. GENERATED FROM PYTHON SOURCE LINES 42-46 .. code-block:: default model = xe.single.ComplexEOF(n_modes=1, use_coslat=True, random_state=7) model.fit(Z, dim="month") .. rst-class:: sphx-glr-script-out .. code-block:: none /home/nrieger/miniconda3/envs/xeofs/lib/python3.11/site-packages/scipy/sparse/linalg/_eigen/_svds.py:483: UserWarning: The problem size 2 minus the constraints size 0 is too small relative to the block size 1. Using a dense eigensolver instead of LOBPCG iterations.No output of the history of the iterations. _, eigvec = lobpcg(XH_X, X, tol=tol ** 2, maxiter=maxiter, .. GENERATED FROM PYTHON SOURCE LINES 47-50 Instead of just extracting the complex-valued components, we can also get the amplitude and phase of these components. Let's start by looking at the amplitude of the first mode: .. GENERATED FROM PYTHON SOURCE LINES 50-58 .. code-block:: default spatial_ampltiudes = model.components_amplitude() spatial_phases = model.components_phase() spatial_ampltiudes.sel(mode=1).plot(col="level") plt.show() .. image-sg:: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_complex_eof_001.png :alt: level = 200, level = 500, level = 850 :srcset: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_complex_eof_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-64 It looks like the first mode picks up a pattern resembling the location of the subtropical jet stream around ±30º latitude, particularly strong in the upper troposphere at 200 hPa and weaker toward the surface. We can also plot the phase of the first mode. To keep the plot clear, we'll only show the phase where the amplitude is above a certain threshold (e.g., 0.004): .. GENERATED FROM PYTHON SOURCE LINES 64-69 .. code-block:: default relevant_phases = spatial_phases.where(spatial_ampltiudes > 0.004) relevant_phases.sel(mode=1).plot(col="level", cmap="twilight") plt.show() .. image-sg:: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_complex_eof_002.png :alt: level = 200, level = 500, level = 850 :srcset: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_complex_eof_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.180 seconds) .. _sphx_glr_download_content_user_guide_auto_examples_1single_plot_complex_eof.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_complex_eof.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_complex_eof.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_