Last active
August 29, 2015 14:19
-
-
Save rhattersley/bed9164759cc827a108c to your computer and use it in GitHub Desktop.
Save callbacks
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
############################################## | |
# | |
# Filter & modify | |
# | |
# ------------------------------ | |
# Callback | |
def callback(cube, field, filename) | |
if field.bar > 3: | |
raise iris.exceptions.IgnoreFieldException | |
field.foo = 9 | |
iris.save(cubes, 'stuff.grib2', callback=callback) | |
# ------------------------------ | |
# Eager evaluation | |
messages = [] | |
for message in iris.fileformats.grib.as_messages(cubes): | |
if message.bar > 3: | |
continue | |
message.foo = 9 | |
messages.append(message) | |
iris.fileformats.grib.save(messages, 'stuff.grib2') | |
# ------------------------------ | |
# Lazy evaluation (generator expression) | |
def modify(message): | |
message.foo = 9 | |
return message | |
messages = iris.fileformats.grib.as_messages(cubes) | |
messages = (modify(message) for message in messages if message.bar > 3) | |
iris.fileformats.grib.save(messages, 'stuff.grib2') | |
# ------------------------------ | |
# Lazy evaluation (generator function) | |
def lazy(cubes): | |
for message in iris.fileformats.grib.as_messages(cubes) | |
if message.bar > 3: | |
continue | |
message.foo = 9 | |
yield message | |
iris.fileformats.grib.save(lazy(cubes), 'stuff.grib2') |
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
############################################## | |
# | |
# Filter & modify using corresponding cube | |
# | |
# ------------------------------ | |
# Callback | |
def callback(cube, field, filename) | |
if field.bar > 3: | |
raise iris.exceptions.IgnoreFieldException | |
field.foo = cube.attributes['foo'] | |
iris.save(cubes, 'stuff.grib2', callback=callback) | |
# ------------------------------ | |
# Eager evaluation | |
messages = [] | |
for cube in cubes: | |
for message in iris.fileformats.grib.as_messages(cubes): | |
if message.bar > 3: | |
continue | |
message.foo = cube.attributes['foo'] | |
messages.append(message) | |
iris.fileformats.grib.save(messages, 'stuff.grib2') | |
# ------------------------------ | |
# Lazy evaluation (generator function) | |
def lazy(cubes): | |
for cube in cubes: | |
for message in iris.fileformats.grib.as_messages(cubes): | |
if message.bar > 3: | |
continue | |
message.foo = cube.attributes['foo'] | |
yield message | |
iris.fileformats.grib.save(lazy(cubes), 'stuff.grib2') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment