Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve levels auto_range with proper symmetry #17

Merged
merged 3 commits into from
Oct 18, 2024
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
28 changes: 13 additions & 15 deletions src/earthkit/plots/styles/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import math

import numpy as np

from earthkit.plots.schemas import schema
Expand Down Expand Up @@ -50,28 +48,28 @@ def auto_range(data, divergence_point=None, n_levels=schema.default_style_levels

if divergence_point is not None:
max_diff = max(max_value - divergence_point, divergence_point - min_value)
max_value = divergence_point + max_diff
min_value = divergence_point - max_diff
max_value = max_diff
min_value = -max_diff

data_range = max_value - min_value

initial_bin = data_range / n_levels

magnitude = 10 ** (math.floor(math.log(initial_bin, 10)))
magnitude = 10 ** (np.floor(np.log10(initial_bin)))
bin_width = initial_bin - (initial_bin % -magnitude)

start = min_value - (min_value % magnitude)

levels = np.arange(
start,
start + (bin_width * n_levels) + bin_width,
bin_width,
).tolist()
min_value -= min_value % bin_width
max_value -= max_value % -bin_width

while levels[-2] >= max_value:
levels = levels[:-1]
if divergence_point is not None:
min_value += divergence_point
max_value += divergence_point

return levels
return np.linspace(
min_value,
max_value,
n_levels + 1,
).tolist()


def step_range(data, step, reference=None):
Expand Down