Using Iris to access data from US-IOOS models


In [1]:
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import time
import cmtamu
%matplotlib inline

Note: iris is not a default package in Wakari or Anaconda, but just do

conda install -c https://conda.binstar.org/scitools iris

In [2]:
import iris

In [3]:
def find_timevar(cube):
    """Return the variable attached to
    time axis and rename it to time."""
    try:
        cube.coord(axis='T').rename('time')
        print('Renaming {} to time'.format(cube.coord('time').var_name))
    except:
        pass
    timevar = cube.coord('time')
    return timevar

In [4]:
def time_near(cube, start):
    """Return the nearest time to `start`.
    TODO: Adapt to the new slice syntax"""
    timevar = find_timevar(cube)
    try:
        time1 = timevar.units.date2num(start)
        itime = timevar.nearest_neighbour_index(time1)
    except IndexError:
        itime = -1
    return timevar.points[itime]

In [5]:
def var_lev_date(url=None,var=None,mytime=None,lev=0,subsample=1):
    time0= time.time()
#    cube = iris.load_cube(url,iris.Constraint(name=var.strip()))[0]
    cube = iris.load_cube(url,var)
#    cube = iris.load(url,var)[0]
#    print cube.coord('time')

    try:
        cube.coord(axis='T').rename('time')
    except:
        pass
    slice = cube.extract(iris.Constraint(time=time_near(cube,mytime)))
    slice = slice[lev,::subsample,::subsample]  
    print 'slice retrieved in %f seconds' % (time.time()-time0)
    return slice

In [6]:
import cartopy.crs as ccrs
from cartopy.io import shapereader
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

def make_map(projection=ccrs.PlateCarree(), figsize=(12,8)):
    fig, ax = plt.subplots(figsize=figsize,
                           subplot_kw=dict(projection=projection))
    gl = ax.gridlines(draw_labels=True)
    gl.xlabels_top = gl.ylabels_right = False
    gl.xformatter = LONGITUDE_FORMATTER
    gl.yformatter = LATITUDE_FORMATTER
    return fig, ax

In [7]:
def map_plot(c,model=None):
    fig, ax = make_map()
    lat = c.coord(axis='Y').points
    lon = c.coord(axis='X').points
    time = c.coord('time')[0]
    cs = plt.pcolormesh(lon,lat,
                    np.ma.masked_invalid(c.data),
                    vmin = 4, vmax= 32.,
                    zorder=1, cmap=cmtamu.optiond)
    plt.colorbar()
    date=time.units.num2date(time.points)
    date_str=date[0].strftime('%Y-%m-%d %H:%M:%S %Z')
    plt.title('%s: %s: %s' % (model,c.long_name,date_str));
    _ = ax.coastlines('10m')

In [11]:
# use contraints to select nearest time (UTC)
#mytime=dt.datetime(2008,7,28,12)  #specified time...
mytime=dt.datetime.utcnow() + + dt.timedelta(hours=+3)     # .... or now
print mytime


2015-10-06 15:20:27.198301

In [9]:
model = 'USGS/COAWST'
url = 'http://geoport.whoi.edu/thredds/dodsC/coawst_4/use/fmrc/coawst_4_use_best.ncd'
var = 'sea_water_potential_temperature'
lev = -1
icube = var_lev_date(url=url, var=var, mytime=mytime, lev=lev, subsample=1)
map_plot(icube, model=model)


/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/fileformats/cf.py:1040: UserWarning: Ignoring formula terms variable u'h' referenced by data variable u'v' via variable u's_rho': Dimensions (u'eta_rho', u'xi_rho') do not span (u'time', u's_rho', u'eta_v', u'xi_v')
  warnings.warn(msg)
/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/fileformats/cf.py:1040: UserWarning: Ignoring formula terms variable u'zeta' referenced by data variable u'v' via variable u's_rho': Dimensions (u'time', u'eta_rho', u'xi_rho') do not span (u'time', u's_rho', u'eta_v', u'xi_v')
  warnings.warn(msg)
/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/fileformats/cf.py:1040: UserWarning: Ignoring formula terms variable u'h' referenced by data variable u'u' via variable u's_rho': Dimensions (u'eta_rho', u'xi_rho') do not span (u'time', u's_rho', u'eta_u', u'xi_u')
  warnings.warn(msg)
/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/fileformats/cf.py:1040: UserWarning: Ignoring formula terms variable u'zeta' referenced by data variable u'u' via variable u's_rho': Dimensions (u'time', u'eta_rho', u'xi_rho') do not span (u'time', u's_rho', u'eta_u', u'xi_u')
  warnings.warn(msg)
/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/fileformats/_pyke_rules/compiled_krb/fc_rules_cf_fc.py:1395: UserWarning: Gracefully filling 'time' dimension coordinate masked points
  warnings.warn(msg.format(str(cf_coord_var.cf_name)))
slice retrieved in 9.943563 seconds
/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/fileformats/netcdf.py:441: UserWarning: Unable to find coordinate for variable u'zeta'
  '{!r}'.format(name))
/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/fileformats/netcdf.py:441: UserWarning: Unable to find coordinate for variable u'h'
  '{!r}'.format(name))
