Skip to content

Commit

Permalink
Modified calcMonthlyClimate() to read vectors instead of matrices.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sara Minoli authored and Sara Minoli committed Aug 8, 2022
1 parent 6243bd2 commit d9b295d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
4 changes: 2 additions & 2 deletions cropCalendars/R/calcCropCalendars.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ calcCropCalendars <- function(lon = NULL,
croppar_file <- system.file("extdata", "crop_parameters.csv",
package = "cropCalendars", "mustWork" = TRUE)
}
crop_parameters <- getCropParam(cropparam_file = croppar_file,
crops = crop
crop_parameters <- getCropParam(crops = crop,
cropparam_file = croppar_file
)

# Get monthly weather data of the grid cell
Expand Down
48 changes: 31 additions & 17 deletions cropCalendars/R/calcMonthlyClimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#'
#' @param lat latitude (decimal value)
#' @param temp daily temperature (degree Celsius) for a number of years. It should be passed
#' in form of a matrix with dimensions [nyears, 365].
#' @param temp daily precipitation for a number of years. It should be passed
#' in form of a matrix with dimensions [nyears, 365].
#' in form of a vector.
#' @param prec daily precipitation for a number of years. It should be passed
#' in form of a vector.
#' @param syear start year in the climate time series.
#' @param eyear end yeat in the climate time series.
#' @examples
Expand All @@ -18,11 +18,12 @@
#' syear = 2001, eyear = 2003)
#' @export

calcMonthlyClimate <- function(lat,
temp,
prec,
syear,
eyear
calcMonthlyClimate <- function(lat = NULL,
temp = NULL,
prec = NULL,
syear = NULL,
eyear = NULL,
leap_years = TRUE
) {

years <- syear:eyear
Expand All @@ -31,13 +32,24 @@ calcMonthlyClimate <- function(lat,
ndays <- length(days)
nmonths <- 12

if (leap_years == 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)

# Compute PET (Potential ET)
cat("Computing daily PET ...\n")
pet <- array(NA, dim = dim(temp))
for (yy in seq_len(nyears)) {
for (dd in seq_len(ndays)) {
pet[yy, dd] <- calcPET(temp = temp[yy, dd], lat = lat, day = dd)
}
pet <- NULL
for (i in seq_len(length(temp))) {
pet_today <- calcPET(temp = temp[i], lat = lat, day = d_dates[i])
pet <- c(pet, pet_today)
}

# Compute monthly climate for each year
Expand All @@ -50,10 +62,12 @@ calcMonthlyClimate <- function(lat,
for (yy in seq_len(nyears)) {
for (mm in seq_len(nmonths)) {

# which days belong to month mm
mtemp_y[yy, mm] <- mean(temp) # mean temp
mprec_y[yy, mm] <- sum(prec) # cumulative pr
mpet_y[yy, mm] <- sum(pet) # cumulative pet
# which days belong to year yy and month mm
idx <- which(y_dates == years[yy] & m_dates == mm)

mtemp_y[yy, mm] <- mean(temp[idx]) # mean temp
mprec_y[yy, mm] <- sum(prec[idx]) # cumulative pr
mpet_y[yy, mm] <- sum(pet[idx]) # cumulative pet
mppet_y[yy,mm] <- mprec_y[yy, mm]/mpet_y[yy, mm] # ppet ratio

}
Expand Down
13 changes: 9 additions & 4 deletions cropCalendars/R/getCropParam.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
#'
#' @export

getCropParam <- function(cropparam_file,
crops,
print_all = FALSE
getCropParam <- function(crops = NULL,
cropparam_file = NULL,
print_all = FALSE
) {
# Parameters ----
# If not specified, read default parameter file
if (is.null(cropparam_file)) {
cropparam_file <- system.file("extdata", "crop_parameters.csv",
package = "cropCalendars", "mustWork" = TRUE)
}
# Import crop parameters
crop_parameters_all <- read.csv(cropparam_file,
header = T,
stringsAsFactors = F)
Expand Down
5 changes: 5 additions & 0 deletions cropCalendars/R/zz_dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@ 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 d9b295d

Please sign in to comment.