Repository: cerfacs-globc/icclim

Issue:
BUG: Problem with kg m-2 s-1 units for FractionOfTotal indicators
<!--
If you have encountered a problem while using icclim or you would like to suggest a change,
please follow the structure below to describe your request.
-->
<!-- import icclim; print(icclim.__version__) -->
<!-- import sys; print(sys.version) -->
* icclim version: 6.1.3
* Python version: 3.10.8


### Description
When I try and calculate a `FractionOfTotal` indicator (e.g. `R75pTOT`, `R95pTOT`, `R99pTOT`) with input units `kg m-2 s-1` the output data is all `inf` and `nan`. This problem doesn't happen with any other precipitation indices when the input data has units `kg m-2 s-1`, nor does it happen with these `FractionOfTotal` indices if the input units are `mm d-1`.


### Minimal reproducible example
Based on [Example 2: Index ETR](https://icclim.readthedocs.io/en/stable/how_to/recipes_ecad.html#example-2-index-etr)...
```python
files_pr = [
    "pr_day_CNRM-CM5_historical_r1i1p1_19300101-19341231.nc",
    "pr_day_CNRM-CM5_historical_r1i1p1_19350101-19391231.nc",

out_f = "R75pTOT_year_CNRM-CM5_historical_r1i1p1_1930-1939.nc"  # OUTPUT FILE: annual values of R75pTOT

icclim.index(
    index_name="R75pTOT",
    in_files=files_pr,
    var_name="pr",
    slice_mode="year",
    out_file=out_f,
)
```

### Output received

```python
import xarray as xr

ds = xr.open_dataset('R75pTOT_year_CNRM-CM5_histrorical_r1i1p1_1930-1939.nc')

print(ds['R75pTOT'].values)
```

```
[[[nan nan nan ... nan nan nan]
  [nan nan nan ... nan nan nan]
  [nan nan nan ... nan nan nan]
   ...
  [inf inf inf ... inf inf inf]
  [inf inf inf ... inf inf inf]
  [inf inf inf ... inf inf inf]]

...
```

Discussion:

Patch:
diff --git a/icclim/generic_indices/generic_indicators.py b/icclim/generic_indices/generic_indicators.py
index b5b0843..ce6fd4c 100644
--- a/icclim/generic_indices/generic_indicators.py
+++ b/icclim/generic_indices/generic_indicators.py
@@ -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")


Test patch:
diff --git a/icclim/tests/test_main.py b/icclim/tests/test_main.py
index 3ee2e77..c2ea7da 100644
--- a/icclim/tests/test_main.py
+++ b/icclim/tests/test_main.py
@@ -508,16 +508,21 @@ class Test_Integration:
         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)