Mercurial > repos > greg > insect_phenology_model
comparison insect_phenology_model.R @ 56:829518206949 draft
Uploaded
author | greg |
---|---|
date | Mon, 05 Nov 2018 14:25:19 -0500 |
parents | 5bb1d76c29ca |
children | 20c1451d8869 |
comparison
equal
deleted
inserted
replaced
55:8c2b9eb9f9af | 56:829518206949 |
---|---|
34 | 34 |
35 add_daylight_length = function(temperature_data_frame) { | 35 add_daylight_length = function(temperature_data_frame) { |
36 # Return temperature_data_frame with an added column | 36 # Return temperature_data_frame with an added column |
37 # of daylight length (photoperiod profile). | 37 # of daylight length (photoperiod profile). |
38 num_rows = dim(temperature_data_frame)[1]; | 38 num_rows = dim(temperature_data_frame)[1]; |
39 # From Forsythe 1995. | |
40 p = 0.8333; | |
41 latitude = temperature_data_frame$LATITUDE[1]; | 39 latitude = temperature_data_frame$LATITUDE[1]; |
42 daylight_length_vector = NULL; | 40 daylight_length_vector = NULL; |
43 for (i in 1:num_rows) { | 41 for (i in 1:num_rows) { |
44 # Get the day of the year from the current row | 42 # Get the day of the year from the current row |
45 # of the temperature data for computation. | 43 # of the temperature data for computation. |
46 doy = temperature_data_frame$DOY[i]; | 44 doy = temperature_data_frame$DOY[i]; |
47 theta = 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (doy - 186))); | 45 daylight_length_vector[i] = get_daylen(doy, latitude); |
48 phi = asin(0.39795 * cos(theta)); | |
49 # Compute the length of daylight for the day of the year. | |
50 darkness_length = 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi))); | |
51 daylight_length_vector[i] = 24 - darkness_length; | |
52 } | 46 } |
53 # Append daylight_length_vector as a new column to temperature_data_frame. | 47 # Append daylight_length_vector as a new column to temperature_data_frame. |
54 temperature_data_frame = append_vector(temperature_data_frame, daylight_length_vector, "DAYLEN"); | 48 temperature_data_frame = append_vector(temperature_data_frame, daylight_length_vector, "DAYLEN"); |
55 return(temperature_data_frame); | 49 return(temperature_data_frame); |
56 } | 50 } |
76 for (i in first_norm_row:last_norm_row) { | 70 for (i in first_norm_row:last_norm_row) { |
77 j = j + 1; | 71 j = j + 1; |
78 tmp_data_frame[j,] = get_next_normals_row(norm_data_frame, year, i); | 72 tmp_data_frame[j,] = get_next_normals_row(norm_data_frame, year, i); |
79 } | 73 } |
80 return (tmp_data_frame); | 74 return (tmp_data_frame); |
75 } | |
76 | |
77 get_daylen = function(doy, latitude, p=0.8333) { | |
78 # The default value for p is from Forsythe 1995. | |
79 theta = 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (doy - 186))); | |
80 phi = asin(0.39795 * cos(theta)); | |
81 # Compute the length of daylight for the day of the year. | |
82 darkness_length = 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi))); | |
83 daylight_length = 24 - darkness_length; | |
84 return (daylight_length); | |
81 } | 85 } |
82 | 86 |
83 get_new_norm_data_frame = function(is_leap_year, input_norm=NULL, nrow=0) { | 87 get_new_norm_data_frame = function(is_leap_year, input_norm=NULL, nrow=0) { |
84 # The input_norm data has the following 10 columns: | 88 # The input_norm data has the following 10 columns: |
85 # STATIONID, LATITUDE, LONGITUDE, ELEV_M, NAME, ST, MMDD, DOY, TMIN, TMAX | 89 # STATIONID, LATITUDE, LONGITUDE, ELEV_M, NAME, ST, MMDD, DOY, TMIN, TMAX |
131 mmdd = norm_data_frame[index,"MMDD"][1]; | 135 mmdd = norm_data_frame[index,"MMDD"][1]; |
132 date_str = paste(year, mmdd, sep="-"); | 136 date_str = paste(year, mmdd, sep="-"); |
133 doy = norm_data_frame[index,"DOY"][1]; | 137 doy = norm_data_frame[index,"DOY"][1]; |
134 tmin = norm_data_frame[index,"TMIN"][1]; | 138 tmin = norm_data_frame[index,"TMIN"][1]; |
135 tmax = norm_data_frame[index,"TMAX"][1]; | 139 tmax = norm_data_frame[index,"TMAX"][1]; |
136 return(list(latitude, longitude, date_str, doy, tmin, tmax)); | 140 daylen = get_daylen(doy, latitude); |
137 } | 141 # Calculate the average temperature based on the |
138 | 142 # tmin and tmax values from the 30 year normals data. |
139 get_temperature_at_hour = function(latitude, temperature_data_frame, row) { | 143 temp.profile = get_temperature_at_hour(latitude, row, tmin=tmin, tmax=tmax, daylen=daylen); |
144 mean_temp = temp.profile[1]; | |
145 avg_temp = temp.profile[2]; | |
146 return(list(latitude, longitude, date_str, doy, tmin, tmax, daylen, avg_temp)); | |
147 } | |
148 | |
149 get_temperature_at_hour = function(latitude, row, temperature_data_frame=NULL, tmin=NULL, tmax=NULL, daylen=NULL) { | |
140 # Base development threshold for Brown Marmorated Stink Bug | 150 # Base development threshold for Brown Marmorated Stink Bug |
141 # insect phenology model. | 151 # insect phenology model. |
142 threshold = 14.17; | 152 threshold = 14.17; |
143 # Minimum temperature for current row. | 153 if (is.null(temperature_data_frame)) { |
144 curr_min_temp = temperature_data_frame$TMIN[row]; | 154 # The values of tmin and tmax cannot be NULL. |
145 # Maximum temperature for current row. | 155 curr_min_temp = tmin; |
146 curr_max_temp = temperature_data_frame$TMAX[row]; | 156 curr_max_temp = tmax; |
157 } else { | |
158 # Minimum temperature for current row. | |
159 curr_min_temp = temperature_data_frame$TMIN[row]; | |
160 # Maximum temperature for current row. | |
161 curr_max_temp = temperature_data_frame$TMAX[row]; | |
162 } | |
147 # Mean temperature for current row. | 163 # Mean temperature for current row. |
148 curr_mean_temp = 0.5 * (curr_min_temp + curr_max_temp); | 164 curr_mean_temp = 0.5 * (curr_min_temp + curr_max_temp); |
149 # Initialize degree day accumulation | 165 # Initialize degree day accumulation |
150 averages = 0; | 166 averages = 0; |
151 if (curr_max_temp < threshold) { | 167 if (curr_max_temp < threshold) { |
154 else { | 170 else { |
155 # Initialize hourly temperature. | 171 # Initialize hourly temperature. |
156 T = NULL; | 172 T = NULL; |
157 # Initialize degree hour vector. | 173 # Initialize degree hour vector. |
158 dh = NULL; | 174 dh = NULL; |
159 # Daylight length for current row. | 175 if (is.null(temperature_data_frame)) { |
160 y = temperature_data_frame$DAYLEN[row]; | 176 # The value of daylen cannot be NULL. |
177 y = daylen; | |
178 } else { | |
179 # Daylight length for current row. | |
180 y = temperature_data_frame$DAYLEN[row]; | |
181 } | |
161 # Darkness length. | 182 # Darkness length. |
162 z = 24 - y; | 183 z = 24 - y; |
163 # Lag coefficient. | 184 # Lag coefficient. |
164 a = 1.86; | 185 a = 1.86; |
165 # Darkness coefficient. | 186 # Darkness coefficient. |
579 num_days = 365; | 600 num_days = 365; |
580 } | 601 } |
581 cat("Number of days in year: ", num_days, "\n"); | 602 cat("Number of days in year: ", num_days, "\n"); |
582 } | 603 } |
583 | 604 |
584 # Create copies of the temperature data for generations P, F1 and F2 if we're plotting generations separately. | |
585 if (plot_generations_separately) { | |
586 temperature_data_frame_P = data.frame(temperature_data_frame); | |
587 temperature_data_frame_F1 = data.frame(temperature_data_frame); | |
588 temperature_data_frame_F2 = data.frame(temperature_data_frame); | |
589 } | |
590 | |
591 # Get the ticks date labels for plots. | 605 # Get the ticks date labels for plots. |
592 ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm); | 606 ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm); |
593 ticks = c(unlist(ticks_and_labels[1])); | 607 ticks = c(unlist(ticks_and_labels[1])); |
594 date_labels = c(unlist(ticks_and_labels[2])); | 608 date_labels = c(unlist(ticks_and_labels[2])); |
595 # All latitude values are the same, so get the value for plots from the first row. | 609 # All latitude values are the same, so get the value for plots from the first row. |
819 for (row in 1:total_days) { | 833 for (row in 1:total_days) { |
820 # Get the integer day of the year for the current row. | 834 # Get the integer day of the year for the current row. |
821 doy = temperature_data_frame$DOY[row]; | 835 doy = temperature_data_frame$DOY[row]; |
822 # Photoperiod in the day. | 836 # Photoperiod in the day. |
823 photoperiod = temperature_data_frame$DAYLEN[row]; | 837 photoperiod = temperature_data_frame$DAYLEN[row]; |
824 temp.profile = get_temperature_at_hour(latitude, temperature_data_frame, row); | 838 temp.profile = get_temperature_at_hour(latitude, row, temperature_data_frame=temperature_data_frame); |
825 mean.temp = temp.profile[1]; | 839 mean.temp = temp.profile[1]; |
826 averages.temp = temp.profile[2]; | 840 averages.temp = temp.profile[2]; |
827 averages.day[row] = averages.temp; | 841 averages.day[row] = averages.temp; |
828 # Trash bin for death. | 842 # Trash bin for death. |
829 death.vector = NULL; | 843 death.vector = NULL; |
1209 F2.total_adult[row] = sum((vector.matrix[,1]==2 & vector.matrix[,2]==3) | (vector.matrix[,1]==2 & vector.matrix[,2]==4) | (vector.matrix[,1]==2 & vector.matrix[,2]==5)); | 1223 F2.total_adult[row] = sum((vector.matrix[,1]==2 & vector.matrix[,2]==3) | (vector.matrix[,1]==2 & vector.matrix[,2]==4) | (vector.matrix[,1]==2 & vector.matrix[,2]==5)); |
1210 } | 1224 } |
1211 } | 1225 } |
1212 } # End of days specified in the input_ytd temperature data. | 1226 } # End of days specified in the input_ytd temperature data. |
1213 | 1227 |
1228 # Set the cumulative average temperature (this is never used). | |
1214 averages.cum = cumsum(averages.day); | 1229 averages.cum = cumsum(averages.day); |
1215 | 1230 |
1216 # Define the output values. | 1231 # Define the output values. |
1217 if (process_eggs) { | 1232 if (process_eggs) { |
1218 Eggs.replications[,current_replication] = Eggs; | 1233 Eggs.replications[,current_replication] = Eggs; |
1284 } | 1299 } |
1285 } | 1300 } |
1286 population.replications[,current_replication] = total.population; | 1301 population.replications[,current_replication] = total.population; |
1287 # End processing replications. | 1302 # End processing replications. |
1288 } | 1303 } |
1304 | |
1305 # Append the averages.day vector (i.e., degree-days) | |
1306 # to the various temperature_data_frames. | |
1307 temperature_data_frame = append_vector(temperature_data_frame, averages.day, "DEGREE.DAYS"); | |
1289 | 1308 |
1290 if (process_eggs) { | 1309 if (process_eggs) { |
1291 # Mean value for eggs. | 1310 # Mean value for eggs. |
1292 eggs = apply(Eggs.replications, 1, mean); | 1311 eggs = apply(Eggs.replications, 1, mean); |
1293 temperature_data_frame = append_vector(temperature_data_frame, eggs, "EGG"); | 1312 temperature_data_frame = append_vector(temperature_data_frame, eggs, "EGG"); |
1356 } | 1375 } |
1357 } | 1376 } |
1358 } | 1377 } |
1359 | 1378 |
1360 if (plot_generations_separately) { | 1379 if (plot_generations_separately) { |
1380 # Create copies of the temperature data for generations P, F1 and F2 if we're plotting generations separately. | |
1381 temperature_data_frame_P = data.frame(temperature_data_frame); | |
1382 temperature_data_frame_F1 = data.frame(temperature_data_frame); | |
1383 temperature_data_frame_F2 = data.frame(temperature_data_frame); | |
1384 | |
1361 m_se = get_mean_and_std_error(P.replications, F1.replications, F2.replications); | 1385 m_se = get_mean_and_std_error(P.replications, F1.replications, F2.replications); |
1362 P = m_se[[1]]; | 1386 P = m_se[[1]]; |
1363 P.std_error = m_se[[2]]; | 1387 P.std_error = m_se[[2]]; |
1364 F1 = m_se[[3]]; | 1388 F1 = m_se[[3]]; |
1365 F1.std_error = m_se[[4]]; | 1389 F1.std_error = m_se[[4]]; |