Using Iris to access data from IOOS models


In [1]:
from IPython.core.display import HTML
HTML('<iframe src=http://scitools.org.uk/iris/ width=800 height=350></iframe>')


Out[1]:

In [2]:
import numpy
import matplotlib.pyplot as plt
import datetime as dt

import iris
import iris.quickplot as qplt
import cartopy.crs as ccrs

In [3]:
from collections import defaultdict
d = defaultdict(dict)

mod='USGS/COAWST'
d[mod]['url']='http://geoport.whoi.edu/thredds/dodsC/coawst_4/use/fmrc/coawst_4_use_best.ncd'
d[mod]['var']='potential temperature'
d[mod]['lev']=-1

mod='MARACOOS/ESPRESSO'
d[mod]['url']='http://tds.marine.rutgers.edu/thredds/dodsC/roms/espresso/2009_da/his'
d[mod]['var']='potential temperature'
d[mod]['lev']=-1

mod='SECOORA/NCSU'
d[mod]['url']='http://omgsrv1.meas.ncsu.edu:8080/thredds/dodsC/fmrc/sabgom/SABGOM_Forecast_Model_Run_Collection_best.ncd'
d[mod]['var']='potential temperature'
d[mod]['lev']=-1

mod='HIOOS'
d[mod]['url']='http://oos.soest.hawaii.edu/thredds/dodsC/hioos/roms_assim/hiig/ROMS_Hawaii_Regional_Ocean_Model_Assimilation_best.ncd'
d[mod]['var']='sea_water_potential_temperature'
d[mod]['lev']=-1

mod='CENCOOS/UCSC'
d[mod]['url']='http://oceanmodeling.pmc.ucsc.edu:8080/thredds/dodsC/ccsnrt/fmrc/CCSNRT_Aggregation_best.ncd'
d[mod]['var']='potential temperature'
d[mod]['lev']=-1

mod='AOOS/CSDL'
d[mod]['url']='http://thredds.axiomalaska.com/thredds/dodsC/NOAA_CSDL_ROMS.nc'
d[mod]['var']='Zonal Current'
d[mod]['lev']=-1

In [4]:
def time_near(cube,start):
    #    coord_names = [coord.name() for coord in cube.coords()]
    #    timevar = cube.coord(coord_names[0]))
    timevar=cube.coord(var_name='ocean_time')
    try:
        itime = timevar.nearest_neighbour_index(timevar.units.date2num(start))
    except:
        itime = -1
    return timevar.points[itime]

In [5]:
def var_lev_date(url=None,var=None,mytime=None,lev=0):
    cubes = iris.load(url)
    cube = cubes.extract(iris.Constraint(name=var))[0]
#    coord_names = [cube.name() for coord in cube.coords()]
#    slice = cube.extract(iris.Constraint(coord_values={coord_names[0]:time_near(cube,mytime)}))
    slice = cube.extract(iris.Constraint(var_name=ocean_time=time_near(cube,mytime)))
    return slice[lev,:,:]


  File "<ipython-input-5-dae4e1494ef5>", line 6
    slice = cube.extract(iris.Constraint(var_name=ocean_time=time_near(cube,mytime)))
                                                            ^
SyntaxError: invalid syntax

In [ ]:
def myplot(slice,model=None):
    # make the plot
    figure(figsize=(12,8))
    lat=slice.coord('latitude').points
    lon=slice.coord('longitude').points
    time=slice.coord(var_name='ocean_time')[0]
    Z=slice.data
    Zm = ma.masked_where(np.isnan(Z),Z)
    subplot(111,aspect=(1.0/cos(mean(lat)*pi/180.0)))
    pcolormesh(lon,lat,Zm);
    colorbar()
    grid()
    date_str=time.units.num2date(time.points)
    plt.title('%s: %s: %s' % (mod,slice.long_name,date_str));

In [ ]:
# use contraints to select nearest time
#mytime=dt.datetime(2013,3,1,12)  #specified time...
mytime=dt.datetime.utcnow()      # .... or now

In [ ]:
mod='MARACOOS/ESPRESSO'
slice=var_lev_date(url=d[mod]['url'], var=d[mod]['var'], mytime=mytime,lev=d[mod]['lev'])
myplot(slice,model=mod)

In [ ]:
mod='USGS/COAWST'
slice=var_lev_date(url=d[mod]['url'],var=d[mod]['var'], mytime=mytime, lev=d[mod]['lev'])
myplot(slice,model=mod)

In [ ]:


In [ ]:
mod='SECOORA/NCSU'
slice=var_lev_date(url=d[mod]['url'], var=d[mod]['var'], mytime=mytime,lev=d[mod]['lev'])
myplot(slice,model=mod)

In [ ]:
mod='CENCOOS/UCSC'
slice=var_lev_date(url=d[mod]['url'], var=d[mod]['var'], mytime=mytime,lev=d[mod]['lev'])
myplot(slice,model=mod)

In [ ]:
mod='HIOOS'
slice=var_lev_date(url=d[mod]['url'], var=d[mod]['var'], mytime=mytime,lev=d[mod]['lev'])
myplot(slice,model=mod)

In [ ]:


In [ ]:
mod='MARACOOS/ESPRESSO'
cubes=iris.load(d[mod]['url'])
print cubes

In [ ]:
cube = cubes.extract(iris.Constraint(name=d[mod]['var']))[0]

In [ ]:
cube.coord(var_name='ocean_time')

In [ ]:
slice=cube.extract(iris.Constraint(coord_values={'ocean_time':230824800.0}))

In [ ]:
print slice

In [ ]:
timevar=cube.coord(var_name='ocean_time')

In [ ]:
print timevar

In [ ]:
itime = timevar.nearest_neighbour_index(timevar.units.date2num(mytime))

In [ ]:
itime

In [ ]:
timevar.points[itime]

In [ ]:
file_path='http://omgsrv1.meas.ncsu.edu:8080/thredds/dodsC/fmrc/sabgom/SABGOM_Forecast_Model_Run_Collection_best.ncd'

In [ ]:
def make_timerange_constraint(t1, t2):
    def my_cube_func(cube):
        coord = cube.coord('time')
        time = coord.units.num2date(coord.points[0])
        return t1 < time < t2
    return iris.Constraint(cube_func=my_cube_func)

In [ ]:
timerange_r1 = make_timerange_constraint(datetime(2013,3,1), datetime(2013,3,2))
cubes = iris.load(file_path, timerange_r1)

In [ ]: