-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
314 lines (264 loc) · 13.7 KB
/
main.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
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
import numpy as np
from agrotool_lib.RadiationBalance import calculateBalance
from core import MatplotlibVisualizing
from core import PlotlyVisualizing
from agrotool_classes.TAgroEcoSystem import TWeatherRecord
from agrotool_classes.TRunController import TRunController
from agrotool_lib.EvapBase import Evapotranspiration, SoilTemperature
from agrotool_lib import Precipitation
from agrotool_lib.RadiationAstronomy import GetCurrSumRad, _DayLength
from agrotool_lib.WaterSoilDynamics import WaterSoilDynamics
from agrotool_lib.Development import RecalculateBioTime
from agrotool_lib.NitBal import RecalculateSoilNitrogen
from agrotool_lib.Growing import Growth
from agrotool_lib.Snowmelt import popov_melting
from datetime import datetime, timedelta
import pandas as pd
# const
MAX_COUNT = 4 # // Максимальное количество "битых" погодных записей
def pretty_print(text):
divisor = "-------------------------"
print("%s%s%s" % (divisor, text, divisor))
def OneDayStep(hRunningController: TRunController,
cWR: TWeatherRecord,
nextWR: TWeatherRecord,
stepTimeDelta: timedelta,
historyDict):
print("____________\n____________\n DAY = %s\n____________\n____________\n" % cWR.date.__str__())
Tave = cWR.Tave
sumSnow = hRunningController.agroEcoSystem.airPart.SumSnow
timeForDailyOperation = timedelta(hours=12)
# hours calulation
noonTime = datetime(year=cWR.date.year, # for calculation from sunrise to sunrise of next day
month=cWR.date.month,
day=cWR.date.day,
hour=14)
fi = hRunningController.region.Latitude
currDayLength = timedelta(hours=_DayLength(fi, cWR.date))
nextDayLength = timedelta(hours=_DayLength(fi, nextWR.date))
currSunriseDate = noonTime - currDayLength / 2
nextSunriseDate = noonTime - nextDayLength / 2 + timedelta(days=1)
# temperature calculation
T_history = np.array(
[*np.linspace(cWR.Tmin, cWR.Tmax, num=int((noonTime - currSunriseDate) / stepTimeDelta))[0:-1],
*np.linspace(cWR.Tmax, nextWR.Tmin, num=int((nextSunriseDate - noonTime) / stepTimeDelta) + 2)]
)
timeHistory = [currSunriseDate + stepTimeDelta * i for i in range(0, len(T_history))]
prec_history = Precipitation.get_precipitation_history(cWR.Prec, T_history)
##############################################################################
# ------------------------------ day step (Delta loop) -----------------------
##############################################################################
for cDateTime, T_curr, Prec_curr in zip(timeHistory, T_history, prec_history):
##########################################################################
# ------------------------- Ежедневные операции --------------------------
##########################################################################
pretty_print('Step2')
if cDateTime.second / 60 == timeForDailyOperation.seconds / 60:
# Утренние технологические операции
hRunningController.technologyDescriptor.Irrigation_Regime.stepoAct(hRunningController.agroEcoSystem)
hRunningController.technologyDescriptor.Fertilization_Regime.stepoAct(hRunningController.agroEcoSystem)
hRunningController.technologyDescriptor.Soil_Tillage_Regime.stepoAct(hRunningController.agroEcoSystem)
else:
print("Not now")
##########################################################################
# ------------------------- Расчет баланса снега -------------------------
##########################################################################
# Семантические операции
pretty_print('Step3')
if (T_curr < 0): # Если текущая температура < 0 - количество снега увеличивается
sumSnow = sumSnow + Prec_curr
hRunningController.agroEcoSystem.airPart.alpha_snow = 0
else: # Иначе считаем таяние снега и прибавляем осадки
delSnowPrec = popov_melting(hRunningController.agroEcoSystem, cDateTime, stepTimeDelta,
T_curr) # Проверить формулу Попова
print(cDateTime)
Prec_curr += delSnowPrec
sumSnow = sumSnow - delSnowPrec
##########################################################################
# ---- Радиация и фотосинтез(с потенциальным сопротивлением устьиц) ------
##########################################################################
pretty_print('Step6')
# Запоминаем приходящщу радиацию
# Запоминаем почвенну радиацию
# RadPhotosynthesis(hRunningController.agroEcoSystem, False)#TODO
hRunningController.agroEcoSystem.airPart.SumRad = GetCurrSumRad(fi, cDateTime, stepTimeDelta)
# ---------------------------- get Kex from df -------------------------------------
try:
curKex = float(
hRunningController.weatherDf
[hRunningController.weatherDf['Date'] == cDateTime.strftime("%d/%m/%Y")]
['Kex']
)
except Exception:
curKex = float(
hRunningController.weatherDf
[hRunningController.weatherDf['Date'] == (cDateTime - timedelta(days=1)).strftime("%d/%m/%Y")]
['Kex']
)
# -----------------------------------------------------------------------------------
calculateBalance(
airPart=hRunningController.agroEcoSystem.airPart,
Rs=hRunningController.agroEcoSystem.airPart.SumRad,
Kex=curKex,
LAI=hRunningController.agroEcoSystem.cropPart.Individual_Plant.Shoot.Leaf.LAI,
T_curr=T_curr,
delta_step=stepTimeDelta,
phTime=hRunningController.agroEcoSystem.cropPart.Individual_Plant.Ph_Time
)
##########################################################################
# --------------------- Водные потоки.Транспирация -----------------------
##########################################################################
pretty_print('Step7')
Evapotranspiration(hRunningController.agroEcoSystem)
pretty_print('Step8')
SoilTemperature(
cSystem=hRunningController.agroEcoSystem,
T_curr=T_curr
)
##########################################################################
# ---------------------Расчет сумм осадков и транспирации ---------------- # TODO next step
##########################################################################
pretty_print('Step9')
print("Some simple calculation without functions calls")
hRunningController.agroEcoSystem.airPart.sumTrans = hRunningController.agroEcoSystem.airPart.sumTrans \
+ hRunningController.agroEcoSystem.cropPart.Eplant \
+ hRunningController.agroEcoSystem.cropPart.Esoil
hRunningController.agroEcoSystem.airPart.sumPrec = hRunningController.agroEcoSystem.airPart.sumPrec \
+ cWR.Prec \
+ cWR.Watering
pretty_print('Step10')
# Водные потоки в почве
if (Tave >= 0):
WaterSoilDynamics(hRunningController.agroEcoSystem)
else:
print("RecalculateSoilNitrogen was't called (Tave = %d)" % (Tave))
# Радиация и фотосинтез(с реальным сопротивлением устьиц)
pretty_print('Step11')
# RadPhotosynthesis(hRunningController.agroEcoSystem, True)
# Развитие
pretty_print('Step12')
RecalculateBioTime(hRunningController.agroEcoSystem)
pretty_print('Step13')
# Почвенно - азотный блок
if (Tave >= 0) and (sumSnow < 1):
RecalculateSoilNitrogen(hRunningController.agroEcoSystem)
else:
print("RecalculateSoilNitrogen was't called(Tave = %d, sumSnow = %d)" % (Tave, sumSnow))
pretty_print('Step14')
# Рост.Распределение ассимилятов
if hRunningController.agroEcoSystem.cropPart.Individual_Plant.Ifase > 1:
Growth(hRunningController.agroEcoSystem)
else:
print("Growth was't called(Ifase = %d)" % (
hRunningController.agroEcoSystem.cropPart.Individual_Plant.Ifase))
pretty_print('Step15')
# Освежение динамических переменных
hRunningController.agroEcoSystem.refreshing()
hRunningController.agroEcoSystem.airPart.SumSnow = sumSnow
pretty_print('Step17')
pretty_print('Step18')
# Его присвоение
hRunningController.agroEcoSystem.airPart.currentEnv = cWR
bTime = hRunningController.agroEcoSystem.cropPart.Individual_Plant.Ph_Time
##########################################################################
# ------------------------------ weather history -------------------------
##########################################################################
historyDict['weather'] = historyDict['weather'].append(
pd.DataFrame(
{
"Date": [cDateTime],
"T": [T_curr],
"Rad": [
hRunningController.agroEcoSystem.airPart.SumRad * 10_000 / stepTimeDelta.seconds],
"Prec": [Prec_curr],
"SumSnow": [sumSnow],
"Rnl": [hRunningController.agroEcoSystem.airPart.Rnl],
"Rn": [hRunningController.agroEcoSystem.airPart.Rn],
"G": [hRunningController.agroEcoSystem.airPart.G]
}
)
)
##########################################################################
# ------------------------------ soil history ----------------------------
##########################################################################
t = []
T = np.zeros(len(hRunningController.agroEcoSystem.soilPart.soilLayers))
layers = list(range(len(T)))
for i, layer in enumerate(hRunningController.agroEcoSystem.soilPart.soilLayers):
t.append(cDateTime)
T[i] = layer.T
historyDict['soil'] = historyDict['soil'].append(
pd.DataFrame(
{
't': t,
'layer': layers,
'layer_sym': list(map(lambda layer: "layer_%d" % layer, layers)),
'T': T
}
)
)
# Временной шаг
cDateTime += stepTimeDelta
def ContinousRunning(hRunningController: TRunController):
# Организация цикла по суточным шагам
historyDict = {
'weather': pd.DataFrame(
{
"Date": np.empty(0),
"T": np.empty(0),
"Rad": np.empty(0),
"Prec": np.empty(0),
"SumSnow": np.empty(0),
"Rnl": np.empty(0),
"Rn": np.empty(0),
"G": np.empty(0)
}
),
'soil': pd.DataFrame(
{
't': np.empty(0),
'layer': np.empty(0),
'layer_sym': np.empty(0),
'T': np.empty(0)
}
)
}
weatherIter = iter(hRunningController.weatherDf.to_dict(orient='records'))
weatherIter.__next__()
for cWR in hRunningController.weatherDf.to_dict(orient='records'):
cWR = TWeatherRecord(cWR)
try: # nextWR necessary in OneDayStep
nextWR = TWeatherRecord(weatherIter.__next__())
except StopIteration: # for last day
nextWR = cWR.__copy__()
nextWR.date += timedelta(days=1)
OneDayStep(
hRunningController,
cWR=cWR, nextWR=nextWR,
stepTimeDelta=timedelta(hours=1 / 2),
historyDict=historyDict
)
historyDict['weather'].set_index("Date", inplace=True)
print("plotting simple histories")
MatplotlibVisualizing.show_from_df(df=historyDict['weather'])
PlotlyVisualizing.show_from_df(df=historyDict['weather'])
print("plotting temperature history in soil (it will be longer than previous plotting)")
# show soil_layers.T for 10 days on dynamic graphic
show_day_num = int(len(historyDict['soil']) * 10 / len(hRunningController.weatherDf))
historyDict['soil']['t'] = pd.to_numeric(historyDict['soil']['t'])
PlotlyVisualizing.show_soil_from_df(
df=historyDict['soil']
.head(show_day_num)
.sort_values(by=['layer', 't'])
)
def main():
hRunningController = TRunController(
weatherPath="environments/test_1/weather1.json",
modelPath="environments/test_1/model_params.json",
placePath="environments/test_1/place.json"
)
hRunningController.update_params('environments/test_1/query_solidgrids.json')
hRunningController.init_start(jsonPath='environments/test_1/initial_state.json')
ContinousRunning(hRunningController=hRunningController)
if __name__ == '__main__':
main()