-
Notifications
You must be signed in to change notification settings - Fork 0
/
phenology.r
361 lines (309 loc) · 10.9 KB
/
phenology.r
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
phenology = function(meteo, f.variety, sowing, lat)
{
# PROVA
# definition of parameters
Parameters = list()
Parameters$TSUMEM = -99 # Temp. sum for emergence
Parameters$TBASEM = -99 # Base temp. for emergence
Parameters$TEFFMX = -99 # Max eff temperature for emergence
Parameters$TSUM1 = -99 # Temperature sum emergence to anthesis
Parameters$TSUM2 = -99 # Temperature sum anthesis to maturity
Parameters$IDSL = -99 # Switch for photoperiod (1) and vernalisation (2)
Parameters$DLO = -99 # Optimal day length for phenol. development
Parameters$DLC = -99 # Critical day length for phenol. development
Parameters$DVSI = -99 # Initial development stage
Parameters$DVSEND = -99 # Final development stage
Parameters$VERNSAT = -99
Parameters$VERNBASE = -99
Parameters$VERNDVS = -99
#Parameters$DTSMTB = AfgenTrait() # Temperature response function for phenol.
Parameters$crop_start_type = NA
Parameters$crop_end_type=NA
# definition of rate variables
RateVariables = list()
RateVariables$DTSUME = -99 # increase in temperature sum for emergence
RateVariables$DTSUM = -99 # increase in temperature sum
RateVariables$DVR = -99 # development rate
RateVariables$VERNR = -99
RateVariables$VERNFAC = -99
RateVariables$DVRED = -99
# definition of state variables
StateVariables = list()
StateVariables$DVS = -99 # Development stage
StateVariables$TSUM = -99 # Temperature sum state
StateVariables$TSUME = -99 # Temperature sum for emergence state
# States which register phenological events
StateVariables$DOS = -99 # Day of sowing
StateVariables$DOE = -99 # Day of emergence
StateVariables$DOA = -99 # Day of anthesis
StateVariables$DOM = -99 # Day of maturity
StateVariables$DOH = -99 # Day of harvest
StateVariables$VERN = -99
StateVariables$DOV = NA
StateVariables$ISVERNALISED = FALSE
StateVariables$STAGE = NA
#########
self = list(params=Parameters, rates=RateVariables, states=StateVariables, force_vernalisation=FALSE)
# initialize DVS
meteo$DVS = NA
for(i in 1:length(sowing))
{
which_ = which(meteo$DAY==sowing[i])
meteo_ = meteo[which_:ifelse((which_+365) <= dim(meteo)[1],which_+365,dim(meteo)[1]),]
self = initialize(self, sowing[i], f.variety, start.type="sowing", end.type="maturity", vernalisation=TRUE)
days = 1
drv = c()
drv$LAT = lat
while(self$states$DVS <= 2 && days < dim(meteo_)[1])
{
drv$TEMP = meteo_$TEMPERATURE_AVG[days]#(meteo_$TEMPERATURE_MAX[days] + meteo_$TEMPERATURE_MIN[days]) / 2
self = calc_rates(self, meteo_$DAY[days], drv)
self = integrate.dvs(self, meteo_$DAY[days])
meteo$DVS[meteo$DAY==meteo_$DAY[days]] = self$states$DVS
days = days + 1
}
}
return(meteo)
}
# crop start type: "sowing", "emergence"
# crop_end_type : "maturity", "harvest", "earliest"
initialize = function(self, day, f.variety, start.type="sowing", end.type="maturity", vernalisation=TRUE)
{
self$params$TSUMEM = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="TSUMEM"]
self$params$TBASEM = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="TBASEM"]
self$params$TEFFMX = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="TEFFMX"]
self$params$TSUM1 = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="TSUM1"]
self$params$TSUM2 = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="TSUM2"]
self$params$DVSI = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="DVSI"]
self$params$IDSL = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="IDSL"]
self$params$DLO = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="DLO"]
self$params$DLC = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="DLC"]
self$params$DVSEND = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="DVSEND"]
self$params$crop_start_type = start.type
self$params$crop_end_type = end.type
# Define initial states
states.curr = get_initial_stage(self,day)
self$states$DOS = states.curr$DOS # Day of sowing
self$states$DOE = states.curr$DOE # Day of emergence
self$states$DOA = NA # Day of anthesis
self$states$DOM = NA # Day of maturity
self$states$DOH = NA # Day of harvest
self$states$DVS = 0
self$states$TSUM = 0
self$states$TSUME = 0
self$states$STAGE = states.curr$STAGE
self$rates$DTSUM = 0
self$rates$DTSUME = 0
self$rates$DVR = 0
self$rates$DVRED = 1
# vernalisation
if(vernalisation)
self$params$IDSL = 2
else
self$params$IDSL = 0
if(self$params$IDSL >= 2) {
self$params$VERNDVS = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="VERNDVS"]
self$params$VERNBASE = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="VERNBASE"]
self$params$VERNSAT = f.variety$PARAMETER_XVALUE[f.variety$PARAMETER_CODE=="VERNSAT"]
self$rates$VERNR = 0
self$rates$VERNFAC = 0
self$states$VERN = 0
self$states$DOV = NA
self$states$ISVERNALISED = FALSE
}
return(self)
}
calc_rates = function(self, day, drv)
{
# Day length sensitivity
DVRED = 1
if(self$params$IDSL >= 1) {
DAYLP = as.numeric(daylength.1(day, drv$LAT))
DVRED = (DAYLP - self$params$DLC)/(self$params$DLO - self$params$DLC)
if(DVRED < 0)
DVRED = 0
if(DVRED > 1)
DVRED = 1
}
# Vernalisation
VERNFAC = 1.
if(self$params$IDSL >= 2) {
if(self$states$STAGE == "vegetative") {
self = calc.rates.vernalisation(self, day, drv)
VERNFAC = self$rates$VERNFAC
}
}
# Development rates
if(self$states$STAGE == "emerging") {
self$rates$DTSUME = drv$TEMP - self$params$TBASEM
if(self$rates$DTSUME < 0)
self$rates$DTSUME = 0
if(self$rates$DTSUME > (self$params$TEFFMX - self$params$TBASEM))
self$rates$DTSUME = self$params$TEFFMX - self$params$TBASEM
self$rates$DTSUM = 0.
self$rates$DVR = 0.
}
if(self$states$STAGE == "vegetative") {
self$rates$DTSUME = 0.
self$rates$DTSUM = dtsmtb(drv$TEMP) * VERNFAC * DVRED
self$rates$DVR = self$rates$DTSUM/self$params$TSUM1
}
if(self$states$STAGE == "reproductive" || self$states$STAGE== "mature") {
self$rates$DTSUME = 0.
self$rates$DTSUM = dtsmtb(drv$TEMP)
self$rates$DVR = self$rates$DTSUM/self$params$TSUM2
}
if(is.na(self$states$STAGE))
error = 1
return(self)
}
integrate.dvs = function(self, day, delt=1.0)
{
error = 0
#Updates the state variable and checks for phenologic stages
# Integrate vernalisation module
if(self$params$IDSL >= 2) {
if(self$states$STAGE == "vegetative") {
self = integrate.vernalisation(self, day, delt)
}
}
# Integrate phenologic states
self$states$TSUME = self$states$TSUME + self$rates$DTSUME
self$states$DVS = self$states$DVS + self$rates$DVR
self$states$TSUM = self$states$TSUM + self$rates$DTSUM
# Check if a new stage is reached
if(self$states$STAGE == "emerging") {
if(self$states$TSUME >= self$params$TSUMEM) {
self = next_stage(self, day)
}
}
if(self$states$STAGE == "vegetative") {
if(self$states$DVS >= 1) {
self = next_stage(self, day)
self$states$DVS = 1.0
}
}
if(self$states$STAGE == "reproductive") {
if(self$states$DVS >= self$params$DVSEND) {
self = next_stage(self, day)
}
}
if(is.na(self$states$STAGE)) {
msg = c("No STAGE defined in phenology submodule")
error = 1
}
return(self)
}
get_initial_stage = function(self, day)
{
error = 0
if(self$params$crop_start_type == "emergence") {
STAGE = "vegetative"
DOE = day
DOS = NA
}
if(self$params$crop_start_type == "sowing") {
STAGE = "emerging"
DOS = day
DOE = NA
}
if(self$params$crop_start_type != "emergence" || self$params$crop_start_type != "sowing")
error = 1
return(list(DOS=DOS, DOE = DOE, STAGE = STAGE))
}
next_stage = function(self, day)
{
curr.STAGE = self$states$STAGE
if(curr.STAGE == "emerging") {
self$states$STAGE = "vegetative"
self$states$DOE = day
}
if(curr.STAGE == "vegetative") {
self$states$STAGE = "reproductive"
self$states$DOA = day
}
if(curr.STAGE == "reproductive") {
self$states$STAGE = "mature"
self$states$DOM = day
}
if(curr.STAGE == "mature") {
msg = "Cannot move to next phenology stage: maturity already reached!"
error = 1
}
return(self)
}
calc.rates.vernalisation = function(self, day, drv)
{
DVS = self$states$DVS
if(!self$states$ISVERNALISED) {
if(DVS < self$params$VERNDVS) {
self$rates$VERNR = vernrtb(drv$TEMP)
r = (self$states$VERN - self$params$VERNBASE)/(self$params$VERNSAT-self$params$VERNBASE)
if(r < 0)
r = 0
if(r > 1)
r = 1
self$rates$VERNFAC = r
} else {
self$rates$VERNR = 0.
self$rates$VERNFAC = 1.0
self$force_vernalisation = TRUE
}
} else {
self$rates$VERNR = 0.
self$rates$VERNFAC = 1.0
}
return(self)
}
integrate.vernalisation = function(self, day, delt=1.0)
{
self$states$VERN = self$states$VERN + self$rates$VERNR
if(self$states$VERN >= self$params$VERNSAT) {
# Vernalisation requirements reached
self$states$ISVERNALISED = TRUE
if(is.na(self$states$DOV)) {
self$states$DOV = day
}
}
if(self$force_vernalisation && self$states$VERN < self$params$VERNSAT) { # Critical DVS for vernalisation reached
# Force vernalisation, but do not set DOV
self$states$ISVERNALISED = TRUE
# Write log message to warn about forced vernalisation
}
if(self$states$VERN < self$params$VERNSAT && !self$force_vernalisation) # Reduction factor for phenologic development
self$states$ISVERNALISED = FALSE
return(self)
}
vernrtb = function(temp)
{
params.v = matrix(data=c(-30,-8,-4,3, 10,17,20, 50,0,0,0,1,1,0,0,0), nrow=8, ncol=2)
return(approx(params.v[,1], params.v[,2], temp)$y)
}
dtsmtb = function(temp) {
params.v = matrix(data=c(-50,0,30,45, 50, 0,0,30,30,30), nrow=5,ncol=5)
return(approx(params.v[,1], params.v[,2], temp)$y)
}
daylength.1 = function(day, latitude, angle=-4)
{
# Calculate day-of-year from date object day
IDAY = as.numeric(format(day, "%j"))
# constants
RAD = 0.0174533
PI = 3.1415926
# calculate daylength
ANGLE = angle
LAT = latitude
DEC = -asin(sin(23.45*RAD)*cos(2.*PI*((IDAY)+10.)/365.))
SINLD = sin(RAD*LAT)*sin(DEC)
COSLD = cos(RAD*LAT)*cos(DEC)
AOB = (-sin(ANGLE*RAD)+SINLD)/COSLD
# daylength
if(abs(AOB) <= 1.0)
DAYLP = 12.0*(1.+2.*asin((-sin(ANGLE*RAD)+SINLD)/COSLD)/PI)
if(AOB > 1.0)
DAYLP = 24.0
if(abs(AOB) > 1.0 && AOB < 1.0)
DAYLP = 0.0
# store results in cache
return(DAYLP)
}