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

<p>One way to put this in a numpy array is:</p>

<pre><code>uwind = np.zeros((lat,lon), np.float)
uwind = U.variables['uwnd'][1,:,:]
</code></pre>

<p>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 <code>1</code> into the array.  <code>lat</code> and <code>lon</code> above are your grid dimensions 73 and 144 and <code>np</code> is the <code>numpy</code> module.</p>

<p>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 <code>x</code> and <code>y</code> array as well as <code>uwind</code> and <code>vwind</code> you can plot the wind barbs using matplotlib as:</p>

<pre><code>plt.barbs(x, y, u, v)
</code></pre>

<p>and can <a href="http://matplotlib.org/examples/pylab_examples/barb_demo.html">customize their look with various options</a>. The <code>plt</code> reference is the <code>matplotlib.pyplot</code> module.</p>

<p>For further reading, here are <a href="http://schubert.atmos.colostate.edu/~cslocum/netcdf_example.html">some examples of plotting netcdf data</a> from a friend of mine at CSU.</p>

CC BY-SA 3.0
2015-12-03T00:15:28.340Z
2015-12-03T01:43:51.387Z
2015-12-03T01:43:51.387Z
casey
<p>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:
<a href="https://i.sstatic.net/G9XEA.png" rel="noreferrer"><img src="https://i.sstatic.net/G9XEA.png" alt="enter image description here"></a>
How do I read a uwnd variable in a netCDF file in python?</p>

<pre><code>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
</code></pre>

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

<pre><code>OrderedDict([(u'lat', &lt;type 'netCDF4._netCDF4.Variable'&gt;
float32 lat(lat)
    units: degrees_north
    actual_range: [ 90. -90.]
    long_name: Latitude
unlimited dimensions: 
current shape = (73,)
filling off
), (u'lon', &lt;type 'netCDF4._netCDF4.Variable'&gt;
float32 lon(lon)
    units: degrees_east
    long_name: Longitude
    actual_range: [   0.   357.5]
unlimited dimensions: 
current shape = (144,)
filling off
), (u'time', &lt;type 'netCDF4._netCDF4.Variable'&gt;
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', &lt;type 'netCDF4._netCDF4.Variable'&gt;
float32 uwnd(time, lat, lon)
    long_name: u wind
    valid_range: [-99999.  99999.]
    actual_range: [ -26.84999084  377.20001221]
    units: m/s
    add_offset: 0.0
    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'
</code></pre>

<p>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?</p>

<p>Here is the site I found on how to read netCDF files:
<a href="http://www.hydro.washington.edu/~jhamman/hydro-logic/blog/2013/10/12/plot-netcdf-data/" rel="noreferrer">http://www.hydro.washington.edu/~jhamman/hydro-logic/blog/2013/10/12/plot-netcdf-data/</a></p>

CC BY-SA 3.0
2015-12-03T20:14:54.427Z
If you can link to the netcdf files I can show a more complete example.
casey
CC BY-SA 3.0
2015-12-04T06:15:41.817Z
http://meta.earthscience.stackexchange.com/questions/1513/are-programming-questions-on-topic
casey
<meteorology><wind><visualization><netcdf><python>
Plotting wind barbs in python
Brian B.
# Plotting wind barbs in python

(Asked by: username_0 on 2015-12-02)

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:
[![enter image description here](https://i.sstatic.net/G9XEA.png)](https://i.sstatic.net/G9XEA.png)
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
    add_offset: 0.0
    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/>

**Tags:** meteorology, wind, visualization, netcdf, python

> **Comments:**
> **username_1**: If you can link to the netcdf files I can show a more complete example.  
> **username_1**: http://meta.earthscience.stackexchange.com/questions/1513/are-programming-questions-on-topic  

---

## 1 answers

### Answer by username_1 on 2015-12-03. Score: 14 | Accepted answer

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](http://matplotlib.org/examples/pylab_examples/barb_demo.html). The `plt` reference is the `matplotlib.pyplot` module.

For further reading, here are [some examples of plotting netcdf data](http://schubert.atmos.colostate.edu/~cslocum/netcdf_example.html) from a friend of mine at CSU.


---