/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/fileformats/netcdf.py:558: UserWarning: Unable to construct Ocean s-coordinate, generic form 1 factory due to insufficient source coordinates.
  warnings.warn('{}'.format(e))

In [10]:
model='MARACOOS/ESPRESSO'
url='http://tds.marine.rutgers.edu/thredds/dodsC/roms/espresso/2013_da/his_Best/ESPRESSO_Real-Time_v2_History_Best_Available_best.ncd'
var='sea_water_potential_temperature'
lev=-1
icube = var_lev_date(url=url,var=var, mytime=mytime, lev=lev)
map_plot(icube, model=model)


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-10-e7204d1ce1fa> in <module>()
      3 var='sea_water_potential_temperature'
      4 lev=-1
----> 5 icube = var_lev_date(url=url,var=var, mytime=mytime, lev=lev)
      6 map_plot(icube, model=model)

<ipython-input-5-5e2ab57a0f3b> in var_lev_date(url, var, mytime, lev, subsample)
      2     time0= time.time()
      3 #    cube = iris.load_cube(url,iris.Constraint(name=var.strip()))[0]
----> 4     cube = iris.load_cube(url,var)
      5 #    cube = iris.load(url,var)[0]
      6 #    print cube.coord('time')

/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/__init__.pyc in load_cube(uris, constraint, callback)
    331         raise ValueError('only a single constraint is allowed')
    332 
--> 333     cubes = _load_collection(uris, constraints, callback).merged().cubes()
    334 
    335     try:

/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/__init__.pyc in _load_collection(uris, constraints, callback)
    271     try:
    272         cubes = _generate_cubes(uris, callback, constraints)
--> 273         result = iris.cube._CubeFilterCollection.from_cubes(cubes, constraints)
    274     except EOFError as e:
    275         raise iris.exceptions.TranslationError(

/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/cube.pyc in from_cubes(cubes, constraints)
    137         pairs = [_CubeFilter(constraint) for constraint in constraints]
    138         collection = _CubeFilterCollection(pairs)
--> 139         for cube in cubes:
    140             collection.add_cube(cube)
    141         return collection

/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/__init__.pyc in _generate_cubes(uris, callback, constraints)
    262         elif scheme in ['http', 'https']:
    263             urls = [':'.join(x) for x in groups]
--> 264             for cube in iris.io.load_http(urls, callback):
    265                 yield cube
    266         else:

/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/io/__init__.pyc in load_http(urls, callback)
    220     # Call each iris format handler with the appropriate filenames
    221     for handling_format_spec, fnames in handler_map.iteritems():
--> 222         for cube in handling_format_spec.handler(fnames, callback):
    223             yield cube
    224 

/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/fileformats/netcdf.pyc in load_cubes(filenames, callback)
    543     for filename in filenames:
    544         # Ingest the netCDF file.
--> 545         cf = iris.fileformats.cf.CFReader(filename)
    546 
    547         # Process each CF data variable.

/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/iris/fileformats/cf.pyc in __init__(self, filename, warn, monotonic)
    913         self.cf_group = CFGroup()
    914 
--> 915         self._dataset = netCDF4.Dataset(self._filename, mode='r')
    916 
    917         # Issue load optimisation warning.

/home/usgs/miniconda/envs/ioos/lib/python2.7/site-packages/netCDF4/_netCDF4.so in netCDF4._netCDF4.Dataset.__init__ (netCDF4/_netCDF4.c:9689)()

RuntimeError: NetCDF: file not found

In [ ]:
model='SECOORA/NCSU'
url='http://omgsrv1.meas.ncsu.edu:8080/thredds/dodsC/fmrc/sabgom/SABGOM_Forecast_Model_Run_Collection_best.ncd'
var='sea_water_potential_temperature'
lev=-1
icube = var_lev_date(url=url,var=var, mytime=mytime, lev=lev)
map_plot(icube, model=model)

In [ ]:
model='CENCOOS/UCSC'
url='http://oceanmodeling.pmc.ucsc.edu:8080/thredds/dodsC/ccsnrt/fmrc/CCSNRT_Aggregation_best.ncd'
var='potential temperature'
lev=-1
icube = var_lev_date(url=url,var=var, mytime=mytime, lev=lev)
map_plot(icube, model=model)

In [ ]:
model='HIOOS'
url='http://oos.soest.hawaii.edu/thredds/dodsC/hioos/roms_assim/hiig/ROMS_Hawaii_Regional_Ocean_Model_Assimilation_best.ncd'
var='sea_water_potential_temperature'
lev=0
icube = var_lev_date(url=url,var=var, mytime=mytime, lev=lev)
map_plot(icube, model=model)

In [ ]:
model='Global RTOFS/NCEP'
url='http://ecowatch.ncddc.noaa.gov/thredds/dodsC/hycom/hycom_reg1_agg/HYCOM_Region_1_Aggregation_best.ncd'
var='sea_water_temperature'  
lev=1
icube = var_lev_date(url=url,var=var, mytime=mytime, lev=lev, subsample=1)
map_plot(icube, model=model)

In [ ]:
print icube