MetPy Mondays #4 - Units in MetPy

I remember the first class I took in which the professor required that we include units by every quantity in every step of every calculation we did… or it was wrong. I thought this policy was a bit harsh, but after one or two assignments, I was getting the hang of it. By the end of the semester I realized that it was insane to work any other way. In science, we are dealing with physical quantities that represent things in the real world – and things in the real world have units. Keeping track of units throughout a calculation caught many errors I made while solving and rearranging equations. If keeping track of units on paper is a good idea, why is computing any different? In this MetPy Monday, we'll look at how MetPy uses units and how to convert between different units.

To get started, open up a terminal and activate an environment with MetPy installed. Checkout out the Conda Environments MetPy Monday if you need help with this. I'll be using the Unidata workshop environment. I'll also be working in a Jupyter notebook.

MetPy relies on a Python library called pint to handle our units. By using some special code under the hood, MetPy will even tell you if you pass a function values with incorrect units. But before we get to that, we need to understand how to attach units to a number. First, we need to import the units registry from metpy.units:

Let's create a temperature value and assign it the units of degrees Celcius. The first part of this assignment looks perfectly normal, but then we multiply the value by the appropriate units we want to attach. Printing the value out we can see that units are indeed attached.

Converting between units is as simple as using the .to command:

Easy! But what about quantities with more complex units? The units library is capable of parsing a string for the correct units. Let's create u and v wind components with different units:

Units can parse m/s as meters/second and assign the unit. We can now call some of MetPy's calculation routines and let them worry about making sure everything is the correct unit before performing any calculations.

Nice! The u and v components were converted to a common unit before the calculation was performed. We can also convert the result to whatever we desire, even the absurd.

What if we pass the incorrect dimensions to a calculation? In other words there is no unit conversion to make things work out correctly and something has gone horribly wrong. Maybe we accidently reassigned a variable somewhere and what we think is u component wind is really a temperature.

MetPy gives us an error message telling us that there are incorrect dimensions for the calculation. Using units can make your calculations more reliable and means you have to write fewer lines of code. Never again will you write a function to convert degrees Fahrenheit to degrees Celsius, nor should you! Thanks for joining us on this week's MetPy Monday!

Comments:

Post a Comment:
  • HTML Syntax: Allowed
Unidata Developer's Blog
A weblog about software development by Unidata developers*
Unidata Developer's Blog
A weblog about software development by Unidata developers*

Welcome

FAQs

News@Unidata blog

Take a poll!

What if we had an ongoing user poll in here?

Browse By Topic
Browse by Topic
« October 2017
SunMonTueWedThuFriSat
1
3
4
5
6
7
8
10
11
12
13
14
15
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
       
Today