"NetCDF is an abstraction that supports a view of data as a collection of self-describing, portable objects that can be accessed through a simple interface. Array values may be accessed directly, without knowing details of how the data are stored. Auxiliary information about the data, such as what units are used, may be stored with the data. Generic utilities and application programs can access netCDF datasets and transform, combine, analyze, or display specified fields of the data. The development of such applications has led to improved accessibility of data and improved re-usability of software for array-oriented data management, analysis, and display." from http://www.unidata.ucar.edu/software/netcdf/docs/netcdf_introduction.html
netCDF files are arranged in a organized 'Dataset' heiarchy consisting of groups, dimensions, variables and attributes. For todays example we will ignore groups, but think of groups as sub-containers which hold their own dimensions, variables and attributes. Dimensions, variables and attributes will be discussed below but a general layout of a possible netCDF dataset may look like;
The first thing we need to do is create a new netCDF 'Dataset' by opening a netCDF file for reading from or writing to. We will use three parameters to create our Dataset file; file name, mode, and format. (eg. Dataset('filename', mode, format))
Want we want to do is open one netCDF Dataset, grab the data of interest, and plot it with matplotlib
In [18]:
from netCDF4 import Dataset
import numpy as np
import numpy.ma as ma
filename = "tos_O1_2001-2002.nc"
ds = Dataset(filename, mode="r")
In [8]:
print(ds)
In [9]:
md = ds.__dict__
for k in md:
print("{0}: {1}".format(k, md[k]))
In [10]:
print(ds.dimensions)
In [11]:
print(ds.variables['tos'])
In [12]:
time = ds.variables['time']
print(time)
print(time[:])
lats = ds.variables['lat'][:]
lons = ds.variables['lon'][:]
tos = ds.variables["tos"][:,:,:]
print(tos[0,:,:])
In [13]:
print ('time from {0} to {1}'.format(time[0], time[-1]))
tos_min = ma.min(tos)
tos_max = ma.max(tos)
tos_avg = ma.average(tos)
tos_med = ma.median(tos)[0]
print("Sea surface temperature: min={0}, max={1}".format(tos_min, tos_max))
print("Sea surface temperature: average={0}, median={1}".format(tos_avg, tos_med))
In [14]:
%matplotlib inline
import matplotlib.pyplot as plt
cp = plt.contour(tos[12,:,:], 100)
In [15]:
cp = plt.contour(tos[12,:,:], 100)
cbar = plt.colorbar(cp)
cbar.set_label("Temperature [K]")
plt.title("Sea surface temperature")
Out[15]:
In [22]:
from mpl_toolkits.basemap import Basemap
fig=plt.figure(figsize=(16,16))
# Create the map
m = Basemap(llcrnrlon=np.min(lons),llcrnrlat=np.min(lats),\
urcrnrlon=np.max(lons),urcrnrlat=np.max(lats),\
projection='merc',resolution='l')
m.drawcoastlines(linewidth=2)
m.fillcontinents(color='gray')
m.drawcountries(linewidth=1)
plons, plats = np.meshgrid(lons, lats)
x, y = m(plons, plats)
cp = m.pcolor(x,y,tos[12,:,:])
cbar = plt.colorbar(cp)
cbar.set_label("Temperature [K]")
plt.title("Sea surface temperature")
plt.show()
In [ ]: