Skip to content

Commit

Permalink
feat: add range_len for easing progress information
Browse files Browse the repository at this point in the history
  • Loading branch information
spool committed Oct 5, 2024
1 parent df50b31 commit e00b984
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
31 changes: 31 additions & 0 deletions python/clim_recal/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,37 @@ def from_year_range_to_str(
DEFAULT_CPM_START_MONTH_DAY: Final[MonthDay] = MonthDay(month=12, day=1)


def range_len(
maximum: int, start: int = 0, stop: int | None = None, step: int = 1
) -> int:
"""Cacluate the total length of range with indexing.
Parameters
----------
maximum
Maximum range length.
start
Index to start from.
stop
Index to stop at.
step
Steps between `start` and `stop` indexes
Examples
--------
>>> range_len(100)
100
>>> range_len(100, 90)
10
>>> range_len(100, 20, 30)
10
>>> range_len(100, 20, 30, 2)
5
"""
stop = stop or maximum
return (stop - start - 1) // step + 1


def run_callable_attr(
instance: object, method_name: str = "execute", *args, **kwargs
) -> Any:
Expand Down
13 changes: 7 additions & 6 deletions python/clim_recal/utils/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
ISO_DATE_FORMAT_STR,
climate_data_mount_path,
console,
range_len,
results_path,
)
from .data import (
Expand Down Expand Up @@ -1513,8 +1514,8 @@ class XarrayTimeSeriesCalcManager(Sequence):
>>> tmp_save: Path = getfixture('tmp_path') / 'xarray-time-series-summary-manager'
>>> xr_var_managers = XarrayTimeSeriesCalcManager(save_folder=tmp_save)
>>> save_paths: tuple[Path, ...] = xr_var_managers.save_joined_xr_time_series(stop=2, ts_stop=2)
Aggregating '05' 'tasmax' ...
Aggregating '06' 'tasmax' ...
Aggregating '05' 'tasmax' (1/2)...
Aggregating '06' 'tasmax' (2/2)...
>>> pprint(save_paths)
(...Path('.../median-tasmax-05.nc'),
...Path('.../median-tasmax-06.nc'))
Expand Down Expand Up @@ -1583,14 +1584,14 @@ def join_xr_time_series_vars_iter(
) -> Iterable[tuple[T_Dataset, Path]]:
if not self.source_folders:
self._set_source_folders()
aggregate_count: int = range_len(
maximum=len(self), start=start, stop=stop, step=step
)
for i, var_path in enumerate(islice(self, start, stop, step)):
var_path = Path(var_path)
run, variable = self._get_var_run(var_path)
log_str: str = f"Aggregating '{variable}' '{run}'"
if not start and not stop and step == 1:
console.print(f"{log_str} ({i + 1}/{len(self)})...")
else:
console.print(f"{log_str} ...")
console.print(f"{log_str} ({i + 1}/{aggregate_count})...")
yield join_xr_time_series_var(
var_path, start=ts_start, stop=ts_stop, step=ts_step
), var_path
Expand Down

0 comments on commit e00b984

Please sign in to comment.