NCAS-CMS / cf-python

A CF-compliant Earth Science data analysis library
http://ncas-cms.github.io/cf-python
MIT License
125 stars 19 forks source link

Feature request for a method which allows one to subspace something which has a structure #684

Open bewithankit opened 1 year ago

bewithankit commented 1 year ago

Feature request for a cf.Field.group() method like subspace, but which allows one to subspace something which has a structure:

E.g., Subsetting data only for the extended winter (ONDJFM) from 2018-2020:

>>> ondjfm = a.subspace(T=cf.seasons(2, 10)[0] & cf.wi(cf.dt('2018-10-1'), cf.dt('2020-03-1')))
>>> print(ondjfm.coordinate("long_name=time").dtarray)
[cftime.DatetimeGregorian(2018, 10, 1, 0, 0, 0, 0, has_year_zero=False)
 cftime.DatetimeGregorian(2018, 11, 1, 0, 0, 0, 0, has_year_zero=False)
 cftime.DatetimeGregorian(2018, 12, 1, 0, 0, 0, 0, has_year_zero=False)
 cftime.DatetimeGregorian(2019, 1, 1, 0, 0, 0, 0, has_year_zero=False)
 cftime.DatetimeGregorian(2019, 2, 1, 0, 0, 0, 0, has_year_zero=False)
 cftime.DatetimeGregorian(2019, 3, 1, 0, 0, 0, 0, has_year_zero=False)
 cftime.DatetimeGregorian(2019, 10, 1, 0, 0, 0, 0, has_year_zero=False)
 cftime.DatetimeGregorian(2019, 11, 1, 0, 0, 0, 0, has_year_zero=False)
 cftime.DatetimeGregorian(2019, 12, 1, 0, 0, 0, 0, has_year_zero=False)
 cftime.DatetimeGregorian(2020, 1, 1, 0, 0, 0, 0, has_year_zero=False)
 cftime.DatetimeGregorian(2020, 2, 1, 0, 0, 0, 0, has_year_zero=False)
 cftime.DatetimeGregorian(2020, 3, 1, 0, 0, 0, 0, has_year_zero=False)]

It would also take parameters like _groupspan (to include groups whose actual span is not equal to a given value) and _groupcontiguous (to include non-contiguous groups, or any contiguous group containing overlapping cells). A similar functionality is embedded in collapse but it works only when one has to collapse axes of the field.