Computational Guided Inquiry for Modeling Earth's Climate (Neshyba, 2023)¶

Unit conversions with pint¶

The goal of this module is to introduce you to a super-handy unit manipulation package called pint.

Units vs dimensions¶

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.

Climate context¶

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.

e-format convention for entering numbers that are very big and very small¶

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).

Learning goals¶

The main learning goals of this exercise can be phrased follows.

  1. I'm familiar with key aspects of pint, e.g., specifying quantities with units, and using the ".ito" syntax for conversions.
  2. I can enter numbers in scientific notation using the e-format convention.
  3. I can print using f-string syntax.
In [1]:
from pint import UnitRegistry; AssignQuantity = UnitRegistry().Quantity

e-format notation¶

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.

In [2]:
kappa = 6.14e-1
print(kappa)
0.614

Your turn¶

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").

In [3]:
### BEGIN SOLUTION
sigma = 5.67e-8
c = 3e8
### END SOLUTION

Defining variables with units¶

In the cell below, we use pint's AssignQuantity function to define $c$ as a variable with units.

In [4]:
c = AssignQuantity(3e8,'m/s')
print(c)
300000000.0 meter / second

Your turn¶

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:

  • Use AssignQuantity to define $T=287 K$, the average temperature of the earth in pre-industrial times (call it "T");
  • Use AssignQuantity to define $m=1.67 \times 10^{-24} g$, the mass of a proton (and call it "m").

Print all three variables.

In [5]:
### 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

Unit conversions¶

The cell below shows how to convert a quantity that has units, to other units.

In [6]:
c.ito('miles/hour')
print(c)
671080887.6163206 mile / hour

Your turn¶

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}$.

In [7]:
# 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

Calculating with variables that have units¶

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.

In [8]:
E = m*c**2; print(E)
E.ito('J'); print(E)
7.520837613989276e-07 gram * mile ** 2 / hour ** 2
1.503e-10 joule

Predicting units¶

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$.

BEGIN SOLUTION¶

Should be joule/m^2

END SOLUTION¶

Your turn¶

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.

In [9]:
### BEGIN SOLUTION
OLR = sigma*kappa*T**4
print(OLR)
### END SOLUTION
236.1995234026218 watt / meter ** 2

f-string printing¶

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.

In [10]:
print(f"sigma: {sigma}")
sigma: 5.67e-08 watt / kelvin ** 4 / meter ** 2

Your turn¶

In the cell below, print the temperature and the OLR using the f-string method.

In [11]:
### BEGIN SOLUTION
print(f"T: {T}")
print(f"OLR: {OLR}")
### END SOLUTION
T: 287 kelvin
OLR: 236.1995234026218 watt / meter ** 2

Validating and finishing up¶

Assuming all this has gone smoothly and a Kernel/Restart & Run All has run without error, don't forget to

  1. Close this notebook using the "File/Close and Halt" dropdown menu
  2. Using the Assignments tab, submit this notebook
  3. Press the Logout tab of the Home Page