Added task for filtering frames based on their timestamp.
@@ -6,6 +6,8 @@ the time-dependent features.
from eolearn.core import EOTask, FeatureType
import numpy as np
+from datetime import datetime
+
import logging
LOGGER = logging.getLogger(__name__)
@@ -81,3 +83,49 @@ class SimpleFilterTask(EOTask):
value=np.asarray([value[idx] for idx in good_idxs]))
return eopatch
+
+
+class FilterTimeSeries(EOTask):
+ """
+ Removes all frames in the time-series with dates outside the user specified time interval.
+ """
+ def __init__(self, start_date, end_date):
+ """
+ :param start_date: Start date. All frames within the time-series taken after this date will be kept.
+ :type start_date: datetime.datetime
+ :param end_date: End date. All frames within the time-series taken before this date will be kept.
+ :type end_date: datetime.datetime
+ """
+ self.start_date = start_date
+ self.end_date = end_date
+
+ if not isinstance(start_date, datetime):
+ raise ValueError('Start date is not of correct type. Please provide the start_date as datetime.datetime.')
+
+ if not isinstance(end_date, datetime):
+ raise ValueError('End date is not of correct type. Please provide the end_date as datetime.datetime.')
+
+ def execute(self, eopatch):
+ good_idxs = []
+ for idx, date in enumerate(eopatch.timestamp):
+ diff_to_begin = (date - self.start_date)
+ diff_to_end = (date - self.end_date)
+
+ if (diff_to_begin.total_seconds() > 0) and (diff_to_end.total_seconds() < 0):
+ good_idxs.append(idx)
+
+ LOGGER.debug("good_idxs: %s", good_idxs)
+
+ time_dependent = [FeatureType.DATA, FeatureType.MASK, FeatureType.SCALAR, FeatureType.LABEL]
+
+ eopatch.timestamp = [eopatch.timestamp[idx] for idx in good_idxs]
+
+ for attr_type in time_dependent:
+ attr = getattr(eopatch, attr_type.value)
+ assert isinstance(attr, dict)
+ for target_field in attr:
+ value = attr[target_field]
+ eopatch.add_feature(attr_type=attr_type, field=target_field,
+ value=np.asarray([value[idx] for idx in good_idxs]))
+
+ return eopatch
