Source code for mapchete_eo.sort

"""
This module holds all code required to sort products or slices.
"""

from typing import Callable, List, Optional

from pydantic import BaseModel

from mapchete_eo.protocols import DateTimeProtocol, GetPropertyProtocol
from mapchete_eo.time import timedelta, to_datetime
from mapchete_eo.types import DateTimeLike


[docs] class SortMethodConfig(BaseModel): func: Callable
[docs] def sort_objects_by_target_date( objects: List[DateTimeProtocol], target_date: Optional[DateTimeLike] = None, reverse: bool = False, **kwargs, ) -> List[DateTimeProtocol]: """ Sort objects by their distance to a target date. """ if len(objects) == 0: return objects if target_date is None: time_list = [to_datetime(object.datetime) for object in objects] start_time = min(time_list) end_time = max(time_list) target_datetime = start_time + (end_time - start_time) / 2 else: target_datetime = to_datetime(target_date) objects.sort(key=lambda x: timedelta(x.datetime, target_datetime), reverse=reverse) return objects
[docs] class TargetDateSort(SortMethodConfig): func: Callable = sort_objects_by_target_date target_date: Optional[DateTimeLike] = None reverse: bool = False
[docs] def sort_objects_by_cloud_cover( objects: List[GetPropertyProtocol], reverse: bool = False ) -> List[GetPropertyProtocol]: if len(objects) == 0: # pragma: no cover return objects objects.sort(key=lambda x: x.get_property("eo:cloud_cover"), reverse=reverse) return objects
[docs] class CloudCoverSort(SortMethodConfig): func: Callable = sort_objects_by_cloud_cover reverse: bool = False