Skip to content

Commit

Permalink
Merge pull request #134 from Open-EO/133-compression-uint8
Browse files Browse the repository at this point in the history
Added rescaling method
  • Loading branch information
GriffinBabe authored Jun 20, 2024
2 parents 7bdc251 + 8eede68 commit deb5cdd
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/openeo_gfmap/preprocessing/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
import openeo


def _compress(
cube: openeo.DataCube,
min_val: int,
max_val: int,
alpha: float,
beta: float,
):
if (
alpha != 1.0 or beta != 0.0
): # Avoid adding a node in the computing graph if scaling is not necessary
cube = (cube * alpha) + beta

return cube.linear_scale_range(min_val, max_val, min_val, max_val)


def compress_uint16(
cube: openeo.DataCube, alpha: float = 1.0, beta: float = 0.0
) -> openeo.DataCube:
Expand All @@ -23,9 +38,28 @@ def compress_uint16(
cube : openeo.DataCube
The datacube with the data linearly scaled and compressed to uint16 and rescaled frome.
"""
if (
alpha != 1.0 or beta != 0.0
): # Avoid adding a node in the computing graph if scaling is not necessary
cube = (cube * alpha) + beta
return _compress(cube, 0, 65534, alpha, beta)


return cube.linear_scale_range(0, 65534, 0, 65534)
def compress_uint8(
cube: openeo.DataCube, alpha: float = 1.0, beta: float = 0.0
) -> openeo.DataCube:
"""
Scales the data linearly using the formula `output = (input * a) + b` and compresses values
from float32 to uint8 for memory optimization.
Parameters
----------
cube : openeo.DataCube
The input datacube to compress, only meteo data should be present.
alpha : float, optional (default=1.0)
The scaling factor. Values in the input datacube are multiplied by this coefficient.
beta : float, optional (default=0.0)
The offset. Values in the input datacube are added by this value.
Returns
-------
cube : openeo.DataCube
The datacube with the data linearly scaled and compressed to uint8 and rescaled frome.
"""
return _compress(cube, 0, 253, alpha, beta)

0 comments on commit deb5cdd

Please sign in to comment.