File size: 7,190 Bytes
98a2104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import numpy as np
import pandas as pd
from pvlib.solarposition import sun_rise_set_transit_spa
from agroclimatic_indicators import (
    agro_indicators,
    animal_indicators,
    climatic_indicators,
)


## Compute Agronomics
def compute_vpd(df: pd.DataFrame):
    """
    Compute VPD.

    Parameters
    ----------
    df : DataFrame
        The input dataframe containing sensor data.

    Returns
    -------
    arraylike
        VPD at df's timestep
    """
    return agro_indicators.vpd(df.air_temperature, df.relative_humidity)



def compute_et0(
    df: pd.DataFrame,
    latitude: float,
    longitude: float
):
    """
    Compute reference evapotranspiration.

    Parameters
    ----------
    df : DataFrame
        The input dataframe containing sensor data.

    latitude : float
        Latitude of the location.
    longitude : float
        Longitude of the location

    Returns
    -------
    arraylike
        Daily reference evapotranspiration.
    """

    irradiance = (
        (df.photon_flux_density.resample("1h").mean() / 2.1).resample("1d").sum()
    )
    T = df.air_temperature.resample("1d").mean()
    Tmin = df.air_temperature.resample("1d").min()
    Tmax = df.air_temperature.resample("1d").max()
    RHmin = df.relative_humidity.resample("1d").min()
    RHmax = df.relative_humidity.resample("1d").max()
    WS = df.wind_speed.resample("1d").mean()
    JJulien = np.unique(df.index.day_of_year)

    l = [
        agro_indicators.et0(
            irradiance.iloc[i],
            T.iloc[i],
            Tmax.iloc[i],
            Tmin.iloc[i],
            RHmin.iloc[i],
            RHmax.iloc[i],
            WS.iloc[i],
            JJulien[i],
            latitude,
            np.array([longitude]),
        )
        for i in range(len(JJulien))
    ]

    if len(JJulien) == 1:
        et0 = l[0]
    else:
        et0 = l

    return et0


## Compute Climatics


def compute_frostday(df: pd.DataFrame):
    """
    Define if day is a frost day (min temperature below 0°C).

    Parameters
    ----------
    df : DataFrame
        Air sensors data.

    Returns
    -------
    bool
        True if day is a frost day, else False.
    """

    T = df.air_temperature.resample("1d").min()

    ind = climatic_indicators.frost_bool(T, 0)
    if ind.shape[0] == 1:
        ind = ind.iloc[0]
    return ind


def compute_strongfrostday(df: pd.DataFrame):
    """
    Define if day is a strong frost day (min temperature below -3°C).

    Parameters
    ----------
    df : DataFrame
        The input dataframe containing temperature data.

    Returns
    -------
    bool
        True if day is a strong frost day, else False.
    """

    T = df.air_temperature.resample("1d").min()

    ind = climatic_indicators.frost_bool(T, -3)
    if ind.shape[0] == 1:
        ind = ind.iloc[0]

    return ind


def compute_thermalstressday(df: pd.DataFrame, stress_threshold: float = 35):
    """
    Define if daily temperature is a source of thermal stress (max temperature above stress threshold).

    Parameters
    ----------
    df : DataFrame
        The input dataframe containing air temperature data.

    stress_threshold : float
        Threshold temperature of stress (degrees Celsius).

    Returns
    -------
    bool
        True if day is a day with thermal stress, else False.
    """

    T = df.air_temperature.resample("1d").max()

    ind = climatic_indicators.thermalstress_bool(T, stress_threshold)
    if ind.shape[0] == 1:
        ind = ind.iloc[0]

    return ind


def compute_summerday(df: pd.DataFrame):
    """
    Define if day is a summer day (max temperature above 25°C).

    Parameters
    ----------
    df : DataFrame
        The input dataframe containing air temperature data.

    Returns
    -------
    bool
        True if day is a summer day, else False.
    """
    T = df.air_temperature.resample("1d").max()

    ind = climatic_indicators.summerday_bool(T)
    if ind.shape[0] == 1:
        ind = ind.iloc[0]

    return ind


def compute_scorchday(df: pd.DataFrame, scorch_threshold: float = 25):
    """
    Define if day is a scorching day (jour échaudant) (max temperature above scorch threshold).

    Parameters
    ----------
    df : DataFrame
        The input dataframe containing air temperature data.

    scorch_threshold : float
        Temperature threshold above which the day is considered scorching (degrees Celsius).

    Returns
    -------
    bool
        True if day is a scorching day, else False.
    """
    T = df.air_temperature.resample("1d").max()

    ind = climatic_indicators.scorch_bool(T, scorch_threshold)

    if ind.shape[0] == 1:
        ind = ind.iloc[0]

    return ind


def compute_tropicalnight(df: pd.DataFrame, latitude: float, longitude: float):
    """
    Define if night is a tropical night (min temperature above 20°C).

    Parameters
    ----------
    df : DataFrame
        The input dataframe containing air temperature data.

    latitude : float
        Latitude of the location.

    longitude : float
        Longitude of the location.

    Returns
    -------
    bool
        True if night is a tropical night, else False.
    """
    if len(df) == 0:
        return None

    df = sun_rise_set_transit_spa(
        times=df.index, latitude=latitude, longitude=longitude
    ).merge(df["air_temperature"], left_index=True, right_index=True)
    df = df.loc[(df.index < df["sunrise"]) | (df.index > df["sunset"])]

    df_minnight = df.resample("24h", offset="12h")["air_temperature"].min()

    tropicalnight = climatic_indicators.tropicalnight_bool(df_minnight)

    if tropicalnight.shape[0] == 1:
        tropicalnight = tropicalnight.iloc[0]

    return tropicalnight


def compute_mintempnight(df: pd.DataFrame, latitude: float, longitude: float):
    """
    Return minimal night temperature.

    Parameters
    ----------
    df : DataFrame
        The input dataframe containing air temperature data.

    latitude : float
        Latitude of the location.

    longitude : float
        Longitude of the location.

    Returns
    -------
    float
        Min night temperature (degrees Celsius).
    """
    if len(df) == 0:
        return None

    df = sun_rise_set_transit_spa(df.index, latitude, longitude).merge(
        df["air_temperature"], left_index=True, right_index=True
    )
    df = df.loc[(df.index < df["sunrise"]) | (df.index > df["sunset"])]

    df_minnight = df.resample("24h", offset="12h")["air_temperature"].min()

    if df_minnight.shape[0] == 1:
        df_minnight = df_minnight.iloc[0]

    return df_minnight


def compute_hli(
    df: pd.DataFrame,
):
    """
    Computes HLI (heat load index).

    Parameters
    ----------
    df : DataFrame
        The input dataframe containing air temperature data.

    Returns
    -------
    float
        HLI value.
    """

    ind = animal_indicators.hli(
        irradiance=df.photon_flux_density,
        air_temperature=df.air_temperature,
        RH=df.relative_humidity,
        wind_speed=df.wind_speed,
    )

    if ind.size == 1:
        df_ind = float(ind)
    else:
        df_ind = pd.Series(ind, index=df.index)

    return df_ind