Skip to content

Commit

Permalink
pre-commit fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lee1043 committed May 2, 2024
1 parent 953f1a5 commit a56b94c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
1 change: 0 additions & 1 deletion pcmdi_metrics/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .adjust_units import adjust_units

from .custom_season import (
custom_season_average,
custom_season_departure,
Expand Down
27 changes: 27 additions & 0 deletions pcmdi_metrics/utils/adjust_units.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import xarray as xr


def adjust_units(da: xr.DataArray, adjust_tuple: tuple) -> xr.DataArray:
"""Convert unit following information in the given tuple
Parameters
----------
da : xr.DataArray
input data array
adjust_tuple : tuple with at least 3 elements (4th element is optional for units)
e.g.: (True, 'multiply', 86400., 'mm d-1'): e.g., kg m-2 s-1 to mm d-1
(False, 0, 0, 0): no unit conversion
Returns
-------
xr.DataArray
data array that contains converted values and attributes
"""
action_dict = {"multiply": "*", "divide": "/", "add": "+", "subtract": "-"}
if adjust_tuple[0]:
print("Converting units by ", adjust_tuple[1], adjust_tuple[2])
cmd = " ".join(["da", str(action_dict[adjust_tuple[1]]), str(adjust_tuple[2])])
da = eval(cmd)
if len(adjust_tuple) > 3:
da.assign_attrs(units=adjust_tuple[3])
return da

0 comments on commit a56b94c

Please sign in to comment.