Skip to content

Commit

Permalink
clean v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JoranAngevaare committed Apr 22, 2024
1 parent 4a514e6 commit 1648eac
Show file tree
Hide file tree
Showing 22 changed files with 146 additions and 1,607 deletions.
2 changes: 0 additions & 2 deletions optim_esm_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
from . import config
from . import analyze
from . import region_finding
from . import synda_files
from . import _test_utils
from . import plotting

# Forward some of the essential tools to this main
from .analyze.cmip_handler import read_ds
from .analyze.io import load_glob
from .plotting.map_maker import MapMaker
from .utils import print_versions
from .config import get_logger
34 changes: 7 additions & 27 deletions optim_esm_tools/analyze/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,9 @@ def _build_cluster_with_kw(
clusters, sub_masks = build_clusters(**cluster_kw, keep_masks=True)

if global_mask is None:
global_mask = np.ones(lat.shape, dtype=np.bool_)
raise ValueError('global_mask is required')
clusters = [np.rad2deg(cluster) for cluster in clusters]
if show_tqdm is not None:
get_logger().warning(
'Calling "_build_cluster_with_kw" with show_tqdm is deprecated',
)

if lat.shape != lon.shape:
raise ValueError(
f'Got inconsistent input {lat.shape} != {lon.shape}',
Expand Down Expand Up @@ -327,33 +324,16 @@ def infer_max_step_size(
"""
if off_by_factor is None:
off_by_factor = float(config['analyze']['clustering_fudge_factor'])
if len(lat.shape) == 1:
return off_by_factor * np.max(calculate_distance_map(lat, lon)) # type: ignore

get_logger().info(
'(Irregular) grid, max_step_size based on first points above equator',
)
# We have to get two points from the potentially irregular grid and guess the distance from
# that. This is not as reliable as calculating this for a regular grid.

equator_idx = np.argmin(np.abs(np.mean(lat, axis=1)) - 90)
lon_0 = lon[0]
coords = [
[[lat[equator_idx][0], lon_0[0]], [lat[equator_idx][0], lon_0[1]]],
[[lat[equator_idx][0], lon_0[0]], [lat[equator_idx + 1][0], lon_0[0]]],
[[lat[equator_idx][0], lon_0[0]], [lat[equator_idx + 1][0], lon_0[1]]],
]
# assert False, coords
# Return the distance between grid cells * off_by_factor
return off_by_factor * max(_distance(c) for c in coords)
assert len(lat.shape) == 1
# Simple 1D array
return off_by_factor * np.max(calculate_distance_map(lat, lon))


def calculate_distance_map(lat: np.ndarray, lon: np.ndarray) -> np.ndarray:
"""For each point in a spanned lat lon grid, calculate the distance to the
neighboring points."""
if isinstance(lat, xr.DataArray):
lat = lat.values
lon = lon.values
if isinstance(lat, xr.DataArray): # pragma: no cover
raise ValueError('Numpy array required')
return _calculate_distance_map(lat, lon)


Expand Down
2 changes: 0 additions & 2 deletions optim_esm_tools/analyze/combine_variables.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import string
import typing as ty
from collections import defaultdict
Expand All @@ -11,7 +10,6 @@
from matplotlib.legend_handler import HandlerTuple

import optim_esm_tools as oet
from optim_esm_tools.analyze.time_statistics import default_thresholds


class VariableMerger:
Expand Down
2 changes: 1 addition & 1 deletion optim_esm_tools/analyze/find_matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def associate_parent(
data_set = _read_dataset(data_set=data_set, required_file=required_file, path=path)
base = base_from_path(path, look_back_extra=look_back_extra)
search = _get_search_kw(data_set)
search = _check_search_kw(search)
search = _check_search_kw(search=search, data_set=data_set, log=log, path=path)

if all(v == '*' for v in search.values()) or search['source_id'] == '*':
raise ValueError(f'Unclear search for {path} - attributes are missing')
Expand Down
Loading

0 comments on commit 1648eac

Please sign in to comment.