{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Converting a touchstone s2p file to xml and html\n",
    "This example shows how to use pyMez to load a touchstone s2p file, convert it to xml and then to html.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import pyMez and pyMez.Code.DataHandlers.Translations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Importing pyMez, this should take roughly 30 seconds\n",
      "Importing Code.DataHandlers.GeneralModels\n",
      "It took 2.215127 s to import Code.DataHandlers.GeneralModels\n",
      "Importing Code.DataHandlers.HTMLModels\n",
      "It took 0.782045 s to import Code.DataHandlers.HTMLModels\n",
      "Importing Code.DataHandlers.NISTModels\n",
      "The module smithplot was not found,please put it on the python path\n",
      "It took 2.949168 s to import Code.DataHandlers.NISTModels\n",
      "Importing Code.DataHandlers.TouchstoneModels\n",
      "It took 0.001001 s to import Code.DataHandlers.TouchstoneModels\n",
      "Importing Code.DataHandlers.XMLModels\n",
      "It took 0.35402 s to import Code.DataHandlers.XMLModels\n",
      "Importing Code.DataHandlers.ZipModels\n",
      "It took 0.024001 s to import Code.DataHandlers.ZipModels\n",
      "Importing Code.InstrumentControl.Experiments\n",
      "It took 6.909395 s to import Code.InstrumentControl.Experiments\n",
      "Importing Code.InstrumentControl.Instruments\n",
      "It took 0.001 s to import Code.InstrumentControl.Instruments\n",
      "Importing Code.Utils.Names\n",
      "It took 0.0 s to import Code.Utils.Names\n",
      "It took 13.235757 s to import all of the active modules\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\sandersa\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\odo\\backends\\pandas.py:102: FutureWarning: pandas.tslib is deprecated and will be removed in a future version.\n",
      "You can access NaTType as type(pandas.NaT)\n",
      "  @convert.register((pd.Timestamp, pd.Timedelta), (pd.tslib.NaTType, type(None)))\n"
     ]
    }
   ],
   "source": [
    "from pyMez import *\n",
    "from pyMez.Code.DataHandlers.Translations import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load the s2p file into the S2PV1 class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "s2p_file_path=os.path.join(os.getcwd(),\"./S2p_to_Xml_Files/20160301_30ft_cable_0.s2p\")\n",
    "s2p=S2PV1(s2p_file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 67024 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 8 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# if you want to see the file use the .show() method, for a pop-out plot use the %matplotlib wx magic\n",
    "s2p.show();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Convert to xml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# use the function found in Translations.py\n",
    "xml=S2PV1_to_XmlDataTable(s2p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Convert to html using the appropriate style sheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Frequency',\n",
       " 'argS11',\n",
       " 'argS12',\n",
       " 'argS21',\n",
       " 'argS22',\n",
       " 'dbS11',\n",
       " 'dbS12',\n",
       " 'dbS21',\n",
       " 'dbS22']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# to know the format of the xml file we can use .get_attribute_names()\n",
    "xml.get_attribute_names()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#now we set the proper xsl style sheet, they are currently in DataHandlers/XSL\n",
    "xml.options[\"style_sheet\"]=os.path.join(TESTS_DIRECTORY,\"../XSL/S2P_DB_STYLE.xsl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# now we can convert to text or save_HTML, note xml.to_HTML(xsl_path) works also\n",
    "html_text=xml.to_HTML()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# if we want to visualize and change it as a python object we can use\n",
    "html=HTMLBase(None,html_text=html_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file://C:/Users/sandersa/AppData/Local/Temp/1/tmp8uqu06__.html\n"
     ]
    }
   ],
   "source": [
    "html.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'./S2p_to_Xml_Files/20160301_30ft_cable_0_db_format.html'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html.save(\"./S2p_to_Xml_Files/20160301_30ft_cable_0_db_format.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file://C:/Users/sandersa/AppData/Local/Temp/1/tmp0ie6hlfy.html\n",
      "file://C:/Users/sandersa/AppData/Local/Temp/1/tmpedqf2afk.html\n"
     ]
    }
   ],
   "source": [
    "# this can be done for all three of the common formats\n",
    "\n",
    "xml_RI=S2PV1_to_XmlDataTable(s2p,format=\"RI\",style_sheet=os.path.join(TESTS_DIRECTORY,\"../XSL/S2P_RI_STYLE.xsl\"))\n",
    "html_RI=HTMLBase(html_text=xml_RI.to_HTML())\n",
    "html_RI.show()\n",
    "xml_MA=S2PV1_to_XmlDataTable(s2p,format=\"MA\",style_sheet=os.path.join(TESTS_DIRECTORY,\"../XSL/S2P_MA_STYLE.xsl\"))\n",
    "html_MA=HTMLBase(html_text=xml_MA.to_HTML())\n",
    "html_MA.show()\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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
