In [1]:
%matplotlib inline
See also http://esgf-pyclient.readthedocs.io/en/latest/examples.html
In [2]:
from pyesgf.search import SearchConnection
conn = SearchConnection('http://esgf-data.dkrz.de/esg-search', distrib=False)
In [3]:
ctx = conn.new_context(project='CORDEX', query='temperature')
print 'Hits:', ctx.hit_count
print 'Institute:'
ctx.facet_counts['institute']
Out[3]:
In [4]:
ctx = conn.new_context(project='CORDEX', institute='MPI-CSC', experiment='historical', time_frequency='day')
print 'Hits:', ctx.hit_count
print 'Domain:', ctx.facet_counts['domain']
print 'Ensembles:', ctx.facet_counts['ensemble']
print 'Variable:', ctx.facet_counts['variable']
In [5]:
ctx = ctx.constrain(domain='EUR-11', ensemble='r1i1p1', variable='tasmax')
print 'Hits:', ctx.hit_count
In [6]:
result = ctx.search()[0]
agg_ctx = result.aggregation_context()
agg = agg_ctx.search()[0]
print agg.opendap_url
In [7]:
from pyesgf.logon import LogonManager
lm = LogonManager()
lm.logoff()
lm.is_logged_on()
Out[7]:
... logon on now with you username and password:
In [8]:
lm.logon(hostname="esgf-data.dkrz.de", bootstrap=True, interactive=True)
In [9]:
lm.is_logged_on()
Out[9]:
this updates you proxy certificate in ~/.esg/ and prepares a ~/.dodsrc config file for secured opendap access.
In [10]:
from netCDF4 import Dataset
ds = Dataset(agg.opendap_url, 'r')
show attributes:
In [11]:
ds.ncattrs()
ds.getncattr('experiment')
Out[11]:
show variables:
In [12]:
ds.variables.keys()
Out[12]:
details about time variable:
In [13]:
ds.variables['time']
Out[13]:
show details about tasmax:
In [14]:
ds.variables['tasmax']
Out[14]:
see http://scitools.org.uk/cartopy/docs/latest/matplotlib/advanced_plotting.html?highlight=netcdf
In [15]:
import matplotlib.pyplot as plt
from cartopy import config
import cartopy.crs as ccrs
timestep = 0
tasmax = ds.variables['tasmax'][timestep, :, :]
lats = ds.variables['lat'][:]
lons = ds.variables['lon'][:]
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
ax.set_global()
fig = plt.contourf(lons, lats, tasmax, 60, transform=ccrs.PlateCarree())
#plt.show()
In [16]:
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
ax.set_global()
plt.contourf(lons.clip(min=0, max=20), lats.clip(min=40, max=60), tasmax, 60, transform=ccrs.PlateCarree())
#plt.show()
Out[16]:
One needs to create a new Dataset object and copy all the attributes and data needed. This is a bit lengthly.
See the following example:
http://schubert.atmos.colostate.edu/~cslocum/netcdf_example.html
In [17]:
from nco import Nco
nco = Nco()
subset first timestamp
In [18]:
nc_out = '/tmp/out.nc'
nco.ncks(input=str(agg.opendap_url), output=nc_out, options="-O -d time,0,0 -d rlon,0,20 -d rlat,40,60 --netcdf4")
Out[18]:
In [19]:
from cdo import Cdo
cdo = Cdo()
In [20]:
cdo.sinfo(input=nc_out)
Out[20]:
In [21]:
cdo.showname(input=nc_out)
Out[21]: