Created
September 22, 2014 13:03
-
-
Save rhattersley/25f2c566cbe9cfb69850 to your computer and use it in GitHub Desktop.
Subsetting unstructured grid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import time | |
import iris | |
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 | |
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 | |
def time_slice(cube, start, stop=None): | |
"""TODO: Re-write to use `iris.FUTURE.cell_datetime_objects`.""" | |
istart = time_near(cube, start) | |
if stop: | |
istop = time_near(cube, stop) | |
if istart == istop: | |
raise ValueError('istart must be different from istop!' | |
'Got istart {!r} and ' | |
' istop {!r}'.format(istart, istop)) | |
return cube[istart:istop, ...] | |
else: | |
return cube[istart, ...] | |
iris.FUTURE.netcdf_promote = True | |
print 'Initial load...' | |
t = time.time() | |
url = ('http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/' | |
'Forecasts/NECOFS_FVCOM_OCEAN_MASSBAY_FORECAST.nc') | |
cube = iris.load_cube(url, 'sea_water_potential_temperature') | |
print ' ...done in {} s'.format(time.time() - t) | |
print `cube` | |
print 'Slicing...' | |
t = time.time() | |
start = datetime.datetime.utcnow() - datetime.timedelta(days=7) | |
cube = time_slice(cube, start, None) | |
print ' ...done in {} s'.format(time.time() - t) | |
print `cube` | |
print 'Converting units...' | |
t = time.time() | |
units = iris.unit.Unit('Kelvin') | |
cube.convert_units(units) | |
print ' ...done in {} s'.format(time.time() - t) | |
print `cube` | |
print 'Intersecting...' | |
t = time.time() | |
bbox = [-70.8, 41.4, -69.9, 42.3] | |
cube = cube.intersection(longitude=(bbox[0], bbox[2]), | |
latitude=(bbox[1], bbox[3])) | |
print ' ...done in {} s'.format(time.time() - t) | |
print `cube` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Initial load... | |
...done in 134.799810886 s | |
<iris 'Cube' of sea_water_potential_temperature / (degrees_C) (time: 145; -- : 10; -- : 98432)> | |
Slicing... | |
...done in 0.0312080383301 s | |
<iris 'Cube' of sea_water_potential_temperature / (degrees_C) (-- : 10; -- : 98432)> | |
Converting units... | |
...done in 8.565335989 s | |
<iris 'Cube' of sea_water_potential_temperature / (Kelvin) (-- : 10; -- : 98432)> | |
Intersecting... | |
...done in 0.0781300067902 s | |
<iris 'Cube' of sea_water_potential_temperature / (Kelvin) (-- : 10; -- : 44575)> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment