MetOffice / cube_helper

A Python module, for easier manipulation of Cubes with Iris
https://cube-helper.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
2 stars 4 forks source link

issues with extracting bounded sections of cubes (decouple _fix_partial_datetime()) #34

Open TheElectricFlock opened 4 years ago

TheElectricFlock commented 4 years ago

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)

jonseddon commented 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))

jonseddon commented 4 years ago

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.

jonseddon commented 4 years ago

Bounds comparison is possible with:

date_range = iris.Constraint(time=lambda cell: pdt1 <= cell.bound[0] and pdt2 > cell.bound[1])