77
88usethis :: use_pipe(export = TRUE )
99
10- # The double-logistic function equation
11- model_str <- " m1 + (m2 - m7 * t) * ((1 / (1 + exp((m3 - t) / m4))) -
12- (1 / (1 + exp((m5 - t) / m6))))"
1310
14-
15- # ' Make a standard color transparent.
16- # ' This function is borrowed from 'yarrr' package, but I changed the trans.val
17- # ' to use alpha value directly.
18- # ' @param orig.col: the original color, can be a color name, a hexadecimal code,
19- # ' or a rgb vector.
20- # ' @param alpha: define the transparent level.
21- # ' @param maxColorValue: used to convert the color to rgb format before making
22- # ' it transparent.
23- # ' @return color code.
24- # '
25- # ' @noRd
26- Transparent <- function (orig.col , alpha = 1 , maxColorValue = 255 ) {
27- n.cols <- length(orig.col )
28- orig.col <- grDevices :: col2rgb(orig.col )
29- final.col <- rep(NA , n.cols )
30- for (i in 1 : n.cols ) {
31- final.col [i ] <- grDevices :: rgb(
32- orig.col [1 , i ], orig.col [2 , i ], orig.col [3 , i ],
33- alpha = alpha [i ] * 255 ,
34- maxColorValue = maxColorValue
35- )
36- }
37- return (final.col )
38- }
39-
40-
41- # ' Format input date and VI vectors to the structure needed for fitting averaged
42- # ' phenology models such as Fisher et al 2006, Elmore et al 2012.
43- # '
44- # ' @param date_vec the date vector, be sure to convert the vector to "Date"
45- # ' format or use "yyyy-mm-dd" format string.
46- # ' @param vi_vec The vegetation index vector.
47- # ' @return A list that contains formated data.
48- # ' @import data.table
49- # '
50- # ' @noRd
51- FormatAvgData <- function (date_vec , vi_vec ) {
52- # Check if date_vec is in Date format
53- if (sum(! is.na(lubridate :: parse_date_time(date_vec , orders = " ymd" ))) !=
54- length(date_vec )) {
55- stop(" There're invalid Date values in the `date_vec`!
56- Be sure to use `yyyy-mm-dd` format." )
57- }
58-
59- # Make it a data table
60- vi_dt <- data.table :: data.table(
61- date = as.Date(date_vec ),
62- evi2 = vi_vec ,
63- avg_date = " "
64- )
65- vi_dt [, avg_date : = as.Date(paste0(" 1970" , substr(vi_dt $ date , 5 , 10 )))]
66- vi_dt <- stats :: na.omit(vi_dt )
67- data.table :: setorder(vi_dt , date )
68-
69- # Find unique dates in the averaged year
70- unique_dates <- unique(vi_dt $ avg_date )
71-
72- # Deal with multiple observations on the same date in the averaged year.
73- # When that happens, we choose the one whose EVI2 value is the highest.
74- merge_dt <- sapply(unique_dates , function (x ) {
75- # find how many records this day has
76- evi2 <- NA
77- find_idx <- which(x == vi_dt $ avg_date )
78- if (length(find_idx ) == 1 ) {
79- evi2 <- vi_dt [find_idx ]$ evi2
80- } else if (length(find_idx ) > 1 ) { # we have multiple values for this date
81- # compute the max
82- evi2 <- max(vi_dt [avg_date == x ]$ evi2 , na.rm = TRUE )
83- }
84- return (list (date = x , evi2 = evi2 ))
85- })
86- merge_dt <- data.table :: as.data.table(t(merge_dt ))
87-
88- return (merge_dt )
89- }
0 commit comments