Science projects with the Python MetPy library
Homework Helper
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 >>> tempToday.to(units.degC) <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.
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).
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
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.