{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## XAFS Data Processing (wxPython)\n", "\n", "This example shows how to use Larch from Python/Jupyter for basic XAFS data processing:\n", "\n", " 1. read data from an Athena Project File\n", " 2. do XAFS background subtraction with autobk()\n", " 3. do XAFS Fourier transform with xfft()\n", "\n", "In this version, plotting will be done with wxPython, which will plot to external windows and give\n", "plots that at essentially identical to the Larix GUI. This method works only when running Jupyter \n", "on a local machine. If running on Jupyter hub or binder-like site, you should either use the plotly \n", "or inline matplotlib to plot data\n", "\n", "For more details on Larch, see https://xraypy.github.io/xraylarch/xafs/\n", "\n", "We'll start with reading and exploring the Athena Project File:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from larch.io import read_athena\n", "project = read_athena('../xafsdata/fe_athena.prj')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "fe2o3_rt1_xmu \n", "fe3c_rt_xdi \n", "feo_rt1_xmu \n" ] } ], "source": [ "for name, group in project._athena_groups.items():\n", " print(name, group)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> **NOTE** For additional features of working with Athena project files -> [IO_Athena_project.ipynb](./IO_Athena_project.ipynb) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's get that data group for Fe2O3 and look at what that Group object contains" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "athena_id \n", "athena_params \n", "atsym \n", "callargs \n", "d2mude \n", "dmude \n", "e0 \n", "edge \n", "edge_step \n", "edge_step_poly \n", "energy \n", "epsk \n", "epsr \n", "filename \n", "flat \n", "i0 \n", "journal \n", "label \n", "mu \n", "norm \n", "norm_poly \n", "post_edge \n", "pre_edge \n", "pre_edge_details \n", "sel \n", "signal \n" ] } ], "source": [ "fe2o3 = project.fe2o3_rt1_xmu\n", "fe2o3.filename = 'Fe2O3'\n", "for attr in dir(fe2o3):\n", " print(attr, type(getattr(fe2o3, attr)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are several components to this Group of data. We note that there are arrays for `energy` and `mu`. These are what represent $\\mu(E)$.\n", "\n", "----------\n", "\n", "To plot this data with wx, we need to tell Jupyter to use wx as its GUI, and then import the Larch plotting functions" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No conda env active, defaulting to base\n" ] } ], "source": [ "%gui wx\n", "from larch.wxlib import _plot as plot\n", "from larch.wxlib.xafsplots import plot_mu, plot_bkg, plot_chik, plot_chir\n", "\n", "plot_mu(fe2o3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's remove the XAFS background and extract the EXAFS $\\chi(k)$. We'll use the `autobk()` function (https://xraypy.github.io/xraylarch/xafs/autobk.html)\n", "\n", "Note that we pass in the *Group* as the first argument. The `autobk()` function will add several attributes to this Group -- it will be the container for this dataset." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from larch.xafs import autobk\n", "autobk(fe2o3, rbkg=0.9, kweight=2)\n", "\n", "plot_bkg(fe2o3, win=2) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll also want to plot $\\chi(k)$." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "plot_chik(fe2o3, kweight=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "but we usually want to plot $k$\n", "-weighte $\\chi(k)$\r\n", ":" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "plot_chik(fe2o3, kweight=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and now we will do the Fourier transform from $\\chi(k)$ to $\\chi(R)$, using the `xftf()` (XAFS Fourier Transform Forward) function. We'll do the Fourier transform on $k^2$ weighted $\\chi(k)$ with a Kaiser-Bessel Window. " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from larch.xafs import xftf\n", "xftf(fe2o3, kweight=2, kmin=2, kmax=13.0, dk=5, kwindow='Kaiser-Bessel')\n", "plot_chik(fe2o3, kweight=2, show_window=True, new=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and we can show that data as $\\chi(R)$" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "plot_chir(fe2o3, show_mag=True, show_real=True, win=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "let's try another dataset with all the processing and plotting at once:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "feo = project.feo_rt1_xmu\n", "feo.filename = 'FeO'\n", "\n", "autobk(feo, rbkg=0.9, kweight=2)\n", "xftf(feo, kweight=2, kmin=2, kmax=13.0, dk=5, kwindow='Kaiser-Bessel')\n", "\n", "plot_chik(feo, kweight=2)\n", "plot_chir(feo, show_mag=True, win=2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }