In [12]:
#
# test_FVCOM.py
#
# purpose:  Test FVCOM with iris
# author:   Filipe P. A. Fernandes
# e-mail:   ocefpaf@gmail
# web:      http://ocefpaf.github.io/
# created:  14-Jan-2015
# modified: Wed 14 Jan 2015 08:41:07 PM BRT
#
# obs:
#

import pytz
from datetime import datetime, timedelta

import iris
from iris.cube import CubeList

In [14]:
!conda list iris


# packages in environment at /home/usgs/anaconda:
#
iris                      1.7.2_DEV_9e1fd13      np18py27_0    rsignell

In [13]:
iris.__version__


Out[13]:
'1.7.2-DEV'

In [8]:
def time_coord(cube):
    """Return the variable attached to time axis and rename it to time."""
    try:
        cube.coord(axis='T').rename('time')
    except CoordinateNotFoundError:
        pass
    timevar = cube.coord('time')
    return timevar

In [9]:
def time_near(cube, datetime):
    """Return the nearest index to a `datetime`."""
    timevar = time_coord(cube)
    try:
        time = timevar.units.date2num(datetime)
        idx = timevar.nearest_neighbour_index(time)
    except IndexError:
        idx = -1
    return idx

In [10]:
stop = datetime(2014, 7, 7, 12)
stop = stop.replace(tzinfo=pytz.utc)
start = stop - timedelta(days=7)

bbox = [-87.40, 24.25, -74.70, 36.70]

units = iris.unit.Unit('celsius')

name_list = ['sea_water_temperature',
             'sea_surface_temperature',
             'sea_water_potential_temperature',
             'equivalent_potential_temperature',
             'sea_water_conservative_temperature',
             'pseudo_equivalent_potential_temperature']

url = "http://crow.marine.usf.edu:8080/thredds/dodsC/FVCOM-Nowcast-Agg.nc"
cubes = iris.load_raw(url)

in_list = lambda cube: cube.standard_name in name_list
cubes = CubeList([cube for cube in cubes if in_list(cube)])
cube = cubes.merge_cube()

lat = iris.Constraint(latitude=lambda cell: bbox[1] <= cell < bbox[3])
lon = iris.Constraint(longitude=lambda cell: bbox[0] <= cell <= bbox[2])
cube = cube.extract(lon & lat)

istart = time_near(cube, start)
istop = time_near(cube, stop)
cube = cube[istart:istop, ...]

In [11]:
print cube


sea_water_potential_temperature / (degrees_C) (-- : 756; -- : 10; -- : 51387)
     Auxiliary coordinates:
          time                                    x         -        -
          sea_surface_height_above_geoid          x         -        x
          ocean_sigma_coordinate                  -         x        x
          latitude                                -         -        x
          longitude                               -         -        x
          sea_floor_depth_below_geoid             -         -        x
     Attributes:
          Conventions: UGRID-0.9.0
          CoordinateProjection: none
          DODS.dimName: DateStrLen
          DODS.strlen: 26
          Metadata_Conventions: Unidata Dataset Discovery v1.0
          cdm_data_type: any
          contact: email: lzheng@mail.usf.edu  Tel: 727-553-1639
          institute: USF College of Marine Science
          location: node
          mesh: fvcom_mesh
          naming_authority: SECOORA
          reference: http://fvcom.smast.umassd.edu
          source: FVCOM v2.7 with modification by L. Zheng
          standard_name_vocabulary: http://www.cgd.ucar.edu/cms/eaton/cf-metadata/standard_name.html
          summary: Nowcast Aggregation for USF FVCOM
          title: USF FVCOM - Nowcast Aggregation
          type: data

In [11]: