# Plotting wind barbs in python

I'm trying to plot wind barbs in basemap and I have two separate netCDF files. Here is what I am trying to produce, or something like it: How do I read a uwnd variable in a netCDF file in python?

from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset, date2index
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
from IPython.display import Image
from IPython.core.display import HTML
import netCDF4 as nc
plt.clf()
nc_file = '042711_V.nc'
nc_file2 = '042711_U.nc'
V = Dataset(nc_file, mode='r')
U = Dataset(nc_file2, mode='r')
print U.variables


This verifies that uwnd is indeed a variable, but why won't it read?

OrderedDict([(u'lat', <type 'netCDF4._netCDF4.Variable'>
float32 lat(lat)
units: degrees_north
actual_range: [ 90. -90.]
long_name: Latitude
unlimited dimensions:
current shape = (73,)
filling off
), (u'lon', <type 'netCDF4._netCDF4.Variable'>
float32 lon(lon)
units: degrees_east
long_name: Longitude
actual_range: [   0.   357.5]
unlimited dimensions:
current shape = (144,)
filling off
), (u'time', <type 'netCDF4._netCDF4.Variable'>
float64 time(time)
units: hours since 1800-1-1 00:00:0.0
long_name: Time
actual_range: [ 71870952.  71870952.]
delta_t: 0000-01-00 00:00:00
unlimited dimensions: time
current shape = (1,)
filling off
), (u'uwnd', <type 'netCDF4._netCDF4.Variable'>
float32 uwnd(time, lat, lon)
long_name: u wind
valid_range: [-99999.  99999.]
actual_range: [ -26.84999084  377.20001221]
units: m/s
scale_factor: 1.0
missing_value: -9.96921e+36
precision: 99
least_significant_digit: 99
var_desc: u wind
dataset: CDC Derived NCEP Reanalysis Products
level_desc:  500mb Pressure Level
statistic: Composite
parent_stat: Other
unlimited dimensions: time
current shape = (1, 73, 144)
filling off
)])

KeyError: 'uwnd'


I'm trying to plot some widnbarbs on the map. i have successfully gotten it to read the time, longitude, and latitude, bu what does the uwnd mean and how do I read it into the numpy format?

Here is the site I found on how to read netCDF files: http://www.hydro.washington.edu/~jhamman/hydro-logic/blog/2013/10/12/plot-netcdf-data/

Your uwnd variable holds 32 bit floats and has shape (1,73,144) corresponding to time, lat, lon and is located in the Dataset you have called 'U'.

One way to put this in a numpy array is:

uwind = np.zeros((lat,lon), np.float)
uwind = U.variables['uwnd'][1,:,:]


The first line sets the size of the uwind array, which is helpful from a performance standpoint and the second loads the data from time 1 into the array. lat and lon above are your grid dimensions 73 and 144 and np is the numpy module.

The u wind is just the east-west component of the wind. The north-south component will be in the v wind variable. You will need both to plot the wind. To plot the barbs you will need to setup arrays for your x and y coordinates which will depend on lat,lon and possibly your map projection. once you have an x and y array as well as uwind and vwind you can plot the wind barbs using matplotlib as:

plt.barbs(x, y, u, v)


and can customize their look with various options. The plt reference is the matplotlib.pyplot module.

For further reading, here are some examples of plotting netcdf data from a friend of mine at CSU.