.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/gallery/units_demo.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_gallery_units_demo.py: ================================ Quantities, Units, and Constants ================================ The purpose of this demo is to demonstrate the capabilities of astropy `~astropy.units.Unit`, `~astropy.units.Quantity`, and `~astropy.units.Constant`. .. GENERATED FROM PYTHON SOURCE LINES 12-15 The astropy `~astropy.units.Quantity` object handles defining, converting between, and performing arithmetic with physical quantities, such as meters, seconds, Hz, etc. .. GENERATED FROM PYTHON SOURCE LINES 15-18 .. code-block:: default from astropy import units as u import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 19-21 You can define a `~astropy.units.Quantity` (a number with a unit) in a number of different ways. .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: default 42.0 * u.meter [1., 2., 3.] * u.s np.arange(10) * u.Hz .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 26-28 These objects work as you would expect with most Python operators or numpy functions .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: default np.power(2 * u.s, 3) (2 * u.s) ** 2 .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 32-33 If needed you can get the value as well as the unit .. GENERATED FROM PYTHON SOURCE LINES 33-36 .. code-block:: default q = 42.0 * u.meter print("The value is {0} and the unit is {1}".format(q.value, q.unit)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none The value is 42.0 and the unit is m .. GENERATED FROM PYTHON SOURCE LINES 37-39 Using the `~astropy.units.Quantity.to` function we can easily converted to another unit. .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: default print(q.to('parsec')) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 1.3611273015666334e-15 pc .. GENERATED FROM PYTHON SOURCE LINES 42-43 and imperial units as also supported .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: default from astropy.units import imperial print(q.to(imperial.mile)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 0.026097590073968023 mi .. GENERATED FROM PYTHON SOURCE LINES 47-48 Units that “cancel out” become a special unit called the “dimensionless unit”: .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: default u.m / u.m .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Unit(dimensionless) .. GENERATED FROM PYTHON SOURCE LINES 51-55 More complex conversions are also supported using `~astropy.units.equivalencies`. For example, we can convert the GOES wavelength range to Hz or keV easily using the `~astropy.units.equivalencies.spectral`. .. GENERATED FROM PYTHON SOURCE LINES 55-59 .. code-block:: default print(([0.5, 4.0] * u.angstrom).to('Hz', u.spectral())) print(([0.5, 4.0] * u.angstrom).to('keV', u.spectral())) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [5.99584916e+18 7.49481145e+17] Hz [24.79683969 3.09960496] keV .. GENERATED FROM PYTHON SOURCE LINES 60-61 Astropy provides a number of reference constants .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: default from astropy import constants as astropy_const .. GENERATED FROM PYTHON SOURCE LINES 64-65 SunPy also provides a number of relevant solar reference constants. .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: default from sunpy.sun import constants as sunpy_const .. GENERATED FROM PYTHON SOURCE LINES 68-70 `~astropy.units.Constant` are simply quantities but they also provide an uncertainty and a reference .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: default M_earth = astropy_const.M_earth print("The mass of the Earth is {0} +/- {1} {2} [ref {3}].".format(M_earth.value, M_earth.uncertainty, M_earth.unit, M_earth.reference)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none The mass of the Earth is 5.972167867791379e+24 +/- 1.3422009501651213e+20 kg [ref IAU 2015 Resolution B 3 + CODATA 2018]. .. GENERATED FROM PYTHON SOURCE LINES 74-75 The light travel time in minutes from the Sun to the Earth can be calculated .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: default print((sunpy_const.au / astropy_const.c).to('min')) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 8.316746397269274 min .. GENERATED FROM PYTHON SOURCE LINES 78-80 Let's define a function to calculate the plasma beta, with quantities we don't have to worry about much beyond getting the equation correct .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: default def plasma_beta(n, T, B): return (2 * n * astropy_const.k_B * T) / (B ** 2 / (2 * astropy_const.mu0)) .. GENERATED FROM PYTHON SOURCE LINES 84-86 The plasma beta for the solar corona using appropriate parameters is given by the following. The decompose function works to simplify the units. .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. code-block:: default print(plasma_beta(1e9 * u.cm**-3, 3e6 * u.Kelvin, 10 * u.Gauss).decompose()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 0.2081969643814698 .. GENERATED FROM PYTHON SOURCE LINES 89-94 If the input is given in the wrong units then an error may occur but a better way is to inforce the units on input. Let's consider a simpler example here to calculate velocity. We use a function annotation to specify the units (this is a Python 3.5+ feature, see the `quantity_input `_ documentation for more details and Python 2 instructions): .. GENERATED FROM PYTHON SOURCE LINES 94-98 .. code-block:: default @u.quantity_input def speed(length: u.m, time: u.s): return length / time .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.010 seconds) .. _sphx_glr_download_generated_gallery_units_demo.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/HeliophysicsPy/gallery/gh-pages?urlpath=lab/tree/notebooks/generated/gallery/units_demo.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: units_demo.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: units_demo.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_