Mercurial > repos > greg > insect_phenology_model
comparison insect_phenology_model.R @ 122:8946ddb9d72c draft
Uploaded
author | greg |
---|---|
date | Thu, 31 May 2018 13:09:32 -0400 |
parents | 635b6dc6b268 |
children | e69e30d853fb |
comparison
equal
deleted
inserted
replaced
121:da67a24b04ba | 122:8946ddb9d72c |
---|---|
31 | 31 |
32 parser <- OptionParser(usage="%prog [options] file", option_list=option_list); | 32 parser <- OptionParser(usage="%prog [options] file", option_list=option_list); |
33 args <- parse_args(parser, positional_arguments=TRUE); | 33 args <- parse_args(parser, positional_arguments=TRUE); |
34 opt <- args$options; | 34 opt <- args$options; |
35 | 35 |
36 add_daylight_length = function(temperature_data_frame, num_rows) { | 36 add_daylight_length = function(temperature_data_frame) { |
37 # Return a vector of daylight length (photoperido profile) for | 37 # Return temperature_data_frame with an added column |
38 # the number of days specified in the input_ytd temperature data | 38 # of daylight length (photoperido profile). |
39 # (from Forsythe 1995). | 39 num_rows = dim(temperature_data_frame)[1]; |
40 # From Forsythe 1995. | |
40 p = 0.8333; | 41 p = 0.8333; |
41 latitude = temperature_data_frame$LATITUDE[1]; | 42 latitude = temperature_data_frame$LATITUDE[1]; |
42 daylight_length_vector = NULL; | 43 daylight_length_vector = NULL; |
43 for (i in 1:num_rows) { | 44 for (i in 1:num_rows) { |
44 # Get the day of the year from the current row | 45 # Get the day of the year from the current row |
66 } | 67 } |
67 | 68 |
68 extract_date_interval_rows = function(df, start_date, end_date) { | 69 extract_date_interval_rows = function(df, start_date, end_date) { |
69 date_interval_rows = df[df$DATE >= start_date & df$DATE <= end_date]; | 70 date_interval_rows = df[df$DATE >= start_date & df$DATE <= end_date]; |
70 return(date_interval_rows); | 71 return(date_interval_rows); |
72 } | |
73 | |
74 from_30_year_normals = function(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy) { | |
75 # The data we want is fully contained within the 30 year normals data. | |
76 first_norm_row = which(norm_data_frame$DOY==start_date_doy); | |
77 last_norm_row = which(norm_data_frame$DOY==end_date_doy); | |
78 norm_data_frame_rows = last_norm_row - first_norm_row; | |
79 j = 0; | |
80 for (i in first_norm_row:last_norm_row) { | |
81 j = j + 1; | |
82 temperature_data_frame[j,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); | |
83 } | |
84 return (temperature_data_frame); | |
71 } | 85 } |
72 | 86 |
73 get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) { | 87 get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) { |
74 if (!is.null(life_stage_nymph)) { | 88 if (!is.null(life_stage_nymph)) { |
75 lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph); | 89 lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph); |
253 } else { | 267 } else { |
254 return(365); | 268 return(365); |
255 } | 269 } |
256 } | 270 } |
257 | 271 |
258 get_x_axis_ticks_and_labels = function(temperature_data_frame, num_rows, start_doy_ytd, end_doy_ytd) { | 272 get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, restricted_date_interval) { |
259 # Keep track of the years to see if spanning years. | 273 # Generate a list of ticks and labels for plotting the |
274 # x axis. There are several scenarios that affect this. | |
275 # 1. If restricted_date_interval is TRUE: | |
276 # a. | |
277 if (prepend_end_doy_norm > 0) { | |
278 prepend_end_norm_row = which(temperature_data_frame$DOY==prepend_end_doy_norm); | |
279 } else { | |
280 prepend_end_norm_row = 0; | |
281 } | |
282 if (append_start_doy_norm > 0) { | |
283 append_start_norm_row = which(temperature_data_frame$DOY==append_start_doy_norm); | |
284 } else { | |
285 append_start_norm_row = 0; | |
286 } | |
287 num_rows = dim(temperature_data_frame)[1]; | |
260 month_labels = list(); | 288 month_labels = list(); |
261 ticks = list(); | 289 ticks = list(); |
262 current_month_label = NULL; | 290 current_month_label = NULL; |
263 last_tick = 0; | 291 last_tick = 0; |
264 for (i in 1:num_rows) { | 292 for (i in 1:num_rows) { |
265 if (start_doy_ytd > 1 & i==start_doy_ytd-1) { | 293 # We're plotting the entire year, so ticks will |
294 # occur on Sundays and the first of each month. | |
295 if (i == prepend_end_norm_row) { | |
266 # Add a tick for the end of the 30 year normnals data | 296 # Add a tick for the end of the 30 year normnals data |
267 # that was prepended to the year-to-date data. | 297 # that was prepended to the year-to-date data. |
268 tick_index = get_tick_index(i, last_tick, ticks, month_labels) | 298 tick_index = get_tick_index(i, last_tick, ticks, month_labels) |
269 ticks[tick_index] = i; | 299 ticks[tick_index] = i; |
270 month_labels[tick_index] = "End prepended 30 year normals"; | 300 month_labels[tick_index] = "End prepended 30 year normals"; |
271 last_tick = i; | 301 last_tick = i; |
272 } else if (end_doy_ytd > 0 & i==end_doy_ytd+1) { | 302 } else if (i == append_start_doy_norm) { |
273 # Add a tick for the start of the 30 year normnals data | 303 # Add a tick for the start of the 30 year normnals data |
274 # that was appended to the year-to-date data. | 304 # that was appended to the year-to-date data. |
275 tick_index = get_tick_index(i, last_tick, ticks, month_labels) | 305 tick_index = get_tick_index(i, last_tick, ticks, month_labels) |
276 ticks[tick_index] = i; | 306 ticks[tick_index] = i; |
277 month_labels[tick_index] = "Start appended 30 year normals"; | 307 month_labels[tick_index] = "Start appended 30 year normals"; |
289 # Get the month label. | 319 # Get the month label. |
290 items = strsplit(date, "-")[[1]]; | 320 items = strsplit(date, "-")[[1]]; |
291 month = items[2]; | 321 month = items[2]; |
292 month_label = month.abb[as.integer(month)]; | 322 month_label = month.abb[as.integer(month)]; |
293 if (!identical(current_month_label, month_label)) { | 323 if (!identical(current_month_label, month_label)) { |
294 # Add an x-axis tick for the month. | 324 # Add a tick for the month. |
295 tick_index = get_tick_index(i, last_tick, ticks, month_labels) | 325 tick_index = get_tick_index(i, last_tick, ticks, month_labels) |
296 ticks[tick_index] = i; | 326 ticks[tick_index] = i; |
297 month_labels[tick_index] = month_label; | 327 month_labels[tick_index] = month_label; |
298 current_month_label = month_label; | 328 current_month_label = month_label; |
299 last_tick = i; | 329 last_tick = i; |
300 } | 330 } |
301 tick_index = get_tick_index(i, last_tick, ticks, month_labels) | 331 tick_index = get_tick_index(i, last_tick, ticks, month_labels) |
302 if (!is.null(tick_index)) { | 332 if (!is.null(tick_index)) { |
303 # Get the day. | 333 if (restricted_date_interval) { |
304 day = weekdays(as.Date(date)); | 334 # Add a tick for every day. |
305 if (day=="Sunday") { | |
306 # Add an x-axis tick if we're on a Sunday. | |
307 ticks[tick_index] = i; | 335 ticks[tick_index] = i; |
308 # Add a blank month label so it is not displayed. | 336 # Add a blank month label so it is not displayed. |
309 month_labels[tick_index] = ""; | 337 month_labels[tick_index] = ""; |
310 last_tick = i; | 338 last_tick = i; |
339 } else { | |
340 # Get the day. | |
341 day = weekdays(as.Date(date)); | |
342 if (day=="Sunday") { | |
343 # Add a tick if we're on a Sunday. | |
344 ticks[tick_index] = i; | |
345 # Add a blank month label so it is not displayed. | |
346 month_labels[tick_index] = ""; | |
347 last_tick = i; | |
348 } | |
311 } | 349 } |
312 } | 350 } |
313 } | 351 } |
314 } | 352 } |
315 return(list(ticks, month_labels)); | 353 return(list(ticks, month_labels)); |
358 mortality.probability = temperature * 0.0008 + 0.03; | 396 mortality.probability = temperature * 0.0008 + 0.03; |
359 } | 397 } |
360 return(mortality.probability); | 398 return(mortality.probability); |
361 } | 399 } |
362 | 400 |
363 parse_input_data = function(input_ytd, input_norm, num_days, location, start_date, end_date) { | 401 parse_input_data = function(input_ytd, input_norm, location, start_date, end_date) { |
402 # The end DOY for norm data prepended to ytd data. | |
403 prepend_end_doy_norm = 0; | |
404 # The start DOY for norm data appended to ytd data. | |
405 append_start_doy_norm = 0; | |
364 if (is.null(input_ytd)) { | 406 if (is.null(input_ytd)) { |
365 # We're analysing only the 30 year normals data, so create an empty | 407 # We're processing only the 30 year normals data. |
408 processing_year_to_date_data = FALSE; | |
409 } else { | |
410 processing_year_to_date_data = TRUE; | |
411 } | |
412 if (is.null(start_date) && is.null(end_date)) { | |
413 # We're processing the entire year, possibly merging | |
414 # data from input_norm with data from input_ytd. | |
415 restricted_date_interval = FALSE; | |
416 } else { | |
417 restricted_date_interval = TRUE; | |
418 # Get the DOY for start_date and end_date. | |
419 start_date_doy = strftime(start_date, format="%j"); | |
420 end_date_doy = strftime(end_date, format="%j"); | |
421 } | |
422 # Read the input_norm temperature datafile into a data frame. | |
423 # The input_norm data has the following 10 columns: | |
424 # STATIONID, LATITUDE, LONGITUDE, ELEV_M, NAME, ST, MMDD, DOY, TMIN, TMAX | |
425 norm_data_frame = read.csv(file=input_norm, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=","); | |
426 # Set the norm_data_frame column names for access. | |
427 colnames(norm_data_frame) = c("STATIONID", "LATITUDE","LONGITUDE", "ELEV_M", "NAME", "ST", "MMDD", "DOY", "TMIN", "TMAX"); | |
428 if (processing_year_to_date_data) { | |
429 # Read the input_ytd temperature data file into a data frame. | |
430 # The input_ytd data has the following 6 columns: | |
431 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX | |
432 temperature_data_frame = read.csv(file=input_ytd, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=","); | |
433 # Set the temperature_data_frame column names for access. | |
434 colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX"); | |
435 if (restricted_date_interval) { | |
436 # We're plotting a date interval. | |
437 start_date_ytd_row = which(temperature_data_frame$DATE==start_date); | |
438 if (start_date_ytd_row > 0) { | |
439 # The start date is contained within the input_ytd data. | |
440 start_doy_ytd = as.integer(temperature_data_frame$DOY[start_date_ytd_row]); | |
441 } else { | |
442 # The start date is contained within the input_norm data. | |
443 start_date_norm_row = which(norm_data_frame$DATE==start_date); | |
444 } | |
445 end_date_ytd_row = which(temperature_data_frame$DATE==end_date); | |
446 if (end_date_ytd_row > 0) { | |
447 # The end date is contained within the input_ytd data. | |
448 end_doy_ytd = as.integer(temperature_data_frame$DOY[end_date_ytd_row]); | |
449 } | |
450 date_str = start_date; | |
451 } else { | |
452 # We're plotting an entire year. | |
453 # Get the number of days contained in temperature_data_frame. | |
454 num_rows = dim(temperature_data_frame)[1]; | |
455 # Get the start date and end date from temperature_data_frame. | |
456 start_date_ytd_row = 1; | |
457 start_date = temperature_data_frame$DATE[1]; | |
458 end_date_ytd_row = num_rows; | |
459 end_date = temperature_data_frame$DATE[num_rows]; | |
460 date_str = format(start_date); | |
461 # Save the first DOY to later check if start_date is Jan 1. | |
462 start_doy_ytd = as.integer(temperature_data_frame$DOY[1]); | |
463 end_doy_ytd = as.integer(temperature_data_frame$DOY[num_rows]); | |
464 } | |
465 # Extract the year from the start date. | |
466 date_str_items = strsplit(date_str, "-")[[1]]; | |
467 year = date_str_items[1]; | |
468 } else { | |
469 # We're processing only the 30 year normals data, so create an empty | |
366 # data frame for containing temperature data after it is converted | 470 # data frame for containing temperature data after it is converted |
367 # from the 30 year normals format to the year-to-date format. | 471 # from the 30 year normals format to the year-to-date format. |
368 temperature_data_frame = data.frame(matrix(ncol=6, nrow=0)); | 472 temperature_data_frame = data.frame(matrix(ncol=6, nrow=0)); |
369 colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX"); | 473 colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX"); |
370 # Base all dates on the current date since 30 year | 474 if (restricted_date_interval) { |
371 # normals data does not include any dates. | 475 # We're plotting a date interval. |
372 year = format(Sys.Date(), "%Y"); | 476 # Extract the year, month and day from the start date. |
373 if (is.null(start_date) && is.null(end_date)) { | |
374 start_date = paste(year, "01", "01", sep="-"); | |
375 end_date = paste(year, "12", "31", sep="-"); | |
376 } else { | |
377 # Extract the month and day from the start date. | |
378 start_date_str = format(start_date); | 477 start_date_str = format(start_date); |
379 start_date_str_items = strsplit(start_date_str, "-")[[1]]; | 478 start_date_str_items = strsplit(start_date_str, "-")[[1]]; |
479 year = start_date_str_items[1]; | |
380 start_date_month = start_date_str_items[2]; | 480 start_date_month = start_date_str_items[2]; |
381 start_date_day = start_date_str_items[3]; | 481 start_date_day = start_date_str_items[3]; |
382 start_date = paste(year, start_date_month, start_date_day, sep="-"); | 482 start_date = paste(year, start_date_month, start_date_day, sep="-"); |
383 # Extract the month and day from the end date. | 483 # Extract the month and day from the end date. |
384 end_date_str = format(start_date); | 484 end_date_str = format(start_date); |
385 end_date_str_items = strsplit(end_date_str, "-")[[1]]; | 485 end_date_str_items = strsplit(end_date_str, "-")[[1]]; |
386 end_date_month = end_date_str_items[2]; | 486 end_date_month = end_date_str_items[2]; |
387 end_date_day = end_date_str_items[3]; | 487 end_date_day = end_date_str_items[3]; |
388 end_date = paste(year, start_date_month, start_date_day, sep="-"); | 488 end_date = paste(year, start_date_month, start_date_day, sep="-"); |
389 } | |
390 # Set invalid start and end DOY. | |
391 start_doy_ytd = 0; | |
392 end_doy_ytd = 0; | |
393 } else { | |
394 # Read the input_ytd temperature data file into a data frame. | |
395 # The input_ytd data has the following 6 columns: | |
396 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX | |
397 temperature_data_frame = read.csv(file=input_ytd, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=","); | |
398 # Set the temperature_data_frame column names for access. | |
399 colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX"); | |
400 if (is.null(start_date) && is.null(end_date)) { | |
401 # Get the start date. | |
402 start_date = temperature_data_frame$DATE[1]; | |
403 end_date = temperature_data_frame$DATE[num_days]; | |
404 } else { | 489 } else { |
405 # Extract the custom date interval from temperature_data_frame. | 490 # We're plotting an entire year. |
406 temperature_data_frame = extract_date_interval_rows(temperature_data_frame, start_date, end_date); | 491 # Base all dates on the current date since 30 year |
407 } | 492 # normals data does not include any dates. |
408 # Extract the year from the start date. | 493 year = format(Sys.Date(), "%Y"); |
409 date_str = format(start_date); | 494 start_date = paste(year, "01", "01", sep="-"); |
410 date_str_items = strsplit(date_str, "-")[[1]]; | 495 end_date = paste(year, "12", "31", sep="-"); |
411 year = date_str_items[1]; | 496 } |
412 # Save the first DOY to later check if start_date is Jan 1. | |
413 start_doy_ytd = as.integer(temperature_data_frame$DOY[1]); | |
414 end_doy_ytd = as.integer(temperature_data_frame$DOY[num_days]); | |
415 } | 497 } |
416 # See if we're in a leap year. | 498 # See if we're in a leap year. |
417 is_leap_year = is_leap_year(start_date); | 499 is_leap_year = is_leap_year(start_date); |
418 # Read the input_norm temperature datafile into a data frame. | |
419 # The input_norm data has the following 10 columns: | |
420 # STATIONID, LATITUDE, LONGITUDE, ELEV_M, NAME, ST, MMDD, DOY, TMIN, TMAX | |
421 norm_data_frame = read.csv(file=input_norm, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=","); | |
422 # Set the norm_data_frame column names for access. | |
423 colnames(norm_data_frame) = c("STATIONID", "LATITUDE","LONGITUDE", "ELEV_M", "NAME", "ST", "MMDD", "DOY", "TMIN", "TMAX"); | |
424 # All normals data includes Feb 29 which is row 60 in | 500 # All normals data includes Feb 29 which is row 60 in |
425 # the data, so delete that row if we're not in a leap year. | 501 # the data, so delete that row if we're not in a leap year. |
426 if (!is_leap_year) { | 502 if (!is_leap_year) { |
427 norm_data_frame = norm_data_frame[-c(60),]; | 503 norm_data_frame = norm_data_frame[-c(60),]; |
428 } | 504 } |
429 if (is.null(start_date) && is.null(end_date)) { | 505 # Set the location to be the station name if the user elected not to enter it. |
430 # Get the number of days in the year. | 506 if (is.null(location) | length(location) == 0) { |
431 total_days = get_total_days(is_leap_year); | 507 location = norm_data_frame$NAME[1]; |
508 } | |
509 if (processing_year_to_date_data) { | |
510 # Merge the year-to-date data with the 30 year normals data. | |
511 if (restricted_date_interval) { | |
512 # The values of start_date_ytd_row and end_date_ytd_row were set above. | |
513 if (start_date_ytd_row > 0 & end_date_ytd_row > 0) { | |
514 # The date interval is contained within the input_ytd | |
515 # data, so we don't need to merge the 30 year normals data. | |
516 temperature_data_frame = temperature_data_frame[start_date_ytd_row:end_date_ytd_row,]; | |
517 } else if (start_date_ytd_row == 0 & end_date_ytd_row > 0) { | |
518 # The date interval starts in input_norm and ends in | |
519 # input_ytd, so prepend appropriate rows from input_norm | |
520 # to appropriate rows from input_ytd. | |
521 first_norm_row = which(norm_data_frame$DOY==start_date_doy); | |
522 # Get the first DOY from temperature_data_frame. | |
523 first_ytd_doy = temperature_data_frame$DOY[1]; | |
524 # End DOY of input_norm data prepended to input_ytd. | |
525 prepend_end_doy_norm = first_ytd_doy - 1; | |
526 # Get the number of rows for the restricted date interval | |
527 # that are contained in temperature_data_frame. | |
528 temperature_data_frame_rows = end_date_ytd_row; | |
529 # Get the last row needed from the 30 year normals data. | |
530 last_norm_row = which(norm_data_frame$DOY==prepend_end_doy_norm); | |
531 # Get the number of rows for the restricted date interval | |
532 # that are contained in norm_data_frame. | |
533 norm_data_frame_rows = last_norm_row - first_norm_row; | |
534 # Create a temporary data frame to contain the 30 year normals | |
535 # data from the start date to the date immediately prior to the | |
536 # first row of the input_ytd data. | |
537 tmp_norm_data_frame = data.frame(matrix(ncol=6, nrow=temperature_data_frame_rows+norm_data_frame_rows)); | |
538 for (i in first_norm_row:last_norm_row) { | |
539 # Populate the temp_data_frame row with | |
540 # values from norm_data_frame. | |
541 tmp_norm_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); | |
542 } | |
543 # Create a second temporary data frame containing the | |
544 # appropriate rows from temperature_data_frame. | |
545 tmp_temperature_data_frame = temperature_data_frame[1:first_norm_row-1,]; | |
546 # Merge the 2 temporary data frames. | |
547 temperature_data_frame = rbind(tmp_norm_data_frame, tmp_temperature_data_frame); | |
548 } else if (start_date_ytd_row > 0 & end_date_ytd_row == 0) { | |
549 # The date interval starts in input_ytd and ends in input_norm, | |
550 # so append appropriate rows from input_norm to appropriate rows | |
551 # from input_ytd. | |
552 num_rows = dim(temperature_data_frame)[1]; | |
553 # Get the number of rows for the restricted date interval | |
554 # that are contained in temperature_data_frame. | |
555 temperature_data_frame_rows = num_rows - start_date_ytd_row | |
556 # Get the DOY of the last row in the input_ytd data. | |
557 last_ytd_doy = temperature_data_frame$DOY[num_rows]; | |
558 # Get the DOYs for the first and last rows from norm_data_frame | |
559 # that will be appended to temperature_data_frame. | |
560 append_start_doy_norm = last_ytd_doy + 1; | |
561 # Get the row from norm_data_frame containing first_norm_doy. | |
562 first_norm_row = which(norm_data_frame$DOY == append_start_doy_norm); | |
563 # Get the row from norm_data_frame containing end_date_doy. | |
564 last_norm_row = which(norm_data_frame$DOY == end_date_doy); | |
565 # Get the number of rows for the restricted date interval | |
566 # that are contained in norm_data_frame. | |
567 norm_data_frame_rows = last_norm_row - first_norm_row; | |
568 # Create a temporary data frame to contain the data | |
569 # taken from both temperateu_data_frame and norm_data_frame | |
570 # for the date interval. | |
571 tmp_data_frame = data.frame(matrix(ncol=6, nrow=temperature_data_frame_rows+norm_data_frame_rows)); | |
572 # Populate tmp_data_frame with the appropriate rows from temperature_data_frame. | |
573 tmp_data_frame[temperature_data_frame_rows,] = temperature_data_frame[start_date_ytd_row:temperature_data_frame_rows,]; | |
574 # Apppend the appropriate rows from norm_data_frame to tmp_data_frame. | |
575 for (i in first_norm_row:last_norm_row) { | |
576 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); | |
577 } | |
578 temperature_data_frame = tmp_data_frame[,]; | |
579 } else if (start_date_ytd_row == 0 & end_date_ytd_row == 0) { | |
580 # The date interval is contained witin input_norm. | |
581 temperature_data_frame = from_30_year_normals(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy); | |
582 } | |
583 } else { | |
584 # We're plotting an entire year. | |
585 if (start_doy_ytd > 1) { | |
586 # The input_ytd data starts after Jan 1, so prepend | |
587 # appropriate rows from input_norm to temperature_data_frame. | |
588 prepend_end_doy_norm = start_doy_ytd - 1; | |
589 first_norm_row = 1; | |
590 last_norm_row = which(norm_data_frame$DOY == prepend_end_doy_norm); | |
591 # Create a temporary data frame to contain the input_norm data | |
592 # from Jan 1 to the date immediately prior to start_date. | |
593 tmp_data_frame = temperature_data_frame[FALSE,]; | |
594 # Populate tmp_data_frame with appropriate rows from norm_data_frame. | |
595 for (i in first_norm_row:last_norm_row) { | |
596 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); | |
597 } | |
598 # Merge the temporary data frame with temperature_data_frame. | |
599 temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame); | |
600 } | |
601 # Set the value of total_days. | |
602 total_days = get_total_days(is_leap_year); | |
603 if (end_doy_ytd < total_days) { | |
604 # Define the next row for the year-to-date data from the 30 year normals data. | |
605 append_start_doy_norm = end_doy_ytd + 1; | |
606 first_norm_row = which(norm_data_frame$DOY == append_start_doy_norm); | |
607 last_norm_row = which(norm_data_frame$DOY == total_days); | |
608 # Append the 30 year normals data to the year-to-date data. | |
609 for (i in first_norm_row:last_norm_row) { | |
610 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); | |
611 } | |
612 } | |
613 } | |
432 } else { | 614 } else { |
433 # Extract the custom date interval from norm_data_frame. | 615 # We're processing only the 30 year normals data. |
434 norm_data_frame = extract_date_interval_rows(norm_data_frame, start_date, end_date); | 616 if (restricted_date_interval) { |
435 # Use the pre-determined num_days for total_days. | 617 # Populate temperature_data_frame from norm_data_frame. |
436 total_days = num_days | 618 temperature_data_frame = from_30_year_normals(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy); |
437 } | 619 } else { |
438 # Set the location to be the station name if the user elected no to enter it. | 620 total_days = get_total_days(is_leap_year); |
439 if (is.null(location) | length(location)==0) { | 621 for (i in 1:total_days) { |
440 location = norm_data_frame$NAME[1]; | 622 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); |
441 } | 623 } |
442 if (is.null(input_ytd)) { | |
443 # Convert the 30 year normals data to the year-to-date format. | |
444 for (i in 1:total_days) { | |
445 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); | |
446 } | |
447 } else { | |
448 # Merge the year-to-date data with the 30 year normals data. | |
449 if (start_doy_ytd > 1) { | |
450 # The year-to-date data starts after Jan 1, so create a | |
451 # temporary data frame to contain the 30 year normals data | |
452 # from Jan 1 to the date immediately prior to start_date. | |
453 tmp_data_frame = temperature_data_frame[FALSE,]; | |
454 for (i in 1:start_doy_ytd-1) { | |
455 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); | |
456 } | |
457 # Next merge the temporary data frame with the year-to-date data frame. | |
458 temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame); | |
459 } | |
460 # Define the next row for the year-to-date data from the 30 year normals data. | |
461 first_normals_append_row = end_doy_ytd + 1; | |
462 # Append the 30 year normals data to the year-to-date data. | |
463 for (i in first_normals_append_row:total_days) { | |
464 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); | |
465 } | 624 } |
466 } | 625 } |
467 # Add a column containing the daylight length for each day. | 626 # Add a column containing the daylight length for each day. |
468 temperature_data_frame = add_daylight_length(temperature_data_frame, total_days); | 627 temperature_data_frame = add_daylight_length(temperature_data_frame); |
469 return(list(temperature_data_frame, start_date, end_date, start_doy_ytd, end_doy_ytd, is_leap_year, total_days, location)); | 628 return(list(temperature_data_frame, start_date, end_date, prepend_end_doy_norm, append_start_doy_norm, is_leap_year, location)); |
470 } | 629 } |
471 | 630 |
472 render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval, | 631 render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval, |
473 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL, | 632 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL, |
474 life_stages_adult=NULL, life_stages_nymph=NULL) { | 633 life_stages_adult=NULL, life_stages_nymph=NULL) { |
565 stop_err(msg); | 724 stop_err(msg); |
566 } | 725 } |
567 return(valid_date); | 726 return(valid_date); |
568 } | 727 } |
569 | 728 |
729 # Parse the inputs. | |
730 data_list = parse_input_data(opt$input_ytd, opt$input_norm, opt$location, opt$start_date, opt$end_date); | |
731 temperature_data_frame = data_list[[1]]; | |
732 # Information needed for plots, some of these values are | |
733 # being reset here since in some case they were set above. | |
734 start_date = data_list[[2]]; | |
735 end_date = data_list[[3]]; | |
736 prepend_end_doy_norm = data_list[[4]]; | |
737 append_start_doy_norm = data_list[[5]]; | |
738 is_leap_year = data_list[[6]]; | |
739 location = data_list[[7]]; | |
740 | |
741 if (is.null(input_ytd)) { | |
742 processing_year_to_date_data = FALSE; | |
743 } else { | |
744 processing_year_to_date_data = TRUE; | |
745 } | |
570 # Determine if we're plotting generations separately. | 746 # Determine if we're plotting generations separately. |
571 if (opt$plot_generations_separately=="yes") { | 747 if (opt$plot_generations_separately=="yes") { |
572 plot_generations_separately = TRUE; | 748 plot_generations_separately = TRUE; |
573 } else { | 749 } else { |
574 plot_generations_separately = FALSE; | 750 plot_generations_separately = FALSE; |
575 } | 751 } |
576 # If opt$start_date and opt$end_date have values, then the | |
577 # user chose to plot a custom date interval rather than the | |
578 # entire contents of the input temperature data, so we'll | |
579 # calaculate the number of days in the custom date interval | |
580 # rather than using the number of rows in the input temperature | |
581 # data. | |
582 if (is.null(opt$start_date) && is.null(opt$end_date)) { | 752 if (is.null(opt$start_date) && is.null(opt$end_date)) { |
583 # Use the default number of rows in the input temperature | 753 # We're plotting an entire year. |
584 # data as the number of days. | 754 restricted_date_interval = FALSE; |
585 num_days = opt$num_days_ytd; | 755 # Display the total number of days in the Galaxy history item blurb. |
756 if (processing_year_to_date_data) { | |
757 cat("Number of days year-to-date: ", opt$num_days_ytd, "\n"); | |
758 } else { | |
759 if (is_leap_year) { | |
760 num_days = 366; | |
761 } else { | |
762 num_days = 365; | |
763 } | |
764 cat("Number of days in year: ", num_days, "\n"); | |
765 } | |
586 } else { | 766 } else { |
587 # FIXME: currently custom date fields are free text, but | 767 # FIXME: currently custom date fields are free text, but |
588 # Galaxy should soon include support for a date selector | 768 # Galaxy should soon include support for a date selector |
589 # at which point this tool should be enhanced to use it. | 769 # at which point this tool should be enhanced to use it. |
590 # Validate start_date. | 770 # Validate start_date. |
591 start_date = validate_date(opt$start_date); | 771 restricted_date_interval = TRUE; |
772 # Calaculate the number of days in the date interval rather | |
773 # than using the number of rows in the input temperature data. | |
774 start_date = validate_date(opt$start_date); | |
592 # Validate end_date. | 775 # Validate end_date. |
593 end_date = validate_date(opt$end_date); | 776 end_date = validate_date(opt$end_date); |
594 if (start_date >= end_date) { | 777 if (start_date >= end_date) { |
595 stop_err("The start date must be before the end date for custom date intervals."); | 778 stop_err("The start date must be between 1 and 50 days before the end date when setting date intervals for plots."); |
596 } | 779 } |
597 # Calculate the number of days in the custom date interval. | 780 # Calculate the number of days in the date interval. |
598 num_days = difftime(start_date, end_date, units=c("days")); | 781 num_days = difftime(start_date, end_date, units=c("days")); |
599 if (num_days > 50) { | 782 if (num_days > 50) { |
600 # We need to restrict custom date intervals since | 783 # We need to restrict date intervals since |
601 # plots render tick marks for each day. | 784 # plots render tick marks for each day. |
602 stop_err("Custom date intervals cannot exceed 50 days."); | 785 stop_err("Date intervals for plotting cannot exceed 50 days."); |
603 } | 786 } |
604 } | 787 # Display the total number of days in the Galaxy history item blurb. |
605 # Display the total number of days in the Galaxy history item blurb. | 788 cat("Number of days in date interval: ", num_days, "\n"); |
606 cat("Year-to-date number of days: ", num_days, "\n"); | 789 } |
607 # Parse the inputs. | |
608 data_list = parse_input_data(opt$input_ytd, opt$input_norm, num_days, opt$location, opt$start_date, opt$end_date); | |
609 temperature_data_frame = data_list[[1]]; | |
610 # Information needed for plots, some of these values are | |
611 # being reset here since in some case they were set above. | |
612 start_date = data_list[[2]]; | |
613 end_date = data_list[[3]]; | |
614 start_doy_ytd = data_list[[4]]; | |
615 end_doy_ytd = data_list[[5]]; | |
616 is_leap_year = data_list[[6]]; | |
617 total_days = data_list[[7]]; | |
618 total_days_vector = c(1:total_days); | |
619 location = data_list[[8]]; | |
620 | 790 |
621 # Create copies of the temperature data for generations P, F1 and F2 if we're plotting generations separately. | 791 # Create copies of the temperature data for generations P, F1 and F2 if we're plotting generations separately. |
622 if (plot_generations_separately) { | 792 if (plot_generations_separately) { |
623 temperature_data_frame_P = data.frame(temperature_data_frame); | 793 temperature_data_frame_P = data.frame(temperature_data_frame); |
624 temperature_data_frame_F1 = data.frame(temperature_data_frame); | 794 temperature_data_frame_F1 = data.frame(temperature_data_frame); |
625 temperature_data_frame_F2 = data.frame(temperature_data_frame); | 795 temperature_data_frame_F2 = data.frame(temperature_data_frame); |
626 } | 796 } |
627 | 797 |
628 # Get the ticks date labels for plots. | 798 # Get the ticks date labels for plots. |
629 ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, total_days, start_doy_ytd, end_doy_ytd); | 799 ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, restricted_date_interval); |
630 ticks = c(unlist(ticks_and_labels[1])); | 800 ticks = c(unlist(ticks_and_labels[1])); |
631 date_labels = c(unlist(ticks_and_labels[2])); | 801 date_labels = c(unlist(ticks_and_labels[2])); |
632 # All latitude values are the same, so get the value for plots from the first row. | 802 # All latitude values are the same, so get the value for plots from the first row. |
633 latitude = temperature_data_frame$LATITUDE[1]; | 803 latitude = temperature_data_frame$LATITUDE[1]; |
634 | 804 |
690 process_total_adults = TRUE; | 860 process_total_adults = TRUE; |
691 } | 861 } |
692 } | 862 } |
693 } | 863 } |
694 # Initialize matrices. | 864 # Initialize matrices. |
865 total_days = dim(temperature_data_frame)[1]; | |
695 if (process_eggs) { | 866 if (process_eggs) { |
696 Eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications); | 867 Eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications); |
697 } | 868 } |
698 if (process_young_nymphs | process_total_nymphs) { | 869 if (process_young_nymphs | process_total_nymphs) { |
699 YoungNymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications); | 870 YoungNymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications); |
1534 # Save the analyzed data for generation F2. | 1705 # Save the analyzed data for generation F2. |
1535 file_path = paste("output_data_dir", "03_generation_F2.csv", sep="/"); | 1706 file_path = paste("output_data_dir", "03_generation_F2.csv", sep="/"); |
1536 write.csv(temperature_data_frame_F2, file=file_path, row.names=F); | 1707 write.csv(temperature_data_frame_F2, file=file_path, row.names=F); |
1537 } | 1708 } |
1538 | 1709 |
1710 total_days_vector = c(1:dim(temperature_data_frame)[1]); | |
1539 if (plot_generations_separately) { | 1711 if (plot_generations_separately) { |
1540 for (life_stage in life_stages) { | 1712 for (life_stage in life_stages) { |
1541 if (life_stage == "Egg") { | 1713 if (life_stage == "Egg") { |
1542 # Start PDF device driver. | 1714 # Start PDF device driver. |
1543 dev.new(width=20, height=30); | 1715 dev.new(width=20, height=30); |