comparison insect_phenology_model.R @ 73:9bc7ab4da9e7 draft

Uploaded
author greg
date Tue, 14 Nov 2017 13:54:27 -0500
parents 3c5a149d4255
children 9843894544a8
comparison
equal deleted inserted replaced
72:3c5a149d4255 73:9bc7ab4da9e7
24 24
25 parser <- OptionParser(usage="%prog [options] file", option_list=option_list) 25 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
26 args <- parse_args(parser, positional_arguments=TRUE) 26 args <- parse_args(parser, positional_arguments=TRUE)
27 opt <- args$options 27 opt <- args$options
28 28
29 add_daylight_length = function(latitude, temperature_data_matrix, num_days) { 29 parse_input_data = function(input_file, num_rows, num_columns) {
30 # Read in the input temperature datafile into a data frame.
31 if (num_rows == 6) {
32 # The input data has the following 6 columns:
33 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
34 temperature_data_frame <- read.csv(file=input_file, header=T, strip.white=TRUE, sep=",")
35 # Add a column containing the daylight length for each day.
36 temperature_data_frame <- add_daylight_length(latitude, temperature_data_frame, opt$num_days)
37 temperature_data_frame
38 }
39 }
40
41 add_daylight_length = function(latitude, temperature_data_frame, num_days) {
30 # Return a vector of daylight length (photoperido profile) for 42 # Return a vector of daylight length (photoperido profile) for
31 # the number of days specified in the input temperature data 43 # the number of days specified in the input temperature data
32 # (from Forsythe 1995). 44 # (from Forsythe 1995).
33 p = 0.8333 45 p = 0.8333
34 daylight_length_vector <- NULL 46 daylight_length_vector <- NULL
35 for (i in 1:num_days) { 47 for (i in 1:num_days) {
36 # Get the day of the year from the current row 48 # Get the day of the year from the current row
37 # of the temperature data for computation. 49 # of the temperature data for computation.
38 doy <- temperature_data_matrix[i, 4] 50 doy <- temperature_data_frame[i, 4]
39 theta <- 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (doy - 186))) 51 theta <- 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (doy - 186)))
40 phi <- asin(0.39795 * cos(theta)) 52 phi <- asin(0.39795 * cos(theta))
41 # Compute the length of daylight for the day of the year. 53 # Compute the length of daylight for the day of the year.
42 daylight_length_vector[i] <- 24 - (24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi)))) 54 daylight_length_vector[i] <- 24 - (24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi))))
43 } 55 }
44 # Append daylight_length_vector as a new column to temperature_data_matrix. 56 # Append daylight_length_vector as a new column to temperature_data_frame.
45 temperature_data_matrix[, 7] <- daylight_length_vector 57 temperature_data_frame[, 7] <- daylight_length_vector
46 # Return the altered temperature_data_matrix. 58 # Return the altered temperature_data_frame.
47 temperature_data_matrix 59 temperature_data_frame
48 } 60 }
49 61
50 get_temperature_at_hour = function(latitude, temperature_data_matrix, row, num_days) { 62 get_temperature_at_hour = function(latitude, temperature_data_frame, row, num_days) {
51 # Base development threshold for Brown Marmolated Stink Bug 63 # Base development threshold for Brown Marmolated Stink Bug
52 # insect phenology model. 64 # insect phenology model.
53 # TODO: Pass insect on the command line to accomodate more 65 # TODO: Pass insect on the command line to accomodate more
54 # the just the Brown Marmolated Stink Bub. 66 # the just the Brown Marmolated Stink Bub.
55 threshold <- 14.17 67 threshold <- 14.17
56 68
57 # Input temperature currently has the following columns. 69 # Input temperature currently has the following columns.
58 # # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX 70 # # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
59 # Minimum temperature for current row. 71 # Minimum temperature for current row.
60 dnp <- temperature_data_matrix[row, 5] 72 dnp <- temperature_data_frame[row, 5]
61 # Maximum temperature for current row. 73 # Maximum temperature for current row.
62 dxp <- temperature_data_matrix[row, 6] 74 dxp <- temperature_data_frame[row, 6]
63 # Mean temperature for current row. 75 # Mean temperature for current row.
64 dmean <- 0.5 * (dnp + dxp) 76 dmean <- 0.5 * (dnp + dxp)
65 # Initialize degree day accumulation 77 # Initialize degree day accumulation
66 dd <- 0 78 dd <- 0
67 if (dxp < threshold) { 79 if (dxp < threshold) {
71 # Initialize hourly temperature. 83 # Initialize hourly temperature.
72 T <- NULL 84 T <- NULL
73 # Initialize degree hour vector. 85 # Initialize degree hour vector.
74 dh <- NULL 86 dh <- NULL
75 # Daylight length for current row. 87 # Daylight length for current row.
76 y <- temperature_data_matrix[row, 7] 88 y <- temperature_data_frame[row, 7]
77 # Darkness length. 89 # Darkness length.
78 z <- 24 - y 90 z <- 24 - y
79 # Lag coefficient. 91 # Lag coefficient.
80 a <- 1.86 92 a <- 1.86
81 # Darkness coefficient. 93 # Darkness coefficient.
186 } 198 }
187 return = mort.prob 199 return = mort.prob
188 return 200 return
189 } 201 }
190 202
191 # Read in the input temperature datafile into a Data Frame object. 203 temperature_data_frame <- parse_input_data(opt$input, opt$num_rows, opt$num_columns)
192 # The input data currently must have 6 columns: 204 latitude <- temperature_data_frame[1, 1]
193 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
194 temperature_data_matrix <- read.csv(file=opt$input, header=T, strip.white=TRUE, sep=",")
195 start_date <- temperature_data_matrix[1, 3]
196 end_date <- temperature_data_matrix[opt$num_days, 3]
197 latitude <- temperature_data_matrix[1, 1]
198 temperature_data_matrix <- add_daylight_length(latitude, temperature_data_matrix, opt$num_days)
199 205
200 cat("Number of days: ", opt$num_days, "\n") 206 cat("Number of days: ", opt$num_days, "\n")
201 print(temperature_data_matrix)
202 207
203 # Initialize matrix for results from all replications. 208 # Initialize matrix for results from all replications.
204 S0.rep <- S1.rep <- S2.rep <- S3.rep <- S4.rep <- S5.rep <- matrix(rep(0, opt$num_days * opt$replications), ncol = opt$replications) 209 S0.rep <- S1.rep <- S2.rep <- S3.rep <- S4.rep <- S5.rep <- matrix(rep(0, opt$num_days * opt$replications), ncol = opt$replications)
205 newborn.rep <- death.rep <- adult.rep <- pop.rep <- g0.rep <- g1.rep <- g2.rep <- g0a.rep <- g1a.rep <- g2a.rep <- matrix(rep(0, opt$num_days * opt$replications), ncol=opt$replications) 210 newborn.rep <- death.rep <- adult.rep <- pop.rep <- g0.rep <- g1.rep <- g2.rep <- g0a.rep <- g1a.rep <- g2a.rep <- matrix(rep(0, opt$num_days * opt$replications), ncol=opt$replications)
206 211
226 dd.day <- rep(0, opt$num_days) 231 dd.day <- rep(0, opt$num_days)
227 232
228 # All the days included in the input temperature dataset. 233 # All the days included in the input temperature dataset.
229 for (row in 1:opt$num_days) { 234 for (row in 1:opt$num_days) {
230 # Get the integer day of the year for the current row. 235 # Get the integer day of the year for the current row.
231 doy <- temperature_data_matrix[row, 4] 236 doy <- temperature_data_frame[row, 4]
232 # Photoperiod in the day. 237 # Photoperiod in the day.
233 photoperiod <- temperature_data_matrix[row, 7] 238 photoperiod <- temperature_data_frame[row, 7]
234 temp.profile <- get_temperature_at_hour(latitude, temperature_data_matrix, row, opt$num_days) 239 temp.profile <- get_temperature_at_hour(latitude, temperature_data_frame, row, opt$num_days)
235 mean.temp <- temp.profile[1] 240 mean.temp <- temp.profile[1]
236 dd.temp <- temp.profile[2] 241 dd.temp <- temp.profile[2]
237 dd.day[row] <- dd.temp 242 dd.day[row] <- dd.temp
238 # Trash bin for death. 243 # Trash bin for death.
239 death.vec <- NULL 244 death.vec <- NULL
530 } 535 }
531 536
532 # Data analysis and visualization can currently 537 # Data analysis and visualization can currently
533 # plot only within a single calendar year. 538 # plot only within a single calendar year.
534 # TODO: enhance this to accomodate multiple calendar years. 539 # TODO: enhance this to accomodate multiple calendar years.
540 start_date <- temperature_data_frame[1, 3]
541 end_date <- temperature_data_frame[opt$num_days, 3]
542
535 n.yr <- 1 543 n.yr <- 1
536 day.all <- c(1:opt$num_days * n.yr) 544 day.all <- c(1:opt$num_days * n.yr)
537 545
538 # mean value for adults 546 # mean value for adults
539 sa <- apply((S3.rep + S4.rep + S5.rep), 1, mean) 547 sa <- apply((S3.rep + S4.rep + S5.rep), 1, mean)