Skip to content
Merged
Changes from all 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
12 changes: 8 additions & 4 deletions nlmod/dims/attributes_encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,13 @@ def get_encodings(

def compute_scale_and_offset(min_value, max_value):
"""
Computes the scale_factor and offset for the dataset using a min_value and max_value,
and int16. Useful for maximizing the compression of a dataset.

Reduce precision of the dataset by storing it as int16 and thereby reducing the precision.

Computes the scale_factor and offset for the dataset using a min_value and max_value to
transform the range of the dataset to the range of valid int16 values. The packed value
is computed as:
packed_value = (unpacked_value - add_offset) / scale_factor

Parameters
----------
min_value : float
Expand All @@ -250,7 +254,7 @@ def compute_scale_and_offset(min_value, max_value):
# from -32766 to 32767, because -32767 is the default fillvalue.
width = 32766 + 32767
scale_factor = (max_value - min_value) / width
add_offset = 1.0 + scale_factor * (width - 1) / 2
add_offset = max_value - scale_factor * 32767
return scale_factor, add_offset


Expand Down