Repository 'insect_phenology_model'
hg clone https://toolshed.g2.bx.psu.edu/repos/greg/insect_phenology_model

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