Mercurial > repos > greg > insect_phenology_model
comparison insect_phenology_model.R @ 49:1b6864c5b50a draft
Uploaded
author | greg |
---|---|
date | Tue, 05 Jun 2018 07:52:59 -0400 |
parents | 58a823b1d940 |
children | 927321ed0322 |
comparison
equal
deleted
inserted
replaced
48:99e1c1300fcd | 49:1b6864c5b50a |
---|---|
4 | 4 |
5 option_list <- list( | 5 option_list <- list( |
6 make_option(c("--adult_mortality"), action="store", dest="adult_mortality", type="integer", help="Adjustment rate for adult mortality"), | 6 make_option(c("--adult_mortality"), action="store", dest="adult_mortality", type="integer", help="Adjustment rate for adult mortality"), |
7 make_option(c("--adult_accumulation"), action="store", dest="adult_accumulation", type="integer", help="Adjustment of degree-days accumulation (old nymph->adult)"), | 7 make_option(c("--adult_accumulation"), action="store", dest="adult_accumulation", type="integer", help="Adjustment of degree-days accumulation (old nymph->adult)"), |
8 make_option(c("--egg_mortality"), action="store", dest="egg_mortality", type="integer", help="Adjustment rate for egg mortality"), | 8 make_option(c("--egg_mortality"), action="store", dest="egg_mortality", type="integer", help="Adjustment rate for egg mortality"), |
9 make_option(c("--end_date"), action="store", dest="end_date", default=NULL, help="End date for custom date interval"), | |
9 make_option(c("--input_norm"), action="store", dest="input_norm", help="30 year normals temperature data for selected station"), | 10 make_option(c("--input_norm"), action="store", dest="input_norm", help="30 year normals temperature data for selected station"), |
10 make_option(c("--input_ytd"), action="store", dest="input_ytd", default=NULL, help="Year-to-date temperature data for selected location"), | 11 make_option(c("--input_ytd"), action="store", dest="input_ytd", default=NULL, help="Year-to-date temperature data for selected location"), |
11 make_option(c("--insect"), action="store", dest="insect", help="Insect name"), | 12 make_option(c("--insect"), action="store", dest="insect", help="Insect name"), |
12 make_option(c("--insects_per_replication"), action="store", dest="insects_per_replication", type="integer", help="Number of insects with which to start each replication"), | 13 make_option(c("--insects_per_replication"), action="store", dest="insects_per_replication", type="integer", help="Number of insects with which to start each replication"), |
13 make_option(c("--life_stages"), action="store", dest="life_stages", help="Selected life stages for plotting"), | 14 make_option(c("--life_stages"), action="store", dest="life_stages", help="Selected life stages for plotting"), |
22 make_option(c("--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"), | 23 make_option(c("--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"), |
23 make_option(c("--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"), | 24 make_option(c("--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"), |
24 make_option(c("--plot_generations_separately"), action="store", dest="plot_generations_separately", help="Plot Plot P, F1 and F2 as separate lines or pool across them"), | 25 make_option(c("--plot_generations_separately"), action="store", dest="plot_generations_separately", help="Plot Plot P, F1 and F2 as separate lines or pool across them"), |
25 make_option(c("--plot_std_error"), action="store", dest="plot_std_error", help="Plot Standard error"), | 26 make_option(c("--plot_std_error"), action="store", dest="plot_std_error", help="Plot Standard error"), |
26 make_option(c("--replications"), action="store", dest="replications", type="integer", help="Number of replications"), | 27 make_option(c("--replications"), action="store", dest="replications", type="integer", help="Number of replications"), |
28 make_option(c("--start_date"), action="store", dest="start_date", default=NULL, help="Start date for custom date interval"), | |
27 make_option(c("--young_nymph_accumulation"), action="store", dest="young_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (egg->young nymph)") | 29 make_option(c("--young_nymph_accumulation"), action="store", dest="young_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (egg->young nymph)") |
28 ) | 30 ) |
29 | 31 |
30 parser <- OptionParser(usage="%prog [options] file", option_list=option_list); | 32 parser <- OptionParser(usage="%prog [options] file", option_list=option_list); |
31 args <- parse_args(parser, positional_arguments=TRUE); | 33 args <- parse_args(parser, positional_arguments=TRUE); |
32 opt <- args$options; | 34 opt <- args$options; |
33 | 35 |
34 add_daylight_length = function(temperature_data_frame, num_rows) { | 36 add_daylight_length = function(temperature_data_frame) { |
35 # Return a vector of daylight length (photoperido profile) for | 37 # Return temperature_data_frame with an added column |
36 # the number of days specified in the input_ytd temperature data | 38 # of daylight length (photoperido profile). |
37 # (from Forsythe 1995). | 39 num_rows = dim(temperature_data_frame)[1]; |
40 # From Forsythe 1995. | |
38 p = 0.8333; | 41 p = 0.8333; |
39 latitude = temperature_data_frame$LATITUDE[1]; | 42 latitude = temperature_data_frame$LATITUDE[1]; |
40 daylight_length_vector = NULL; | 43 daylight_length_vector = NULL; |
41 for (i in 1:num_rows) { | 44 for (i in 1:num_rows) { |
42 # Get the day of the year from the current row | 45 # Get the day of the year from the current row |
59 # Append vector vec as a new column to data_frame. | 62 # Append vector vec as a new column to data_frame. |
60 data_frame[,num_columns+1] = vec; | 63 data_frame[,num_columns+1] = vec; |
61 # Reset the column names with the additional column for later access. | 64 # Reset the column names with the additional column for later access. |
62 colnames(data_frame) = append(current_column_names, new_column_name); | 65 colnames(data_frame) = append(current_column_names, new_column_name); |
63 return(data_frame); | 66 return(data_frame); |
67 } | |
68 | |
69 extract_date_interval_rows = function(df, start_date, end_date) { | |
70 date_interval_rows = df[df$DATE >= start_date & df$DATE <= end_date]; | |
71 return(date_interval_rows); | |
72 } | |
73 | |
74 from_30_year_normals = function(norm_data_frame, start_date_doy, end_date_doy, year) { | |
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 # Add 1 to the number of rows to ensure that the end date is included. | |
79 tmp_data_frame_rows = last_norm_row - first_norm_row + 1; | |
80 tmp_data_frame = get_new_temperature_data_frame(nrow=tmp_data_frame_rows); | |
81 j = 0; | |
82 for (i in first_norm_row:last_norm_row) { | |
83 j = j + 1; | |
84 tmp_data_frame[j,] = get_next_normals_row(norm_data_frame, year, i); | |
85 } | |
86 return (tmp_data_frame); | |
64 } | 87 } |
65 | 88 |
66 get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) { | 89 get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) { |
67 if (!is.null(life_stage_nymph)) { | 90 if (!is.null(life_stage_nymph)) { |
68 lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph); | 91 lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph); |
121 # F2 standard error. | 144 # F2 standard error. |
122 f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications); | 145 f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications); |
123 return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se)) | 146 return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se)) |
124 } | 147 } |
125 | 148 |
126 get_next_normals_row = function(norm_data_frame, year, is_leap_year, index) { | 149 get_new_norm_data_frame = function(is_leap_year, input_norm=NULL, nrow=0) { |
150 # The input_norm data has the following 10 columns: | |
151 # STATIONID, LATITUDE, LONGITUDE, ELEV_M, NAME, ST, MMDD, DOY, TMIN, TMAX | |
152 column_names = c("STATIONID", "LATITUDE","LONGITUDE", "ELEV_M", "NAME", "ST", "MMDD", "DOY", "TMIN", "TMAX"); | |
153 if (is.null(input_norm)) { | |
154 norm_data_frame = data.frame(matrix(ncol=10, nrow)); | |
155 # Set the norm_data_frame column names for access. | |
156 colnames(norm_data_frame) = column_names; | |
157 } else { | |
158 norm_data_frame = read.csv(file=input_norm, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=","); | |
159 # Set the norm_data_frame column names for access. | |
160 colnames(norm_data_frame) = column_names; | |
161 if (!is_leap_year) { | |
162 # All normals data includes Feb 29 which is row 60 in | |
163 # the data, so delete that row if we're not in a leap year. | |
164 norm_data_frame = norm_data_frame[-c(60),]; | |
165 # Since we've removed row 60, we need to subtract 1 from | |
166 # each value in the DOY column of the data frame starting | |
167 # with the 60th row. | |
168 num_rows = dim(norm_data_frame)[1]; | |
169 for (i in 60:num_rows) { | |
170 leap_year_doy = norm_data_frame$DOY[i]; | |
171 non_leap_year_doy = leap_year_doy - 1; | |
172 norm_data_frame$DOY[i] = non_leap_year_doy; | |
173 } | |
174 } | |
175 } | |
176 return (norm_data_frame); | |
177 } | |
178 | |
179 get_new_temperature_data_frame = function(input_ytd=NULL, nrow=0) { | |
180 # The input_ytd data has the following 6 columns: | |
181 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX | |
182 if (is.null(input_ytd)) { | |
183 temperature_data_frame = data.frame(matrix(ncol=6, nrow)); | |
184 } else { | |
185 temperature_data_frame = read.csv(file=input_ytd, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=","); | |
186 } | |
187 colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX"); | |
188 return(temperature_data_frame); | |
189 } | |
190 | |
191 get_next_normals_row = function(norm_data_frame, year, index) { | |
127 # Return the next 30 year normals row formatted | 192 # Return the next 30 year normals row formatted |
128 # appropriately for the year-to-date data. | 193 # appropriately for the year-to-date data. |
129 latitude = norm_data_frame[index,"LATITUDE"][1]; | 194 latitude = norm_data_frame[index,"LATITUDE"][1]; |
130 longitude = norm_data_frame[index,"LONGITUDE"][1]; | 195 longitude = norm_data_frame[index,"LONGITUDE"][1]; |
131 # Format the date. | 196 # Format the date. |
132 mmdd = norm_data_frame[index,"MMDD"][1]; | 197 mmdd = norm_data_frame[index,"MMDD"][1]; |
133 date_str = paste(year, mmdd, sep="-"); | 198 date_str = paste(year, mmdd, sep="-"); |
134 doy = norm_data_frame[index,"DOY"][1]; | 199 doy = norm_data_frame[index,"DOY"][1]; |
135 if (!is_leap_year) { | |
136 # Since all normals data includes Feb 29, we have to | |
137 # subtract 1 from DOY if we're not in a leap year since | |
138 # we removed the Feb 29 row from the data frame above. | |
139 doy = as.integer(doy) - 1; | |
140 } | |
141 tmin = norm_data_frame[index,"TMIN"][1]; | 200 tmin = norm_data_frame[index,"TMIN"][1]; |
142 tmax = norm_data_frame[index,"TMAX"][1]; | 201 tmax = norm_data_frame[index,"TMAX"][1]; |
143 return(list(latitude, longitude, date_str, doy, tmin, tmax)); | 202 return(list(latitude, longitude, date_str, doy, tmin, tmax)); |
144 } | 203 } |
145 | 204 |
146 get_temperature_at_hour = function(latitude, temperature_data_frame, row, num_days) { | 205 get_temperature_at_hour = function(latitude, temperature_data_frame, row) { |
147 # Base development threshold for Brown Marmorated Stink Bug | 206 # Base development threshold for Brown Marmorated Stink Bug |
148 # insect phenology model. | 207 # insect phenology model. |
149 threshold = 14.17; | 208 threshold = 14.17; |
150 # Minimum temperature for current row. | 209 # Minimum temperature for current row. |
151 curr_min_temp = temperature_data_frame$TMIN[row]; | 210 curr_min_temp = temperature_data_frame$TMIN[row]; |
212 averages = sum(dh) / 24; | 271 averages = sum(dh) / 24; |
213 } | 272 } |
214 return(c(curr_mean_temp, averages)) | 273 return(c(curr_mean_temp, averages)) |
215 } | 274 } |
216 | 275 |
217 get_tick_index = function(index, last_tick, ticks, month_labels) { | 276 get_tick_index = function(index, last_tick, ticks, tick_labels, tick_sep) { |
218 # The R code tries hard not to draw overlapping tick labels, and so | 277 # The R code tries hard not to draw overlapping tick labels, and so |
219 # will omit labels where they would abut or overlap previously drawn | 278 # will omit labels where they would abut or overlap previously drawn |
220 # labels. This can result in, for example, every other tick being | 279 # labels. This can result in, for example, every other tick being |
221 # labelled. We'll keep track of the last tick to make sure all of | 280 # labelled. We'll keep track of the last tick to make sure all of |
222 # the month labels are displayed, and missing ticks are restricted | 281 # the month labels are displayed, and missing ticks are restricted |
223 # to Sundays which have no labels anyway. | 282 # to Sundays which have no labels anyway. |
224 if (last_tick==0) { | 283 if (last_tick==0) { |
225 return(length(ticks)+1); | 284 return(length(ticks)+1); |
226 } | 285 } |
227 last_saved_tick = ticks[[length(ticks)]]; | 286 last_saved_tick = ticks[[length(ticks)]]; |
228 if (index-last_saved_tick<3) { | 287 if (index-last_saved_tick<tick_sep) { |
229 last_saved_month = month_labels[[length(month_labels)]]; | 288 last_saved_month = tick_labels[[length(tick_labels)]]; |
230 if (last_saved_month=="") { | 289 if (last_saved_month=="") { |
231 # We're safe overwriting a tick | 290 # We're safe overwriting a tick |
232 # with no label (i.e., a Sunday tick). | 291 # with no label (i.e., a Sunday tick). |
233 return(length(ticks)); | 292 return(length(ticks)); |
234 } else { | 293 } else { |
246 } else { | 305 } else { |
247 return(365); | 306 return(365); |
248 } | 307 } |
249 } | 308 } |
250 | 309 |
251 get_x_axis_ticks_and_labels = function(temperature_data_frame, num_rows, start_doy_ytd, end_doy_ytd) { | 310 get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, date_interval) { |
252 # Keep track of the years to see if spanning years. | 311 # Generate a list of ticks and labels for plotting the x axis. |
253 month_labels = list(); | 312 if (prepend_end_doy_norm > 0) { |
313 prepend_end_norm_row = which(temperature_data_frame$DOY==prepend_end_doy_norm); | |
314 } else { | |
315 prepend_end_norm_row = 0; | |
316 } | |
317 if (append_start_doy_norm > 0) { | |
318 append_start_norm_row = which(temperature_data_frame$DOY==append_start_doy_norm); | |
319 } else { | |
320 append_start_norm_row = 0; | |
321 } | |
322 num_rows = dim(temperature_data_frame)[1]; | |
323 tick_labels = list(); | |
254 ticks = list(); | 324 ticks = list(); |
255 current_month_label = NULL; | 325 current_month_label = NULL; |
256 last_tick = 0; | 326 last_tick = 0; |
327 if (date_interval) { | |
328 tick_sep = 0; | |
329 } else { | |
330 tick_sep = 3; | |
331 } | |
257 for (i in 1:num_rows) { | 332 for (i in 1:num_rows) { |
258 if (start_doy_ytd > 1 & i==start_doy_ytd-1) { | 333 # Get the year and month from the date which |
334 # has the format YYYY-MM-DD. | |
335 date = format(temperature_data_frame$DATE[i]); | |
336 # Get the month label. | |
337 items = strsplit(date, "-")[[1]]; | |
338 month = items[2]; | |
339 month_label = month.abb[as.integer(month)]; | |
340 day = as.integer(items[3]); | |
341 doy = as.integer(temperature_data_frame$DOY[i]); | |
342 # We're plotting the entire year, so ticks will | |
343 # occur on Sundays and the first of each month. | |
344 if (i == prepend_end_norm_row) { | |
259 # Add a tick for the end of the 30 year normnals data | 345 # Add a tick for the end of the 30 year normnals data |
260 # that was prepended to the year-to-date data. | 346 # that was prepended to the year-to-date data. |
261 tick_index = get_tick_index(i, last_tick, ticks, month_labels) | 347 label_str = "End prepended 30 year normals"; |
348 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep) | |
262 ticks[tick_index] = i; | 349 ticks[tick_index] = i; |
263 month_labels[tick_index] = "End prepended 30 year normals"; | 350 if (date_interval) { |
351 # Append the day to label_str | |
352 tick_labels[tick_index] = paste(label_str, day, sep=" "); | |
353 } else { | |
354 tick_labels[tick_index] = label_str; | |
355 } | |
264 last_tick = i; | 356 last_tick = i; |
265 } else if (end_doy_ytd > 0 & i==end_doy_ytd+1) { | 357 } else if (doy == append_start_doy_norm) { |
266 # Add a tick for the start of the 30 year normnals data | 358 # Add a tick for the start of the 30 year normnals data |
267 # that was appended to the year-to-date data. | 359 # that was appended to the year-to-date data. |
268 tick_index = get_tick_index(i, last_tick, ticks, month_labels) | 360 label_str = "Start appended 30 year normals"; |
361 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep) | |
269 ticks[tick_index] = i; | 362 ticks[tick_index] = i; |
270 month_labels[tick_index] = "Start appended 30 year normals"; | 363 if (!identical(current_month_label, month_label)) { |
364 # Append the month to label_str. | |
365 label_str = paste(label_str, month_label, spe=" "); | |
366 current_month_label = month_label; | |
367 } | |
368 if (date_interval) { | |
369 # Append the day to label_str | |
370 label_str = paste(label_str, day, sep=" "); | |
371 } | |
372 tick_labels[tick_index] = label_str; | |
271 last_tick = i; | 373 last_tick = i; |
272 } else if (i==num_rows) { | 374 } else if (i==num_rows) { |
273 # Add a tick for the last day of the year. | 375 # Add a tick for the last day of the year. |
274 tick_index = get_tick_index(i, last_tick, ticks, month_labels) | 376 label_str = ""; |
377 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep) | |
275 ticks[tick_index] = i; | 378 ticks[tick_index] = i; |
276 month_labels[tick_index] = ""; | 379 if (!identical(current_month_label, month_label)) { |
277 last_tick = i; | 380 # Append the month to label_str. |
381 label_str = month_label; | |
382 current_month_label = month_label; | |
383 } | |
384 if (date_interval) { | |
385 # Append the day to label_str | |
386 label_str = paste(label_str, day, sep=" "); | |
387 } | |
388 tick_labels[tick_index] = label_str; | |
278 } else { | 389 } else { |
279 # Get the year and month from the date which | |
280 # has the format YYYY-MM-DD. | |
281 date = format(temperature_data_frame$DATE[i]); | |
282 # Get the month label. | |
283 items = strsplit(date, "-")[[1]]; | |
284 month = items[2]; | |
285 month_label = month.abb[as.integer(month)]; | |
286 if (!identical(current_month_label, month_label)) { | 390 if (!identical(current_month_label, month_label)) { |
287 # Add an x-axis tick for the month. | 391 # Add a tick for the month. |
288 tick_index = get_tick_index(i, last_tick, ticks, month_labels) | 392 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep) |
289 ticks[tick_index] = i; | 393 ticks[tick_index] = i; |
290 month_labels[tick_index] = month_label; | 394 if (date_interval) { |
395 # Append the day to the month. | |
396 tick_labels[tick_index] = paste(month_label, day, sep=" "); | |
397 } else { | |
398 tick_labels[tick_index] = month_label; | |
399 } | |
291 current_month_label = month_label; | 400 current_month_label = month_label; |
292 last_tick = i; | 401 last_tick = i; |
293 } | 402 } |
294 tick_index = get_tick_index(i, last_tick, ticks, month_labels) | 403 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep) |
295 if (!is.null(tick_index)) { | 404 if (!is.null(tick_index)) { |
296 # Get the day. | 405 if (date_interval) { |
297 day = weekdays(as.Date(date)); | 406 # Add a tick for every day. The first tick is the |
298 if (day=="Sunday") { | 407 # month label, so add a tick only if i is not 1 |
299 # Add an x-axis tick if we're on a Sunday. | 408 if (i>1 & day>1) { |
300 ticks[tick_index] = i; | 409 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep) |
301 # Add a blank month label so it is not displayed. | 410 ticks[tick_index] = i; |
302 month_labels[tick_index] = ""; | 411 # Add the day as the label. |
303 last_tick = i; | 412 tick_labels[tick_index] = day; |
304 } | 413 last_tick = i; |
305 } | 414 } |
306 } | 415 } else { |
307 } | 416 # Get the day. |
308 return(list(ticks, month_labels)); | 417 day = weekdays(as.Date(date)); |
418 if (day=="Sunday") { | |
419 # Add a tick if we're on a Sunday. | |
420 ticks[tick_index] = i; | |
421 # Add a blank month label so it is not displayed. | |
422 tick_labels[tick_index] = ""; | |
423 last_tick = i; | |
424 } | |
425 } | |
426 } | |
427 } | |
428 } | |
429 return(list(ticks, tick_labels)); | |
430 } | |
431 | |
432 get_year_from_date = function(date_str) { | |
433 date_str_items = strsplit(date_str, "-")[[1]]; | |
434 return (date_str_items[1]); | |
309 } | 435 } |
310 | 436 |
311 is_leap_year = function(date_str) { | 437 is_leap_year = function(date_str) { |
312 # Extract the year from the date_str. | 438 # Extract the year from the date_str. |
313 date = format(date_str); | 439 date = format(date_str); |
351 mortality.probability = temperature * 0.0008 + 0.03; | 477 mortality.probability = temperature * 0.0008 + 0.03; |
352 } | 478 } |
353 return(mortality.probability); | 479 return(mortality.probability); |
354 } | 480 } |
355 | 481 |
356 parse_input_data = function(input_ytd, input_norm, num_days_ytd, location) { | 482 parse_input_data = function(input_ytd, input_norm, location, start_date, end_date) { |
483 # The end DOY for norm data prepended to ytd data. | |
484 prepend_end_doy_norm = 0; | |
485 # The start DOY for norm data appended to ytd data. | |
486 append_start_doy_norm = 0; | |
487 if (is.null(start_date) && is.null(end_date)) { | |
488 # We're not dealing with a date interval. | |
489 date_interval = FALSE; | |
490 if (is.null(input_ytd)) { | |
491 # Base all dates on the current date since 30 year | |
492 # normals data does not include any dates. | |
493 year = format(Sys.Date(), "%Y"); | |
494 } | |
495 } else { | |
496 date_interval = TRUE; | |
497 year = get_year_from_date(start_date); | |
498 # Get the DOY for start_date and end_date. | |
499 start_date_doy = as.integer(strftime(start_date, format="%j")); | |
500 end_date_doy = as.integer(strftime(end_date, format="%j")); | |
501 } | |
357 if (is.null(input_ytd)) { | 502 if (is.null(input_ytd)) { |
358 # We're analysing only the 30 year normals data, so create an empty | 503 # We're processing only the 30 year normals data. |
504 processing_year_to_date_data = FALSE; | |
505 if (is.null(start_date) && is.null(end_date)) { | |
506 # We're processing the entire year, so we can | |
507 # set the start_date to Jan 1. | |
508 start_date = paste(year, "01", "01", sep="-"); | |
509 } | |
510 } else { | |
511 processing_year_to_date_data = TRUE; | |
512 # Read the input_ytd temperature data file into a data frame. | |
513 temperature_data_frame = get_new_temperature_data_frame(input_ytd=input_ytd); | |
514 num_ytd_rows = dim(temperature_data_frame)[1]; | |
515 if (!date_interval) { | |
516 start_date = temperature_data_frame$DATE[1]; | |
517 year = get_year_from_date(start_date); | |
518 } | |
519 } | |
520 # See if we're in a leap year. | |
521 is_leap_year = is_leap_year(start_date); | |
522 # Read the input_norm temperature datafile into a data frame. | |
523 norm_data_frame = get_new_norm_data_frame(is_leap_year, input_norm=input_norm); | |
524 if (processing_year_to_date_data) { | |
525 if (date_interval) { | |
526 # We're plotting a date interval. | |
527 start_date_ytd_row = which(temperature_data_frame$DATE==start_date); | |
528 if (length(start_date_ytd_row) > 0) { | |
529 # The start date is contained within the input_ytd data. | |
530 start_date_ytd_row = start_date_ytd_row[1]; | |
531 start_doy_ytd = as.integer(temperature_data_frame$DOY[start_date_ytd_row]); | |
532 } else { | |
533 # The start date is contained within the input_norm data. | |
534 start_date_ytd_row = 0; | |
535 start_date_norm_row = which(norm_data_frame$DOY==start_date_doy); | |
536 } | |
537 end_date_ytd_row = which(temperature_data_frame$DATE==end_date); | |
538 if (length(end_date_ytd_row) > 0) { | |
539 end_date_ytd_row = end_date_ytd_row[1]; | |
540 # The end date is contained within the input_ytd data. | |
541 end_doy_ytd = as.integer(temperature_data_frame$DOY[end_date_ytd_row]); | |
542 } else { | |
543 end_date_ytd_row = 0; | |
544 } | |
545 } else { | |
546 # We're plotting an entire year. | |
547 # Get the start date and end date from temperature_data_frame. | |
548 start_date_ytd_row = 1; | |
549 # Temporarily set start_date to get the year. | |
550 start_date = temperature_data_frame$DATE[1]; | |
551 end_date_ytd_row = num_ytd_rows; | |
552 end_date = temperature_data_frame$DATE[num_ytd_rows]; | |
553 date_str = format(start_date); | |
554 # Extract the year from the start date. | |
555 date_str_items = strsplit(date_str, "-")[[1]]; | |
556 # Get the year. | |
557 year = date_str_items[1]; | |
558 # Properly set the start_date to be Jan 1 of the year. | |
559 start_date = paste(year, "01", "01", sep="-"); | |
560 # Properly set the end_date to be Dec 31 of the year. | |
561 end_date = paste(year, "12", "31", sep="-"); | |
562 # Save the first DOY to later check if start_date is Jan 1. | |
563 start_doy_ytd = as.integer(temperature_data_frame$DOY[1]); | |
564 end_doy_ytd = as.integer(temperature_data_frame$DOY[num_ytd_rows]); | |
565 } | |
566 } else { | |
567 # We're processing only the 30 year normals data, so create an empty | |
359 # data frame for containing temperature data after it is converted | 568 # data frame for containing temperature data after it is converted |
360 # from the 30 year normals format to the year-to-date format. | 569 # from the 30 year normals format to the year-to-date format. |
361 temperature_data_frame = data.frame(matrix(ncol=6, nrow=0)); | 570 temperature_data_frame = get_new_temperature_data_frame(); |
362 colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX"); | 571 if (date_interval) { |
363 # Base all dates on the current date since 30 year | 572 # We're plotting a date interval. |
364 # normals data does not include any dates. | 573 # Extract the year, month and day from the start date. |
365 year = format(Sys.Date(), "%Y"); | 574 start_date_str = format(start_date); |
366 start_date = paste(year, "01", "01", sep="-"); | 575 start_date_str_items = strsplit(start_date_str, "-")[[1]]; |
367 end_date = paste(year, "12", "31", sep="-"); | 576 year = start_date_str_items[1]; |
368 # Set invalid start and end DOY. | 577 start_date_month = start_date_str_items[2]; |
369 start_doy_ytd = 0; | 578 start_date_day = start_date_str_items[3]; |
370 end_doy_ytd = 0; | 579 start_date = paste(year, start_date_month, start_date_day, sep="-"); |
580 # Extract the month and day from the end date. | |
581 end_date_str = format(start_date); | |
582 end_date_str_items = strsplit(end_date_str, "-")[[1]]; | |
583 end_date_month = end_date_str_items[2]; | |
584 end_date_day = end_date_str_items[3]; | |
585 end_date = paste(year, end_date_month, end_date_day, sep="-"); | |
586 } else { | |
587 # We're plotting an entire year. | |
588 start_date = paste(year, "01", "01", sep="-"); | |
589 end_date = paste(year, "12", "31", sep="-"); | |
590 } | |
591 } | |
592 # Set the location to be the station name if the user elected not to enter it. | |
593 if (is.null(location) | length(location) == 0) { | |
594 location = norm_data_frame$NAME[1]; | |
595 } | |
596 if (processing_year_to_date_data) { | |
597 # Merge the year-to-date data with the 30 year normals data. | |
598 if (date_interval) { | |
599 # The values of start_date_ytd_row and end_date_ytd_row were set above. | |
600 if (start_date_ytd_row > 0 & end_date_ytd_row > 0) { | |
601 # The date interval is contained within the input_ytd | |
602 # data, so we don't need to merge the 30 year normals data. | |
603 temperature_data_frame = temperature_data_frame[start_date_ytd_row:end_date_ytd_row,]; | |
604 } else if (start_date_ytd_row == 0 & end_date_ytd_row > 0) { | |
605 # The date interval starts in input_norm and ends in | |
606 # input_ytd, so prepend appropriate rows from input_norm | |
607 # to appropriate rows from input_ytd. | |
608 first_norm_row = which(norm_data_frame$DOY==start_date_doy); | |
609 # Get the first DOY from temperature_data_frame. | |
610 first_ytd_doy = temperature_data_frame$DOY[1]; | |
611 # End DOY of input_norm data prepended to input_ytd. | |
612 prepend_end_doy_norm = first_ytd_doy - 1; | |
613 # Get the number of rows for the restricted date interval | |
614 # that are contained in temperature_data_frame. | |
615 num_temperature_data_frame_rows = end_date_ytd_row; | |
616 # Get the last row needed from the 30 year normals data. | |
617 last_norm_row = which(norm_data_frame$DOY==prepend_end_doy_norm); | |
618 # Get the number of rows for the restricted date interval | |
619 # that are contained in norm_data_frame. | |
620 num_norm_data_frame_rows = last_norm_row - first_norm_row; | |
621 # Create a temporary data frame to contain the 30 year normals | |
622 # data from the start date to the date immediately prior to the | |
623 # first row of the input_ytd data. | |
624 tmp_norm_data_frame = get_new_temperature_data_frame(nrow=num_temperature_data_frame_rows+num_norm_data_frame_rows); | |
625 j = 1; | |
626 for (i in first_norm_row:last_norm_row) { | |
627 # Populate the temp_data_frame row with | |
628 # values from norm_data_frame. | |
629 tmp_norm_data_frame[j,] = get_next_normals_row(norm_data_frame, year, i); | |
630 j = j + 1; | |
631 } | |
632 # Create a second temporary data frame containing the | |
633 # appropriate rows from temperature_data_frame. | |
634 tmp_temperature_data_frame = temperature_data_frame[1:num_temperature_data_frame_rows,]; | |
635 # Merge the 2 temporary data frames. | |
636 temperature_data_frame = rbind(tmp_norm_data_frame, tmp_temperature_data_frame); | |
637 } else if (start_date_ytd_row > 0 & end_date_ytd_row == 0) { | |
638 # The date interval starts in input_ytd and ends in input_norm, | |
639 # so append appropriate rows from input_norm to appropriate rows | |
640 # from input_ytd. First, get the number of rows for the restricted | |
641 # date interval that are contained in temperature_data_frame. | |
642 num_temperature_data_frame_rows = num_ytd_rows - start_date_ytd_row + 1; | |
643 # Get the DOY of the last row in the input_ytd data. | |
644 last_ytd_doy = temperature_data_frame$DOY[num_ytd_rows]; | |
645 # Get the DOYs for the first and last rows from norm_data_frame | |
646 # that will be appended to temperature_data_frame. | |
647 append_start_doy_norm = last_ytd_doy + 1; | |
648 # Get the row from norm_data_frame containing first_norm_doy. | |
649 first_norm_row = which(norm_data_frame$DOY == append_start_doy_norm); | |
650 # Get the row from norm_data_frame containing end_date_doy. | |
651 last_norm_row = which(norm_data_frame$DOY == end_date_doy); | |
652 # Get the number of rows for the restricted date interval | |
653 # that are contained in norm_data_frame. | |
654 num_norm_data_frame_rows = last_norm_row - first_norm_row; | |
655 # Create a temporary data frame to contain the data | |
656 # taken from both temperature_data_frame and norm_data_frame | |
657 # for the date interval. | |
658 tmp_data_frame = get_new_temperature_data_frame(nrow=num_temperature_data_frame_rows+num_norm_data_frame_rows); | |
659 # Populate tmp_data_frame with the appropriate rows from temperature_data_frame. | |
660 j = start_date_ytd_row; | |
661 for (i in 1:num_temperature_data_frame_rows) { | |
662 tmp_data_frame[i,] = temperature_data_frame[j,]; | |
663 j = j + 1; | |
664 } | |
665 # Apppend the appropriate rows from norm_data_frame to tmp_data_frame. | |
666 current_iteration = num_temperature_data_frame_rows + 1; | |
667 num_iterations = current_iteration + num_norm_data_frame_rows; | |
668 j = first_norm_row; | |
669 for (i in current_iteration:num_iterations) { | |
670 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, j); | |
671 j = j + 1; | |
672 } | |
673 temperature_data_frame = tmp_data_frame[,]; | |
674 } else if (start_date_ytd_row == 0 & end_date_ytd_row == 0) { | |
675 # The date interval is contained witin input_norm. | |
676 temperature_data_frame = from_30_year_normals(norm_data_frame, start_date_doy, end_date_doy, year); | |
677 } | |
678 } else { | |
679 # We're plotting an entire year. | |
680 if (start_doy_ytd > 1) { | |
681 # The input_ytd data starts after Jan 1, so prepend | |
682 # appropriate rows from input_norm to temperature_data_frame. | |
683 prepend_end_doy_norm = start_doy_ytd - 1; | |
684 last_norm_row = which(norm_data_frame$DOY == prepend_end_doy_norm); | |
685 # Create a temporary data frame to contain the input_norm data | |
686 # from Jan 1 to the date immediately prior to start_date. | |
687 tmp_data_frame = temperature_data_frame[FALSE,]; | |
688 # Populate tmp_data_frame with appropriate rows from norm_data_frame. | |
689 for (i in 1:last_norm_row) { | |
690 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i); | |
691 } | |
692 # Merge the temporary data frame with temperature_data_frame. | |
693 temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame); | |
694 } | |
695 # Set the value of total_days. | |
696 total_days = get_total_days(is_leap_year); | |
697 if (end_doy_ytd < total_days) { | |
698 # Define the next row for the year-to-date data from the 30 year normals data. | |
699 append_start_doy_norm = end_doy_ytd + 1; | |
700 first_norm_row = which(norm_data_frame$DOY == append_start_doy_norm); | |
701 # Append the 30 year normals data to the year-to-date data. | |
702 for (i in first_norm_row:total_days) { | |
703 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i); | |
704 } | |
705 } | |
706 } | |
371 } else { | 707 } else { |
372 # Read the input_ytd temperature datafile into a data frame. | 708 # We're processing only the 30 year normals data. |
373 # The input_ytd data has the following 6 columns: | 709 if (date_interval) { |
374 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX | 710 # Populate temperature_data_frame from norm_data_frame. |
375 temperature_data_frame = read.csv(file=input_ytd, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=","); | 711 temperature_data_frame = from_30_year_normals(norm_data_frame, start_date_doy, end_date_doy, year); |
376 # Set the temperature_data_frame column names for access. | 712 } else { |
377 colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX"); | 713 total_days = get_total_days(is_leap_year); |
378 # Get the start date. | 714 for (i in 1:total_days) { |
379 start_date = temperature_data_frame$DATE[1]; | 715 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i); |
380 end_date = temperature_data_frame$DATE[num_days_ytd]; | 716 } |
381 # Extract the year from the start date. | |
382 date_str = format(start_date); | |
383 date_str_items = strsplit(date_str, "-")[[1]]; | |
384 year = date_str_items[1]; | |
385 # Save the first DOY to later check if start_date is Jan 1. | |
386 start_doy_ytd = as.integer(temperature_data_frame$DOY[1]); | |
387 end_doy_ytd = as.integer(temperature_data_frame$DOY[num_days_ytd]); | |
388 } | |
389 # See if we're in a leap year. | |
390 is_leap_year = is_leap_year(start_date); | |
391 # Get the number of days in the year. | |
392 total_days = get_total_days(is_leap_year); | |
393 # Read the input_norm temperature datafile into a data frame. | |
394 # The input_norm data has the following 10 columns: | |
395 # STATIONID, LATITUDE, LONGITUDE, ELEV_M, NAME, ST, MMDD, DOY, TMIN, TMAX | |
396 norm_data_frame = read.csv(file=input_norm, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=","); | |
397 # Set the norm_data_frame column names for access. | |
398 colnames(norm_data_frame) = c("STATIONID", "LATITUDE","LONGITUDE", "ELEV_M", "NAME", "ST", "MMDD", "DOY", "TMIN", "TMAX"); | |
399 # All normals data includes Feb 29 which is row 60 in | |
400 # the data, so delete that row if we're not in a leap year. | |
401 if (!is_leap_year) { | |
402 norm_data_frame = norm_data_frame[-c(60),]; | |
403 } | |
404 # Set the location to be the station name if the user elected no to enter it. | |
405 if (is.null(location) | length(location)==0) { | |
406 location = norm_data_frame$NAME[1]; | |
407 } | |
408 if (is.null(input_ytd)) { | |
409 # Convert the 30 year normals data to the year-to-date format. | |
410 for (i in 1:total_days) { | |
411 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); | |
412 } | |
413 } else { | |
414 # Merge the year-to-date data with the 30 year normals data. | |
415 if (start_doy_ytd > 1) { | |
416 # The year-to-date data starts after Jan 1, so create a | |
417 # temporary data frame to contain the 30 year normals data | |
418 # from Jan 1 to the date immediately prior to start_date. | |
419 tmp_data_frame = temperature_data_frame[FALSE,]; | |
420 for (i in 1:start_doy_ytd-1) { | |
421 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); | |
422 } | |
423 # Next merge the temporary data frame with the year-to-date data frame. | |
424 temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame); | |
425 } | |
426 # Define the next row for the year-to-date data from the 30 year normals data. | |
427 first_normals_append_row = end_doy_ytd + 1; | |
428 # Append the 30 year normals data to the year-to-date data. | |
429 for (i in first_normals_append_row:total_days) { | |
430 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i); | |
431 } | 717 } |
432 } | 718 } |
433 # Add a column containing the daylight length for each day. | 719 # Add a column containing the daylight length for each day. |
434 temperature_data_frame = add_daylight_length(temperature_data_frame, total_days); | 720 temperature_data_frame = add_daylight_length(temperature_data_frame); |
435 return(list(temperature_data_frame, start_date, end_date, start_doy_ytd, end_doy_ytd, is_leap_year, total_days, location)); | 721 return(list(temperature_data_frame, start_date, end_date, prepend_end_doy_norm, append_start_doy_norm, is_leap_year, location)); |
436 } | 722 } |
437 | 723 |
438 render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval, | 724 render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval, |
439 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL, | 725 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL, |
440 life_stages_adult=NULL, life_stages_nymph=NULL) { | 726 life_stages_adult=NULL, life_stages_nymph=NULL) { |
517 lines(days, group3-group3_std_error, col=4, lty=2); | 803 lines(days, group3-group3_std_error, col=4, lty=2); |
518 } | 804 } |
519 } | 805 } |
520 } | 806 } |
521 | 807 |
808 stop_err = function(msg) { | |
809 cat(msg, file=stderr()); | |
810 quit(save="no", status=1); | |
811 } | |
812 | |
813 validate_date = function(date_str) { | |
814 valid_date = as.Date(date_str, format="%Y-%m-%d"); | |
815 if( class(valid_date)=="try-error" || is.na(valid_date)) { | |
816 msg = paste("Invalid date: ", date_str, ", valid date format is yyyy-mm-dd.", sep=""); | |
817 stop_err(msg); | |
818 } | |
819 return(valid_date); | |
820 } | |
821 | |
822 if (is.null(opt$input_ytd)) { | |
823 processing_year_to_date_data = FALSE; | |
824 } else { | |
825 processing_year_to_date_data = TRUE; | |
826 } | |
522 # Determine if we're plotting generations separately. | 827 # Determine if we're plotting generations separately. |
523 if (opt$plot_generations_separately=="yes") { | 828 if (opt$plot_generations_separately=="yes") { |
524 plot_generations_separately = TRUE; | 829 plot_generations_separately = TRUE; |
525 } else { | 830 } else { |
526 plot_generations_separately = FALSE; | 831 plot_generations_separately = FALSE; |
527 } | 832 } |
528 # Display the total number of days in the Galaxy history item blurb. | |
529 cat("Year-to-date number of days: ", opt$num_days_ytd, "\n"); | |
530 | |
531 # Parse the inputs. | 833 # Parse the inputs. |
532 data_list = parse_input_data(opt$input_ytd, opt$input_norm, opt$num_days_ytd, opt$location); | 834 data_list = parse_input_data(opt$input_ytd, opt$input_norm, opt$location, opt$start_date, opt$end_date); |
533 temperature_data_frame = data_list[[1]]; | 835 temperature_data_frame = data_list[[1]]; |
534 # Information needed for plots. | 836 # Information needed for plots, some of these values are |
837 # being reset here since in some case they were set above. | |
535 start_date = data_list[[2]]; | 838 start_date = data_list[[2]]; |
536 end_date = data_list[[3]]; | 839 end_date = data_list[[3]]; |
537 start_doy_ytd = data_list[[4]]; | 840 prepend_end_doy_norm = data_list[[4]]; |
538 end_doy_ytd = data_list[[5]]; | 841 append_start_doy_norm = data_list[[5]]; |
539 is_leap_year = data_list[[6]]; | 842 is_leap_year = data_list[[6]]; |
540 total_days = data_list[[7]]; | 843 location = data_list[[7]]; |
541 total_days_vector = c(1:total_days); | 844 |
542 location = data_list[[8]]; | 845 if (is.null(opt$start_date) && is.null(opt$end_date)) { |
543 | 846 # We're plotting an entire year. |
847 date_interval = FALSE; | |
848 # Display the total number of days in the Galaxy history item blurb. | |
849 if (processing_year_to_date_data) { | |
850 cat("Number of days year-to-date: ", opt$num_days_ytd, "\n"); | |
851 } else { | |
852 if (is_leap_year) { | |
853 num_days = 366; | |
854 } else { | |
855 num_days = 365; | |
856 } | |
857 cat("Number of days in year: ", num_days, "\n"); | |
858 } | |
859 } else { | |
860 # FIXME: currently custom date fields are free text, but | |
861 # Galaxy should soon include support for a date selector | |
862 # at which point this tool should be enhanced to use it. | |
863 # Validate start_date. | |
864 date_interval = TRUE; | |
865 # Calaculate the number of days in the date interval rather | |
866 # than using the number of rows in the input temperature data. | |
867 start_date = validate_date(opt$start_date); | |
868 # Validate end_date. | |
869 end_date = validate_date(opt$end_date); | |
870 if (start_date >= end_date) { | |
871 stop_err("The start date must be between 1 and 50 days before the end date when setting date intervals for plots."); | |
872 } | |
873 # Calculate the number of days in the date interval. | |
874 num_days = difftime(end_date, start_date, units=c("days")); | |
875 # Add 1 to the number of days to make the dates inclusive. For | |
876 # example, if the user enters a date range of 2018-01-01 to | |
877 # 2018-01-31, they likely expect the end date to be included. | |
878 num_days = num_days + 1; | |
879 if (num_days > 50) { | |
880 # We need to restrict date intervals since | |
881 # plots render tick marks for each day. | |
882 stop_err("Date intervals for plotting cannot exceed 50 days."); | |
883 } | |
884 # Display the total number of days in the Galaxy history item blurb. | |
885 cat("Number of days in date interval: ", num_days, "\n"); | |
886 } | |
544 # Create copies of the temperature data for generations P, F1 and F2 if we're plotting generations separately. | 887 # Create copies of the temperature data for generations P, F1 and F2 if we're plotting generations separately. |
545 if (plot_generations_separately) { | 888 if (plot_generations_separately) { |
546 temperature_data_frame_P = data.frame(temperature_data_frame); | 889 temperature_data_frame_P = data.frame(temperature_data_frame); |
547 temperature_data_frame_F1 = data.frame(temperature_data_frame); | 890 temperature_data_frame_F1 = data.frame(temperature_data_frame); |
548 temperature_data_frame_F2 = data.frame(temperature_data_frame); | 891 temperature_data_frame_F2 = data.frame(temperature_data_frame); |
549 } | 892 } |
550 | 893 |
551 # Get the ticks date labels for plots. | 894 # Get the ticks date labels for plots. |
552 ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, total_days, start_doy_ytd, end_doy_ytd); | 895 ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, date_interval); |
553 ticks = c(unlist(ticks_and_labels[1])); | 896 ticks = c(unlist(ticks_and_labels[1])); |
554 date_labels = c(unlist(ticks_and_labels[2])); | 897 date_labels = c(unlist(ticks_and_labels[2])); |
555 # All latitude values are the same, so get the value for plots from the first row. | 898 # All latitude values are the same, so get the value for plots from the first row. |
556 latitude = temperature_data_frame$LATITUDE[1]; | 899 latitude = temperature_data_frame$LATITUDE[1]; |
557 | 900 |
613 process_total_adults = TRUE; | 956 process_total_adults = TRUE; |
614 } | 957 } |
615 } | 958 } |
616 } | 959 } |
617 # Initialize matrices. | 960 # Initialize matrices. |
961 total_days = dim(temperature_data_frame)[1]; | |
618 if (process_eggs) { | 962 if (process_eggs) { |
619 Eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications); | 963 Eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications); |
620 } | 964 } |
621 if (process_young_nymphs | process_total_nymphs) { | 965 if (process_young_nymphs | process_total_nymphs) { |
622 YoungNymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications); | 966 YoungNymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications); |
776 for (row in 1:total_days) { | 1120 for (row in 1:total_days) { |
777 # Get the integer day of the year for the current row. | 1121 # Get the integer day of the year for the current row. |
778 doy = temperature_data_frame$DOY[row]; | 1122 doy = temperature_data_frame$DOY[row]; |
779 # Photoperiod in the day. | 1123 # Photoperiod in the day. |
780 photoperiod = temperature_data_frame$DAYLEN[row]; | 1124 photoperiod = temperature_data_frame$DAYLEN[row]; |
781 temp.profile = get_temperature_at_hour(latitude, temperature_data_frame, row, total_days); | 1125 temp.profile = get_temperature_at_hour(latitude, temperature_data_frame, row); |
782 mean.temp = temp.profile[1]; | 1126 mean.temp = temp.profile[1]; |
783 averages.temp = temp.profile[2]; | 1127 averages.temp = temp.profile[2]; |
784 averages.day[row] = averages.temp; | 1128 averages.day[row] = averages.temp; |
785 # Trash bin for death. | 1129 # Trash bin for death. |
786 death.vector = NULL; | 1130 death.vector = NULL; |
1457 # Save the analyzed data for generation F2. | 1801 # Save the analyzed data for generation F2. |
1458 file_path = paste("output_data_dir", "03_generation_F2.csv", sep="/"); | 1802 file_path = paste("output_data_dir", "03_generation_F2.csv", sep="/"); |
1459 write.csv(temperature_data_frame_F2, file=file_path, row.names=F); | 1803 write.csv(temperature_data_frame_F2, file=file_path, row.names=F); |
1460 } | 1804 } |
1461 | 1805 |
1806 total_days_vector = c(1:dim(temperature_data_frame)[1]); | |
1462 if (plot_generations_separately) { | 1807 if (plot_generations_separately) { |
1463 for (life_stage in life_stages) { | 1808 for (life_stage in life_stages) { |
1464 if (life_stage == "Egg") { | 1809 if (life_stage == "Egg") { |
1465 # Start PDF device driver. | 1810 # Start PDF device driver. |
1466 dev.new(width=20, height=30); | 1811 dev.new(width=20, height=30); |