Mercurial > repos > greg > insect_phenology_model
comparison insect_phenology_model.R @ 142:b06b3881ecf0 draft
Uploaded
author | greg |
---|---|
date | Thu, 20 Dec 2018 09:08:50 -0500 |
parents | 14afb6e85581 |
children | 7628ba67c7ff |
comparison
equal
deleted
inserted
replaced
141:14afb6e85581 | 142:b06b3881ecf0 |
---|---|
248 mortality.probability = temperature * 0.0005 + 0.02; | 248 mortality.probability = temperature * 0.0005 + 0.02; |
249 } | 249 } |
250 return(mortality.probability) | 250 return(mortality.probability) |
251 } | 251 } |
252 | 252 |
253 mortality.egg = function(temperature, adj=0) { | 253 #mortality.egg = function(temperature, adj=0) { |
254 # If no input from adjustment, default | 254 # # If no input from adjustment, default |
255 # value is 0 (data from Nielsen, 2008). | 255 # # value is 0 (data from Nielsen, 2008). |
256 T.mortality = c(15, 17, 20, 25, 27, 30, 33, 35); | 256 # T.mortality = c(15, 17, 20, 25, 27, 30, 33, 35); |
257 egg.mortality = c(50, 2, 1, 0, 0, 0, 5, 100); | 257 # egg.mortality = c(50, 2, 1, 0, 0, 0, 5, 100); |
258 # Calculates slopes and intercepts for lines. | 258 # # Calculates slopes and intercepts for lines. |
259 slopes = NULL; | 259 # slopes = NULL; |
260 intercepts = NULL; | 260 # intercepts = NULL; |
261 for (i in 1:length(T.mortality)) { | 261 # for (i in 1:length(T.mortality)) { |
262 slopes[i] = (egg.mortality[i+1] - egg.mortality[i]) / (T.mortality[i+1] - T.mortality[i]); | 262 # slopes[i] = (egg.mortality[i+1] - egg.mortality[i]) / (T.mortality[i+1] - T.mortality[i]); |
263 intercepts[i] = -slopes[i] * T.mortality[i] + egg.mortality[i]; | 263 # intercepts[i] = -slopes[i] * T.mortality[i] + egg.mortality[i]; |
264 } | 264 # } |
265 # Calculates mortality based on temperature. | 265 # # Calculates mortality based on temperature. |
266 mortality.probability = NULL; | 266 # mortality.probability = NULL; |
267 for (j in 1:length(temperature)) { | 267 # for (j in 1:length(temperature)) { |
268 mortality.probability[j] = if(temperature[j] <= T.mortality[2]) { | 268 # mortality.probability[j] = if(temperature[j] <= T.mortality[2]) { |
269 temperature[j] * slopes[1] + intercepts[1]; | 269 # temperature[j] * slopes[1] + intercepts[1]; |
270 } else if (temperature[j] > T.mortality[2] && temperature[j] <= T.mortality[3]) { | 270 # } else if (temperature[j] > T.mortality[2] && temperature[j] <= T.mortality[3]) { |
271 temperature[j] * slopes[2] + intercepts[2]; | 271 # temperature[j] * slopes[2] + intercepts[2]; |
272 } else if (temperature[j] > T.mortality[3] && temperature[j] <= T.mortality[4]) { | 272 # } else if (temperature[j] > T.mortality[3] && temperature[j] <= T.mortality[4]) { |
273 temperature[j] * slopes[3] + intercepts[3]; | 273 # temperature[j] * slopes[3] + intercepts[3]; |
274 } else if (temperature[j] > T.mortality[4] && temperature[j] <= T.mortality[5]) { | 274 # } else if (temperature[j] > T.mortality[4] && temperature[j] <= T.mortality[5]) { |
275 temperature[j] * slopes[4] + intercepts[4]; | 275 # temperature[j] * slopes[4] + intercepts[4]; |
276 } else if (temperature[j] > T.mortality[5] && temperature[j] <= T.mortality[6]) { | 276 # } else if (temperature[j] > T.mortality[5] && temperature[j] <= T.mortality[6]) { |
277 temperature[j] * slopes[5] + intercepts[5]; | 277 # temperature[j] * slopes[5] + intercepts[5]; |
278 } else if (temperature[j] > T.mortality[6] && temperature[j] <= T.mortality[7]) { | 278 # } else if (temperature[j] > T.mortality[6] && temperature[j] <= T.mortality[7]) { |
279 temperature[j] * slopes[6] + intercepts[6]; | 279 # temperature[j] * slopes[6] + intercepts[6]; |
280 } else if (temperature[j] > T.mortality[7]) { | 280 # } else if (temperature[j] > T.mortality[7]) { |
281 temperature[j] * slopes[7] + intercepts[7]; | 281 # temperature[j] * slopes[7] + intercepts[7]; |
282 } | 282 # } |
283 # If mortality > 100, make it equal to 100. | 283 # # If mortality > 100, make it equal to 100. |
284 mortality.probability[mortality.probability>100] = 100; | 284 # mortality.probability[mortality.probability>100] = 100; |
285 # If mortality <0, make equal to 0. | 285 # # If mortality <0, make equal to 0. |
286 mortality.probability[mortality.probability<0] = 0; | 286 # mortality.probability[mortality.probability<0] = 0; |
287 } | 287 # } |
288 # Make mortality adjustments based on adj parameter. | 288 # # Make mortality adjustments based on adj parameter. |
289 mortality.probability = (100 - mortality.probability) * adj + mortality.probability; | 289 # mortality.probability = (100 - mortality.probability) * adj + mortality.probability; |
290 # if mortality > 100, make it equal to 100. | 290 # # if mortality > 100, make it equal to 100. |
291 mortality.probability[mortality.probability>100] = 100; | 291 # mortality.probability[mortality.probability>100] = 100; |
292 # If mortality <0, make equal to 0. | 292 # # If mortality <0, make equal to 0. |
293 mortality.probability[mortality.probability<0] = 0; | 293 # mortality.probability[mortality.probability<0] = 0; |
294 # Change percent to proportion. | 294 # # Change percent to proportion. |
295 mortality.probability = mortality.probability / 100; | 295 # mortality.probability = mortality.probability / 100; |
296 return(mortality.probability) | 296 # return(mortality.probability) |
297 #} | |
298 | |
299 mortality.egg = function(temperature) { | |
300 if (temperature < 12.7) { | |
301 mortality.probability = 0.8; | |
302 } else { | |
303 mortality.probability = 0.8 - temperature / 40.0; | |
304 if (mortality.probability < 0) { | |
305 mortality.probability = 0.01; | |
306 } | |
307 } | |
308 return (mortality.probability); | |
297 } | 309 } |
298 | 310 |
299 mortality.nymph = function(temperature) { | 311 mortality.nymph = function(temperature) { |
300 if (temperature < 12.7) { | 312 if (temperature < 12.7) { |
301 mortality.probability = 0.03; | 313 mortality.probability = 0.03; |
309 parse_input_data = function(input_ytd, input_norm, location, start_date, end_date) { | 321 parse_input_data = function(input_ytd, input_norm, location, start_date, end_date) { |
310 # The end DOY for norm data prepended to ytd data. | 322 # The end DOY for norm data prepended to ytd data. |
311 prepend_end_doy_norm = 0; | 323 prepend_end_doy_norm = 0; |
312 # The start DOY for norm data appended to ytd data. | 324 # The start DOY for norm data appended to ytd data. |
313 append_start_doy_norm = 0; | 325 append_start_doy_norm = 0; |
326 cat("start_date: ", start_date, "\n"); | |
327 cat("end_date: ", end_date, "\n"); | |
314 if (is.null(start_date) && is.null(end_date)) { | 328 if (is.null(start_date) && is.null(end_date)) { |
315 # We're not dealing with a date interval. | 329 # We're not dealing with a date interval. |
316 date_interval = FALSE; | 330 date_interval = FALSE; |
317 if (is.null(input_ytd)) { | 331 if (is.null(input_ytd)) { |
318 # Base all dates on the current date since 30 year | 332 # Base all dates on the current date since 30 year |
324 year = get_year_from_date(start_date); | 338 year = get_year_from_date(start_date); |
325 # Get the DOY for start_date and end_date. | 339 # Get the DOY for start_date and end_date. |
326 start_date_doy = as.integer(strftime(start_date, format="%j")); | 340 start_date_doy = as.integer(strftime(start_date, format="%j")); |
327 end_date_doy = as.integer(strftime(end_date, format="%j")); | 341 end_date_doy = as.integer(strftime(end_date, format="%j")); |
328 } | 342 } |
343 cat("date_interval: ", date_interval, "\n"); | |
329 if (is.null(input_ytd)) { | 344 if (is.null(input_ytd)) { |
330 # We're processing only the 30 year normals data. | 345 # We're processing only the 30 year normals data. |
331 processing_year_to_date_data = FALSE; | 346 processing_year_to_date_data = FALSE; |
332 if (is.null(start_date) && is.null(end_date)) { | 347 if (is.null(start_date) && is.null(end_date)) { |
333 # We're processing the entire year, so we can | 348 # We're processing the entire year, so we can |
361 start_date_ytd_row = 0; | 376 start_date_ytd_row = 0; |
362 start_date_norm_row = which(norm_data_frame$DOY==start_date_doy); | 377 start_date_norm_row = which(norm_data_frame$DOY==start_date_doy); |
363 } | 378 } |
364 end_date_ytd_row = which(temperature_data_frame$DATE==end_date); | 379 end_date_ytd_row = which(temperature_data_frame$DATE==end_date); |
365 if (length(end_date_ytd_row) > 0) { | 380 if (length(end_date_ytd_row) > 0) { |
381 cat("I'm here...\n"); | |
366 end_date_ytd_row = end_date_ytd_row[1]; | 382 end_date_ytd_row = end_date_ytd_row[1]; |
367 # The end date is contained within the input_ytd data. | 383 # The end date is contained within the input_ytd data. |
368 end_doy_ytd = as.integer(temperature_data_frame$DOY[end_date_ytd_row]); | 384 end_doy_ytd = as.integer(temperature_data_frame$DOY[end_date_ytd_row]); |
369 if (end_doy_ytd > end_date_ytd_row + 1) { | 385 cat("end_doy_ytd: ", end_doy_ytd, "\n"); |
370 # The input year-to-date dataset is missing 1 or more | 386 cat("end_date_ytd_row: ", end_date_ytd_row, "\n"); |
371 # days of data. | 387 cat("start_date_ytd_row: ", start_date_ytd_row, "\n"); |
372 days_missing = end_doy_ytd - end_date_ytd_row; | |
373 msg = cat("The year-to-date dataset is missing ", days_missing, " days of data.\n"); | |
374 stop_err(msg); | |
375 } | |
376 } else { | 388 } else { |
377 end_date_ytd_row = 0; | 389 end_date_ytd_row = 0; |
378 } | 390 } |
379 } else { | 391 } else { |
380 # We're plotting an entire year. | 392 # We're plotting an entire year. |
394 # Properly set the end_date to be Dec 31 of the year. | 406 # Properly set the end_date to be Dec 31 of the year. |
395 end_date = paste(year, "12", "31", sep="-"); | 407 end_date = paste(year, "12", "31", sep="-"); |
396 # Save the first DOY to later check if start_date is Jan 1. | 408 # Save the first DOY to later check if start_date is Jan 1. |
397 start_doy_ytd = as.integer(temperature_data_frame$DOY[1]); | 409 start_doy_ytd = as.integer(temperature_data_frame$DOY[1]); |
398 end_doy_ytd = as.integer(temperature_data_frame$DOY[num_ytd_rows]); | 410 end_doy_ytd = as.integer(temperature_data_frame$DOY[num_ytd_rows]); |
399 if (end_doy_ytd > end_date_ytd_row + 1) { | 411 cat("I'm here 2...\n"); |
400 # The input year-to-date dataset is missing 1 or more | 412 cat("end_doy_ytd: ", end_doy_ytd, "\n"); |
401 # days of data. | 413 cat("end_date_ytd_row: ", end_date_ytd_row, "\n"); |
402 days_missing = end_doy_ytd - end_date_ytd_row; | 414 cat("start_date_ytd_row: ", start_date_ytd_row, "\n"); |
403 msg = cat("The year-to-date dataset is missing ", days_missing, " days of data.\n"); | |
404 stop_err(msg); | |
405 } | |
406 } | 415 } |
407 } else { | 416 } else { |
408 # We're processing only the 30 year normals data, so create an empty | 417 # We're processing only the 30 year normals data, so create an empty |
409 # data frame for containing temperature data after it is converted | 418 # data frame for containing temperature data after it is converted |
410 # from the 30 year normals format to the year-to-date format. | 419 # from the 30 year normals format to the year-to-date format. |
555 for (i in 1:total_days) { | 564 for (i in 1:total_days) { |
556 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i); | 565 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i); |
557 } | 566 } |
558 } | 567 } |
559 } | 568 } |
569 # Ensure all DOY values are consectuive integers. | |
570 validate_doys(temperature_data_frame); | |
560 # Add a column containing the daylight length for each day. | 571 # Add a column containing the daylight length for each day. |
561 temperature_data_frame = add_daylight_length(temperature_data_frame); | 572 temperature_data_frame = add_daylight_length(temperature_data_frame); |
562 return(list(temperature_data_frame, start_date, end_date, prepend_end_doy_norm, append_start_doy_norm, is_leap_year, location)); | 573 return(list(temperature_data_frame, start_date, end_date, prepend_end_doy_norm, append_start_doy_norm, is_leap_year, location)); |
563 } | 574 } |
564 | 575 |
854 post.mortality = 2; | 865 post.mortality = 2; |
855 day.kill = 250; | 866 day.kill = 250; |
856 } | 867 } |
857 if (vector.individual[2] == 0) { | 868 if (vector.individual[2] == 0) { |
858 # Egg. | 869 # Egg. |
859 death.probability = opt$egg_mortality * mortality.egg(mean.temp, adj=opt$egg_mortality); | 870 # death.probability = opt$egg_mortality * mortality.egg(mean.temp, adj=opt$egg_mortality); |
871 death.probability = opt$egg_mortality * mortality.egg(mean.temp); | |
860 } | 872 } |
861 else if (vector.individual[2] == 1 | vector.individual[2] == 2) { | 873 else if (vector.individual[2] == 1 | vector.individual[2] == 2) { |
862 # Nymph. | 874 # Nymph. |
863 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp); | 875 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp); |
864 } | 876 } |