Skip to content

Commit

Permalink
Monthly mean (#39)
Browse files Browse the repository at this point in the history
* add monthly weighted

* typo
  • Loading branch information
malmans2 committed Aug 2, 2023
1 parent 4139d97 commit 8d5bb2b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
64 changes: 64 additions & 0 deletions c3s_eqc_automatic_quality_control/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"annual_weighted_mean",
"annual_weighted_std",
"grid_cell_area",
"monthly_weighted_mean",
"monthly_weighted_std",
"regrid",
"rolling_weighted_filter",
"seasonal_weighted_mean",
Expand Down Expand Up @@ -239,6 +241,37 @@ def attrs_func(attrs: dict[str, Any]) -> dict[str, Any]:
return _apply_attrs_func(coverage, obj, attrs_func)


def monthly_weighted_mean(
obj: xr.DataArray | xr.Dataset,
time_name: Hashable | None = None,
weights: xr.DataArray | bool = True,
**kwargs: Any,
) -> xr.DataArray | xr.Dataset:
"""
Calculate monthly weighted mean.
Parameters
----------
obj: DataArray or Dataset
Input data
time_name: str, optional
Name of time coordinate
weights: DataArray, bool, default: True
Weights to apply:
- True: weights are the number of days in each month
- False: unweighted
- DataArray: custom weights
Returns
-------
DataArray or Dataset
Reduced object
"""
return _time_weighted.TimeWeighted(obj, time_name, weights).reduce(
"mean", "month", **kwargs
)


def seasonal_weighted_mean(
obj: xr.DataArray | xr.Dataset,
time_name: Hashable | None = None,
Expand Down Expand Up @@ -270,6 +303,37 @@ def seasonal_weighted_mean(
)


def monthly_weighted_std(
obj: xr.DataArray | xr.Dataset,
time_name: Hashable | None = None,
weights: xr.DataArray | bool = True,
**kwargs: Any,
) -> xr.DataArray | xr.Dataset:
"""
Calculate monthly weighted std.
Parameters
----------
obj: DataArray or Dataset
Input data
time_name: str, optional
Name of time coordinate
weights: DataArray, bool, default: True
Weights to apply:
- True: weights are the number of days in each month
- False: unweighted
- DataArray: custom weights
Returns
-------
DataArray or Dataset
Reduced object
"""
return _time_weighted.TimeWeighted(obj, time_name, weights).reduce(
"std", "month", **kwargs
)


def seasonal_weighted_std(
obj: xr.DataArray | xr.Dataset,
time_name: Hashable | None = None,
Expand Down
22 changes: 22 additions & 0 deletions tests/test_21_time_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ def test_time_weighted_std(
actual = diagnostics.time_weighted_std(obj, weights=weights)
xr.testing.assert_equal(expected, actual)

def test_monthly_weighted_mean(
self, obj: xr.DataArray | xr.Dataset, weights: bool
) -> None:
if weights:
expected = (obj).groupby("time.month").map(weighted_mean)
expected = expected.where(expected != 0)
else:
expected = obj.groupby("time.month").mean("time")
actual = diagnostics.monthly_weighted_mean(obj, weights=weights)
xr.testing.assert_equal(expected, actual)

def test_seasonal_weighted_mean(
self, obj: xr.DataArray | xr.Dataset, weights: bool
) -> None:
Expand All @@ -85,6 +96,17 @@ def test_seasonal_weighted_mean(
actual = diagnostics.seasonal_weighted_mean(obj, weights=weights)
xr.testing.assert_equal(expected, actual)

def test_monthly_weighted_std(
self, obj: xr.DataArray | xr.Dataset, weights: bool
) -> None:
if weights:
expected = obj.groupby("time.month").map(weighted_std)
expected = expected.where(expected != 0)
else:
expected = obj.groupby("time.month").std("time")
actual = diagnostics.monthly_weighted_std(obj, weights=weights)
xr.testing.assert_equal(expected, actual)

def test_seasonal_weighted_std(
self, obj: xr.DataArray | xr.Dataset, weights: bool
) -> None:
Expand Down

0 comments on commit 8d5bb2b

Please sign in to comment.