changeset 43:fd3c00392fce draft

Uploaded
author greg
date Mon, 23 Apr 2018 09:49:08 -0400
parents 64132300c62e
children c61d3d9d44db
files insect_phenology_model.R
diffstat 1 files changed, 58 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/insect_phenology_model.R	Mon Apr 23 09:48:58 2018 -0400
+++ b/insect_phenology_model.R	Mon Apr 23 09:49:08 2018 -0400
@@ -7,7 +7,7 @@
     make_option(c("--adult_accumulation"), action="store", dest="adult_accumulation", type="integer", help="Adjustment of degree-days accumulation (old nymph->adult)"),
     make_option(c("--egg_mortality"), action="store", dest="egg_mortality", type="integer", help="Adjustment rate for egg mortality"),
     make_option(c("--input_norm"), action="store", dest="input_norm", help="30 year normals temperature data for selected station"),
-    make_option(c("--input_ytd"), action="store", dest="input_ytd", help="Year-to-date temperature data for selected location"),
+    make_option(c("--input_ytd"), action="store", dest="input_ytd", default=NULL, help="Year-to-date temperature data for selected location"),
     make_option(c("--insect"), action="store", dest="insect", help="Insect name"),
     make_option(c("--insects_per_replication"), action="store", dest="insects_per_replication", type="integer", help="Number of insects with which to start each replication"),
     make_option(c("--life_stages"), action="store", dest="life_stages", help="Selected life stages for plotting"),
@@ -16,7 +16,7 @@
     make_option(c("--location"), action="store", dest="location", help="Selected location"),
     make_option(c("--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"),
     make_option(c("--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"),
-    make_option(c("--num_days_ytd"), action="store", dest="num_days_ytd", type="integer", help="Total number of days in the temperature dataset"),
+    make_option(c("--num_days_ytd"), action="store", dest="num_days_ytd", default=NULL, type="integer", help="Total number of days in the year-to-date temperature dataset"),
     make_option(c("--nymph_mortality"), action="store", dest="nymph_mortality", type="integer", help="Adjustment rate for nymph mortality"),
     make_option(c("--old_nymph_accumulation"), action="store", dest="old_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (young nymph->old nymph)"),
     make_option(c("--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"),
@@ -262,7 +262,7 @@
             ticks[tick_index] = i;
             month_labels[tick_index] = "End prepended 30 year normals";
             last_tick = i;
-        } else  if (i==end_doy_ytd+1) {
+        } else  if (end_doy_ytd > 0 & i==end_doy_ytd+1) {
             # Add a tick for the start of the 30 year normnals data
             # that was appended to the year-to-date data.
             tick_index = get_tick_index(i, last_tick, ticks, month_labels)
@@ -354,26 +354,42 @@
 }
 
 parse_input_data = function(input_ytd, input_norm, num_days_ytd) {
-    # Read the input_ytd temperature datafile into a data frame.
-    # The input_ytd data has the following 6 columns:
-    # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
-    temperature_data_frame = read.csv(file=input_ytd, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=",");
-    # Set the temperature_data_frame column names for access.
-    colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
-    # Get the start date.
-    start_date = temperature_data_frame$DATE[1];
-    end_date = temperature_data_frame$DATE[num_days_ytd];
+    if (is.null(input_ytd)) {
+        # We're analysing only the 30 year normals data, so create an empty
+        # data frame for containing temperature data after it is converted
+        # from the 30 year normals format to the year-to-date format.
+        temperature_data_frame = data.frame(matrix(ncol=6, nrow=0));
+        colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
+        # Base all dates on the current date since 30 year
+        # normals data does not include any dates.
+        year = format(Sys.Date(), "%Y");
+        start_date = paste(year, "01", "01", sep="-");
+        end_date = paste(year, "12", "31", sep="-");
+        # Set invalid start and end DOY.
+        start_doy_ytd = 0;
+        end_doy_ytd = 0;
+    } else {
+        # Read the input_ytd temperature datafile into a data frame.
+        # The input_ytd data has the following 6 columns:
+        # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
+        temperature_data_frame = read.csv(file=input_ytd, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=",");
+        # Set the temperature_data_frame column names for access.
+        colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
+        # Get the start date.
+        start_date = temperature_data_frame$DATE[1];
+        end_date = temperature_data_frame$DATE[num_days_ytd];
+        # Extract the year from the start date.
+        date_str = format(start_date);
+        date_str_items = strsplit(date_str, "-")[[1]];
+        year = date_str_items[1];
+        # Save the first DOY to later check if start_date is Jan 1.
+        start_doy_ytd = as.integer(temperature_data_frame$DOY[1]);
+        end_doy_ytd = as.integer(temperature_data_frame$DOY[num_days_ytd]);
+    }
     # See if we're in a leap year.
     is_leap_year = is_leap_year(start_date);
     # Get the number of days in the year.
     total_days = get_total_days(is_leap_year);
-    # Extract the year from the start date.
-    date_str = format(start_date);
-    date_str_items = strsplit(date_str, "-")[[1]];
-    year = date_str_items[1];
-    # Save the first DOY to later check if start_date is Jan 1.
-    start_doy_ytd = as.integer(temperature_data_frame$DOY[1]);
-    end_doy_ytd = as.integer(temperature_data_frame$DOY[num_days_ytd]);
     # Read the input_norm temperature datafile into a data frame.
     # The input_norm data has the following 10 columns:
     # STATIONID, LATITUDE, LONGITUDE, ELEV_M, NAME, ST, MMDD, DOY, TMIN, TMAX
@@ -385,22 +401,30 @@
     if (!is_leap_year) {
         norm_data_frame = norm_data_frame[-c(60),];
     }
-    if (start_doy_ytd > 1) {
-        # The year-to-date data starts after Jan 1, so create a
-        # temporary data frame to contain the 30 year normals data
-        # from Jan 1 to the date immediately prior to start_date.
-        tmp_data_frame = temperature_data_frame[FALSE,];
-        for (i in 1:start_doy_ytd-1) {
-            tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
+    if (is.null(input_ytd)) {
+        # Convert the 30 year normals data to the year-to-date format.
+        for (i in 1:total_days) {
+            temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
         }
-        # Next merge the temporary data frame with the year-to-date data frame.
-        temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame);
-    }
-    # Define the next row for the year-to-date data from the 30 year normals data.
-    first_normals_append_row = end_doy_ytd + 1;
-    # Append the 30 year normals data to the year-to-date data.
-    for (i in first_normals_append_row:total_days) {
-        temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
+    } else {
+        # Merge the year-to-date data with the 30 year normals data.
+        if (start_doy_ytd > 1) {
+            # The year-to-date data starts after Jan 1, so create a
+            # temporary data frame to contain the 30 year normals data
+            # from Jan 1 to the date immediately prior to start_date.
+            tmp_data_frame = temperature_data_frame[FALSE,];
+            for (i in 1:start_doy_ytd-1) {
+                tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
+            }
+            # Next merge the temporary data frame with the year-to-date data frame.
+            temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame);
+        }
+        # Define the next row for the year-to-date data from the 30 year normals data.
+        first_normals_append_row = end_doy_ytd + 1;
+        # Append the 30 year normals data to the year-to-date data.
+        for (i in first_normals_append_row:total_days) {
+            temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
+        }
     }
     # Add a column containing the daylight length for each day.
     temperature_data_frame = add_daylight_length(temperature_data_frame, total_days);