{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Function in Python\n",
    "\n",
    "Trainer: Md. Jalal Uddin, Founder and director of Research Society\n",
    "    \n",
    "email: dmjalal90@gmail.com, 20205103002@nuist.edu.cn\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Built-in Functions in Python\n",
    "https://docs.python.org/3/library/functions.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# User-defined Functions"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "def function_name(parameters):\n",
    "    \n",
    "    \"\"\"function description\"\"\"\n",
    "    \n",
    "    statement(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "function_name(argument(s))      # call function\n",
    "\n",
    "\n",
    "Note: \n",
    "    \n",
    "def               = defined\n",
    "\n",
    "return (optional) = a statement to return a value from the function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def avg (num):\n",
    "    \"\"\"\n",
    "    average = sum data / n\n",
    "    \n",
    "    \"\"\"\n",
    "    sum_data = 0\n",
    "    for i in num:\n",
    "        sum_data = sum_data + i        \n",
    "        avg_data = sum_data / len(num)          \n",
    "    return avg_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.5\n"
     ]
    }
   ],
   "source": [
    "data = [1, 2, 3, 4, 5, 6]\n",
    "\n",
    "avg_cal = avg(data)\n",
    "print(avg_cal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.5\n"
     ]
    }
   ],
   "source": [
    "from statistics import mean\n",
    "\n",
    "avg_cal = mean(data)\n",
    "print(avg_cal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def windspeed(u,v):\n",
    "    \n",
    "    \"\"\"Calculate wind speed\n",
    "    \n",
    "    Parameters\n",
    "    ----------\n",
    "    u : int or float\n",
    "    v : int or float\n",
    "\n",
    "    Returns\n",
    "    ------\n",
    "    speed : int or float\n",
    "    \"\"\"\n",
    "    \n",
    "    z     = (u*u) + (v*v)\n",
    "       \n",
    "    import numpy as np\n",
    "    \n",
    "    speed = np.sqrt(z)\n",
    "    return speed"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MetPy\n",
    "\n",
    "pip install metpy --user\n",
    "\n",
    "https://unidata.github.io/MetPy/latest/userguide/installguide.html\n",
    "\n",
    "https://unidata.github.io/MetPy/latest/api/generated/metpy.calc.wind_speed.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "def wind_speed(u, v):\n",
    "    r\"\"\"Compute the wind speed from u and v-components.\n",
    "\n",
    "    Parameters\n",
    "    ----------\n",
    "    u : `pint.Quantity`\n",
    "        Wind component in the X (East-West) direction\n",
    "    v : `pint.Quantity`\n",
    "        Wind component in the Y (North-South) direction\n",
    "\n",
    "    Returns\n",
    "    -------\n",
    "    wind speed: `pint.Quantity`\n",
    "        The speed of the wind\n",
    "\n",
    "    See Also\n",
    "    --------\n",
    "    wind_components\n",
    "\n",
    "    \"\"\"\n",
    "    speed = np.sqrt(u * u + v * v)\n",
    "    return speed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22.360679774997898\n"
     ]
    }
   ],
   "source": [
    "U = 10\n",
    "V = 20\n",
    "\n",
    "winspd = windspeed(U,V)\n",
    "print(winspd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22.360679774997898\n"
     ]
    }
   ],
   "source": [
    "winspd = wind_speed(U,V)\n",
    "print(winspd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.4142135623730951\n",
      "2.23606797749979\n",
      "3.1622776601683795\n",
      "4.123105625617661\n",
      "5.0990195135927845\n",
      "6.082762530298219\n",
      "2.23606797749979\n",
      "2.8284271247461903\n",
      "3.605551275463989\n",
      "4.47213595499958\n",
      "5.385164807134504\n",
      "6.324555320336759\n",
      "3.1622776601683795\n",
      "3.605551275463989\n",
      "4.242640687119285\n",
      "5.0\n",
      "5.830951894845301\n",
      "6.708203932499369\n",
      "4.123105625617661\n",
      "4.47213595499958\n",
      "5.0\n",
      "5.656854249492381\n",
      "6.4031242374328485\n",
      "7.211102550927978\n",
      "5.0990195135927845\n",
      "5.385164807134504\n",
      "5.830951894845301\n",
      "6.4031242374328485\n",
      "7.0710678118654755\n",
      "7.810249675906654\n",
      "6.082762530298219\n",
      "6.324555320336759\n",
      "6.708203932499369\n",
      "7.211102550927978\n",
      "7.810249675906654\n",
      "8.48528137423857\n",
      "Wind speed: \n",
      " [1.4142135623730951, 2.23606797749979, 3.1622776601683795, 4.123105625617661, 5.0990195135927845, 6.082762530298219, 2.23606797749979, 2.8284271247461903, 3.605551275463989, 4.47213595499958, 5.385164807134504, 6.324555320336759, 3.1622776601683795, 3.605551275463989, 4.242640687119285, 5.0, 5.830951894845301, 6.708203932499369, 4.123105625617661, 4.47213595499958, 5.0, 5.656854249492381, 6.4031242374328485, 7.211102550927978, 5.0990195135927845, 5.385164807134504, 5.830951894845301, 6.4031242374328485, 7.0710678118654755, 7.810249675906654, 6.082762530298219, 6.324555320336759, 6.708203932499369, 7.211102550927978, 7.810249675906654, 8.48528137423857]\n"
     ]
    }
   ],
   "source": [
    "data1 = [1, 2, 3, 4, 5, 6]\n",
    "data2 = [1, 2, 3, 4, 5, 6]\n",
    "\n",
    "wspeed = []\n",
    "\n",
    "for i in data1:\n",
    "    for j in data2:\n",
    "        winspd = windspeed(i,j)\n",
    "        wspeed.append(winspd)\n",
    "        print(winspd)\n",
    "        \n",
    "print(\"Wind speed: \\n\", wspeed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
