{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# This cell is added by sphinx-gallery\n\n%matplotlib inline\n\nimport mrsimulator\nprint(f'You are using mrsimulator v{mrsimulator.__version__}')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# RbNO3, 87Rb (I=3/2) STMAS\n\n87Rb (I=3/2) staellite-transition off magic-angle spinning simulation.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following is an example of the STMAS simulation of $\\text{RbNO}_3$. The\n$^{87}\\text{Rb}$ tensor parameters were obtained from Massiot `et. al.` [#f1]_.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport mrsimulator.signal_processing as sp\nimport mrsimulator.signal_processing.apodization as apo\nfrom mrsimulator import Simulator, SpinSystem, Site\nfrom mrsimulator.methods import ST1_VAS\n\n# global plot configuration\nfont = {\"size\": 9}\nmpl.rc(\"font\", **font)\nmpl.rcParams[\"figure.figsize\"] = [4.25, 3.0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generate the site and spin system objects.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "Rb87_1 = Site(\n    isotope=\"87Rb\",\n    isotropic_chemical_shift=-27.4,  # in ppm\n    quadrupolar={\"Cq\": 1.68e6, \"eta\": 0.2},  # Cq is in Hz\n)\nRb87_2 = Site(\n    isotope=\"87Rb\",\n    isotropic_chemical_shift=-28.5,  # in ppm\n    quadrupolar={\"Cq\": 1.94e6, \"eta\": 1.0},  # Cq is in Hz\n)\nRb87_3 = Site(\n    isotope=\"87Rb\",\n    isotropic_chemical_shift=-31.3,  # in ppm\n    quadrupolar={\"Cq\": 1.72e6, \"eta\": 0.5},  # Cq is in Hz\n)\n\nsites = [Rb87_1, Rb87_2, Rb87_3]  # all sites\nspin_systems = [SpinSystem(sites=[s]) for s in sites]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 2:** Select a satellite-transition variable-angle spinning method. The\nfollowing `ST1_VAS` method correlates the frequencies from the two inner-satellite\ntransitions to the central transition. Note, STMAS measurements are highly suspectable\nto rotor angle mismatch. In the following, we show two methods, first set to\nmagic-angle and the second deliberately miss-sets by approximately 0.0059 degrees.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "angles = [54.7359, 54.73]\nmethod = []\nfor angle in angles:\n    method.append(\n        ST1_VAS(\n            channels=[\"87Rb\"],\n            magnetic_flux_density=7,  # in T\n            rotor_angle=angle * 3.14159 / 180,  # in rad (magic angle)\n            spectral_dimensions=[\n                {\n                    \"count\": 256,\n                    \"spectral_width\": 3e3,  # in Hz\n                    \"reference_offset\": -2.4e3,  # in Hz\n                    \"label\": \"Isotropic dimension\",\n                },\n                {\n                    \"count\": 512,\n                    \"spectral_width\": 5e3,  # in Hz\n                    \"reference_offset\": -4e3,  # in Hz\n                    \"label\": \"MAS dimension\",\n                },\n            ],\n        )\n    )"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create the Simulator object, add the method and spin system objects, and\nrun the simulation.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim = Simulator()\nsim.spin_systems = spin_systems  # add the spin systems\nsim.methods = method  # add the methods.\nsim.run()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The plot of the simulation.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data = [sim.methods[0].simulation, sim.methods[1].simulation]\nfig, ax = plt.subplots(1, 2, figsize=(8.5, 3), subplot_kw={\"projection\": \"csdm\"})\n\ntitles = [\"STVAS @ magic-angle\", \"STVAS @ 0.0059 deg off magic-angle\"]\nfor i, item in enumerate(data):\n    cb1 = ax[i].imshow(item / item.max(), aspect=\"auto\", cmap=\"gist_ncar_r\")\n    ax[i].set_title(titles[i])\n    plt.colorbar(cb1, ax=ax[i])\n    ax[i].invert_xaxis()\n    ax[i].invert_yaxis()\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Add post-simulation signal processing.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "processor = sp.SignalProcessor(\n    operations=[\n        # Gaussian convolution along both dimensions.\n        sp.IFFT(dim_index=(0, 1)),\n        apo.Gaussian(FWHM=\"50 Hz\", dim_index=0),\n        apo.Gaussian(FWHM=\"50 Hz\", dim_index=1),\n        sp.FFT(dim_index=(0, 1)),\n    ]\n)\nprocessed_data = []\nfor item in data:\n    processed_data.append(processor.apply_operations(data=item))\n    processed_data[-1] /= processed_data[-1].max()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The plot of the simulation after signal processing.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = plt.subplot(projection=\"csdm\")\ncb = ax.imshow(processed_data[1].real, cmap=\"gist_ncar_r\", aspect=\"auto\")\nplt.colorbar(cb)\nax.invert_xaxis()\nax.invert_yaxis()\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. [#f1] Massiot, D., Touzoa, B., Trumeaua, D., Coutures, J.P., Virlet, J., Florian,\n      P., Grandinetti, P.J. Two-dimensional magic-angle spinning isotropic\n      reconstruction sequences for quadrupolar nuclei, ssnmr, (1996), **6**, *1*,\n      73-83. `DOI: 10.1016/0926-2040(95)01210-9\n      <https://doi.org/10.1016/0926-2040(95)01210-9>`_\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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.8.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}