This entire presentation was created using Python 3 and Jupyter Notebooks
All three example notebooks and the data sets are available from our web site:
Feel free to download them and play with the notebooks
NetCDF (Network Common Data Format)
GRIB (GRIdded Binary or General Regularly-distributed Information in Binary form)
BUFR (Binary Universal Form for the Representation of meteorological data)
NetCDF can be read with any of the following libraries:
netCDF4-python
xarray
PyNIO
In this example we'll use xarray to read a Global RTOFS NetCDF dataset, plot a parameter (SST), and select a subregion.
The xarray library can be installed via pip, conda (or whatever package manager comes with your Python installation), or distutils (python setup.py).
import matplotlib.pyplot as plt # standard graphics library
import xarray
import cartopy.crs as ccrs # cartographic coord reference system
import cartopy.feature as cfeature # features: land, borders, coastlines
dataset = xarray.open_dataset('rtofs_glo_2ds_n000_daily_prog.nc',
decode_times = True)
decode_times = True
will automatically decode the datetime values from NetCDF convention to Python datetime objects
Note that this reads a local data set, but you can replace the filename with the URL of the corresponding NOMADS OpenDAP data set and continue without further changes.
dataset
<xarray.Dataset> Dimensions: (Layer: 1, MT: 1, X: 4500, Y: 3298) Coordinates: * MT (MT) datetime64[ns] 2018-08-26 Date (MT) float64 ... * Layer (Layer) int32 1 * Y (Y) int32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ... * X (X) int32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ... Latitude (Y, X) float32 ... Longitude (Y, X) float32 ... Data variables: u_velocity (MT, Layer, Y, X) float32 ... v_velocity (MT, Layer, Y, X) float32 ... sst (MT, Y, X) float32 ... sss (MT, Y, X) float32 ... layer_density (MT, Layer, Y, X) float32 ... Attributes: Conventions: CF-1.0 title: HYCOM ATLb2.00 institution: National Centers for Environmental Prediction source: HYCOM archive file experiment: 09.2 history: archv2ncdf2d
dataset = dataset.drop('Date')
del dataset['Date']
There's a quirk in the Global RTOFS datasets -- the bottom row of the longitude field is unused by the model and is filled with junk data.
I'll use array subsetting to get rid of it, and save just the SST parameter to a separate DataArray.
sst = dataset.sst[0,0:-1,:] # this can be shortened to [0,:-1,]
sst
<xarray.DataArray 'sst' (Y: 3297, X: 4500)> [14836500 values with dtype=float32] Coordinates: MT datetime64[ns] 2018-08-26 * Y (Y) int32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ... * X (X) int32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ... Latitude (Y, X) float32 ... Longitude (Y, X) float32 ... Attributes: standard_name: sea_surface_temperature units: degC valid_range: [-2.1907086 34.93205 ] long_name: sea surf. temp. [09.2H]
imshow
function to display the SST parameter as an image.plt.figure(dpi = 90) # open a new figure window and set the resolution
plt.imshow(sst, cmap = 'gist_ncar');
print(sst.Longitude.min().data, sst.Longitude.max().data)
74.1199951171875 434.1199951171875
pcolormesh()
.cmap=
and the vmin=, vmax=
values for your data.plt.figure(dpi = 100)
ax = plt.axes(projection = ccrs.Mercator())
ax.add_feature(cfeature.LAND) # fill in the land areas
ax.coastlines() # use the default coastline
gl = ax.gridlines(draw_labels = True) # default is to label all axes.
gl.xlabels_top = False # turn off two of them.
gl.ylabels_right = False
sst.plot(x = 'Longitude', y = 'Latitude', cmap = 'gist_ncar',
vmin = sst.min(), vmax=sst.max(),
transform = ccrs.PlateCarree());
object.where()
method with the lat/lon limits.hawaii = sst.where((sst.Longitude >= -163%360) &
(sst.Longitude <= -153%360) &
(sst.Latitude >= 17) &
(sst.Latitude <= 24), drop = True)
drop = True
option, which instructs the .where()
method to subset the data.
Otherwise it will retain the full array size and simply mask out the unwanted data.plt.figure(dpi = 100)
ax = plt.axes(projection = ccrs.Mercator())
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.GSHHSFeature()) # use a high-resolution GSHHS coastline
gl = ax.gridlines(draw_labels=True)
gl.xlabels_top = False
gl.ylabels_right = False
hawaii.plot.contourf(x = 'Longitude',y = 'Latitude',levels = 20,
cmap = 'gist_ncar',add_colorbar = True,
transform = ccrs.PlateCarree());