...
- Go to an OPeNDAP server (e.g. http://opendap.deltares.nl) and pick a netCDF file by copying the contents of the Data URL box.
- Define the associated url you just copied.
Code Block url_grid = r'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/vaklodingen/vaklodingenKB116_4544.nc' url_time = r'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/waterbase/concentration_of_suspended_matter_in_water/id410-DELFZBTHVN.nc'
- Extract the data.
Code Block # Get grid data grid = opendap(url_grid) G_x = grid.variables['x'] G_y = grid.variables['y'] G_z = grid.variables['z'] G = {} # dictionary ~ Matlab struct G['x'] = G_x[:].squeeze() G['y'] = G_y[:].squeeze() G['z'] = G_z[1,:,:].squeeze() # download only one temporal slice # represent fillValue from data as Masked Array # the next release of netcdf4 will return a masked array by default, handling NaNs # correctly too (http://code.google.com/p/netcdf4-python/issues/detail?id=168) G['z'] = np.ma.masked_invalid(G['z'])
Code Block # Get time series data time = opendap(url_time) T_t = time.variables['time'] T_z = time.variables['concentration_of_suspended_matter_in_water'] T = {} # dictionary ~ Matlab struct T['t'] = netCDF4.num2date(T_t[:], units=T_t.units) T['z'] = T_z[:].squeeze()
- plot ...
Code Block # plot grid data matplotlib.pyplot.pcolormesh(G['x'],G['y'],G['z']) pylab.xlabel('x [m]') pylab.ylabel('y [m]') matplotlib.pyplot.colorbar() matplotlib.pyplot.axis('tight') matplotlib.pyplot.axis('equal') matplotlib.pyplot.title(url_grid) pylab.savefig('vaklodingenKB116_4544') # plot time series data pylab.clf() matplotlib.pyplot.plot_date(T['t'], T['z'], fmt='b-', xdate=True, ydate=False) pylab.ylabel('SPM [kg/m3]') matplotlib.pyplot.title(url_time) pylab.savefig('DELFZBTHVN')
- et voila
Download the code of this python example (repos,manual download) + opendap.py pydap2netCDF4 module (repos, manual download).
See also: OPeNDAP subsetting with python,Accessing netCDF/OPeNDAP unstructured grids with python, Accessing netCDF/OPeNDAP data with R, Accessing netCDF/OPeNDAP data with Matlab, curvilinear model data subsetting with python