{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Unit calculations in Python\n",
    "\n",
    "* Many units packages [are available](https://pypi.python.org/pypi?%3Aaction=search&term=unit+conversion&submit=search).\n",
    "* The [pint package](https://pint.readthedocs.io/en/latest/) is demonstrated here.\n",
    "    * The pint website had detailed documentation and tutorials.\n",
    "    \n",
    "### Installation\n",
    "* Pint doesn't come with anaconda, so you have it install it separately.\n",
    "* Open an anaconda prompt (the black window).\n",
    "* Type the following\n",
    "```conda install -c conda-forge pint```\n",
    "\n",
    "### Summary\n",
    "* Add units to variables using ```* u.m```, where ```m``` is meters, or some other unit.\n",
    "* 2 parts\n",
    "    1. ```L.magnitude``` gives the number part\n",
    "    * ```L.units``` gives the units part\n",
    "* Convert units\n",
    "    * ```L.to(u.ft)```\n",
    "    * ```L.ito(u.ft)```\n",
    "    * ```L.to_base_units()```\n",
    "* Be careful with temperature units\n",
    "* [Big list of units](https://github.com/hgrecco/pint/blob/master/pint/default_en.txt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "### Import library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import pint              # import the library\n",
    "u = pint.UnitRegistry()  # get the units from the UnitRegistry"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Basic units"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 meter\n",
      "2 second\n",
      "2.5 meter / second\n"
     ]
    }
   ],
   "source": [
    "L = 5 * u.m              # variable with units of meters\n",
    "t = 2 * u.s              # variable with units of time\n",
    "v = L/t                  # new variable from L and t with velocity units\n",
    "\n",
    "print(L)\n",
    "print(t)\n",
    "print(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Magnitude and units parts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "meter\n"
     ]
    }
   ],
   "source": [
    "print( L.magnitude )     # access the number part\n",
    "print( L.units )         # access the units part"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Convert units: ```to```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.592340730136006 mile_per_hour\n",
      "8.202099737532809 foot / second\n"
     ]
    }
   ],
   "source": [
    "v_mph = v.to(u.mph)      # make a new variable in mph\n",
    "v_fps = v.to(u.ft/u.s)   # make a new variable in ft/s\n",
    "                         # \"to\" function (v is unchanged)\n",
    "print(v_mph)\n",
    "print(v_fps)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Convert units: ```ito```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5 meter / second\n",
      "8.202099737532809 foot / second\n"
     ]
    }
   ],
   "source": [
    "print(v)                 # v is currently in m/s\n",
    "\n",
    "v.ito(u.ft/u.s)          # change v itself to ft/s: \"ito\" function\n",
    "print(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Automatic units conversion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.1335999999999995 meter\n",
      "23.404199475065617 foot\n"
     ]
    }
   ],
   "source": [
    "L1 = 5 *u.m\n",
    "L2 = 7 *u.ft\n",
    "\n",
    "print(L1 + L2)           # automatic conversion to L1's units\n",
    "print(L2 + L1)           # automatic conversion to L2's units"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Ideal gas law: mixed unit conversion\n",
    "$$\\rho = \\frac{MP}{R_gT}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0007750322039243355 kilogram * mole * standard_atmosphere / kilomole / liter / torr\n",
      "0.5890244749824949 kilogram / meter ** 3\n",
      "0.03677159670259146 pound / foot ** 3\n"
     ]
    }
   ],
   "source": [
    "M  = 29     * u.kg/u.kmol\n",
    "P  = 1      * u.atm\n",
    "T  = 600    * u.K\n",
    "Rg = 62.363 * u.L*u.torr/u.mol/u.K\n",
    "\n",
    "ρ = M*P/Rg/T\n",
    "\n",
    "print(ρ)\n",
    "print(ρ.to_base_units())    # function \"to_base_units\"   \n",
    "print(ρ.to(u.lb/u.ft**3))   # some other desired units"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Multiple unit names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "E2 = 100 * u.Btu         # these are the same\n",
    "E3 = 100 * u.BTU \n",
    "\n",
    "m1 = 1 * u.lb           # these are the same\n",
    "m2 = 1 * u.pound\n",
    "\n",
    "x1 = 2 * u.m            # these are the same\n",
    "x2 = 2 * u.meter\n",
    "\n",
    "x3 = 2 * u.km           # these are the same\n",
    "x4 = 2 * u.kilometer    \n",
    "\n",
    "T1 = 278 * u.K          # these are the same \n",
    "T1 = 278 * u.degK       # Note: use degR, degF, degC, not R, F, C\n",
    "\n",
    "f1 = 10 * u.N           # these are the same\n",
    "f2 = 10 * u.newton\n",
    "\n",
    "f3 = 10 * u.force_pound # these are the same\n",
    "f4 = 10 * u.lbf\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Temperature\n",
    "* Best to just work with Kelvin or Rankine."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "491.6699999999999 degree_Rankine\n",
      "31.999999999999936 degree_Fahrenheit\n",
      "0.0 degree_Celsius\n",
      "300.92777777777775 kelvin\n"
     ]
    }
   ],
   "source": [
    "T = 273.15 * u.degK\n",
    "print( T.to(u.degR) )\n",
    "print( T.to(u.degF) )\n",
    "print( T.to(u.degC) )\n",
    "\n",
    "\n",
    "print( 278.15*u.degK + 41*u.degR )  # This works\n",
    "# print( 278.15*u.degK + 41*u.degF) # doesn't work (offeset issue)\n",
    "\n",
    "# T = 32 * u.degF                   # does't work (offset issue)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "base",
   "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.14.6"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
