comparison insect_phenology_model.R @ 124:534658644efe draft

Uploaded
author greg
date Fri, 01 Jun 2018 08:00:53 -0400
parents e69e30d853fb
children 24f389a2dd93
comparison
equal deleted inserted replaced
123:e69e30d853fb 124:534658644efe
77 last_norm_row = which(norm_data_frame$DOY==end_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; 78 norm_data_frame_rows = last_norm_row - first_norm_row;
79 j = 0; 79 j = 0;
80 for (i in first_norm_row:last_norm_row) { 80 for (i in first_norm_row:last_norm_row) {
81 j = j + 1; 81 j = j + 1;
82 temperature_data_frame[j,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); 82 temperature_data_frame[j,] = get_next_normals_row(norm_data_frame, year, i);
83 } 83 }
84 return (temperature_data_frame); 84 return (temperature_data_frame);
85 } 85 }
86 86
87 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) {
142 # F2 standard error. 142 # F2 standard error.
143 f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications); 143 f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications);
144 return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se)) 144 return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se))
145 } 145 }
146 146
147 get_next_normals_row = function(norm_data_frame, year, is_leap_year, index) { 147 get_next_normals_row = function(norm_data_frame, year, index) {
148 # Return the next 30 year normals row formatted 148 # Return the next 30 year normals row formatted
149 # appropriately for the year-to-date data. 149 # appropriately for the year-to-date data.
150 latitude = norm_data_frame[index,"LATITUDE"][1]; 150 latitude = norm_data_frame[index,"LATITUDE"][1];
151 longitude = norm_data_frame[index,"LONGITUDE"][1]; 151 longitude = norm_data_frame[index,"LONGITUDE"][1];
152 # Format the date. 152 # Format the date.
153 mmdd = norm_data_frame[index,"MMDD"][1]; 153 mmdd = norm_data_frame[index,"MMDD"][1];
154 date_str = paste(year, mmdd, sep="-"); 154 date_str = paste(year, mmdd, sep="-");
155 doy = norm_data_frame[index,"DOY"][1]; 155 doy = norm_data_frame[index,"DOY"][1];
156 if (!is_leap_year) {
157 # Since all normals data includes Feb 29, we have to
158 # subtract 1 from DOY if we're not in a leap year since
159 # we removed the Feb 29 row from the data frame above.
160 doy = as.integer(doy) - 1;
161 }
162 tmin = norm_data_frame[index,"TMIN"][1]; 156 tmin = norm_data_frame[index,"TMIN"][1];
163 tmax = norm_data_frame[index,"TMAX"][1]; 157 tmax = norm_data_frame[index,"TMAX"][1];
164 return(list(latitude, longitude, date_str, doy, tmin, tmax)); 158 return(list(latitude, longitude, date_str, doy, tmin, tmax));
165 } 159 }
166 160
545 # first row of the input_ytd data. 539 # first row of the input_ytd data.
546 tmp_norm_data_frame = data.frame(matrix(ncol=6, nrow=temperature_data_frame_rows+norm_data_frame_rows)); 540 tmp_norm_data_frame = data.frame(matrix(ncol=6, nrow=temperature_data_frame_rows+norm_data_frame_rows));
547 for (i in first_norm_row:last_norm_row) { 541 for (i in first_norm_row:last_norm_row) {
548 # Populate the temp_data_frame row with 542 # Populate the temp_data_frame row with
549 # values from norm_data_frame. 543 # values from norm_data_frame.
550 tmp_norm_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); 544 tmp_norm_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i);
551 } 545 }
552 # Create a second temporary data frame containing the 546 # Create a second temporary data frame containing the
553 # appropriate rows from temperature_data_frame. 547 # appropriate rows from temperature_data_frame.
554 tmp_temperature_data_frame = temperature_data_frame[1:first_norm_row-1,]; 548 tmp_temperature_data_frame = temperature_data_frame[1:first_norm_row-1,];
555 # Merge the 2 temporary data frames. 549 # Merge the 2 temporary data frames.
580 tmp_data_frame = data.frame(matrix(ncol=6, nrow=temperature_data_frame_rows+norm_data_frame_rows)); 574 tmp_data_frame = data.frame(matrix(ncol=6, nrow=temperature_data_frame_rows+norm_data_frame_rows));
581 # Populate tmp_data_frame with the appropriate rows from temperature_data_frame. 575 # Populate tmp_data_frame with the appropriate rows from temperature_data_frame.
582 tmp_data_frame[temperature_data_frame_rows,] = temperature_data_frame[start_date_ytd_row:temperature_data_frame_rows,]; 576 tmp_data_frame[temperature_data_frame_rows,] = temperature_data_frame[start_date_ytd_row:temperature_data_frame_rows,];
583 # Apppend the appropriate rows from norm_data_frame to tmp_data_frame. 577 # Apppend the appropriate rows from norm_data_frame to tmp_data_frame.
584 for (i in first_norm_row:last_norm_row) { 578 for (i in first_norm_row:last_norm_row) {
585 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); 579 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i);
586 } 580 }
587 temperature_data_frame = tmp_data_frame[,]; 581 temperature_data_frame = tmp_data_frame[,];
588 } else if (start_date_ytd_row == 0 & end_date_ytd_row == 0) { 582 } else if (start_date_ytd_row == 0 & end_date_ytd_row == 0) {
589 # The date interval is contained witin input_norm. 583 # The date interval is contained witin input_norm.
590 temperature_data_frame = from_30_year_normals(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy); 584 temperature_data_frame = from_30_year_normals(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy);
593 # We're plotting an entire year. 587 # We're plotting an entire year.
594 if (start_doy_ytd > 1) { 588 if (start_doy_ytd > 1) {
595 # The input_ytd data starts after Jan 1, so prepend 589 # The input_ytd data starts after Jan 1, so prepend
596 # appropriate rows from input_norm to temperature_data_frame. 590 # appropriate rows from input_norm to temperature_data_frame.
597 prepend_end_doy_norm = start_doy_ytd - 1; 591 prepend_end_doy_norm = start_doy_ytd - 1;
598 first_norm_row = 1;
599 last_norm_row = which(norm_data_frame$DOY == prepend_end_doy_norm); 592 last_norm_row = which(norm_data_frame$DOY == prepend_end_doy_norm);
600 # Create a temporary data frame to contain the input_norm data 593 # Create a temporary data frame to contain the input_norm data
601 # from Jan 1 to the date immediately prior to start_date. 594 # from Jan 1 to the date immediately prior to start_date.
602 tmp_data_frame = temperature_data_frame[FALSE,]; 595 tmp_data_frame = temperature_data_frame[FALSE,];
603 # Populate tmp_data_frame with appropriate rows from norm_data_frame. 596 # Populate tmp_data_frame with appropriate rows from norm_data_frame.
604 for (i in first_norm_row:last_norm_row) { 597 for (i in 1:last_norm_row) {
605 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); 598 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i);
606 } 599 }
607 # Merge the temporary data frame with temperature_data_frame. 600 # Merge the temporary data frame with temperature_data_frame.
608 temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame); 601 temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame);
609 } 602 }
610 # Set the value of total_days. 603 # Set the value of total_days.
613 # Define the next row for the year-to-date data from the 30 year normals data. 606 # Define the next row for the year-to-date data from the 30 year normals data.
614 append_start_doy_norm = end_doy_ytd + 1; 607 append_start_doy_norm = end_doy_ytd + 1;
615 first_norm_row = which(norm_data_frame$DOY == append_start_doy_norm); 608 first_norm_row = which(norm_data_frame$DOY == append_start_doy_norm);
616 # Append the 30 year normals data to the year-to-date data. 609 # Append the 30 year normals data to the year-to-date data.
617 for (i in first_norm_row:total_days) { 610 for (i in first_norm_row:total_days) {
618 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); 611 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i);
619 } 612 }
620 } 613 }
621 } 614 }
622 } else { 615 } else {
623 # We're processing only the 30 year normals data. 616 # We're processing only the 30 year normals data.
625 # Populate temperature_data_frame from norm_data_frame. 618 # Populate temperature_data_frame from norm_data_frame.
626 temperature_data_frame = from_30_year_normals(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy); 619 temperature_data_frame = from_30_year_normals(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy);
627 } else { 620 } else {
628 total_days = get_total_days(is_leap_year); 621 total_days = get_total_days(is_leap_year);
629 for (i in 1:total_days) { 622 for (i in 1:total_days) {
630 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); 623 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i);
631 } 624 }
632 } 625 }
633 } 626 }
634 # Add a column containing the daylight length for each day. 627 # Add a column containing the daylight length for each day.
635 temperature_data_frame = add_daylight_length(temperature_data_frame); 628 temperature_data_frame = add_daylight_length(temperature_data_frame);
743 if (opt$plot_generations_separately=="yes") { 736 if (opt$plot_generations_separately=="yes") {
744 plot_generations_separately = TRUE; 737 plot_generations_separately = TRUE;
745 } else { 738 } else {
746 plot_generations_separately = FALSE; 739 plot_generations_separately = FALSE;
747 } 740 }
741 # Parse the inputs.
742 data_list = parse_input_data(opt$input_ytd, opt$input_norm, opt$location, opt$start_date, opt$end_date);
743 temperature_data_frame = data_list[[1]];
744 # Information needed for plots, some of these values are
745 # being reset here since in some case they were set above.
746 start_date = data_list[[2]];
747 end_date = data_list[[3]];
748 prepend_end_doy_norm = data_list[[4]];
749 append_start_doy_norm = data_list[[5]];
750 is_leap_year = data_list[[6]];
751 location = data_list[[7]];
752
748 if (is.null(opt$start_date) && is.null(opt$end_date)) { 753 if (is.null(opt$start_date) && is.null(opt$end_date)) {
749 # We're plotting an entire year. 754 # We're plotting an entire year.
750 date_interval = FALSE; 755 date_interval = FALSE;
751 # Display the total number of days in the Galaxy history item blurb. 756 # Display the total number of days in the Galaxy history item blurb.
752 if (processing_year_to_date_data) { 757 if (processing_year_to_date_data) {
781 stop_err("Date intervals for plotting cannot exceed 50 days."); 786 stop_err("Date intervals for plotting cannot exceed 50 days.");
782 } 787 }
783 # Display the total number of days in the Galaxy history item blurb. 788 # Display the total number of days in the Galaxy history item blurb.
784 cat("Number of days in date interval: ", num_days, "\n"); 789 cat("Number of days in date interval: ", num_days, "\n");
785 } 790 }
786 # Parse the inputs.
787 data_list = parse_input_data(opt$input_ytd, opt$input_norm, opt$location, opt$start_date, opt$end_date);
788 temperature_data_frame = data_list[[1]];
789 # Information needed for plots, some of these values are
790 # being reset here since in some case they were set above.
791 start_date = data_list[[2]];
792 end_date = data_list[[3]];
793 prepend_end_doy_norm = data_list[[4]];
794 append_start_doy_norm = data_list[[5]];
795 is_leap_year = data_list[[6]];
796 location = data_list[[7]];
797
798 # 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.
799 if (plot_generations_separately) { 792 if (plot_generations_separately) {
800 temperature_data_frame_P = data.frame(temperature_data_frame); 793 temperature_data_frame_P = data.frame(temperature_data_frame);
801 temperature_data_frame_F1 = data.frame(temperature_data_frame); 794 temperature_data_frame_F1 = data.frame(temperature_data_frame);
802 temperature_data_frame_F2 = data.frame(temperature_data_frame); 795 temperature_data_frame_F2 = data.frame(temperature_data_frame);