Skip to content

Commit

Permalink
feat: Trim retrieved arrays by defaults. Refs #34 🎁
Browse files Browse the repository at this point in the history
  • Loading branch information
luukvdmeer committed Jan 16, 2024
1 parent 5b2baf6 commit 51e8362
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions semantique/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ class Opendatacube(Datacube):
Additional keyword arguments tuning the data retrieval configuration.
Valid options are:
* **trim** (:obj:`bool`): Should each retrieved data layer be trimmed?
Trimming means that dimension coordinates for which all values are
missing are removed from the array. The spatial dimensions are trimmed
only at the edges, to maintain their regularity. Defaults to
:obj:`True`.
* **group_by_solar_day** (:obj:`bool`): Should the time dimension be
resampled to the day level, using solar day to keep scenes together?
Defaults to :obj:`True`.
Expand Down Expand Up @@ -183,6 +189,7 @@ def tz(self, value):
@property
def _default_config(self):
return {
"trim": True,
"group_by_solar_day": True,
"value_type_mapping": {
"nominal": "nominal",
Expand Down Expand Up @@ -280,6 +287,10 @@ def retrieve(self, *reference, extent):
f"All values for data layer '{reference}' are invalid within the "
"specified spatio-temporal extent"
)
# Trim the array if requested.
# This will remove dimension coordinates with only missing or invalid data.
if self.config["trim"]:
data = data.sq.trim()
# PROVISIONAL FIX: Convert value type to float.
# Sentinel-2 data may be loaded as unsigned integers.
# This gives problems e.g. with divisions that return negative values.
Expand Down Expand Up @@ -386,6 +397,12 @@ class GeotiffArchive(Datacube):
Additional keyword arguments tuning the data retrieval configuration.
Valid options are:
* **trim** (:obj:`bool`): Should each retrieved data layer be trimmed?
Trimming means that dimension coordinates for which all values are
missing are removed from the array. The spatial dimensions are trimmed
only at the edges, to maintain their regularity. Defaults to
:obj:`True`.
* **value_type_mapping** (:obj:`dict`): How do value type encodings in
the layout map to the value types used by semantique?
Defaults to a one-to-one mapping: ::
Expand Down Expand Up @@ -458,6 +475,7 @@ def tz(self, value):
@property
def _default_config(self):
return {
"trim": True,
"value_type_mapping": {
"nominal": "nominal",
"ordinal": "ordinal",
Expand Down Expand Up @@ -556,6 +574,10 @@ def retrieve(self, *reference, extent):
f"All values for data layer '{reference}' are invalid within the "
"specified spatio-temporal extent"
)
# Trim the array if requested.
# This will remove dimension coordinates with only missing or invalid data.
if self.config["trim"]:
data = data.sq.trim()
# PROVISIONAL FIX: Convert value type to float.
# Sentinel-2 data may be loaded as unsigned integers.
# This gives problems e.g. with divisions that return negative values.
Expand Down

0 comments on commit 51e8362

Please sign in to comment.