{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Chapter 4: Mathematical Operators and Functions\n", "## \n", "Operators are the commands that manipulate the values of variables, especially for numeric data types.\n", "- Can be used to change the value of an object using basic math operators ( + - x / )\n", "- There are also operators, which can truncate, compare, and round.\n", "- More sophisticated math functions can performed using the numpy module (trigonometry, exponentials/logarithms, inverse trig. and hyperbolic, and other numeric functions.\n", "\n", "

\n", "**Before starting:** Make sure that you open up a Jupyter notebook session using OnDemand so you can interactively follow along with today's lecture! If you have forgotten how to do this, refer to the previous lecture and class notes. Also, be sure to copy this script into your atmos_5340/module_3 subdirectory!\n", "\n", "
\n", "\n", "# Operators\n", "You can use Python much like a calculator. Type the following statements and press `enter` to run the line of code: \n", "\n", " 1+2\n", "\n", " 1/2\n", "\n", " 2**2\n", " \n", " 5%2\n", "> ## Do it yourself\n", "> Try different numbers and find out what each of the operators do.\n", ">- What does the `+` operator do? \n", ">- What does the `-` operator do? \n", ">- What does the `*` operator do? \n", ">- What does the `/` operator do? \n", ">- What does `//` operator do? \n", ">- What does the `**` operator do? \n", ">- What does the `%` \"modulo\" operator do? \n", "\n", "[Reference: Python Operators](https://www.tutorialspoint.com/python3/python_basic_operators.htm)\n" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 // 2" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Package Version\n", "----------------------------- ----------------------------\n", "affine 2.3.1\n", "aiobotocore 2.3.4\n", "aiohttp 3.8.1\n", "aioitertools 0.10.0\n", "aiosignal 1.2.0\n", "anyio 3.6.1\n", "appdirs 1.4.4\n", "argon2-cffi 21.3.0\n", "argon2-cffi-bindings 21.2.0\n", "asttokens 2.0.5\n", "async-timeout 4.0.2\n", "attrs 21.4.0\n", "Babel 2.10.3\n", "backcall 0.2.0\n", "backports.functools-lru-cache 1.6.4\n", "beautifulsoup4 4.11.1\n", "bleach 5.0.1\n", "botocore 1.24.21\n", "Bottleneck 1.3.5\n", "branca 0.5.0\n", "brotlipy 0.7.0\n", "Cartopy 0.20.3\n", "certifi 2022.6.15.1\n", "cffi 1.15.0\n", "cfgrib 0.9.10.1\n", "cftime 1.6.1\n", "charset-normalizer 2.1.0\n", "click 8.1.3\n", "click-plugins 1.1.1\n", "cligj 0.7.2\n", "contextily 1.2.0\n", "cryptography 37.0.4\n", "cycler 0.11.0\n", "debugpy 1.6.0\n", "decorator 5.1.1\n", "defusedxml 0.7.1\n", "descartes 1.1.0\n", "eccodes 1.4.2\n", "entrypoints 0.4\n", "executing 0.8.3\n", "fastjsonschema 2.16.1\n", "findlibs 0.0.2\n", "Fiona 1.8.21\n", "flit_core 3.7.1\n", "folium 0.12.1.post1\n", "fonttools 4.25.0\n", "frozenlist 1.3.0\n", "fsspec 2022.5.0\n", "future 0.18.2\n", "GDAL 3.5.1\n", "geographiclib 1.52\n", "geojson 2.5.0\n", "geopandas 0.11.0\n", "geoplot 0.5.1\n", "geoplotlib 0.3.2\n", "geopy 2.2.0\n", "idna 3.3\n", "importlib-metadata 4.11.4\n", "importlib-resources 5.8.0\n", "ipykernel 6.15.1\n", "ipython 8.4.0\n", "ipython-genutils 0.2.0\n", "jedi 0.18.1\n", "Jinja2 3.1.2\n", "jmespath 1.0.1\n", "joblib 1.1.0\n", "json5 0.9.5\n", "jsonschema 4.7.2\n", "jupyter-client 7.3.4\n", "jupyter_core 4.11.1\n", "jupyter-server 1.18.1\n", "jupyterlab 3.4.7\n", "jupyterlab-pygments 0.2.2\n", "jupyterlab-server 2.15.0\n", "kiwisolver 1.4.2\n", "mapclassify 2.4.3\n", "MarkupSafe 2.1.1\n", "matplotlib 3.5.2\n", "matplotlib-inline 0.1.3\n", "mercantile 1.2.1\n", "MetPy 1.3.1\n", "mistune 0.8.4\n", "multidict 6.0.2\n", "munch 2.5.0\n", "munkres 1.1.4\n", "nbclassic 0.4.3\n", "nbclient 0.6.6\n", "nbconvert 6.5.0\n", "nbformat 5.4.0\n", "nest-asyncio 1.5.5\n", "netCDF4 1.6.0\n", "networkx 2.8.5\n", "notebook 6.4.12\n", "notebook-shim 0.1.0\n", "numexpr 2.8.3\n", "numpy 1.22.3\n", "packaging 21.3\n", "pandas 1.4.3\n", "pandocfilters 1.5.0\n", "parso 0.8.3\n", "patsy 0.5.2\n", "pexpect 4.8.0\n", "pickleshare 0.7.5\n", "Pillow 9.2.0\n", "Pint 0.19.2\n", "pip 22.1.2\n", "ply 3.11\n", "pooch 1.6.0\n", "prometheus-client 0.14.1\n", "prompt-toolkit 3.0.30\n", "protobuf 3.20.1\n", "psutil 5.9.1\n", "ptyprocess 0.7.0\n", "pure-eval 0.2.2\n", "pycparser 2.21\n", "pyglet 1.5.16\n", "Pygments 2.12.0\n", "pygrib 2.1.4\n", "pyOpenSSL 22.0.0\n", "pyparsing 3.0.9\n", "pyproj 3.3.1\n", "PyQt5 5.15.7\n", "PyQt5-sip 12.11.0\n", "pyrsistent 0.18.1\n", "pyshp 2.3.0\n", "PySocks 1.7.1\n", "pyspharm 1.0.9\n", "python-dateutil 2.8.2\n", "pytz 2022.1\n", "pyzmq 23.2.0\n", "rasterio 1.3.0\n", "requests 2.28.1\n", "Rtree 1.0.0\n", "s3fs 2022.5.0\n", "scikit-learn 1.1.1\n", "scipy 1.8.1\n", "seaborn 0.11.2\n", "Send2Trash 1.8.0\n", "setuptools 61.2.0\n", "Shapely 1.8.2\n", "sip 6.6.2\n", "siphon 0.9\n", "six 1.16.0\n", "sniffio 1.2.0\n", "snuggs 1.4.7\n", "soupsieve 2.3.2.post1\n", "stack-data 0.3.0\n", "statsmodels 0.13.2\n", "terminado 0.15.0\n", "threadpoolctl 3.1.0\n", "tinycss2 1.1.1\n", "toml 0.10.2\n", "tomli 2.0.1\n", "tornado 6.1\n", "traitlets 5.3.0\n", "typing_extensions 4.3.0\n", "urllib3 1.26.10\n", "wcwidth 0.2.5\n", "webencodings 0.5.1\n", "websocket-client 1.3.3\n", "wheel 0.37.1\n", "windspharm 0+untagged.58.g5bbec33.dirty\n", "wrapt 1.14.1\n", "xarray 2022.6.0\n", "xyzservices 2022.6.0\n", "yarl 1.7.2\n", "zipp 3.8.0\n" ] } ], "source": [ "!pip list" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.5" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(9/2)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4142135623730951" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**(1./2.)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4142135623730951" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "np.sqrt(2)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5%3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "

\n", "# Variable Assignment\n", "In math/physics, variables are often part of equations/formulas. In computing languages, variables are much more flexible and can be assigned a value, then reused with a different value, etc.\n", "\n", "Variables are names that values are assigned to with the `=` operator. In programing, `=` means \"assigned to\" _not_ \"equals\".\n", "\n", "A variable is a value of some quantity, but to describe it requires:\n", "- a name\n", "- a storage location\n", "\n", "Normally, the variable name references the stored value. Keeping the name and content separately allows the name to be used independently of the information it represents.\n", "\n", "When a program executes, the name is bound by the computer source code to a value but the value of the variable may change during program execution.\n", "\n", "
\n", "\n", "\n", "|Rules for Variable Names| Example|\n", "|--|--|\n", "|Can be a combos of letters and numbers.|`alpha3`, `Timer8`\n", "|Names are case sensitive.|`Pressure` is different from `pressure` and `PRESSURE`\n", "|Must start with a letter.| Good: `var1 = 5`
Bad: `1var = 5`\n", "|Can use underscores.| `potential_temperature`|\n", "|Should be descriptive.| `temp` or `temperature` is better than `t`.|\n", "|By convention, use all CAPS for constants:| `TEMP_0C_IN_K = 273.15`\n", "\n", "> Note: There are 32 keyword names that are reserved becuase they have special meaning in Python. Variable names cannot be words like `del`, `and`, `if`, `global`, `for`, etc. \n", ">\n", ">[Reference: Python Keywords](https://www.programiz.com/python-programming/keyword-list)\n", "\n", "\n", "We can assign values to a variable and use the variables in expressions.\n", "\n" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.0\n" ] } ], "source": [ "# Calculate the hypotenuse of a triangle\n", "a = 3\n", "b = 4\n", "c = (a**2 + b**2)**(1/2)\n", "print(c)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Do it Yourself #1\n", "Try assigning a few variables and add the variables together. For this example, let's convert temperature in Celsius to degrees Fahrenheit! Let us know when you are done!" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "30 86.0 90\n" ] } ], "source": [ "C = 30\n", "f = 1.8 * C + 32\n", "F = 2 * C + 30\n", "print(C,f,F)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Augmented Assignment Operators\n", "\n", "Equal sign (=) assigns a value to a variable. Augmented Assignment Operators are just shorthand to repeat an operation on the original variable as follows\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
x += yx = x + y
x -= yx = x - y
x *= yx = x * y
x /= yx = x / y
\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100 5 10\n", "500\n", "50.0\n" ] } ], "source": [ "# try a couple\n", "a = 100 ; b = 5; c = 10\n", "print(a,b,c)\n", "a *= b\n", "print (a)\n", "a /= c\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "\n", "## Boolean Types\n", "\n", "**Recall that booleans are either `True` or `False`**. \n", " \n", "- True is equivalent to 1 or 1.0\n", "- False is equivalent to 0 or 0.0\n", "\n", "Try: " ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n", "False\n", "\n" ] } ], "source": [ "print(bool(0))\n", "print(bool(3))\n", "print(bool(1-1))\n", "print(type(bool(1-1)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Booleans are often used with \"comparison operators\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparison operators\n", "- Return boolean values of True or False\n", "- You can compare integer, floats, and strings\n", "\n", "|Description|Operator|\n", "|--|--|\n", "|Equal to| == |\n", "| Not equal to | != |\n", "| Less than | < |\n", "| Greater than | > |\n", "| Greater than or equal to | >= |\n", "| Less than or equal to | <= |\n", "\n", "Try:" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 > 9 # Greater than\n" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 == 3 # Equal to\n" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12 <= 12 # Less than or equal to\n" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12 != 3.4 # Not equal to\n", " " ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "\n" ] } ], "source": [ "b = 3 > 8\n", "print(b)\n", "print(type(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 🏈 Did Utah beat BYU last Thursday?" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Did Utah beat BYU last Thursday?\n", "True\n", "Final Score: Utah 30 -- BYU 12\n" ] } ], "source": [ "# Utah and BYU scores by quarter\n", "Utah_q1 = 3\n", "Utah_q2 = 6\n", "Utah_q3 = 7\n", "Utah_q4 = 14\n", "\n", "BYU_q1 = 0\n", "BYU_q2 = 6\n", "BYU_q3 = 0\n", "BYU_q4 = 6\n", "\n", "Utah_final = Utah_q1 + Utah_q2 + Utah_q3 + Utah_q4\n", "BYU_final = BYU_q1 + BYU_q2 + BYU_q3 + BYU_q4\n", "\n", "print('Did Utah beat BYU last Thursday?')\n", "print(Utah_final>BYU_final)\n", "print('Final Score: Utah', Utah_final, '--','BYU', BYU_final)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For more on math operators: \n", "[Reference: Python Comparison Operators](https://www.tutorialspoint.com/python3/comparison_operators_example.htm)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Math Functions\n", "\n", "There are not a lot of math functions built into Python, so it is really better to use numpy functions. But some of the basics are there:\n", "* abs\n", "* max\n", "* min\n", "* round (be careful and read the docs on a few gotchas)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Numpy\n", "\n", "In addition to doing basic math and comparison operators, Python has the ability to perform slightly more complicated tasks using the *Numpy module*. Numpy stands for the Numerical Python module, which expands upons the basic functions are features that comes with the base version of Python. Most distributions of Python already include this module, so often times you do not need to install this on your own!\n", "\n", "To activate the Numpy module, do the following command:" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "import numpy as np " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This command simply tells Python to load the *Numpy module* and denote this package as *np*, where is a shorthand version of Numpy. Whenever you use a function from Numpy, you will need to follow the function with a `numpy.`. With the above shortcut, we can call a numpy function with `np.`, which is shorter, and more elegant looking. \n", "\n", "Through Numpy, we can do things like Trignometry or work with exponentials or logarithmic numbers. Numpy also comes with some useful tools for rounding numbers, computing the absolute value, or doing something as simple as taking a mean. \n", "

\n", "\n", "**Listed below are some examples of useful Numpy functions:**" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653589793\n" ] } ], "source": [ "print(np.pi) #Returns constant for pi" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.718281828459045\n" ] } ], "source": [ "print(np.e) #Returns constant for e" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.302585092994046\n" ] } ], "source": [ "print(np.log(10)) #Returns the natural log of x or 10 in this case. Equivalent to ln(x)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.0\n" ] } ], "source": [ "print(np.log10(10)) #Returns the base 10 logarithm of x" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11.0\n" ] } ], "source": [ "print(np.ceil(10.76342)) #Returns the nearest, highest integer" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10.0\n" ] } ], "source": [ "print(np.floor(10.76342)) #Returns the nearest, lower-most integer" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "22.61\n" ] } ], "source": [ "print(np.abs(-22.61)) #Returns the absolute value of -22.61" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.0\n" ] } ], "source": [ "print(np.sqrt(25)) # Returns sqrt of 25" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0\n" ] } ], "source": [ "print(8**(1/3)) # returns the cube root of 8" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0\n" ] } ], "source": [ "print(np.power(8,1/3)) # ditto" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Computes the average of a Python list, tuple, or array**" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "83.92307692307692 83.9\n" ] } ], "source": [ "class_grades = [92,86,78,84,81,70,96,88,80,79,74,88,95]\n", "avg_grades = np.mean(class_grades)\n", "print(avg_grades,np.round(avg_grades,1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Computes the median**" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "84.0\n" ] } ], "source": [ "#Computes the median\n", "med_grades = np.median(class_grades)\n", "print(med_grades)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Rounds our number to the decimal place specified in the argument on the right (2)**" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "83.92\n" ] } ], "source": [ "print(np.round(avg_grades,2)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Computes max value in our list**" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "96\n" ] } ], "source": [ "print(np.max(class_grades)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And you get the idea.... Refer to the DeCaria text for **Chapter 4** for a list of many more useful Numpy functions!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, these are also functions to find NaN values, which are commonly used in the sciences to flag erroneous data or missing data.\n", "\n", "For example...." ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[76, 77, 77, 80, 82, 85, 90, nan, 94, 96, 97, 98, 99, 99, 97, 93, 88, 87, nan]\n" ] } ], "source": [ "temp_slc = [76,77,77,80,82,85,90,np.nan,94,96,97,98,99,99,97,93,88,87,np.nan] \n", "print(temp_slc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Trying to compute the mean with NaN can be problematic... granted we have functions that deal with this!" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nan\n" ] } ], "source": [ "#Yuck, not very useful...\n", "print(np.mean(temp_slc))\n" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False False False False False False False True False False False False\n", " False False False False False False True]\n" ] } ], "source": [ "#How do you find if a value in a list/tuple is a nan value?\n", "print(np.isnan(temp_slc))" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "89.11764705882354\n" ] } ], "source": [ "#A better way to handle\n", "print(np.nanmean(temp_slc))" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[76, 77, 77, 80, 82, 85, 90, nan, 94, 96, 97, 98, 99, 99, 97, 93, 88, 87, nan]\n" ] } ], "source": [ "np.array(temp_slc)\n", "print(temp_slc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Do it Yourself #2\n", "Lets compute the median, max and min of our daily SLC temperatures and print these out to your screen.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "99.0" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.nanmedian(temp_slc)\n", "np.nanmax(temp_slc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Trig functions\n", "\n", "Wind direction is defined such that 0 degrees is a wind blowing from the north, 90 degrees is a wind blowing from the east, etc.\n", "\n", "If we want to convert a wind speed, V, and direction, theta, to the cartesian wind component in the west-east (u) and north-south components (v), then it is important to remember that all calculations need to be in radians" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 45 -2.356194490192345 u= -7.071067811865477 v= -7.071067811865475\n" ] } ], "source": [ "# examples V = 10, theta = 45 (a wind from the northeast)\n", "V = 10\n", "theta = 45\n", "#convert direction to radians but have to orient correctly\n", "th_rad = np.radians(theta -180)\n", "u = V * np.sin(th_rad);\n", "v = V * np.cos(th_rad);\n", "print(V, theta, th_rad,'u=', u, 'v=', v)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 315 2.356194490192345 u= 3.5355339059327386 v= -3.5355339059327373\n" ] } ], "source": [ "# examples V = 5, theta = 315 (a wind from the northwest)\n", "V = 5\n", "theta = 315\n", "#convert direction to radians but have to orient correctly\n", "th_rad = np.radians(theta -180)\n", "u = V * np.sin(th_rad);\n", "v = V * np.cos(th_rad);\n", "print(V, theta, th_rad,'u=', u, 'v=', v)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.000000000000001 315.0\n" ] } ], "source": [ "# use atan2 to convert u and v to theta\n", "th_rad = np.arctan2(u,v)\n", "theta = np.degrees(th_rad)+180\n", "V = np.sqrt(u**2 + v**2)\n", "print(V, theta)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "\n", " \n", "\n", "# Want more practice!\n", "Try this tutorial...https://www.tutorialspoint.com/python3/python_basic_operators.htm
\n", "Or read this (Chapter 3.1 & 3.3): https://www.johnny-lin.com/pyintro/ed01/free_pdfs/ch03.pdf" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.5" } }, "nbformat": 4, "nbformat_minor": 4 }