Unit manipulation ================= Documentation ------------- For more information about the features presented below, you can read the `astropy.units `_ docs. Representing units and quantities ---------------------------------- Astropy includes a powerful framework for units that allows users to attach units to scalars and arrays, and manipulate/combine these, keeping track of the units. Since we may want to use a number of units in expressions, it is easiest and most concise to import the units module with:: from astropy import units as u though note that this will conflict with any variable called ``u``. Units can then be accessed with:: >>> u.m Unit("m") >>> u.pc Unit("pc") >>> u.s Unit("s") >>> u.kg Unit("kg") We can create composite units: >>> u.m / u.kg / u.s**2 Unit("m / (kg s2)") The most useful feature about the units is the ability to attach them to scalars or arrays, creating ``Quantity`` objects:: >>> 3. * u.m >>> import numpy as np >>> np.array([1.2, 2.2, 1.7]) * u.pc / u.year Combining and converting units ------------------------------ Quantities can then be combined:: >>> q1 = 3. * u.m >>> q2 = 5. * u.cm / u.s / u.g**2 >>> q1 * q2 and converted to different units:: >>> (q1 * q2).to(u.m**2 / u.kg**2 / u.s) The units and value of a quantity can be accessed separately via the ``value`` and ``unit`` attributes:: >>> q = 5. * u.pc >>> q.value 5.0 >>> q.unit Unit("pc") Advanced features ----------------- The units of a quantity can be decomposed into a set of base units using the ``decompose()`` method. By default, units will be decomposed to S.I.:: >>> (3. * u.cm * u.pc / u.g / u.year**2).decompose() To decompose into c.g.s. units, one can do:: >>> (3. * u.cm * u.pc / u.g / u.year**2).decompose(u.cgs.bases) Using physical constants ------------------------ The `astropy.constants `_ module contains physical constants relevant for Astronomy, and these are defined with units attached to them using the ``astropy.units`` framework. If we want to compute the Gravitational force felt by a 100. * u.kg space probe by the Sun, at a distance of 3.2au, we can do:: >>> from astropy.constants import G >>> F = (G * 1. * u.M_sun * 100. * u.kg) / (3.2 * u.au)**2 >>> F >>> F.to(u.N) The full list of available physical constants is shown `here `_ (and additions are welcome!). Practical Exercises ------------------- .. admonition:: Level 1 What is 1 barn megaparsecs in teaspoons? .. raw:: html

Click to Show/Hide Solution

:: >>> (1. * u.barn * u.Mpc).to(u.tsp) .. raw:: html
.. admonition:: Level 2 What is 3 nm^2 Mpc / m^3 in dimensionless units? .. raw:: html

Click to Show/Hide Solution

:: >>> (3. * u.nm**2 * u.Mpc / u.m**3).decompose() or to just get the numerical value:: >>> (3. * u.nm**2 * u.Mpc / u.m**3).decompose().value 92570.327444015755 .. raw:: html
.. admonition:: Level 3 Try and convert 3 Angstroms to keV using the units framework. You will need to look through the documentation for `astropy.units `_ to see how this can be made to work. .. raw:: html

Click to Show/Hide Solution

:: >>> (3 * u.angstrom).to(u.keV) ... UnitsException: 'angstrom' (length) and 'keV' (energy) are not convertible >>> (3 * u.angstrom).to(u.keV, equivalencies=u.spectral()) .. raw:: html