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 Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs