/
indices.py
149 lines (128 loc) · 3.83 KB
/
indices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from typing import Optional, Union
import pandas as pd
from pydantic import PositiveInt, validate_arguments
from standard_precip.spi import SPI
from toolbox_utils import tsutils
def _nlarge_nsmall(
pe_data: pd.DataFrame,
nlargest: Optional[PositiveInt],
nsmallest: Optional[PositiveInt],
groupby: str,
):
if nlargest is None and nsmallest is None:
return pe_data
nlarge = pd.Series()
nsmall = pd.Series()
if nlargest is not None:
nlarge = pe_data.resample(groupby).apply(
lambda x: x.nlargest(int(nlargest), x.columns[0])
)
nlarge = nlarge.droplevel(0)
nlarge.sort_index(inplace=True)
nlarge = nlarge.reindex(
pd.date_range(start=nlarge.index[0], end=nlarge.index[-1], freq="D")
)
if nsmallest is not None:
nsmall = pe_data.resample(groupby).apply(
lambda x: x.nsmallest(int(nsmallest), x.columns[0])
)
nsmall = nsmall.droplevel(0)
nsmall.sort_index(inplace=True)
nsmall = nsmall.reindex(
pd.date_range(start=nsmall.index[0], end=nsmall.index[-1], freq="D")
)
if nsmallest is not None and nlargest is None:
return nsmall
if nsmallest is None and nlargest is not None:
return nlarge
return pd.concat([nsmall, nlarge], axis="columns")
@tsutils.transform_args(source_units=tsutils.make_list)
@validate_arguments(config={"arbitrary_types_allowed": True})
def spei(
rainfall: Union[PositiveInt, str, pd.DataFrame],
pet: Union[PositiveInt, str, pd.DataFrame],
source_units,
nsmallest=None,
nlargest=None,
groupby="M",
fit_type="lmom",
dist_type="gam",
scale=1,
start_date=None,
end_date=None,
dropna="no",
clean=False,
round_index=None,
skiprows=None,
index_type="datetime",
):
from tstoolbox.tstoolbox import read
tsd = read(
rainfall,
pet,
names=["rainfall", "pet"],
source_units=source_units,
target_units=["mm", "mm"],
start_date=start_date,
end_date=end_date,
dropna=dropna,
clean=clean,
round_index=round_index,
skiprows=skiprows,
index_type=index_type,
)
tsd["pe"] = tsd["rainfall:mm"] - tsd["pet:mm"]
tsd["date"] = tsd.index
spi = SPI()
# def calculate(self, df: pd.DataFrame, date_col: str, precip_cols: list, freq: str="M",
# scale: int=1, freq_col: str=None, fit_type: str='lmom', dist_type: str='gam',
# **dist_kwargs) -> pd.DataFrame:
tsd = tsutils.asbestfreq(tsd)
ndf = spi.calculate(
tsd,
"date",
"pe",
freq=tsd.index.freqstr,
scale=scale,
fit_type=fit_type,
dist_type=dist_type,
)
return _nlarge_nsmall(ndf, nlargest, nsmallest, groupby)
@tsutils.transform_args(source_units=tsutils.make_list)
@validate_arguments(config={"arbitrary_types_allowed": True})
def pe(
rainfall: Union[PositiveInt, str, pd.DataFrame],
pet: Union[PositiveInt, str, pd.DataFrame],
source_units,
nsmallest=None,
nlargest=None,
groupby="M",
window=30,
min_periods=None,
center=None,
win_type=None,
closed=None,
target_units="mm",
):
from tstoolbox.tstoolbox import read
tsd = read(
rainfall,
pet,
names=["rainfall", "pet"],
source_units=source_units,
target_units=["mm", "mm"],
)
tsd["pe:mm"] = tsd["rainfall:mm"] - tsd["pet:mm"]
pe_data = tsutils._normalize_units(tsd["pe:mm"], "mm", target_units)
pe_data = (
pe_data.astype(float)
.rolling(
window,
min_periods=min_periods,
center=center,
win_type=win_type,
closed=closed,
)
.sum()
)
return _nlarge_nsmall(pe_data, nlargest, nsmallest, groupby)