The goal of this module is to introduce you to a super-handy unit manipulation package called pint.
Units are always a bit of a bother in, well, all quantitative sciences. But a package called pint can help quite a lot. For one, it has a ton of built-in conversions, and it is easy to convert from one unit to another. But a more important point is that this kind of tool takes the focus off units, and puts it on the dimensions of the quantities you're dealing with, which is arguably more fundamental.
To have something to familiar work with, we'll be using math around the idea of Outgoing Longwave Radiation (OLR) that we considered previously. As a reminder: we imagine that we are standing above the earth's surface (like on a satellite), armed with a longwave radiometer. Looking down at the earth's surface, the quantity our longwave radiometer will measure can be predicted by the formula
$$ OLR = \kappa \sigma T^4 \ \ \ \ (1) $$where $T$ is the temperature of Earth's surface, $\sigma$ is called the Stefan-Boltzmann constant, and $\kappa$ is a dimensionless value related to how Earth's atmosphere intercepts longwave radiation emitted by Earth's surface.
When working with real geophysical data, it's important to get used to entering numbers in scientific notation using the e-format convention. For example, the Stefan-Boltzmann's constant, $\sigma=5.67 \times 10^{-8} {J \over m^2 K^4}$, would be entered using the e-format notation as "5.67e-8" (the "e" can be "E", by the way; it's not case-sensitive).
The main learning goals of this exercise can be phrased follows.
from pint import UnitRegistry; AssignQuantity = UnitRegistry().Quantity
To have something to familiar work with, we'll start with the notion of Outgoing Longwave Radiation (OLR) we considered previously. Imagine that you are standing above the earth's surface (like on a satellite), armed with a longwave radiometer, which is an instrument that measures the radiant energy passing through a 1 meter x 1 meter hoop. Looking down at the earth's surface, the quantity our longwave radiometer will measure can be predicted by the formula
$$ OLR = \kappa \sigma T^4 \ \ \ \ (1) $$where $T$ is the temperature of Earth's surface, $\sigma$ is a constant of nature, and $\kappa$ is a dimensionless constant related to the longwave radiation emitted by Earth's surface that gets captured by greenhouse gases in Earth's atmosphere ($\kappa=0.614$). In the cell below, we define the latter using the e-format notation.
kappa = 6.14e-1
print(kappa)
0.614
Now define $\sigma=5.67 \times 10^{-8}$ (you can call the variable "sigma") using the e-format notation. Also, for practice, define $c=3\times 10^8$ (and call it "c").
### BEGIN SOLUTION
sigma = 5.67e-8
c = 3e8
### END SOLUTION
In the cell below, we use pint's AssignQuantity function to define $c$ as a variable with units.
c = AssignQuantity(3e8,'m/s')
print(c)
300000000.0 meter / second
Now use AssignQuantity to define $\sigma=5.67 \times 10^{-8} {W \over m^2 K^4}$ as a variable with units (call it "sigma" again). While you're at it, do this too:
Print all three variables.
### BEGIN SOLUTION
sigma = AssignQuantity(5.67e-8,'W/(m^2 K^4)'); print(sigma)
T = AssignQuantity(287,'K'); print(T)
m = AssignQuantity(1.67e-24,'g'); print(m)
### END SOLUTION
5.67e-08 watt / kelvin ** 4 / meter ** 2 287 kelvin 1.67e-24 gram
The cell below shows how to convert a quantity that has units, to other units.
c.ito('miles/hour')
print(c)
671080887.6163206 mile / hour
Use the ".ito" syntax to convert your value of $\sigma$ to ${W \over cm^2 K^4}$. Then convert it back to ${W \over m^2 K^4}$.
# Convert to cal/(m**2 K**4), and print the result
### BEGIN SOLUTION
sigma.ito('W/(cm**2 K**4)'); print(sigma)
### END SOLUTION
# Convert back to joules/(m**2 K**4), and print the result
### BEGIN SOLUTION
sigma.ito('W/(m**2 K**4)'); print(sigma)
### END SOLUTION
5.67e-12 watt / centimeter ** 2 / kelvin ** 4 5.67e-08 watt / kelvin ** 4 / meter ** 2
If you calculate a number that has units, the result has units too! Assuming you've defined the mass as Python variable "m", and the speed of light as Python variable "c", the cell below computes the energy released when a proton's mass is converted into energy according to Einstein's $E=mc^2$. You'll notice that the first result has crazy units, but then we convert to the more conventional enery unit of Joules.
E = m*c**2; print(E)
E.ito('J'); print(E)
7.520837613989276e-07 gram * mile ** 2 / hour ** 2 1.503e-10 joule
According to Eq. 1 in the Introduction, OLR is the product of three quantities: $\kappa$, $\sigma$, and $T^4$. In the cell below, enter what you think the units of OLR should be, given that $\kappa$ is dimensionless, $\sigma$ has units ${J \over m^2 K^4}$, and $T$ has units $K$.
Now we'll see about your prediction! Use Eq. 1 in the Introduction to calculate the OLR from variables sigma, kappa, and T, and print the result. Double-check that the units of your result are consistent with your prediction.
### BEGIN SOLUTION
OLR = sigma*kappa*T**4
print(OLR)
### END SOLUTION
236.1995234026218 watt / meter ** 2
It's sometimes handy to format print statements in a way that's a bit more descriptive than what we've been doing so far. One such method uses the f-string syntax, as shown below.
print(f"sigma: {sigma}")
sigma: 5.67e-08 watt / kelvin ** 4 / meter ** 2
In the cell below, print the temperature and the OLR using the f-string method.
### BEGIN SOLUTION
print(f"T: {T}")
print(f"OLR: {OLR}")
### END SOLUTION
T: 287 kelvin OLR: 236.1995234026218 watt / meter ** 2
Assuming all this has gone smoothly and a Kernel/Restart & Run All has run without error, don't forget to