Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions geetools/ee_image_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ def aggregateArray(self, properties: list[str] | ee.List | None = None) -> ee.Di
values = keys.map(lambda p: self._obj.aggregate_array(p))
return ee.Dictionary.fromLists(keys, values)

def groupInterval(self, unit: str = "month", duration: int = 1) -> ee.List:
def groupInterval(self, unit: str = "month", duration: int = 1, count_images_per_interval: bool = False) -> ee.List:
"""Transform the :py:class:`ee.ImageCollection` into a list of smaller collection of the specified duration.

For example using unit as "month" and duration as 1, the :py:class:`ee.ImageCollection` will be transformed
Expand All @@ -968,6 +968,7 @@ def groupInterval(self, unit: str = "month", duration: int = 1) -> ee.List:
Args:
unit: The unit of time to split the collection. Available units: ``year``, ``month``, ``week``, ``day``, ``hour``, ``minute`` or ``second``.
duration: The duration of each split.
count_images_per_interval: Whether to add the property `n_images_per_interval` with the number of images used for the reduction in each interval.

Returns:
A list of :py:class:`ee.ImageCollection` grouped by interval
Expand All @@ -988,7 +989,7 @@ def groupInterval(self, unit: str = "month", duration: int = 1) -> ee.List:
split = collection.geetools.groupInterval("month", 1)
print(split.getInfo())
"""
sizeName = "__geetools_generated_size__" # set generated properties name
Comment thread
elitonfilho marked this conversation as resolved.
sizeName = "n_images_per_interval" # set generated properties name

# create an ic variable to avoid calling self._obj multiple times
# and extract the property names to copy
Expand All @@ -1011,10 +1012,11 @@ def add_size(ic):
def delete_size_property(ic):
ic = ee.ImageCollection(ic)
return ee.ImageCollection(ic.copyProperties(ic, properties=toCopy))

imageCollectionList = imageCollectionList.map(add_size).filter(ee.Filter.gt(sizeName, 0))

imageCollectionList = (
imageCollectionList.map(add_size).filter(ee.Filter.gt(sizeName, 0)).map(delete_size_property)
)
if not count_images_per_interval:
Comment thread
elitonfilho marked this conversation as resolved.
Outdated
imageCollectionList = imageCollectionList.map(delete_size_property)

return ee.List(imageCollectionList)

Expand All @@ -1024,6 +1026,7 @@ def reduceInterval(
unit: str = "month",
duration: int = 1,
keep_original_names: bool = True,
count_images_per_interval: bool = False,
) -> ee.ImageCollection:
"""Reduce the images included in the same duration interval using the provided reducer.

Expand All @@ -1037,6 +1040,7 @@ def reduceInterval(
unit: The unit of time to split the collection. Available units: ``year``, ``month``, ``week``, ``day``, ``hour``, ``minute`` or ``second``.
duration: The duration of each split.
keep_original_names: Whether to keep the original band names or not. This is a workaround to preserve older behaviour, it should disappear in the future.
count_images_per_interval: Whether to add the property `n_images_per_interval` with the number of images used for the reduction in each interval.

Returns:
A new :py:class:`ee.ImageCollection` with the reduced images.
Expand All @@ -1059,7 +1063,7 @@ def reduceInterval(
"""
# create a list of image collections to be reduced
# Every subcollection is sorted in case one use the "first" reducer
imageCollectionList = self.groupInterval(unit, duration)
imageCollectionList = self.groupInterval(unit, duration, count_images_per_interval)

# create a reducer from user parameters
red = getattr(ee.Reducer, reducer)() if isinstance(reducer, str) else reducer
Expand Down
19 changes: 19 additions & 0 deletions tests/test_ImageCollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,25 @@ def test_reduce_interval_image_with_system_id(self, s2_sr):
firstImg = ic.first()
assert "system:id" in firstImg.propertyNames().getInfo()

def test_reduce_interval_with_count_images_per_interval(self, jaxa_rainfall):
# get 3 month worth of data and group it with default parameters
ic = jaxa_rainfall.filterDate("2020-01-01", "2020-01-02")
reduced = ic.geetools.reduceInterval("mean", duration=1, unit="day", count_images_per_interval=True)
firstImg = reduced.first()
assert "n_images_per_interval" in firstImg.propertyNames().getInfo()

def test_reduce_interval_with_count_images_per_interval_count(self, jaxa_rainfall):
ic = jaxa_rainfall.filterDate("2020-01-01", "2020-01-02")
reduced = ic.geetools.reduceInterval("mean", duration=1, unit="day", count_images_per_interval=True)
firstImg = reduced.first()
assert firstImg.get('n_images_per_interval').getInfo() == 24

def test_reduce_interval_with_count_images_per_interval_empty_days(self, s2_sr):
ic = s2_sr.filterDate("2021-01-01", "2021-01-07")
reduced = ic.geetools.reduceInterval("mean", duration=1, unit="day", count_images_per_interval=True)
count_images_per_interval = reduced.aggregate_array("n_images_per_interval").getInfo()
assert count_images_per_interval == [0, 1, 0]


class TestClosestDate:
"""Test the ``closestDate`` method."""
Expand Down
Loading