Python
There are two useful libraries for reading OpenDAP. The first is the netCDF4 library. To access opendap urls the linked netcdf.dll/.so/.dylib should have with dap support. The second one is the pydap library.
Library | reads netcdf files | creates netcdf file | netcdf4 support | reads opendap | no need for netcdf.dll |
(recent) |
Other, not actively developed, libraries are: pycdf, ncmodule, pynetcdf,
Suggestion: use netcdf4-python or pupynere + pydap
The pydap client and pupynere client both do not require any dll to be present, which can be convenient. The netcdf4-python has the best support for netcdf4 and is installed by python(x,y) under windows, which increases compatibility.
Install
To install the netCDF4 library: if you're on windows the best way to install these is to use python(x,y). On other platforms emerge/apt-get/port install netcdf and pip install netCDF4 or easy_install netCDF4.
To install the pydap library do easy_install pydap or pip install pydap.
Example
All netcdf libraries have approximately the same interface. Below you can find some examples.
Reading opendap with netCDF4 library
#!/usr/bin/env python # Read data from an opendap server import netCDF4 # specify an url, the JARKUS dataset in this case url = 'http://dtvirt5.deltares.nl:8080/thredds/dodsC/opendap/rijkswaterstaat/jarkus/profiles/transect.nc' # for local windows files, note that '\t' defaults to the tab character in python, so use prefix r to indicate that it is a raw string. url = r'f:\opendap\rijkswaterstaat\jarkus\profiles\transect.nc' # create a dataset object dataset = netCDF4.Dataset(url) # lookup a variable variable = dataset.variables['id'] # print the first 10 values print variable[0:10]
Reading opendap with pydap.client
#!/usr/bin/env python # use the pydap client this time import pydap.client # specify an url, the JARKUS dataset in this case url = 'http://dtvirt5.deltares.nl:8080/thredds/dodsC/opendap/rijkswaterstaat/jarkus/profiles/transect.nc' # create a dataset object dataset = pydap.client.open_url(url) # lookup a variable (directly on the dataset) variable = dataset['id'] # print the first 10 values print variable[0:10]
Creating a netcdf file with pupynere
#!/usr/bin/env python import pupynere # create a new netcdf file f = pupynere.NetCDFFile('test.nc', 'w') # create a dimension with length of 10 f.createDimension('npoints', 10) # create a new variable of type double of dimension npoints variable = f.createVariable('x', 'd', ('npoints',)) # store an attribute variable.unit = 'm' # store some values variable[:] = range(10) f.close() # Now try and open it again f = pupynere.NetCDFFile('test.nc', 'r') # read all the data print f.variables['x'][:] f.close()
Creating a netcdf file with netCDF4
#!/usr/bin/env python import netCDF4 # create a new netcdf file f = netCDF4.Dataset('test.nc', 'w', format='NETCDF3_64BIT') # create a dimension with length of 10 f.createDimension('npoints', 10) # create a new variable of type double of dimension npoints variable = f.createVariable('x', 'd', ('npoints',)) # store an attribute variable.unit = 'm' # store some values variable[:] = range(10) f.close() # Now try and open it again f = netCDF4.Dataset('test.nc', 'r') # read all the data print f.variables['x'][:] f.close()