Child pages
  • Delft Software Days 2013 - OpenGIS Workshop - netcdf
Skip to end of metadata
Go to start of metadata

This page gives an overview of the exercise with reading and creating CF compliant netcdf files presented on November 1th, 2013 by Kees den Heijer.

Data

The exercise deals with bathymetry data of the Dutch coast. Here is the link to browse the netcdf file of concern with your browser:
http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/vaklodingen/vaklodingenKB118_3736.nc.html
This shows you:

The first link on this page (OPeNDAP), goes to a page the shows all the meta information and file structure of a file.
The second link (HTTPServer) can be used to download the whole file to your local hard drive.

The OPeNDAP link brings you to the following page, where the data url can be obtained:

Both in Matlab and in Python, the link to netcdf file can be specified as follows:

ncfile = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/vaklodingen/vaklodingenKB118_3736.nc'

Exercise

  • Read bathymetry data (variable 'z') from the above mentioned netcdf file.
  • Extract the latest time slice only
  • Create the new file with this latest time slice, using the netCDF Kickstarter (for further info see the netCDF kickstarter)

Code suggestions for reading netCDF

Matlab (native)

Basic code elements

ncdisp(ncfile)
info = ncinfo(ncfile)
z = ncread(ncfile, 'z');

Example pcolor plot of the most recent available measurement

ncfile = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/vaklodingen/vaklodingenKB118_3736.nc';

x = ncread(ncfile, 'x');
y = ncread(ncfile, 'y');
z = ncread(ncfile, 'z');

[X, Y] = meshgrid(x, y);

Z = squeeze(z(:,:,end))';

pcolor(X,Y,Z)
shading interp
axis image

Matlab (OET)

nc_dump(ncfile)
info = nc_info(ncfile)
z = nc_varget(ncfile, 'z');

Python

Basic code elements

import netCDF4
ds = netCDF4.Dataset(ncfile)
print ds
z = ds.variables['z']
ds.close()

Example pcolor plot of the most recent available measurement

from netCDF4 import Dataset
import matplotlib.pyplot as plt
import numpy as np

ncfile = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/vaklodingen/vaklodingenKB118_3736.nc'

ds = Dataset(ncfile)
x = ds.variables['x'][:]
y = ds.variables['y'][:]
z = ds.variables['z'][-1,]
ds.close()

X,Y = np.meshgrid(x,y)
fig,ax = plt.subplots(nrows=1, ncols=1, subplot_kw=dict(aspect='equal'))
ax.pcolor(X, Y, z)
fig.savefig('plot.png')