{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib notebook"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nA simple frequency-domain example\n=================================\n\nFor a single frequency and a crossplot frequency vs offset.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import empymod\nimport numpy as np\nimport matplotlib.pyplot as plt\nplt.style.use('ggplot')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define models\n-------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "name = 'Example Model'         # Model name\ndepth = [0, 300, 1000, 1200]   # Layer boundaries\nres = [2e14, 0.3, 1, 50, 1]    # Anomaly resistivities\nresBG = [2e14, 0.3, 1, 1, 1]   # Background resistivities\naniso = [1, 1, 1.5, 1.5, 1.5]  # Layer anis. (same for anomaly & backg.)\n\n# Modelling parameters\nverb = 0\nab = 11   # source and receiver x-directed\n\n# Spatial parameters\nzsrc = 250                    # Src-depth\nzrec = 300                    # Rec-depth\nfx = np.arange(20, 101)*100   # Offsets\nfy = np.zeros(fx.size)        # 0s"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot models\n~~~~~~~~~~~\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pdepth = np.repeat(np.r_[-100, depth], 2)\npdepth[:-1] = pdepth[1:]\npdepth[-1] = 2*depth[-1]\npres = np.repeat(res, 2)\npresBG = np.repeat(resBG, 2)\npani = np.repeat(aniso, 2)\n\n# Create figure\nfig = plt.figure(figsize=(7, 5), facecolor='w')\nfig.subplots_adjust(wspace=.25, hspace=.4)\nplt.suptitle(name, fontsize=20)\n\n# Plot Resistivities\nax1 = plt.subplot(1, 2, 1)\nplt.plot(pres, pdepth, 'r')\nplt.plot(presBG, pdepth, 'k')\nplt.xscale('log')\nplt.xlim([.2*np.array(res).min(), 2*np.array(res)[1:].max()])\nplt.ylim([1.5*depth[-1], -100])\nplt.ylabel('Depth (m)')\nplt.xlabel(r'Resistivity $\\rho_h\\ (\\Omega\\,\\rm{m})$')\n\n# Plot anisotropies\nax2 = plt.subplot(1, 2, 2)\nplt.plot(pani, pdepth, 'k')\nplt.xlim([0, 2])\nplt.ylim([1.5*depth[-1], -100])\nplt.xlabel(r'Anisotropy $\\lambda (-)$')\nax2.yaxis.tick_right()\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "1. Frequency response for f = 1 Hz\n----------------------------------\n\nCompute\n~~~~~~~\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "inpdat = {'src': [0, 0, zsrc], 'rec': [fx, fy, zrec], 'depth': depth,\n          'freqtime': 1, 'aniso': aniso, 'ab': ab,\n          'htarg': {'pts_per_dec': -1}, 'verb': verb}\n\nfEM = empymod.dipole(**inpdat, res=res)\nfEMBG = empymod.dipole(**inpdat, res=resBG)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot\n~~~~\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = plt.figure(figsize=(8, 6), facecolor='w')\nfig.subplots_adjust(wspace=.25, hspace=.4)\nfig.suptitle(name+': src-x, rec-x; f = 1 Hz', fontsize=16, y=1)\n\n# Plot Amplitude\nax1 = plt.subplot(2, 2, 1)\nplt.semilogy(fx/1000, fEMBG.amp(), label='BG')\nplt.semilogy(fx/1000, fEM.amp(), label='Anomaly')\nplt.legend(loc='best')\nplt.title(r'Amplitude (V/(A m$^2$))')\nplt.xlabel('Offset (km)')\n\n# Plot Phase\nax2 = plt.subplot(2, 2, 2)\nplt.title(r'Phase ($^\\circ$)')\nplt.plot(fx/1000, fEMBG.pha(deg=True), label='BG')\nplt.plot(fx/1000, fEM.pha(deg=True), label='Anomaly')\nplt.xlabel('Offset (km)')\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "2. Crossplot\n------------\n\nCompute\n~~~~~~~\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Compute responses\nfreq = np.logspace(-1.5, .5, 33)  # 33 frequencies from -1.5 to 0.5 (logspace)\ninpdat = {'src': [0, 0, zsrc], 'rec': [fx, fy, zrec], 'depth': depth,\n          'freqtime': freq, 'aniso': aniso, 'ab': ab,\n          'htarg': {'pts_per_dec': -1}, 'verb': verb}\n\nxfEM = empymod.dipole(**inpdat, res=res)\nxfEMBG = empymod.dipole(**inpdat, res=resBG)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot\n~~~~\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "lfreq = np.log10(freq)\n\n# Create figure\nfig = plt.figure(figsize=(10, 4), facecolor='w')\nfig.subplots_adjust(wspace=.25, hspace=.4)\n\n# Plot absolute (amplitude) in log10\nax1 = plt.subplot(1, 2, 2)\nplt.title(r'Normalized $|E_A/E_{BG}|\\ (-)$')\nplt.imshow(np.abs(xfEM/xfEMBG), interpolation='bicubic',\n           extent=[fx[0]/1000, fx[-1]/1000, lfreq[0], lfreq[-1]],\n           origin='lower', aspect='auto')\nplt.grid(False)\nplt.colorbar()\nCS = plt.contour(fx/1000, lfreq, np.abs(xfEM/xfEMBG), [1, 3, 5, 7], colors='k')\nplt.clabel(CS, inline=1, fontsize=10)\nplt.ylim([lfreq[0], lfreq[-1]])\nplt.xlim([fx[0]/1000, fx[-1]/1000])\nplt.xlabel('Offset (km)')\nplt.ylabel('Frequency (Hz)')\nplt.yticks([-1.5, -1, -.5, 0, .5], ('0.03', '0.10', '0.32', '1.00', '3.16'))\n\n# Plot normalized\nax2 = plt.subplot(1, 2, 1)\nplt.title(r'Absolute log10$|E_A|$ (V/A/m$^2$)')\nplt.imshow(np.log10(np.abs(xfEM)), interpolation='bicubic',\n           extent=[fx[0]/1000, fx[-1]/1000, lfreq[0], lfreq[-1]],\n           origin='lower', aspect='auto')\nplt.grid(False)\nplt.colorbar()\nCS = plt.contour(fx/1000, lfreq, np.log10(np.abs(xfEM)),\n                 [-14, -13, -12, -11], colors='k')\nplt.clabel(CS, inline=1, fontsize=10)\nplt.ylim([lfreq[0], lfreq[-1]])\nplt.xlim([fx[0]/1000, fx[-1]/1000])\nplt.xlabel('Offset (km)')\nplt.ylabel('Frequency (Hz)')\nplt.yticks([-1.5, -1, -.5, 0, .5], ('0.03', '0.10', '0.32', '1.00', '3.16'))\n\nfig.suptitle(name+': src-x, rec-x', fontsize=18, y=1.05)\n\nplt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "empymod.Report()"
      ]
    }
  ],
  "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.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}