{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "source": [ "# Units and Quantities\n", "\n", "## Objectives\n", "\n", "- Use units\n", "- Create functions that accept quantities as arguments\n", "- Create new units" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "## Basics\n", "\n", "How do we define a Quantity and which parts does it have?" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 1, "cell_type": "code", "source": [ "from astropy import units as u\n" ], "outputs": [], "metadata": { "collapsed": false, "keep": true } }, { "execution_count": 2, "cell_type": "code", "source": [ "# Define a quantity length\n", "# print it\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 3, "cell_type": "code", "source": [ "# Type of quantity\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 4, "cell_type": "code", "source": [ "# Type of unit\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 5, "cell_type": "code", "source": [ "# Quantity\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 6, "cell_type": "code", "source": [ "# value\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 7, "cell_type": "code", "source": [ "# unit\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 8, "cell_type": "code", "source": [ "# information\n" ], "outputs": [], "metadata": { "scrolled": true, "collapsed": false } }, { "source": [ "Quantities can be converted to other units systems or factors by using `to()`" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 9, "cell_type": "code", "source": [ "# Convert it to: km, lyr\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "We can do arithmetic operations when the quantities have the compatible units:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 10, "cell_type": "code", "source": [ "# arithmetic with distances\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Quantities can also be combined, for example to measure speed" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 11, "cell_type": "code", "source": [ "# calculate a speed\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 12, "cell_type": "code", "source": [ "# decompose it\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "
\n", "

Challenges

\n", "\n", "
    \n", "
  1. Convert the speed in imperial units (miles/hour) using:
    \n", "\n", " ```from astropy.units import imperial```\n", "
  2. \n", "
  3. Calculate whether a pint is more than half litre
    \n", " \n", " You can compare quantities as comparing variables.
    \n", " Something strange? Check what deffinition of pint astropy is using.\n", "
  4. \n", "
  5. Does units work with areas? calculate the area of a rectangle of 3 km of side and 5 meter of width. Show them in m^2 and convert them to yards^2
  6. \n", "
" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 13, "cell_type": "code", "source": [ "#1\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 14, "cell_type": "code", "source": [ "#2\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 15, "cell_type": "code", "source": [ "#3\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "## Composed units\n", "\n", "Many units are compositions of others, for example, one could create new combinationes for ease of use:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 16, "cell_type": "code", "source": [ "# create a composite unit\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 17, "cell_type": "code", "source": [ "# and in the imperial system\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "and others are already a composition:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 18, "cell_type": "code", "source": [ "# what can be converted from s-1?\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 19, "cell_type": "code", "source": [ "# or Jules?\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 20, "cell_type": "code", "source": [ "# Unity of R\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Sometime we get *no units* quantitites" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 21, "cell_type": "code", "source": [ "# no units\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "What happen if we add a number to this?" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 22, "cell_type": "code", "source": [ "# arithmetic with no units\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 23, "cell_type": "code", "source": [ "# final value of a no unit quantity\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "## Equivalencies\n", "\n", "Some conversions are not done by a conversion factor as between miles and kilometers, for example converting between wavelength and frequency." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 24, "cell_type": "code", "source": [ "# converting spectral quantities\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 25, "cell_type": "code", "source": [ "# but doing it right\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Other built-in equivalencies are: \n", " - `parallax()`\n", " - Doppler (`dopplr_radio`, `doppler_optical`, `doppler_relativistic`)\n", " - spectral flux density\n", " - brigthness temperature\n", " - temperature energy\n", " - and you can [build your own](http://astropy.readthedocs.org/en/stable/units/equivalencies.html#writing-new-equivalencies)" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 26, "cell_type": "code", "source": [ "# finding the equivalencies\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 27, "cell_type": "code", "source": [ "# but also using other systems\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "## Printing the quantities" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 28, "cell_type": "code", "source": [ "# Printing values with different formats\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "## Arrays\n", "\n", "Quantities can also be applied to arrays" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 29, "cell_type": "code", "source": [ "# different ways of defining a quantity for a single value\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 30, "cell_type": "code", "source": [ "# now with lists\n", "# and arrays\n", "# and its arithmetics\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 31, "cell_type": "code", "source": [ "# angles are smart!\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "## Plotting quantities\n", "\n", "To work nicely with matplotlib we need to do as follows:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 32, "cell_type": "code", "source": [ "# allowing for plotting\n", "from astropy.visualization import quantity_support\n", "quantity_support()\n", "\n", "# loading matplotlib\n", "%matplotlib inline\n", "from matplotlib import pyplot as plt\n" ], "outputs": [], "metadata": { "collapsed": false, "keep": true } }, { "execution_count": 33, "cell_type": "code", "source": [ "# Ploting the previous array\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "## Creating functions with quantities as units\n", "\n", "We want to have functions that contain the information of the untis, and with them we can be sure that we will be always have the *right* result." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 34, "cell_type": "code", "source": [ "# Create a function for the Kinetic energy\n" ], "outputs": [], "metadata": { "collapsed": true } }, { "execution_count": 35, "cell_type": "code", "source": [ "# run with and without units\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "
\n", "

Challenges

\n", "\n", "
    \n", "
  1. Create a function that calculates potential energy where *g* defaults to Earth value, \n", " but could be used for different planets. \n", " Test it for any of the *g* values for any other \n", " planet.\n", "
  2. \n", "
\n", "
" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 36, "cell_type": "code", "source": [ "#4\n" ], "outputs": [], "metadata": { "collapsed": true } }, { "execution_count": 37, "cell_type": "code", "source": [ "# run it for some values\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 38, "cell_type": "code", "source": [ "# on Mars:\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "## Create your own units\n", "\n", "Some times we want to create our own units:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 39, "cell_type": "code", "source": [ "# Create units for a laugh scale\n" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "
\n", "

Challenges

\n", "\n", "
    \n", "
  1. Convert the area calculated before `rectangle_area` in Hectare\n", " (1 hectare = 100 ares; 1 are = 100 m2).\n", "
  2. \n", "
\n", "
" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 40, "cell_type": "code", "source": [ "#5\n" ], "outputs": [], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 2", "name": "python2", "language": "python2" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "2.7.11", "pygments_lexer": "ipython2", "codemirror_mode": { "version": 2, "name": "ipython" } } } }