Introduction
This page is dedicated to the VECTORS workshop in Portorož, Slovenia. The method provided aims on opening a netCDF file from an OPeNDAP server and plots a sequence of values.
Datasource:
Necessary modules:
- numpy (comes with python(xy)
- pydap (pydap module)
The snippet
Please note that if you are using the code, the indents are very important in Python.
# Description: Open netCDF on OPeNDAP for reading, querying and display time # series # --------------------------------------------------------------------------- # load necessary libs import numpy as np from pydap.client import open_url # load netCDF file anc = "http://opendap.deltares.nl/thredds/dodsC/opendap/test/uham/e027.nc" dataset = open_url(anc) # define function to find nearest index of given value # from http://stackoverflow.com/questions/2566412/find-nearest-value-in-numpy-array def find_nearest(array,value): idx=(np.abs(array-value)).argmin() return idx # this is the pydap syntax to read variables lon = dataset['lon'][:] lat = dataset["lat"][:] conc = dataset["concentration"][:] # Basic syntax to retreive index of 1 point # retrieve index for lon = 10 and lat = 50 il = find_nearest(lon,10) iy = find_nearest(lat,50) a = [] # the pydap way for x in range(len(conc)): v = conc.array.data[x,il,iy] if v > 0: a.append(v) plot(a)
The entire snippet is made available as download (download) and has some more functionality. Please try out and adjust to your needs and of course share it with the OpenEarth community.