Open TheElectricFlock opened 4 years ago
There is some discussion on this at: https://groups.google.com/forum/#!topic/scitools-iris/0fdZEqX2g3U
The following works irrespective of whether the time coordinate has bounds:
date_range = iris.Constraint(time=lambda cell: 1960 <= cell.point.year < 1990)
The following only works if the time coordinate doesn't have bounds:
date_range = iris.Constraint(time=lambda cell: PartialDateTime(year=1960) <= cell < PartialDateTime(year=1990))
partial date time comparisons have to be on the left hand side of comparisons with cftime.datetimes (they can be on either side of comparisons with datetime.datetime). It is therefore possible to do a time point comparison between two time points using:
pdt1 = PartialDateTime(1960, 1, 1)
pdt2 = PartialDateTime(1990, 1, 1)
date_range = iris.Constraint(time=lambda cell: pdt1 <= cell.point and pdt2 > cell.point)
To handle comparisons with bounds (rather than points) without getting a TypeError
then it may be possible to replace the lambda with a function to allow some logic. It would be good to look at why Iris gives the TypeError: Cannot determine whether a point lies within a bounded region for datetime-like objects.
error.
Bounds comparison is possible with:
date_range = iris.Constraint(time=lambda cell: pdt1 <= cell.bound[0] and pdt2 > cell.bound[1])
Proposals for a method to extract data from 2 specified points along the time bounds. This could potentially be quite complicated as it would mean de-coupling the lambda construction.
This will be done eventually but it would also require a similar lambda construction for the bounds function. A solution like the one below was originally proposed.
def extract_bounds(cube, lower_bound, upper_bound): constraint = iris.Constraint( time=lambda cell: lower_bound >= cell.point <= upper_bound) return extract(cube, constraint)