Failed to fetch fork details. Try again later.
-
unknown authored9c6d0581
Forked from
HYCAR-Hydro / airGR
Source project has a limited visibility.
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
import numpy as np
from experiment.meteo_france_SCM_models.study.abstract_variable import AbstractVariable
class SafranSnowfallVariable(AbstractVariable):
""""
Safran data is hourly
Hypothesis:
-How to count how much snowfall in one hour ?
I take the average between the rhythm of snowfall per second between the start and the end
and multiply that by 60 x 60 which corresponds to the number of seconds in one hour
-How do how I define the limit of a day ?
From the start, i.e. in August at 4am something like that,then if I add a 24H duration, I arrive to the next day
-How do you aggregate several days ?
We aggregate all the N consecutive days into a value x_i, then we take the max
(but here the problem might be that the x_i are not idnependent, they are highly dependent one from another)
"""
NAME = 'Snowfall'
UNIT = 'kg per m2 or mm'
@classmethod
def keyword(cls):
return 'Snowf'
def __init__(self, variable_array, nb_consecutive_days_of_snowfall=1):
super().__init__(variable_array)
self.nb_consecutive_days_of_snowfall = nb_consecutive_days_of_snowfall
# Compute the daily snowfall in kg/m2
snowfall_rates = variable_array
# Compute the mean snowrate, then multiply it by 60 * 60 * 24
# day_duration_in_seconds = 24 * 60 * 60
# nb_days = len(snowfall_rates) // 24
# print(nb_days)
# daily_snowrate = [np.mean(snowfall_rates[24 * i:24 * (i + 1) + 1], axis=0) for i in range(nb_days)]
# self.daily_snowfall = day_duration_in_seconds * np.array(daily_snowrate)
# Compute the hourly snowfall first, then aggregate
mean_snowfall_rates = 0.5 * (snowfall_rates[:-1] + snowfall_rates[1:])
hourly_snowfall = 60 * 60 * mean_snowfall_rates
# Transform the snowfall amount into a dataframe
nb_days = len(hourly_snowfall) // 24
self.daily_snowfall = [sum(hourly_snowfall[24 * i:24 * (i + 1)]) for i in range(nb_days)]
@property
def daily_time_serie_array(self) -> np.ndarray:
# Aggregate the daily snowfall by the number of consecutive days
shifted_list = [self.daily_snowfall[i:] for i in range(self.nb_consecutive_days_of_snowfall)]
# First element of shifted_list is of length n, Second element of length n-1, Third element n-2....
# The zip is done with respect to the shortest list
snowfall_in_consecutive_days = np.array([sum(e) for e in zip(*shifted_list)])
# The returned array is of size n-nb_days+1 x nb_massif
# so that the length of the vector match a year, we can add zeros (since it corresponds to the July month,
# we are sure that there is no snowfall at this period) However such trick does not work for other variable such as Temperature
return snowfall_in_consecutive_days
class SafranRainfallVariable(SafranSnowfallVariable):
NAME = 'Rainfall'
UNIT = 'kg per m2 or mm'
@classmethod
def keyword(cls):
return 'Rainf'
class SafranTotalPrecipVariable(AbstractVariable):
def __init__(self, snow_variable_array, rain_variable_array, nb_consecutive_days_of_snowfall=1):
super().__init__(None)
self.snow_precipitation = SafranSnowfallVariable(snow_variable_array, nb_consecutive_days_of_snowfall)
self.rain_precipitation = SafranRainfallVariable(rain_variable_array, nb_consecutive_days_of_snowfall)
@classmethod
def keyword(cls):
return [SafranSnowfallVariable.keyword(), SafranRainfallVariable.keyword()]
@property
def daily_time_serie_array(self) -> np.ndarray:
return self.snow_precipitation.daily_time_serie_array + self.rain_precipitation.daily_time_serie_array
class SafranTemperatureVariable(AbstractVariable):
NAME = 'Temperature'
UNIT = 'Celsius Degrees'
@classmethod
def keyword(cls):
return 'Tair'
def __init__(self, variable_array):
super().__init__(variable_array)
# Temperature are in K, I transform them as celsius
self.hourly_temperature = self.variable_array - 273.15
nb_days = len(self.hourly_temperature) // 24
# Compute the mean temperature
self.daily_temperature = [np.mean(self.hourly_temperature[24 * i:24 * (i + 1)], axis=0) for i in range(nb_days)]
@property
def daily_time_serie_array(self):
return np.array(self.daily_temperature)