comparison insect_phenology_model.R @ 13:80d38a26b2e8 draft

Uploaded
author greg
date Thu, 28 Sep 2017 10:45:53 -0400
parents 4114785abbe7
children cd4d17bf3b9e
comparison
equal deleted inserted replaced
12:e82ea5f1a179 13:80d38a26b2e8
10 make_option(c("-e", "--location"), action="store", dest="location", help="Selected location"), 10 make_option(c("-e", "--location"), action="store", dest="location", help="Selected location"),
11 make_option(c("-f", "--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"), 11 make_option(c("-f", "--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"),
12 make_option(c("-i", "--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"), 12 make_option(c("-i", "--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"),
13 make_option(c("-j", "--nymph_mort"), action="store", dest="nymph_mort", type="integer", help="Adjustment rate for nymph mortality"), 13 make_option(c("-j", "--nymph_mort"), action="store", dest="nymph_mort", type="integer", help="Adjustment rate for nymph mortality"),
14 make_option(c("-k", "--old_nymph_accum"), action="store", dest="old_nymph_accum", type="integer", help="Adjustment of DD accumulation (young nymph->old nymph)"), 14 make_option(c("-k", "--old_nymph_accum"), action="store", dest="old_nymph_accum", type="integer", help="Adjustment of DD accumulation (young nymph->old nymph)"),
15 make_option(c("-n", "--num_days"), action="store", dest="num_days", type="integer", help="Total number of days in the temperature dataset"),
15 make_option(c("-o", "--output"), action="store", dest="output", help="Output dataset"), 16 make_option(c("-o", "--output"), action="store", dest="output", help="Output dataset"),
16 make_option(c("-p", "--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"), 17 make_option(c("-p", "--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"),
17 make_option(c("-q", "--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"), 18 make_option(c("-q", "--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"),
18 make_option(c("-s", "--replications"), action="store", dest="replications", type="integer", help="Number of replications"), 19 make_option(c("-s", "--replications"), action="store", dest="replications", type="integer", help="Number of replications"),
19 make_option(c("-t", "--se_plot"), action="store", dest="se_plot", help="Plot SE"), 20 make_option(c("-t", "--se_plot"), action="store", dest="se_plot", help="Plot SE"),
26 args <- parse_args(parser, positional_arguments=TRUE) 27 args <- parse_args(parser, positional_arguments=TRUE)
27 opt <- args$options 28 opt <- args$options
28 29
29 data.input=function(loc, year, temperature.dataset) 30 data.input=function(loc, year, temperature.dataset)
30 { 31 {
31 expdata <- matrix(rep(0, 365 * 3), nrow=365) 32 expdata <- matrix(rep(0, opt$num_days * 3), nrow=opt$num_days)
32 namedat <- paste(loc, year, ".Rdat", sep="") 33 namedat <- paste(loc, year, ".Rdat", sep="")
33 temp.data <- read.csv(file=temperature.dataset, header=T) 34 temp.data <- read.csv(file=temperature.dataset, header=T)
34 35
35 expdata[,1] <- c(1:365) 36 expdata[,1] <- c(1:opt$num_days)
36 # Minimum 37 # Minimum
37 expdata[,2] <- temp.data[c(1:365), 3] 38 expdata[,2] <- temp.data[c(1:opt$num_days), 3]
38 # Maximum 39 # Maximum
39 expdata[,3] <- temp.data[c(1:365), 2] 40 expdata[,3] <- temp.data[c(1:opt$num_days), 2]
40 save(expdata, file=namedat) 41 save(expdata, file=namedat)
41 namedat 42 namedat
42 } 43 }
43 44
44 daylength=function(latitude) 45 daylength=function(latitude, num_days)
45 { 46 {
46 # from Forsythe 1995 47 # from Forsythe 1995
47 p=0.8333 48 p=0.8333
48 dl <- NULL 49 dl <- NULL
49 for (i in 1:365) { 50 for (i in 1:num_days) {
50 theta <- 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (i - 186))) 51 theta <- 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (i - 186)))
51 phi <- asin(0.39795 * cos(theta)) 52 phi <- asin(0.39795 * cos(theta))
52 dl[i] <- 24 - 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi))) 53 dl[i] <- 24 - 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi)))
53 } 54 }
54 dl # return a vector of daylength in 365 days 55 # return a vector of daylength for the number of
55 } 56 # days specifie din the input temperature data
56 57 dl
57 hourtemp=function(latitude, date, temperature_file_path) 58 }
59
60 hourtemp=function(latitude, date, temperature_file_path, num_days)
58 { 61 {
59 load(temperature_file_path) 62 load(temperature_file_path)
60 threshold <- 14.17 # base development threshold for BMSB 63 # base development threshold for BMSB
64 threshold <- 14.17
61 dnp <- expdata[date, 2] # daily minimum 65 dnp <- expdata[date, 2] # daily minimum
62 dxp <- expdata[date, 3] # daily maximum 66 dxp <- expdata[date, 3] # daily maximum
63 dmean <- 0.5 * (dnp + dxp) 67 dmean <- 0.5 * (dnp + dxp)
64 dd <- 0 # initialize degree day accumulation 68 dd <- 0 # initialize degree day accumulation
65 69
66 if (dxp<threshold) { 70 if (dxp<threshold) {
67 dd <- 0 71 dd <- 0
68 } 72 }
69 else { 73 else {
70 dlprofile <- daylength(latitude) # extract daylength data for entire year 74 # extract daylength data for the number of
75 # days specified in the input temperature data
76 dlprofile <- daylength(latitude, num_days)
71 T <- NULL # initialize hourly temperature 77 T <- NULL # initialize hourly temperature
72 dh <- NULL #initialize degree hour vector 78 dh <- NULL #initialize degree hour vector
73 # date <- 200 79 # calculate daylength in given date
74 y <- dlprofile[date] # calculate daylength in given date 80 y <- dlprofile[date]
75 z <- 24 - y # night length 81 # night length
76 a <- 1.86 # lag coefficient 82 z <- 24 - y
77 b <- 2.20 # night coefficient 83 # lag coefficient
78 #tempdata <- read.csv("tempdata.csv") #import raw data set 84 a <- 1.86
79 # Should be outside function otherwise its redundant 85 # night coefficient
80 risetime <- 12 - y / 2 # sunrise time 86 b <- 2.20
81 settime <- 12 + y / 2 # sunset time 87 # sunrise time
88 risetime <- 12 - y / 2
89 # sunset time
90 settime <- 12 + y / 2
82 ts <- (dxp - dnp) * sin(pi * (settime - 5) / (y + 2 * a)) + dnp 91 ts <- (dxp - dnp) * sin(pi * (settime - 5) / (y + 2 * a)) + dnp
83 for (i in 1:24) { 92 for (i in 1:24) {
84 if (i > risetime && i<settime) { 93 if (i > risetime && i<settime) {
85 m <- i - 5 # number of hours after Tmin until sunset 94 # number of hours after Tmin until sunset
95 m <- i - 5
86 T[i]=(dxp - dnp) * sin(pi * m / (y + 2 * a)) + dnp 96 T[i]=(dxp - dnp) * sin(pi * m / (y + 2 * a)) + dnp
87 if (T[i]<8.4) { 97 if (T[i]<8.4) {
88 dh[i] <- 0 98 dh[i] <- 0
89 } 99 }
90 else { 100 else {
203 213
204 # Read in the input temperature datafile 214 # Read in the input temperature datafile
205 temperature_file_path <- data.input(opt$location, opt$year, opt$temperature_dataset) 215 temperature_file_path <- data.input(opt$location, opt$year, opt$temperature_dataset)
206 216
207 # Initialize matrix for results from all replications 217 # Initialize matrix for results from all replications
208 S0.rep <- S1.rep <- S2.rep <- S3.rep <- S4.rep <- S5.rep <- matrix(rep(0, 365 * opt$replications), ncol = opt$replications) 218 S0.rep <- S1.rep <- S2.rep <- S3.rep <- S4.rep <- S5.rep <- matrix(rep(0, opt$num_days * opt$replications), ncol = opt$replications)
209 newborn.rep <- death.rep <- adult.rep <- pop.rep <- g0.rep <- g1.rep <- g2.rep <- g0a.rep <- g1a.rep <- g2a.rep <- matrix(rep(0, 365 * opt$replications), ncol=opt$replications) 219 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 220
211 # loop through replications 221 # loop through replications
212 for (N.rep in 1:opt$replications) { 222 for (N.rep in 1:opt$replications) {
213 # during each replication 223 # during each replication
214 # start with 1000 individuals -- user definable as well? 224 # start with 1000 individuals -- user definable as well?
218 # overwintering, previttelogenic, DD=0, T=0, no-diapause 228 # overwintering, previttelogenic, DD=0, T=0, no-diapause
219 vec.mat <- rep(vec.ini, n) 229 vec.mat <- rep(vec.ini, n)
220 # complete matrix for the population 230 # complete matrix for the population
221 vec.mat <- t(matrix(vec.mat, nrow=5)) 231 vec.mat <- t(matrix(vec.mat, nrow=5))
222 # complete photoperiod profile in a year, requires daylength function 232 # complete photoperiod profile in a year, requires daylength function
223 ph.p <- daylength(opt$latitude) 233 ph.p <- daylength(opt$latitude, opt$num_days)
224 234
225 # time series of population size 235 # time series of population size
226 tot.pop <- NULL 236 tot.pop <- NULL
227 # gen.0 pop size 237 # gen.0 pop size
228 gen0.pop <- rep(0, 365) 238 gen0.pop <- rep(0, opt$num_days)
229 gen1.pop <- rep(0, 365) 239 gen1.pop <- rep(0, opt$num_days)
230 gen2.pop <- rep(0, 365) 240 gen2.pop <- rep(0, opt$num_days)
231 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, 365) 241 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, opt$num_days)
232 g0.adult <- g1.adult <- g2.adult <- rep(0, 365) 242 g0.adult <- g1.adult <- g2.adult <- rep(0, opt$num_days)
233 N.newborn <- N.death <- N.adult <- rep(0, 365) 243 N.newborn <- N.death <- N.adult <- rep(0, opt$num_days)
234 dd.day <- rep(0, 365) 244 dd.day <- rep(0, opt$num_days)
235 245
236 # start tick 246 # start tick
237 ptm <- proc.time() 247 ptm <- proc.time()
238 248
239 # all the days 249 # all the days
240 for (day in 1:365) { 250 for (day in 1:opt$num_days) {
241 # photoperiod in the day 251 # photoperiod in the day
242 photoperiod <- ph.p[day] 252 photoperiod <- ph.p[day]
243 temp.profile <- hourtemp(opt$latitude, day, temperature_file_path) 253 temp.profile <- hourtemp(opt$latitude, day, temperature_file_path, opt$num_days)
244 mean.temp <- temp.profile[1] 254 mean.temp <- temp.profile[1]
245 dd.temp <- temp.profile[2] 255 dd.temp <- temp.profile[2]
246 dd.day[day] <- dd.temp 256 dd.day[day] <- dd.temp
247 # trash bin for death 257 # trash bin for death
248 death.vec <- NULL 258 death.vec <- NULL
512 g2.adult[day] <- sum((vec.mat[,1]== 2 & vec.mat[,2] == 3) | (vec.mat[,1] == 2 & vec.mat[,2] == 4) | (vec.mat[,1] == 2 & vec.mat[,2] == 5)) 522 g2.adult[day] <- sum((vec.mat[,1]== 2 & vec.mat[,2] == 3) | (vec.mat[,1] == 2 & vec.mat[,2] == 4) | (vec.mat[,1] == 2 & vec.mat[,2] == 5))
513 523
514 N.newborn[day] <- n.newborn 524 N.newborn[day] <- n.newborn
515 N.death[day] <- n.death 525 N.death[day] <- n.death
516 N.adult[day] <- n.adult 526 N.adult[day] <- n.adult
517 #print(c(N.rep, day, n, n.adult)) 527 } # end of days specified in the input temperature data
518 } # end of 365 days
519 528
520 dd.cum <- cumsum(dd.day) 529 dd.cum <- cumsum(dd.day)
521 # collect all the outputs 530 # collect all the outputs
522 S0.rep[,N.rep] <- S0 531 S0.rep[,N.rep] <- S0
523 S1.rep[,N.rep] <- S1 532 S1.rep[,N.rep] <- S1
535 g0a.rep[,N.rep] <- g0.adult 544 g0a.rep[,N.rep] <- g0.adult
536 g1a.rep[,N.rep] <- g1.adult 545 g1a.rep[,N.rep] <- g1.adult
537 g2a.rep[,N.rep] <- g2.adult 546 g2a.rep[,N.rep] <- g2.adult
538 } 547 }
539 548
540 # save(dd.day, dd.cum, S0.rep, S1.rep, S2.rep, S3.rep, S4.rep, S5.rep, newborn.rep, death.rep, adult.rep, pop.rep, g0.rep, g1.rep, g2.rep, g0a.rep, g1a.rep, g2a.rep, file=opt$output)
541 # maybe do not need to export this bit, but for now just leave it as-is
542 # do we need to export this Rdat file?
543
544 # Data analysis and visualization 549 # Data analysis and visualization
545 # default: plot 1 year of result 550 # default: plot 1 year of result
546 # but can be expanded to accommodate multiple years 551 # but can be expanded to accommodate multiple years
547 n.yr <- 1 552 n.yr <- 1
548 day.all <- c(1:365 * n.yr) 553 day.all <- c(1:opt$num_days * n.yr)
549 554
550 # mean value for adults 555 # mean value for adults
551 sa <- apply((S3.rep + S4.rep + S5.rep), 1, mean) 556 sa <- apply((S3.rep + S4.rep + S5.rep), 1, mean)
552 # mean value for nymphs 557 # mean value for nymphs
553 sn <- apply((S1.rep + S2.rep), 1,mean) 558 sn <- apply((S1.rep + S2.rep), 1,mean)