Skip to content

Instantly share code, notes, and snippets.

@mullenkamp
mullenkamp / compute_scale_and_offset.py
Last active November 9, 2023 23:02
python scale and offset calculation for shrinking data to a specific integer
import numpy as np
import math
def compute_scale_and_offset(min_value: (int, float, np.number), max_value: (int, float, np.number), dtype: np.dtype):
"""
Computes the scale (slope) and offset for a dataset using a min value, max value, and the required np.dtype.
It leaves one value at the lower extreme to use for the nan fillvalue.
These are the min values set asside for the fillvalue (up to 64 bits).
int8: -128