Skip to content

Commit

Permalink
Merge pull request #283 from cerfacs-globc/fix/rxxpTOT_total_computation
Browse files Browse the repository at this point in the history
Fix/rxxp tot total computation
  • Loading branch information
bzah authored Dec 6, 2023
2 parents 091c4b3 + 731a1ee commit dc88a5a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
8 changes: 5 additions & 3 deletions icclim/generic_indices/generic_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,12 @@ def fraction_of_total(
) -> DataArray:
study, threshold = get_single_var(climate_vars)
if threshold.threshold_min_value is not None:
min_val = threshold.threshold_min_value
min_val = convert_units_to(min_val, study, context="hydro")
total = (
study.where(threshold.operator(study, threshold.threshold_min_value.m))
.resample(time=resample_freq.pandas_freq)
.sum(dim="time")
study.where(threshold.operator(study, min_val))
# study.where(threshold.operator(study, threshold.threshold_min_value.m))
.resample(time=resample_freq.pandas_freq).sum(dim="time")
)
else:
total = study.resample(time=resample_freq.pandas_freq).sum(dim="time")
Expand Down
13 changes: 9 additions & 4 deletions icclim/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,16 +508,21 @@ def test_index_prcptot(self):
np.testing.assert_array_almost_equal(res.PRCPTOT.isel(time=0), 42)

def test_index_r75ptot(self):
pr = stub_pr(value=0.00002)
# 2.32e-06 is about 0.2 mm/day
# they will be ignore in computation because < 1 mm/day
pr = stub_pr(value=2e-06)
pr.attrs["units"] = "kg m-2 s-1"
pr[:10] = 100
pr[:10] = 2e-04 # 10 days of ~20 mm/day
pr[10:110] = 2e-05 # 100 days of ~2 mm/day
res = icclim.r75ptot(
in_files=pr,
out_file=self.OUTPUT_FILE,
slice_mode="year",
save_thresholds=True,
).load()
# 100% of precip are due to the precip above the 75th percentile
assert res.R75pTOT.isel(time=0) == 100
# 75th per is 2e-05
# 50% of precip are due to the precip above the 75th percentile
np.testing.assert_array_almost_equal(res.R75pTOT.isel(time=0), 50)

def test_index_csu(self):
tas = stub_tas(tas_value=26 + K2C)
Expand Down

0 comments on commit dc88a5a

Please sign in to comment.