# Wind Direction

This entry is part 5 of 10 in the series NumPy Weather

Wind is the movement of air due to difference in atmospheric pressure. The KNMI De Bilt data file has a column for  the vector mean wind direction in degrees (360 = north, 90 = east, 180 = south, 270 = west, 0 = calm/variable). We will plot a histogram of that data and compute the corresponding average temperature for each wind direction. It seems reasonable to expect that the direction from which wind originates influences temperature. With other words some locations tend to be warmer or colder so air emanating from there will be warmer or colder respectively. The Netherlands as you may know doesn’t have any mountains, so we don’t have to take that into account. We do have to remind ourselves of the proximity of the North Sea. The Netherlands has a moderate maritime climate with southwestern winds.

Imports

We need to import the same packages we imported yesterday – NumPy (line 1), the NumPy masked arrays (line 2) package and Matplotlib (line 3).

```1 2 3 4 import numpy as np import numpy.ma as ma import matplotlib.pyplot as plt import sys```

Today we will load the wind direction and average temperatures into NumPy arrays (line 2). Wind direction has missing values so some conversion is in order (line 1). We will create a masked array from the wind direction values (line 3).

```1 2 3 4 5 6 to_float = lambda x: float(x.strip() or np.nan) wind_direction, avg_temp = np.loadtxt(sys.argv, delimiter=',', usecols=(2, 11), unpack=True, converters={2: to_float}) wind_direction = ma.masked_invalid(wind_direction)   # Measurements are in .1 degrees Celcius avg_temp = .1 * avg_temp```

Averaging

We can average over wind directions the way we averaged yesterday over years by going over each of the possible wind directions, finding the corresponding temperature values and averaging them.

```1 2 3 4 5 avgs = []   for direction in xrange(360): indices = np.where(direction == wind_direction) avgs.append(avg_temp[indices].mean())```

Plotting the Wind Direction Histogram and Temperatures

We will plot the wind direction histogram and average temperatures per wind direction.

```1 2 3 4 5 6 7 8 plt.subplot(211) plt.title("Wind Direction Histogram") plt.hist(wind_direction.compressed(), 200)   plt.subplot(212) plt.title("Average Temperature vs Wind Direction") plt.plot(np.arange(360), avgs) plt.show()```

We get the plots below as a result. Notice the peak in the histogram that corresponds with a southwestern wind direction. The average temperature seems to be bottoming around 50 degrees.

The code for today’s script is listed below.

```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import numpy as np import numpy.ma as ma import matplotlib.pyplot as plt import sys   to_float = lambda x: float(x.strip() or np.nan) wind_direction, avg_temp = np.loadtxt(sys.argv, delimiter=',', usecols=(2, 11), unpack=True, converters={2: to_float}) wind_direction = ma.masked_invalid(wind_direction)   # Measurements are in .1 degrees Celcius avg_temp = .1 * avg_temp   avgs = []   for direction in xrange(360): indices = np.where(direction == wind_direction) avgs.append(avg_temp[indices].mean())   plt.subplot(211) plt.title("Wind Direction Histogram") plt.hist(wind_direction.compressed(), 200)   plt.subplot(212) plt.title("Average Temperature vs Wind Direction") plt.plot(np.arange(360), avgs) plt.show()```

Books
If you feel that I am going too fast, you might want to check out one of my NumPy books. Links with more information can be found on this page. Found it? Great!

Collected News for April 5, 2013

Short summaries:

• “Scientists say the rapid melting of the Quelccaya ice cap, the world’s largest tropical ice sheet, is the latest sign of global warming.”
• “Hydrogen fuel cell cars have any number of hurdles to overcome, whether it’s widespread adoption or the basic matter of locating a place to fill up.”
• “Findings from a new study, published this week, suggest that technology could soon allow the reading of dreams.”

http://storify.com/inningPalmer/collected-news-for-april-5-2013