Last active
March 5, 2024 18:35
-
-
Save dennissergeev/58a04be5fdfe37cedea838a19203e75e to your computer and use it in GitHub Desktop.
Extract by time in iris
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
"""Extract a cube slice using a time constraint.""" | |
from datetime import datetime | |
import iris | |
# The cube with a time dimension | |
cube = mycubelist.extract_cube("some_variable") | |
# For example, we want to extract the time slice at 2020-04-29 12:00 model time, | |
# which corresponds to a time index of 123. | |
# Method 1. | |
time_constr = iris.Constraint(time=lambda t: t.point == datetime(2020, 4, 29, 12)) | |
# Method 2. | |
tcoord = cube.coord("time") | |
time_constr = iris.Constraint(time=lambda t: t.point == tcoord.units.num2date(tcoord.points[123])) | |
# Extraction | |
cube_slice = cube.extract(time_constr) | |
# We can also extract several slices across the time dimension | |
dates = [tcoord.units.num2date(tcoord.points[i]) for i in [123, 234, 345]] # or use an explicit list of datetime objects | |
cube.extract(iris.Constraint(time=lambda t: t.point in dates)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment