Skip to content

Commit

Permalink
Handling Feb29 in date_to_doy, so that user can choose if to report F…
Browse files Browse the repository at this point in the history
…eb29 as day 61 (skip_feb29 = F) or as 60 (skip_feb29 = T) and treat all consequent days accordingly. In case of skip_feb29 = T, DOY 60 will be repeated twice for Feb 28 and 29.
  • Loading branch information
Sara Minoli authored and Sara Minoli committed Aug 9, 2022
1 parent d9b295d commit 4579bda
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
12 changes: 6 additions & 6 deletions cropCalendars/R/calcMonthlyClimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' in form of a vector.
#' @param syear start year in the climate time series.
#' @param eyear end yeat in the climate time series.
#' @param incl_feb29 time series include February 29th in leap years?
#' @examples
#' d_temp <- matrix(rnorm(365*3, 15), nrow = 3)
#' d_prec <- matrix(rnorm(365*3, 3, 50), nrow = 3)
Expand All @@ -23,26 +24,25 @@ calcMonthlyClimate <- function(lat = NULL,
prec = NULL,
syear = NULL,
eyear = NULL,
leap_years = TRUE
incl_feb29 = TRUE
) {

years <- syear:eyear
nyears <- length(years)
days <- seq_len(365)
ndays <- length(days)
nmonths <- 12

if (leap_years == FALSE) {
if (incl_feb29 == FALSE) {
dates <- createDateSeq(nstep = 365, years = years)

} else {
dates <- seqDates(start_date = paste0(syear, "-01-01"),
end_date = paste0(eyear, "-12-31"),
step = "day")
}
d_dates <- date_to_doy(dates)
m_dates <- date_to_month(dates)
y_dates <- date_to_year(dates)
m_dates <- date_to_month(dates)
d_dates <- date_to_doy(dates, skip_feb29 = TRUE)


# Compute PET (Potential ET)
cat("Computing daily PET ...\n")
Expand Down
42 changes: 34 additions & 8 deletions cropCalendars/R/zz_dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,65 @@ createDateSeq <- function(nstep = 365,
# Create sequence of dates and return as character
seqDates <- function(start_date = "1980-01-01",
end_date = "1980-12-31",
step = "day") {
step = "day"
) {
as.character(
seq.Date(from = as.Date(start_date),
to = as.Date(end_date),
by = step)
)
}

# Check if date is 29th February
is29Feb <- function(date = "1980-01-01"
) {
day <- format(as.Date(date), "%d")
mon <- format(as.Date(date), "%m")

return(paste(mon, day) == "02 29")
}

# Convert day-of-the-year (DOY) to date "YYYY-MM-DD"
doy_to_date <- function(doy = NULL,
year = NULL
) {
) {
if (length(doy) != length(year)) stop("doy and year have different length")

date <- strptime(paste(year, doy), format="%Y %j")
date <- strptime(paste(year, doy), format = "%Y %j")
return(date)
}

# Convert date "YYYY-MM-DD" to day-of-the-year (DOY)
date_to_doy <- function(date = "2010-01-29"
) {
as.integer(format(as.Date(date), "%j"))
date_to_doy <- function(date = "2010-01-29",
skip_feb29 = TRUE
) {

if (skip_feb29 == TRUE) {
# What DOY is Dec. 31 for the year of date, 365 or 366?
dec31 <- paste(format(as.Date(date), "%Y"), "12-31", sep = "-")
doy_dec31 <- as.integer(format(as.Date(dec31), "%j"))
# Convert date to DOY, including Feb. 29
doy1 <- as.integer(format(as.Date(date), "%j"))
# If is leap year, subtract 1 to all DOYs after Feb. 28
doy <- ifelse(doy_dec31 == 366 & doy1 > 28,
doy1 - 1,
doy1)
} else {
# Just convert date to DOY, including Feb. 29
doy <- as.integer(format(as.Date(date), "%j"))
}
return(doy)
}

# Convert date "YYYY-MM-DD" to day-of-the-year (DOY)
date_to_month <- function(date = "2010-01-29"
) {
) {
as.integer(format(as.Date(date), "%m"))
}

# Convert date "YYYY-MM-DD" to day-of-the-year (DOY)
date_to_year <- function(date = "2010-01-29"
) {
) {
as.integer(format(as.Date(date), "%Y"))
}

0 comments on commit 4579bda

Please sign in to comment.