{ "cells": [ { "cell_type": "markdown", "metadata": {}, "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": "code", "execution_count": null, "metadata": { "collapsed": false, "keep": true }, "outputs": [], "source": [ "from astropy import units as u\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Define a quantity length\n", "length = 26.2 * u.meter\n", "# print it\n", "print(length) # length is a quantity" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Type of quantity\n", "type(length)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Type of unit\n", "type(u.meter)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Quantity\n", "length" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# value\n", "length.value" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# unit\n", "length.unit" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "# information\n", "length.info" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Quantities can be converted to other units systems or factors by using `to()`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Convert it to: km, lyr\n", "print(length.to(u.km))\n", "print(length.to(u.lightyear))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do arithmetic operations when the quantities have the compatible units:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# arithmetic with distances\n", "distance_start = 10 * u.mm\n", "distance_end = 23 * u.km\n", "length = distance_end - distance_start\n", "print(length)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Quantities can also be combined, for example to measure speed" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# calculate a speed\n", "time = 15 * u.minute\n", "speed = length / time\n", "print(speed)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# decompose it\n", "print(speed.decompose())\n", "print(speed.si)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "