X-ray Emission Spectroscopy Concentration Correction and Plotting
=================================================================

This example demonstrates processing X-ray emission spectroscopy (XES) data, including
concentration correction, outlier removal, normalization, and visualization.

.. literalinclude:: ../../examples/xes_concentration_correction.py
    :language: python

Detailed Explanations
---------------------

After the import statements and logging setup, we proceed to define the data mappings
and load scan `8` from the HDF5 file and create an measurement from it.

.. code-block:: python

    hdf5_filename = resources.getfile("A1_Kb_XES.h5")
    data_mappings = {
        "x": ".1/measurement/xes_en",
        "signal": ".1/measurement/det_dtc_apd",
        "monitor": ".1/measurement/I02",
    }
    source = Hdf5Source(hdf5_filename, 8, data_mappings=data_mappings)
    measurement = Xes(source)

Next we apply concentration correction using the information from scan `9`.

.. code-block:: python

    conc_corr_scan = 9
    measurement.concentration_correction(conc_corr_scan)

The outliers are first detected using a modified threshold of `9` which will select only
the most prominent outliers. After detection, the outliers are removed by calling the
`remove_outliers` method.

.. code-block:: python

    measurement.find_outliers(threshold=9)
    measurement.remove_outliers()

The data is then normalized by area to prepare it for visualization.

.. code-block:: python

    measurement.normalize(mode="area")

Finally, we plot the processed data using the built-in `plot` method. After recovering
the axis, we set appropriate labels and title for clarity.

.. code-block:: python

    ax = measurement.plot()
    ax.set_xlabel("Energy (keV)")
    ax.set_ylabel("Intensity (a.u.)")
    ax.set_title("X-ray Emission Spectroscopy")
    plt.tight_layout()
    plt.show()

The plot shows the corrected and normalized XES spectrum.

.. image:: ../_static/images/xes_concentration_correction.png
    :alt: X-ray Emission Spectroscopy plot
    :align: center
    :width: 80%
