Repository navigation
Expand file tree
/
Copy pathcustom_functions.R
More file actions
243 lines (212 loc) · 7.72 KB
/
Copy pathcustom_functions.R
File metadata and controls
243 lines (212 loc) · 7.72 KB
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
# functions for processing weather data as adapted from Hermann et al 2016 RSOS
#because these functions rely on accumulation calculations, missing data is problematic
# the replace missing function was written to estimate missing values by simply repeating
#the data from the measurement of that kind taken before. It's not perfect but probably
#the issues come out in the wash
#feed in a vector of values, it'll create a vector object with the missing values replaced
# works for columns in data frames, too
replace.missing<-function(vec){
#create a vector to put our new values into
New = c()
for (i in 1:(length(vec))){
if (is.na(vec[i])){
vec[i]<-vec[i-1]
#if the data is missing, sub in the value from the measurement before
} else{
#if the value is not missing, just pass it through to the result vector
vec[i]<-vec[i]
}
New=c(New, vec[i])
}
if (any(is.na(New))){
replace.missing(New)
}
return(New)
}
#we'll need to operate on a dataset that's sorted in a decending way
#for this because it's easier to think about accumulations that way
#so before using the functions below, make sure data is sorted in decending order
#by date, ie row 1 is January 1 1900, row 2 is january 2 1900, etc
# calculate the degree day accumulation for the first half of the day dd1,
#assuming a sine wave structure of temperature over the day
#use a development threshold of 10C, well, because it's a nice number
#to work with
#we'll use the model presented in Allen 1976 which uses daily max and min temperatures
#and assumes temperature follows a sine wave
#allen function calculates within-day degree day accumulation. We'll calculate
#season-long accumulation with another function
#maxi = vector of max daytime temps
#mini = vector of min daytime temps
#thresh = numeric value of threshold, if not given, function assumes it's 10 C
allen<-function(maxi, mini, thresh){
#if threshold is not given, assume it's 10 Celcius
if(missing(thresh)) {
thresh<-10
} else {
thresh<-thresh
}
dd1<-c()
dd2<-c()
for (i in 1:length(maxi)){
if (maxi[i]>= thresh & mini[i]<thresh) {
#first half of day
#amplitude of temperature difference
alpha1<-(maxi[i]-mini[i])/2
#average temperature
avg1<-(maxi[i]+mini[i])/2
#theta is time point when temperatur crosses the threshold
#assuming temperature is roughly following the sine curve
theta1<-asin((thresh-avg1)/alpha1)
#use these to calculate degree day accumulation over first half of day
dd1.T<-(1/(2*pi))*((avg1-thresh)*(pi/2 - theta1)+alpha1*cos(theta1))
dd1<-c(dd1, dd1.T)
#second half of day
#two possible cases, min temperature on day i+1 could be below thereshold or above
#for below threshold:
if (mini[i+1]<thresh){
#amplitude of temperature difference
alpha2<-(maxi[i]-mini[i+1])/2
#average temperature
avg2<-(maxi[i]+mini[i+1])/2
#theta is time point when temperatur crosses the threshold
#assuming temperature is roughly following the sine curve
theta2<-asin((thresh-avg2)/alpha2)
#use these to calculate degree day accumulation over first half of day
dd2.T<-(1/(2*pi))*((avg2-thresh)*(pi/2 - theta2)+alpha2*cos(theta2))
dd2<-c(dd2, dd2.T)
} else { #for above threshold
#second half of day
avg2<-(maxi[i]+mini[i+1])/2
dd2.T<-(avg2-thresh)/2
dd2<-c(dd2, dd2.T)
}
} else if (mini[i]>=thresh){
#first half of day
avg1<-(maxi[i]+mini[i])/2
dd1.T<-(avg1-thresh)/2
dd1<-c(dd1, dd1.T)
#second half of day, as above, two possible cases
if (mini[i+1]>=thresh){
avg2<-(maxi[i]+mini[i+1])/2
dd2.T<-(avg2-thresh)/2
dd2<-c(dd2, dd2.T)
} else{
#amplitude of temperature difference
alpha2<-(maxi[i]-mini[i+1])/2
#average temperature
avg2<-(maxi[i]+mini[i+1])/2
#theta is time point when temperatur crosses the threshold
#assuming temperature is roughly following the sine curve
theta2<-asin((thresh-avg2)/alpha2)
#use these to calculate degree day accumulation over first half of day
dd2.T<-(1/(2*pi))*((avg2-thresh)*(pi/2 - theta2)+alpha2*cos(theta2))
dd2<-c(dd2, dd2.T)
}
}
else {
#if temperature doesn't get over threshold, no degree days accumulated
#first half of day
dd1<-c(dd1, 0)
#second half of day
dd2<-c(dd2, 0)
}
#total accumulation over the day is just first half of day plus second
}
return(dd1+dd2)
}
#uses allen function to calculate degree day accumulations over the season from a start day
#as specified by the user. Arguments as defined in allen, with DOY as the day of year vector
#and startday is the numeric start date of the calculation- gives zero accumulations on days
#before that date
accum.allen<-function(maxi, mini, thresh, DOY, startday){
#if startday is not given, assume it's day 1
if(missing(startday)) {
startday<-1
} else {
startday<-startday
}
dd<-allen(maxi, mini, thresh)
dd.accum<-c()
for (i in 1:length(dd)){
#hmm, need a way to sum up over the year, starting anew for each year.
#this should do it
if (DOY[i]==1){
dd.accum.day=0
}
#the accumulation on day i is the degree day accumulation before
#plus the dd accumulated on that day
dd.accum.day<-dd.accum.day+dd[i]
#but if the degdays are accumulating before the startday, we want to forget them
if (DOY[i]<startday){
dd.accum.day=0
}
#add that day's accumulation to the vector
dd.accum<-c(dd.accum, dd.accum.day)
}
return (dd.accum)
}
#but what about preciptiation? our lit search indicated that there could be
#important things going on with rain and aphidss. First let's calculate the
#precipitation accumulation over the week, then let's look at the number of rainy
#days in a week
#calculate accumulated precipiation over a week
accum.precip<-function (precip, week){
precip.acc<-c()
counter<-week[1]
accumulation<-0
for (i in 1:length(precip)){
if(week[i]==counter){
accumulation<-accumulation + precip[i]
}else{
counter<-week[i]
accumulation<-precip[i]
}
precip.acc<-c(precip.acc, accumulation)
}
return(precip.acc)
}
#looks good! now let's count rainy days
#this is a simple thing, doesn't really need a function to encode for it, but what the heck
#might as well be consistent with how we've handled processing other weather data
#encoding rain days as 0/1 will allow us to simply sum up the number of rainy days for whatever time
#period we like
rainy.days<-function (precip, week){
rainy.days<-c()
for (i in 1:length(precip)){
if(precip[i]>0){
raindays<-1
}else{
raindays<-0
}
rainy.days<-c(rainy.days, raindays)
}
return(rainy.days)
}
#and now a function to calculate accumulated precipitation over the growing season,
#from the specified start date
accum.precip.time<-function(precip, DOY, startday){
#if startday is not given, assume it's day 1
if(missing(startday)) {
startday<-1
} else {
startday<-startday
}
prec.accum<-c()
for (i in 1:length(DOY)){
#hmm, need a way to sum up over the year, starting anew for each year.
#this should do it
if (DOY[i]==1){
prec.accum.day=0
}
#the accumulation on day i is the precip accumulation before
#plus the precip accumulated on that day
prec.accum.day<-prec.accum.day+precip[i]
#but if the precip is accumulating before the startday, we want to forget them
if (DOY[i]<startday){
prec.accum.day=0
}
#add that day's accumulation to the vector
prec.accum<-c(prec.accum, prec.accum.day)
}
return (prec.accum)
}