hydrotoolbox 2.0.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. hydrotoolbox/__init__.py +11 -0
  2. hydrotoolbox/baseflow/__init__.py +9 -0
  3. hydrotoolbox/baseflow/comparison.py +72 -0
  4. hydrotoolbox/baseflow/example.csv +34004 -0
  5. hydrotoolbox/baseflow/methods/Boughton.py +29 -0
  6. hydrotoolbox/baseflow/methods/CM.py +21 -0
  7. hydrotoolbox/baseflow/methods/Chapman.py +21 -0
  8. hydrotoolbox/baseflow/methods/EWMA.py +20 -0
  9. hydrotoolbox/baseflow/methods/Eckhardt.py +30 -0
  10. hydrotoolbox/baseflow/methods/Fixed.py +26 -0
  11. hydrotoolbox/baseflow/methods/Furey.py +29 -0
  12. hydrotoolbox/baseflow/methods/LH.py +32 -0
  13. hydrotoolbox/baseflow/methods/Local.py +47 -0
  14. hydrotoolbox/baseflow/methods/Slide.py +25 -0
  15. hydrotoolbox/baseflow/methods/UKIH.py +46 -0
  16. hydrotoolbox/baseflow/methods/Willems.py +31 -0
  17. hydrotoolbox/baseflow/methods/__init__.py +15 -0
  18. hydrotoolbox/baseflow/methods/five_day.py +32 -0
  19. hydrotoolbox/baseflow/methods/ihacres.py +24 -0
  20. hydrotoolbox/baseflow/methods/strict.py +17 -0
  21. hydrotoolbox/baseflow/param_estimate.py +95 -0
  22. hydrotoolbox/baseflow/separation.py +98 -0
  23. hydrotoolbox/baseflow/utils.py +94 -0
  24. hydrotoolbox/baseflow_sep.py +979 -0
  25. hydrotoolbox/hydrotoolbox.py +2132 -0
  26. hydrotoolbox/indices/indices.py +1573 -0
  27. hydrotoolbox/toolbox_utils/docs/conf.py +309 -0
  28. hydrotoolbox/toolbox_utils/setup.py +21 -0
  29. hydrotoolbox/toolbox_utils/src/toolbox_utils/__init__.py +0 -0
  30. hydrotoolbox/toolbox_utils/src/toolbox_utils/readers/__init__.py +0 -0
  31. hydrotoolbox/toolbox_utils/src/toolbox_utils/readers/hbn.py +392 -0
  32. hydrotoolbox/toolbox_utils/src/toolbox_utils/readers/plotgen.py +55 -0
  33. hydrotoolbox/toolbox_utils/src/toolbox_utils/readers/wdm.py +233 -0
  34. hydrotoolbox/toolbox_utils/src/toolbox_utils/tssplit/setup.py +38 -0
  35. hydrotoolbox/toolbox_utils/src/toolbox_utils/tssplit/tssplit/__init__.py +1 -0
  36. hydrotoolbox/toolbox_utils/src/toolbox_utils/tssplit/tssplit/tssplit.py +47 -0
  37. hydrotoolbox/toolbox_utils/src/toolbox_utils/tsutils.py +2605 -0
  38. hydrotoolbox/toolbox_utils/tests/test_asbestfreq.py +72 -0
  39. hydrotoolbox/toolbox_utils/tests/test_common_kwds.py +23 -0
  40. hydrotoolbox/toolbox_utils/tests/test_date_slice.py +98 -0
  41. hydrotoolbox/toolbox_utils/tests/test_dateparse.py +41 -0
  42. hydrotoolbox/toolbox_utils/tests/test_extract.py +80 -0
  43. hydrotoolbox/toolbox_utils/tests/test_hbn.py +79 -0
  44. hydrotoolbox/toolbox_utils/tests/test_make_list.py +92 -0
  45. hydrotoolbox/toolbox_utils/tests/test_period_timestamp.py +136 -0
  46. hydrotoolbox/toolbox_utils/tests/test_plotgen.py +760 -0
  47. hydrotoolbox/toolbox_utils/tests/test_range_to_numlist.py +63 -0
  48. hydrotoolbox/toolbox_utils/tests/test_read.py +106 -0
  49. hydrotoolbox/toolbox_utils/tests/test_tssplit.py +23 -0
  50. hydrotoolbox/toolbox_utils/tests/test_wdm.py +59 -0
  51. hydrotoolbox/utils.py +28 -0
  52. hydrotoolbox-2.0.9.dist-info/LICENSE.txt +24 -0
  53. hydrotoolbox-2.0.9.dist-info/METADATA +158 -0
  54. hydrotoolbox-2.0.9.dist-info/RECORD +57 -0
  55. hydrotoolbox-2.0.9.dist-info/WHEEL +5 -0
  56. hydrotoolbox-2.0.9.dist-info/entry_points.txt +2 -0
  57. hydrotoolbox-2.0.9.dist-info/top_level.txt +1 -0
