In [1]:
from astropy import units as u
In [2]:
# Define a quantity length
length = 26.2 * u.meter
# print it
print(length) # length is a quantity
In [3]:
# Type of quantity
type(length)
Out[3]:
In [4]:
# Type of unit
type(u.meter)
Out[4]:
In [5]:
# Quantity
length
Out[5]:
In [6]:
# value
length.value
Out[6]:
In [7]:
# unit
length.unit
Out[7]:
In [8]:
# information
length.info
Out[8]:
Quantities can be converted to other units systems or factors by using to()
In [9]:
# Convert it to: km, lyr
print(length.to(u.km))
print(length.to(u.lightyear))
We can do arithmetic operations when the quantities have the compatible units:
In [10]:
# arithmetic with distances
distance_start = 10 * u.mm
distance_end = 23 * u.km
length = distance_end - distance_start
print(length)
Quantities can also be combined, for example to measure speed
In [11]:
# calculate a speed
time = 15 * u.minute
speed = length / time
print(speed)
In [12]:
# decompose it
print(speed.decompose())
print(speed.si)
In [13]:
#1
from astropy.units import imperial
print(speed.to(imperial.mile/u.hour))
In [14]:
#2
imperial.pint > 0.5 * u.l
# A liquid pint in US is 473 ml; in UK is 568 ml
Out[14]:
In [15]:
#3
rectangle_area = 3 * u.km * 5 * u.m
print(rectangle_area)
print(rectangle_area.decompose())
print(rectangle_area.to(imperial.yard ** 2))
In [16]:
# create a composite unit
cms = u.cm / u.s
speed.to(cms)
Out[16]:
In [17]:
# and in the imperial system
mph = imperial.mile / u.hour
speed.to(mph)
Out[17]:
and others are already a composition:
In [18]:
# what can be converted from s-1?
(u.s ** -1).compose()
Out[18]:
In [19]:
# or Jules?
(u.joule).compose()
Out[19]:
In [20]:
# Unity of R
(13.605692 * u.eV).to(u.Ry)
Out[20]:
Sometime we get no units quantitites
In [21]:
# no units
nounits = 20. * u.cm / (1. * u.m)
nounits
Out[21]:
What happen if we add a number to this?
In [22]:
# arithmetic with no units
nounits + 3
Out[22]:
In [23]:
# final value of a no unit quantity
nounits.decompose() # It's a unitless quantity
Out[23]:
In [24]:
# converting spectral quantities
(656.281 * u.nm).to(u.Hz) # Fails because they are not compatible
In [25]:
# but doing it right
(656.281 * u.nm).to(u.Hz, equivalencies=u.spectral())
Out[25]:
Other built-in equivalencies are:
parallax()dopplr_radio, doppler_optical, doppler_relativistic)
In [26]:
# finding the equivalencies
u.Hz.find_equivalent_units()
Out[26]:
In [27]:
# but also using other systems
u.Hz.find_equivalent_units(equivalencies=u.spectral())
Out[27]:
In [28]:
# Printing values with different formats
print("{0.value:0.03f} {0.unit:FITS}".format(speed))
print("{0.value:0.03f} {0.unit:latex_inline}".format(speed))
In [29]:
# different ways of defining a quantity for a single value
length = 44 * u.m
time = u.Quantity(23, u.s)
speed = length / time
speed
Out[29]:
In [30]:
# now with lists
length_list = [1, 2, 3] * u.m
# and arrays
import numpy as np
time_array = np.array([1, 2, 3]) * u.s
# and its arithmetics
length_list / time_array
Out[30]:
In [31]:
# angles are smart!
angle = u.Quantity(np.arange(180), u.deg)
print(angle[[0, -1]])
print(np.sin(angle[[0, -1]]))
In [32]:
# allowing for plotting
from astropy.visualization import quantity_support
quantity_support()
# loading matplotlib
%matplotlib inline
from matplotlib import pyplot as plt
In [33]:
# Ploting the previous array
plt.plot(angle, np.sin(angle))
Out[33]:
In [34]:
# Create a function for the Kinetic energy
@u.quantity_input(mass=u.kg, speed=u.m/u.s)
def kinetic(mass, speed):
return (mass * speed ** 2 / 2.).to(u.joule)
In [35]:
# run with and without units
kinetic(5, 10) # Fails! it doesn't specify the units.
kinetic(5 * u.kg, 100 * cms)
Out[35]:
In [36]:
#4
@u.quantity_input(mass=u.kg, height=u.m, g=u.m / u.s ** 2)
def potential(mass, height, g=9.8 * u.m / u.s **2):
return (0.5 * mass * g * height).to(u.joule)
In [37]:
# run it for some values
potential(5 * u.kg, 30 *u.cm)
Out[37]:
In [38]:
# on Mars:
potential(5 * u.kg, 1 * u.m, g=3.75 * u.m/u.s**2)
Out[38]:
In [39]:
# Create units for a laugh scale
titter = u.def_unit('titter')
chuckle = u.def_unit('chuckle', 5 * titter)
laugh = u.def_unit('laugh', 4 * chuckle)
guffaw = u.def_unit('guffaw', 3 * laugh)
rofl = u.def_unit('rofl', 4 * guffaw)
death_by_laughing = u.def_unit('death_by_laughing', 10 * rofl)
print((1 * rofl).to(titter))
In [40]:
#5
ares = u.def_unit('ares', (10 * u.m)**2)
hectar = u.def_unit('hectares', 100 * ares)
print(rectangle_area.to(hectar))