Science projects with the Python MetPy library

Homework Helper

© Lead Image © Pavel Romanchenko,

© Lead Image © Pavel Romanchenko,

Article from Issue 235/2020

MetPy can help you answer your kids' science questions.

Are you stumped by your kids' homework? Sometimes answering their science questions can be quite challenging. Luckily, some great resources and software packages can help parents out of a homework jam. A good example is the MetPy Python library [1], which has some awesome thermodynamic and weather functions.

In this article, I introduce the MetPy library and show how it can be used to solve questions like: Can you make snow when it's above freezing? How much thinner is the air with altitude? How do you calculate wind chill?

Getting Started

To install the MetPy Python library, enter:

pip install metpy

One of nice things about MetPy is that it manages scientific units, so a variable is defined with both its value and units. In the code

>>> from metpy.units import units
>>> tempToday = [40] * units.degF
<Quantity([4.44444444], 'degree_Celsius')>

the units module makes a simple temperature conversion. In this example, a temperature of 40°F is converted to 4.4°C with the to() method.

You can also do some interesting mixing of units in math calculations. For example, you can add 6 feet and 4 meters:

>>> print( [6] * units.feet + [4] * units.m)
[19.123359580052494] foot

MetPy has a very large selection of thermodynamic and weather functions, which I demonstrate in the next sections.

Can You Make Snow When It's Above Freezing?

Ski resorts can make snow by forcing water and pressurized air through a "snow gun" (Figure 1). Snowmaking [2] can be an expensive operation, but it allows ski resorts to extend their season.

Figure 1: A snow gun makes snow.

When you get a weather forecast, the temperature is given as the ambient, or dry-bulb, temperature. The wet-bulb temperature takes the dry air temperature and relative humidity into account. The wet-bulb temperature is always less than the outside temperature, so snowmaking can take placer if the wet-bulb temperature is below -2.5°C or 27.5°F.

MetPy has a number of functions that can find the humidity and wet-bulb temperature (wet_bulb_temperature()), which just needs the pressure, dry temperature, and dew point.

In Listing 1, the temperature is below freezing, but it's not possible to make snow, because the wet-bulb temperature is only -0.6°C (not the required -2.5°C).

Listing 1

Wet-Bulb Temp


Knowing that -2.5°C (27.5°F) is the wet-bulb temperature upper limit for snowmaking, the relative_humidity_wet_psychrometric function can be used to create a curve of humidity and dry temperature points that shows where it is possible to make snow (Figure 2).

Figure 2: When you can make snow.

Listing 2 shows how to use the MetPy library to find when snow can be made. The code iterates between the temperatures of -10°C to 10°C (line 18), getting the relative humidity with the relative_humidity_wet_psychrometric call (line 21). The relative humidity is multiplied by 100 to get a percentage and is made unitless by the to_tuple()[0] method (line 23). The results are plotted by Matplotlib library functions (lines 29-34).

Listing 2

When to Make Snow


From the data, you can see that if the humidity is low, it is possible to make snow, even when the temperature is above freezing (+4°C is the typical cut-off temperature).

How Much Thinner is the Air with Altitude?

Everyone knows the air is thinner when you're in an airplane, but how much thinner is it in Denver or Mexico City compared with New York City?

Again, MetPy can help. The height_to_pressure_std function calculates a pressure value by altitude. The to() method converts the pressure to standard atmospheres (Listing 3).

Listing 3

Air Pressure by Altitude


The results show that the air pressure in Denver is 82 percent that of New York's, or 18 percent thinner, and Mexico City's atmosphere is 76 percent that of New York's, or 24 percent thinner.

Listing 4 uses the height_to_pressure_std function to create a chart (Figure 3) of atmospheric pressure between sea level and the top of Mt. Everest (29,000ft). At the top of Mt. Everest, the air is 70 percent thinner than at sea level!

Listing 4

Air Thinning with Altitude


Figure 3: How the atmosphere changes when climbing a mountain.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Instrumented Garden

    Place long-range wireless sensors in a garden and keep track of ambient conditions with gauges and time-based graphs.

  • Sensor Shootout

    Any application that collects a large number of measurements is bound to have some anomalous measurements, but good sensor breakouts should not output such values all the time. We tested eight temperature and humidity sensors for accuracy.

  • Monitoring Beehives

    Beekeepers can get to know their colonies better without continuously disturbing the industrious insects. Using a Raspberry Pi and various sensors, two hobby beekeepers monitor the temperature and humidity of their hives, with plans to monitor their weight.

  • Charly's Column: Weather Page

    To find out what the weather is like, sys admin columnist Charly Kühnast no longer needs to go outdoors get wet, blown away, frozen to death, or sunburned.

  • Adafruit IO API

    The Adafruit IO API offers a convenient means for network-ready sensors and other components.

comments powered by Disqus