Numpy/Scipy Recipes
Taking a quick look at velocity vectors from U and V fields in NetCDF files.
# Invoke ipython with the scipy/numpy/pylab capabilities.
ipython -pylab
# Import the capabilities of the Python-NetCDF package.
import netCDF3,netCDF4
# Create an object containing the NetCDF file file.nc.
nc = netCDF3.Dataset('file.nc','r+')
# Extract the U and V velocity variables called Uwind and Vwind in the NetCDF file.
U = nc.variables['Uwind'][:]
V = nc.variables['Vwind'][:]
# Create a vector plot choosing the first time step and every 5th spatial step.
quiver(U[1,::5,::5], V[1,::5,::5])
Interrogating a NetCDF file to discover what lies within.
# Create an object containing the NetCDF file file.nc.
nc = netCDF3.Dataset('file.nc','r+')
# Discover the variables therein.
nc.variables
{'Uwind': ,
'Vwind': ,
'lat_rho': ,
'lon_rho': ,
'wind_time': }
# Extract the Uwind variable field into u.
u = nc.variables['Uwind'][:]
# Find the shape of u.
u.shape
(248, 246, 288)
# Discover the dimension names therein.
nc.dimensions
{'eta_rho': ,
'wind_time': ,
'xi_rho': }
# Find the size of a given dimension.
len(nc.dimensions['wind_time'])
248
# Discover the file format.
nc.file_format
# Discover the global attributes.
nc.ncattrs()
['title',
'institution',
'source',
'history',
'note1',
'note2',
'note3',
'note4']
# Discover a global attribute value.
nc.title
'ECMWF Global Interim Winds Interpolated to ROMS Oman Grid'
# Discover variable attribute values. First, create an object, e.g.
uu = nc.variables['Uwind']
# Next, find out the attributes for that variable.
uu.ncattrs()
['units',
'long_name',
'time',
'standard_units',
'standard_name',
'origin',
'model',
'center',
'geographical_limits']
# Finally, find the value for a specific attribute.
uu.units
'meter second-1'