# Wind Speed

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

Wind speed is a very important quantity. The KNMI De Bilt data files has daily average wind speed data too, expressed in meters per second.

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```

We will load the wind direction, wind speed and average temperature into NumPy arrays (line 2). Wind direction and speed have missing values so some conversion is in order (line 1). We will create a masked array from the wind direction and speed values (lines 3 and 4).

```1 2 3 4 to_float = lambda x: float(x.strip() or np.nan) wind_direction, wind_speed, avg_temp = np.loadtxt(sys.argv, delimiter=',', usecols=(2, 4, 11), unpack=True, converters={2: to_float, 4: to_float}) wind_direction = ma.masked_invalid(wind_direction) wind_speed = ma.masked_invalid(wind_speed)```

Statistics

We will go through the usual statistics of wind speed – minimum (line 2), maximum (line 2), average (line 3), standard deviation (line 3) and correlation (line 5) with  average temperatures. Note that for the correlation computation we need to match the average temperature values with the valid wind speed values. We do that by negating the mask of the wind speed array giving us the indices of valid values.

```1 2 3 4 5 print "# Wind Speed values", len(wind_speed.compressed()) print "Min speed", wind_speed.min(), "Max speed", wind_speed.max() print "Average", wind_speed.mean(), "Std. Dev", wind_speed.std()   print "Correlation of wind speed and temperature", np.corrcoef(avg_temp[~wind_speed.mask], wind_speed.compressed())```

In the output we see a weak negative correlation between wind speed and temperature.

```# Wind Speed values 39871 Min speed 0.0 Max speed 16.5 Average 4.2211381706 Std. Dev 1.93906822268 Correlation of wind speed and temperature -0.126166541437```

Averaging

We can average the wind speed over wind directions just like we did yesterday.

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

Plotting the Wind Speed Histogram and Average Wind Speeds

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

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

The histogram looks strange, but it seems that’s what wind speed histograms look like. At least I found several similar looking charts on the Web.

The full program code listing is shown below in its entirety.

```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 27 28 29 30 31 32 33 34 35 36 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, wind_speed, avg_temp = np.loadtxt(sys.argv, delimiter=',', usecols=(2, 4, 11), unpack=True, converters={2: to_float, 4: to_float}) wind_direction = ma.masked_invalid(wind_direction) wind_speed = ma.masked_invalid(wind_speed)   # Measurements are in .1 m/s wind_speed = .1 * wind_speed   # Measurements are in .1 degrees avg_temp = .1 * avg_temp   print "# Wind Speed values", len(wind_speed.compressed()) print "Min speed", wind_speed.min(), "Max speed", wind_speed.max() print "Average", wind_speed.mean(), "Std. Dev", wind_speed.std()   print "Correlation of wind speed and temperature", np.corrcoef(avg_temp[~wind_speed.mask], wind_speed.compressed())   avgs = []   for direction in xrange(360): indices = np.where(direction == wind_direction) avgs.append(wind_speed[indices].mean())   plt.subplot(211) plt.title("Wind Speed Histogram") plt.hist(wind_speed.compressed(), 200)   plt.subplot(212) plt.title("Wind Speed vs Wind Direction") plt.plot(np.arange(360), avgs) plt.show()```

List for April 6, 2013

http://storify.com/inningPalmer/list-for-april-6-2013

Series NavigationWind DirectionPrecipitation and Sunshine Duration
By the author of NumPy Beginner's Guide, NumPy Cookbook and Instant Pygame. If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
This entry was posted in programming and tagged , . Bookmark the permalink.