@@ -0,0 +1,29 @@
1
+ import numpy as np
2
+
3
+
4
+ def Boughton(Q, b_LH, a, C, return_exceed=False):
5
+ """Boughton double-parameter filter (Boughton, 2004)
6
+
7
+ Args:
8
+ Q (np.array): streamflow
9
+ a (float): recession coefficient
10
+ C (float): calibrated in baseflow.param_estimate
11
+ """
12
+ b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
13
+ b[0] = b_LH[0]
14
+ first_c = a / (1 + C)
15
+ second_c = C / (1 + C)
16
+ for i in range(Q.shape[0] - 1):
17
+ b[i + 1] = first_c * b[i] + second_c * Q[i + 1]
18
+ if b[i + 1] > Q[i + 1]:
19
+ b[i + 1] = Q[i + 1]
20
+ if return_exceed:
21
+ b[-1] += 1
22
+ return b
23
+
24
+
25
+ def f_Boughton(a):
26
+ def _Boughton(Q, b_LH, C, return_exceed=False):
27
+ return Boughton(Q, b_LH, a, C, return_exceed=return_exceed)
28
+
29
+ return _Boughton
@@ -0,0 +1,21 @@
1
+ import numpy as np
2
+
3
+
4
+ def CM(Q, b_LH, a, return_exceed=False):
5
+ """CM filter (Chapman & Maxwell, 1996)
6
+
7
+ Args:
8
+ Q (np.array): streamflow
9
+ a (float): recession coefficient
10
+ """
11
+ b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
12
+ b[0] = b_LH[0]
13
+ first_c = a / (2 - 1)
14
+ second_c = (1 - a) / (2 - a)
15
+ for i in range(Q.shape[0] - 1):
16
+ b[i + 1] = first_c * b[i] + second_c * Q[i + 1]
17
+ if b[i + 1] > Q[i + 1]:
18
+ b[i + 1] = Q[i + 1]
19
+ if return_exceed:
20
+ b[-1] += 1
21
+ return b
@@ -0,0 +1,21 @@
1
+ import numpy as np
2
+
3
+
4
+ def Chapman(Q, b_LH, a, return_exceed=False):
5
+ """Chapman filter (Chapman, 1991)
6
+
7
+ Args:
8
+ Q (np.array): streamflow
9
+ a (float): recession coefficient
10
+ """
11
+ b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
12
+ b[0] = b_LH[0]
13
+ first_c = (3 * a - 1) / (3 - a)
14
+ second_c = (1 - a) / (3 - a)
15
+ for i in range(Q.shape[0] - 1):
16
+ b[i + 1] = first_c * b[i] + second_c * (Q[i + 1] + Q[i])
17
+ if b[i + 1] > Q[i + 1]:
18
+ b[i + 1] = Q[i + 1]
19
+ if return_exceed:
20
+ b[-1] += 1
21
+ return b
@@ -0,0 +1,20 @@
1
+ import numpy as np
2
+
3
+
4
+ def EWMA(Q, b_LH, e, return_exceed=False):
5
+ """exponential weighted moving average (EWMA) filter (Tularam & Ilahee, 2008)
6
+
7
+ Args:
8
+ Q (np.array): streamflow
9
+ e (float): smoothing parameter
10
+ """
11
+ b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
12
+ b[0] = b_LH[0]
13
+ first_c = 1 - e
14
+ for i in range(Q.shape[0] - 1):
15
+ b[i + 1] = first_c * b[i] + e * Q[i + 1]
16
+ if b[i + 1] > Q[i + 1]:
17
+ b[i + 1] = Q[i + 1]
18
+ if return_exceed:
19
+ b[-1] += 1
20
+ return b
@@ -0,0 +1,30 @@
1
+ import numpy as np
2
+
3
+
4
+ def Eckhardt(Q, b_LH, a, BFImax, return_exceed=False):
5
+ """Eckhardt filter (Eckhardt, 2005)
6
+
7
+ Args:
8
+ Q (np.array): streamflow
9
+ a (float): recession coefficient
10
+ BFImax (float): maximum value of baseflow index (BFI)
11
+ """
12
+ b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
13
+ b[0] = b_LH[0]
14
+ first_c = (1 - BFImax) * a
15
+ second_c = (1 - a) * BFImax
16
+ third_c = 1 - a * BFImax
17
+ for i in range(Q.shape[0] - 1):
18
+ b[i + 1] = (first_c * b[i] + second_c * Q[i + 1]) / (third_c)
19
+ if b[i + 1] > Q[i + 1]:
20
+ b[i + 1] = Q[i + 1]
21
+ if return_exceed:
22
+ b[-1] += 1
23
+ return b
24
+
25
+
26
+ def f_Eckhardt(a):
27
+ def _Eckhardt(Q, b_LH, BFImax, return_exceed=False):
28
+ return Eckhardt(Q, b_LH, a, BFImax, return_exceed=return_exceed)
29
+
30
+ return _Eckhardt
@@ -0,0 +1,26 @@
1
+ import numpy as np
2
+
3
+ from .Local import hysep_interval
4
+
5
+
6
+ def Fixed(Q, area=None):
7
+ """Fixed interval graphical method from HYSEP program (Sloto & Crouse, 1996)
8
+
9
+ Args:
10
+ Q (np.array): streamflow
11
+ area (float): basin area in km^2
12
+ """
13
+ inN = hysep_interval(area)
14
+ return Fixed_interpolation(Q, inN)
15
+
16
+
17
+ def Fixed_interpolation(Q, inN):
18
+ b = np.zeros(Q.shape[0])
19
+ n = Q.shape[0] // inN
20
+ for i in range(n):
21
+ b[inN * i : inN * (i + 1)] = np.min(Q[inN * i : inN * (i + 1)])
22
+ if n * inN != Q.shape[0]:
23
+ b[n * inN :] = np.min(Q[n * inN :])
24
+ mask = b > Q
25
+ b[mask] = Q[mask]
26
+ return b
@@ -0,0 +1,29 @@
1
+ import numpy as np
2
+
3
+
4
+ def Furey(Q, b_LH, a, A, return_exceed=False):
5
+ """Furey digital filter (Furey & Gupta, 2001, 2003)
6
+
7
+ Args:
8
+ Q (np.array): streamflow
9
+ a (float): recession coefficient
10
+ A (float): calibrated in baseflow.param_estimate
11
+ """
12
+ b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
13
+ b[0] = b_LH[0]
14
+ first_c = a - A * (1 - a)
15
+ second_c = A * (1 - a)
16
+ for i in range(Q.shape[0] - 1):
17
+ b[i + 1] = first_c * b[i] + second_c * Q[i]
18
+ if b[i + 1] > Q[i + 1]:
19
+ b[i + 1] = Q[i + 1]
20
+ if return_exceed:
21
+ b[-1] += 1
22
+ return b
23
+
24
+
25
+ def f_Furey(a):
26
+ def _Furey(Q, b_LH, A, return_exceed=False):
27
+ return Furey(Q, b_LH, a, A, return_exceed=return_exceed)
28
+
29
+ return _Furey
@@ -0,0 +1,32 @@
1
+ import numpy as np
2
+
3
+
4
+ def LH(Q, beta=0.925, return_exceed=False):
5
+ """LH digital filter (Lyne & Hollick, 1979)
6
+
7
+ Args:
8
+ Q (np.array): streamflow
9
+ beta (float): filter parameter, 0.925 recommended by (Nathan & McMahon, 1990)
10
+ """
11
+ b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
12
+
13
+ first_c = (1 - beta) / 2
14
+
15
+ # first pass
16
+ b[0] = Q[0] / 2
17
+ for i in range(Q.shape[0] - 1):
18
+ b[i + 1] = beta * b[i] + first_c * (Q[i] + Q[i + 1])
19
+ if b[i + 1] > Q[i + 1]:
20
+ b[i + 1] = Q[i + 1]
21
+ if return_exceed:
22
+ b[-1] += 1
23
+
24
+ # second pass
25
+ b1 = np.copy(b)
26
+ for i in range(Q.shape[0] - 2, -1, -1):
27
+ b[i] = beta * b[i + 1] + first_c * (b1[i + 1] + b1[i])
28
+ if b[i] > Q[i]:
29
+ b[i] = Q[i]
30
+ if return_exceed:
31
+ b[-1] += 1
32
+ return b
@@ -0,0 +1,47 @@
1
+ from typing import Optional
2
+
3
+ import numpy as np
4
+
5
+ try:
6
+ from pydantic import validate_call
7
+ except ImportError:
8
+ from pydantic import validate_arguments as validate_call
9
+
10
+ from .UKIH import linear_interpolation
11
+
12
+
13
+ def Local(Q, b_LH, area=None, return_exceed=False):
14
+ """Local minimum graphical method from HYSEP program (Sloto & Crouse, 1996)
15
+
16
+ Args:
17
+ Q (np.array): streamflow
18
+ area (float): basin area in km^2
19
+ """
20
+ idx_turn = Local_turn(Q, hysep_interval(area))
21
+ b = linear_interpolation(Q, idx_turn, return_exceed=return_exceed)
22
+ b[: idx_turn[0]] = b_LH[: idx_turn[0]]
23
+ b[idx_turn[-1] + 1 :] = b_LH[idx_turn[-1] + 1 :]
24
+ return b
25
+
26
+
27
+ @validate_call
28
+ def hysep_interval(area: Optional[float] = None) -> int:
29
+ # The duration of surface runoff is calculated from the empirical relation:
30
+ # N=A^0.2, (1) where N is the number of days after which surface runoff ceases,
31
+ # and A is the drainage area in square miles (Linsley and others, 1982, p. 210).
32
+ # The interval 2N* used for hydrograph separations is the odd integer between
33
+ # 3 and 11 nearest to 2N (Pettyjohn and Henning, 1979, p. 31).
34
+ N = 5 if area is None else np.power(0.3861022 * area, 0.2)
35
+ inN = np.ceil(2 * N)
36
+ if np.mod(inN, 2) == 0:
37
+ inN = np.ceil(2 * N) - 1
38
+ inN = np.int64(min(max(inN, 3), 11))
39
+ return inN
40
+
41
+
42
+ def Local_turn(Q, inN):
43
+ idx_turn = np.zeros(Q.shape[0], dtype=np.int64)
44
+ for i in range(np.int64((inN - 1) / 2), np.int64(Q.shape[0] - (inN - 1) / 2)):
45
+ if Q[i] == np.min(Q[np.int64(i - (inN - 1) / 2) : np.int64(i + (inN + 1) / 2)]):
46
+ idx_turn[i] = i
47
+ return idx_turn[idx_turn != 0]
@@ -0,0 +1,25 @@
1
+ import numpy as np
2
+
3
+ from .Local import hysep_interval
4
+
5
+
6
+ def Slide(Q, area):
7
+ """Slide interval graphical method from HYSEP program (Sloto & Crouse, 1996)
8
+
9
+ Args:
10
+ Q (np.array): streamflow
11
+ area (float): basin area in km^2
12
+ """
13
+ inN = hysep_interval(area)
14
+ return Slide_interpolation(Q, inN)
15
+
16
+
17
+ def Slide_interpolation(Q, inN):
18
+ b = np.zeros(Q.shape[0])
19
+ for i in range(np.int64((inN - 1) / 2), np.int64(Q.shape[0] - (inN - 1) / 2)):
20
+ b[i] = np.min(Q[np.int64(i - (inN - 1) / 2) : np.int64(i + (inN + 1) / 2)])
21
+ b[: np.int64((inN - 1) / 2)] = np.min(Q[: np.int64((inN - 1) / 2)])
22
+ b[np.int64(Q.shape[0] - (inN - 1) / 2) :] = np.min(
23
+ Q[np.int64(Q.shape[0] - (inN - 1) / 2) :]
24
+ )
25
+ return b
@@ -0,0 +1,46 @@
1
+ import numpy as np
2
+
3
+
4
+ def UKIH(Q, b_LH, return_exceed=False):
5
+ """graphical method developed by UK Institute of Hydrology (UKIH, 1980)
6
+
7
+ Args:
8
+ Q (np.array): streamflow
9
+ """
10
+ N = 5
11
+ block_end = Q.shape[0] // N * N
12
+ idx_min = np.argmin(Q[:block_end].reshape(-1, N), axis=1)
13
+ idx_min = idx_min + np.arange(0, block_end, N)
14
+ idx_turn = UKIH_turn(Q, idx_min)
15
+ b = linear_interpolation(Q, idx_turn, return_exceed=return_exceed)
16
+ b[: idx_turn[0]] = b_LH[: idx_turn[0]]
17
+ b[idx_turn[-1] + 1 :] = b_LH[idx_turn[-1] + 1 :]
18
+ return b
19
+
20
+
21
+ def UKIH_turn(Q, idx_min):
22
+ idx_turn = np.zeros(idx_min.shape[0], dtype=np.int64)
23
+ for i in range(idx_min.shape[0] - 2):
24
+ if (0.9 * Q[idx_min[i + 1]] < Q[idx_min[i]]) & (
25
+ 0.9 * Q[idx_min[i + 1]] < Q[idx_min[i + 2]]
26
+ ):
27
+ idx_turn[i] = idx_min[i + 1]
28
+ return idx_turn[idx_turn != 0]
29
+
30
+
31
+ def linear_interpolation(Q, idx_turn, return_exceed=False):
32
+ b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
33
+ n = 0
34
+ for i in range(idx_turn[0], idx_turn[-1] + 1):
35
+ if i == idx_turn[n + 1]:
36
+ n += 1
37
+ b[i] = Q[i]
38
+ else:
39
+ b[i] = Q[idx_turn[n]] + (Q[idx_turn[n + 1]] - Q[idx_turn[n]]) / (
40
+ idx_turn[n + 1] - idx_turn[n]
41
+ ) * (i - idx_turn[n])
42
+ if b[i] > Q[i]:
43
+ b[i] = Q[i]
44
+ if return_exceed:
45
+ b[-1] += 1
46
+ return b
@@ -0,0 +1,31 @@
1
+ import numpy as np
2
+
3
+
4
+ def Willems(Q, b_LH, a, w, return_exceed=False):
5
+ """digital filter (Willems, 2009)
6
+
7
+ Args:
8
+ Q (np.array): streamflow
9
+ a (float): recession coefficient
10
+ w (float): case-specific average proportion of the quick flow
11
+ in the streamflow, calibrated in baseflow.param_estimate
12
+ """
13
+ b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
14
+ b[0] = b_LH[0]
15
+ v = (1 - w) * (1 - a) / (2 * w)
16
+ first_c = (a - v) / (1 + v)
17
+ second_c = v / (1 + v)
18
+ for i in range(Q.shape[0] - 1):
19
+ b[i + 1] = first_c * b[i] + second_c * (Q[i] + Q[i + 1])
20
+ if b[i + 1] > Q[i + 1]:
21
+ b[i + 1] = Q[i + 1]
22
+ if return_exceed:
23
+ b[-1] += 1
24
+ return b
25
+
26
+
27
+ def f_Willems(a):
28
+ def _Willems(Q, b_LH, w, return_exceed=False):
29
+ return Willems(Q, b_LH, a, w, return_exceed=return_exceed)
30
+
31
+ return _Willems
@@ -0,0 +1,15 @@
1
+ from .Boughton import *
2
+ from .Chapman import *
3
+ from .CM import *
4
+ from .Eckhardt import *
5
+ from .EWMA import *
6
+ from .five_day import *
7
+ from .Fixed import *
8
+ from .Furey import *
9
+ from .ihacres import *
10
+ from .LH import *
11
+ from .Local import *
12
+ from .Slide import *
13
+ from .strict import *
14
+ from .UKIH import *
15
+ from .Willems import *
@@ -0,0 +1,32 @@
1
+ """Tools for hydrology.
2
+
3
+ hydrotoolbox baseflow_sep --area 59.1 --area_units 'mile**2' linear < daily.csv
4
+ hydrotoolbox baseflow_sep sliding < daily.csv
5
+ hydrotoolbox baseflow_sep eckardt,sliding < daily.csv
6
+ ...
7
+
8
+ hydrotoolbox recession """
9
+
10
+ import pandas as pd
11
+
12
+
13
+ def five_day(Q):
14
+ # Create new DataFrame with arbitrary daily index
15
+ ndf = pd.DataFrame(
16
+ Q, index=pd.date_range(start="1/1/2000", periods=len(Q), freq="D")
17
+ )
18
+ vals = ndf.groupby(pd.Grouper(freq="5D")).min().astype("float64")
19
+ srccol = 0.9 * vals
20
+ prevrow = vals.shift(-1)
21
+ nextrow = vals.shift(1)
22
+ mask = (srccol > prevrow) | (srccol > nextrow)
23
+
24
+ vals[mask] = None
25
+ vals = vals.interpolate(method="linear")
26
+
27
+ vals = vals.reindex(ndf.index).ffill()
28
+
29
+ mask = vals[0].values > Q
30
+ vals.loc[mask, 0] = Q[mask]
31
+
32
+ return vals[0].values
@@ -0,0 +1,24 @@
1
+ """Tools for hydrology.
2
+
3
+ hydrotoolbox baseflow_sep --area 59.1 --area_units 'mile**2' linear < daily.csv
4
+ hydrotoolbox baseflow_sep sliding < daily.csv
5
+ hydrotoolbox baseflow_sep eckardt,sliding < daily.csv
6
+ ...
7
+
8
+ hydrotoolbox recession """
9
+
10
+
11
+ def ihacres(
12
+ Q,
13
+ k: float,
14
+ C: float,
15
+ a: float,
16
+ ):
17
+ Qb = Q.copy()
18
+ first_c = k / (1 + C)
19
+ second_c = C / (1 + C)
20
+ for row in range(1, len(Q)):
21
+ Qb[row] = first_c * Qb[row - 1] + second_c * (Q[row] + a * Q[row - 1])
22
+ mask = Qb > Q
23
+ Qb[mask] = Q[mask]
24
+ return Qb
@@ -0,0 +1,17 @@
1
+ """Tools for hydrology.
2
+
3
+ hydrotoolbox baseflow_sep --area 59.1 --area_units 'mile**2' linear < daily.csv
4
+ hydrotoolbox baseflow_sep sliding < daily.csv
5
+ hydrotoolbox baseflow_sep eckardt,sliding < daily.csv
6
+ ...
7
+
8
+ hydrotoolbox recession """
9
+
10
+ from ..comparison import strict_baseflow
11
+
12
+
13
+ def strict(Q):
14
+ Qb = strict_baseflow(Q)
15
+ mask = Qb > Q
16
+ Qb[mask] = Q[mask]
17
+ return Qb
@@ -0,0 +1,95 @@
1
+ import numpy as np
2
+
3
+ from .utils import NSE, moving_average, multi_arange
4
+
5
+
6
+ def recession_coefficient(Q, strict, date=None, ice_period=None):
7
+ if ice_period is None:
8
+ idx_ice = np.full(Q.shape, False)
9
+ else:
10
+ beg, end = ice_period
11
+ if (end[0] > beg[0]) or ((end[0] == beg[0]) & (end[1] > beg[1])):
12
+ idx_ice = (
13
+ (date.M > beg[0]) & (date.M < end[0])
14
+ | ((date.M == beg[0]) & (date.D >= beg[1]))
15
+ | ((date.M == end[0]) & (date.D <= end[1]))
16
+ )
17
+ else:
18
+ idx_ice = (
19
+ (date.M > beg[0])
20
+ | (date.M < end[0])
21
+ | ((date.M == beg[0]) & (date.D >= beg[1]))
22
+ | ((date.M == end[0]) & (date.D <= end[1]))
23
+ )
24
+ dry = strict[~idx_ice]
25
+
26
+ cQ = Q[1:-1][dry[1:-1]]
27
+ dQ = ((Q[2:] - Q[:-2]) / 2)[dry[1:-1]]
28
+
29
+ idx = np.argsort(-dQ / cQ)[np.floor(dQ.shape[0] * 0.05).astype(int)]
30
+ K = -cQ[idx] / dQ[idx]
31
+ return np.exp(-1 / K)
32
+
33
+
34
+ def param_calibrate(param_range, method, Q, b_LH):
35
+ idx_rec = recession_period(Q)
36
+ idx_oth = np.full(Q.shape[0], True)
37
+ idx_oth[idx_rec] = False
38
+ return param_calibrate_jit(param_range, method, Q, b_LH, idx_rec, idx_oth)
39
+
40
+
41
+ def param_calibrate_jit(param_range, method, Q, b_LH, idx_rec, idx_oth):
42
+ log_q = np.log(Q + 1)
43
+ loss = np.zeros(param_range.shape)
44
+ for i in range(param_range.shape[0]):
45
+ p = param_range[i]
46
+ b_exceed = method(Q, b_LH, p, return_exceed=True)
47
+ f_exd, logb = b_exceed[-1] / Q.shape[0], np.log(b_exceed[:-1] + 1)
48
+ NSE_rec = NSE(log_q[idx_rec], logb[idx_rec])
49
+ NSE_oth = NSE(log_q[idx_oth], logb[idx_oth])
50
+ loss[i] = 1 - (1 - (1 - NSE_rec) / (1 - NSE_oth)) * (1 - f_exd)
51
+ return param_range[np.argmin(loss)]
52
+
53
+
54
+ def recession_period(Q):
55
+ idx_dec = np.zeros(Q.shape[0] - 1, dtype=np.int64)
56
+ q_ave = moving_average(Q, 3)
57
+ idx_dec[1:-1] = (q_ave[:-1] - q_ave[1:]) > 0
58
+ idx_beg = np.where(idx_dec[:-1] - idx_dec[1:] == -1)[0] + 1
59
+ idx_end = np.where(idx_dec[:-1] - idx_dec[1:] == 1)[0] + 1
60
+ idx_keep = (idx_end - idx_beg) >= 10
61
+ idx_beg = idx_beg[idx_keep]
62
+ idx_end = idx_end[idx_keep]
63
+ duration = idx_end - idx_beg
64
+ idx_beg = idx_beg + np.ceil(duration * 0.6).astype(np.int64)
65
+ return multi_arange(idx_beg, idx_end)
66
+
67
+
68
+ def maxmium_BFI(Q, b_LH, a, date=None):
69
+ b = Backward(Q, b_LH, a)
70
+
71
+ if date is None:
72
+ idx_end = b.shape[0] // 365 * 365
73
+ annual_b = np.mean(b[:idx_end].reshape(-1, 365), axis=1)
74
+ annual_q = np.mean(Q[:idx_end].reshape(-1, 365), axis=1)
75
+ else:
76
+ idx_year = date.Y - date.Y.min()
77
+ counts = np.bincount(idx_year)
78
+ idx_valid = counts > 0
79
+ annual_b = np.bincount(idx_year, weights=b)[idx_valid] / counts[idx_valid]
80
+ annual_q = np.bincount(idx_year, weights=Q)[idx_valid] / counts[idx_valid]
81
+ annual_bfi = annual_b / annual_q
82
+ bfi_max = np.max(annual_bfi)
83
+ bfi_max = bfi_max if bfi_max < 0.9 else np.sum(annual_b) / np.sum(annual_q)
84
+ return bfi_max
85
+
86
+
87
+ def Backward(Q, b_LH, a):
88
+ b = np.zeros(Q.shape[0])
89
+ b[-1] = b_LH[-1]
90
+ for i in range(Q.shape[0] - 1, 0, -1):
91
+ b[i - 1] = b[i] / a
92
+ if b[i] == 0:
93
+ b[i - 1] = Q[i - 1]
94
+ b[i - 1] = min(b[i - 1], Q[i - 1])
95
+ return b
@@ -0,0 +1,98 @@
1
+ import numpy as np
2
+
3
+ from .comparison import KGE, strict_baseflow
4
+ from .methods import *
5
+ from .param_estimate import param_calibrate, recession_coefficient
6
+
7
+
8
+ def separation(
9
+ Q, date=None, area=None, ice_period=None, method="all", k=None, C=None, a=None
10
+ ):
11
+ if method == "all":
12
+ method = [
13
+ "ukih",
14
+ "local",
15
+ "fixed",
16
+ "slide",
17
+ "lh",
18
+ "chapman",
19
+ "cm",
20
+ "boughton",
21
+ "furey",
22
+ "eckhardt",
23
+ "ewma",
24
+ "willems",
25
+ "ihacres",
26
+ "strict",
27
+ "five_day",
28
+ ]
29
+ elif isinstance(method, str):
30
+ method = [method]
31
+
32
+ strict_bf = strict_baseflow(Q)
33
+ if any(
34
+ m in ["chapman", "cm", "boughton", "furey", "eckhardt", "willems"]
35
+ for m in method
36
+ ):
37
+ a = recession_coefficient(Q, strict_bf, date, ice_period)
38
+
39
+ b_lh = LH(Q)
40
+ b = np.recarray(Q.shape[0], dtype=list(zip(method, [float] * len(method))))
41
+ for m in method:
42
+ if m == "ukih":
43
+ b[m] = UKIH(Q, b_lh)
44
+
45
+ if m == "local":
46
+ b[m] = Local(Q, b_lh, area)
47
+
48
+ if m == "fixed":
49
+ b[m] = Fixed(Q, area)
50
+
51
+ if m == "slide":
52
+ b[m] = Slide(Q, area)
53
+
54
+ if m == "lh":
55
+ b[m] = b_lh
56
+
57
+ if m == "chapman":
58
+ b[m] = Chapman(Q, b_lh, a)
59
+
60
+ if m == "cm":
61
+ b[m] = CM(Q, b_lh, a)
62
+
63
+ if m == "boughton":
64
+ C = param_calibrate(np.arange(0.0001, 1, 0.0001), f_Boughton(a), Q, b_lh)
65
+ b[m] = Boughton(Q, b_lh, a, C)
66
+
67
+ if m == "furey":
68
+ A = param_calibrate(np.arange(0.001, 10, 0.001), f_Furey(a), Q, b_lh)
69
+ b[m] = Furey(Q, b_lh, a, A)
70
+
71
+ if m == "eckhardt":
72
+ bfi_max = param_calibrate(
73
+ np.arange(0.0001, 1, 0.0001), f_Eckhardt(a), Q, b_lh
74
+ )
75
+ b[m] = Eckhardt(Q, b_lh, a, bfi_max)
76
+
77
+ if m == "ewma":
78
+ e = param_calibrate(np.arange(0.0001, 0.5, 0.0001), EWMA, Q, b_lh)
79
+ b[m] = EWMA(Q, b_lh, e)
80
+
81
+ if m == "willems":
82
+ w = param_calibrate(np.arange(0.0001, 1, 0.0001), f_Willems(a), Q, b_lh)
83
+ b[m] = Willems(Q, b_lh, a, w)
84
+
85
+ if m == "ihacres":
86
+ b[m] = ihacres(Q, k=k, C=C, a=a)
87
+
88
+ if m == "strict":
89
+ b[m] = strict(Q)
90
+
91
+ if m == "five_day":
92
+ b[m] = five_day(Q)
93
+
94
+ KGEs = KGE(
95
+ b[strict_bf].view(np.float64).reshape(-1, len(method)),
96
+ np.repeat(Q[strict_bf], len(method)).reshape(-1, len(method)),
97
+ )
98
+ return b, KGEs