From 5e0a7c3362c5b8e579669a844bc5213605ce6b4e Mon Sep 17 00:00:00 2001 From: dsheng1026 Date: Tue, 21 Jan 2025 17:41:41 -0500 Subject: [PATCH] style the package --- R/DAY2MON.R | 27 ---------- R/G2R.R | 46 ----------------- R/HeatStress.R | 100 ------------------------------------ R/MON2ANN.R | 42 --------------- R/cal_heat_stress.R | 107 +++++++++++++++++++++++++++++++++++++++ R/{PWC.R => cal_pwc.R} | 13 +++-- R/daily_to_monthly.R | 29 +++++++++++ R/grid_to_region.R | 53 +++++++++++++++++++ R/monthly_to_annual.R | 48 ++++++++++++++++++ man/DAY2MON.Rd | 23 --------- man/G2R.Rd | 26 ---------- man/HeatStress.Rd | 33 ------------ man/MON2ANN.Rd | 25 --------- man/PWC.Rd | 26 ---------- man/cal_heat_stress.Rd | 40 +++++++++++++++ man/cal_pwc.Rd | 31 ++++++++++++ man/daily_to_monthly.Rd | 26 ++++++++++ man/grid_to_region.Rd | 31 ++++++++++++ man/monthly_to_annual.Rd | 27 ++++++++++ 19 files changed, 401 insertions(+), 352 deletions(-) delete mode 100644 R/DAY2MON.R delete mode 100644 R/G2R.R delete mode 100644 R/HeatStress.R delete mode 100644 R/MON2ANN.R create mode 100644 R/cal_heat_stress.R rename R/{PWC.R => cal_pwc.R} (62%) create mode 100644 R/daily_to_monthly.R create mode 100644 R/grid_to_region.R create mode 100644 R/monthly_to_annual.R delete mode 100644 man/DAY2MON.Rd delete mode 100644 man/G2R.Rd delete mode 100644 man/HeatStress.Rd delete mode 100644 man/MON2ANN.Rd delete mode 100644 man/PWC.Rd create mode 100644 man/cal_heat_stress.Rd create mode 100644 man/cal_pwc.Rd create mode 100644 man/daily_to_monthly.Rd create mode 100644 man/grid_to_region.Rd create mode 100644 man/monthly_to_annual.Rd diff --git a/R/DAY2MON.R b/R/DAY2MON.R deleted file mode 100644 index 0374e7a..0000000 --- a/R/DAY2MON.R +++ /dev/null @@ -1,27 +0,0 @@ -#' DAY2MON -#' -#' @param input_rack Raster rack of daily value -#' -#' @importFrom raster stack overlay calc stackApply as.matrix ncell nlayers -#' @importFrom dplyr %>% -#' -#' @return A raster rack of monthly value -#' @export -#' -#' @examples -#' \dontrun{ -#' month_PWC <- DAY2MON(input_rack = PWC.stack.y) -#' month_HS <- DAY2MON(input_rack = HS.stack.y) -#' } - - -DAY2MON <- function(input_rack){ -# Create a vector of months corresponding to each layer -layer_names <- names(input_rack) -layer_dates <- as.Date(gsub("X", "", layer_names), format = "%Y.%m.%d") -MONTHS <- format(layer_dates, "%m") -# Calculate monthly mean using stackApply -monthly_mean <- stackApply(input_rack, indices = MONTHS, fun = mean) -monthly_mean <- stack(monthly_mean) -return(monthly_mean) -} diff --git a/R/G2R.R b/R/G2R.R deleted file mode 100644 index 213a26f..0000000 --- a/R/G2R.R +++ /dev/null @@ -1,46 +0,0 @@ -#' G2R -#' -#' @param grid_annual_value data.frame of grid-level annual value -#' @param SECTOR choice of sector -#' @param rast_boundary rasterized regional boundaries, 0.5 degree, Set to global extent -#' -#' @importFrom dplyr %>% -#' @import assertthat -#' -#' @return A sf data frame of regional annual value -#' @export -#' -#' @examples -#' \dontrun{ -#' reg_annual_value <- G2R(grid_annual_value = pwc.hothaps.ann, SECTOR = "MAIZ_R", rast_boundary = country_raster) -#' } - - -G2R <- function(grid_annual_value, SECTOR, rast_boundary){ - -# obtain spatial mapping weights information based on the SECTOR choice -SECTOR_INDEX <- which(HELPS::SECTOR_ALL == SECTOR) -smw <- HELPS::SECTOR_SMW[[SECTOR_INDEX]] -smw_df <- smw %>% as.data.frame(xy = T) %>% setNames(c("x", "y", "smw")) - -# transform the rasterized regional boundaries to data.frame -rast_boundary %>% as.data.frame(xy = T) -> rast_boundary_df - -# check if (x,y) are identical between grid_annual_value, smw_df and rast_boundary_df -assertthat::assert_that( - identical(round(grid_annual_value$x,2), round(smw_df$x,2), round(rast_boundary_df$x,2)), - msg = paste0("longtitudes don't match between grid_annual_value and smw_df ")) - -assertthat::assert_that( - identical(round(grid_annual_value$y,2), round(smw_df$y,2), round(rast_boundary_df$y,2)), - msg = paste0("latitudes don't match between grid_annual_value and smw_df ")) - -grid_annual_value$smw <- smw_df$smw -grid_annual_value$region_id <- rast_boundary_df[,3] - -grid_annual_value %>% - dplyr::group_by(region_id) %>% - dplyr::summarise(value = weighted.mean(value, smw, na.rm = T)) -> - reg_annual_value -return(reg_annual_value) -} diff --git a/R/HeatStress.R b/R/HeatStress.R deleted file mode 100644 index 1479713..0000000 --- a/R/HeatStress.R +++ /dev/null @@ -1,100 +0,0 @@ -#' HeatStress -#' -#' @param TempRes temporal resolution of input, "day" or "month" -#' @param SECTOR choice of sector -#' @param HS choice of heat stress function -#' @param YEAR_INPUT a vector of years of interest -#' @param ... individual .nc file name of climate variables in order -#' -#' @importFrom raster stack overlay calc stackApply as.matrix ncell nlayers -#' @importFrom terra rast -#' @importFrom dplyr %>% -#' @import assertthat -#' -#' @return A raster stack of daily grid-level heat stress level -#' @export -#' -#' @examples -#' \dontrun{ -#' GD_HS <- DAY2ANN(TempRes = "day", SECTOR = "MAIZ_I", HS = WBGT1, YEAR_INPUT = 2027, -#' "hurs_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4", -#' "tas_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4", -#' "ps_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4") -#' } - -HeatStress <- function(TempRes, SECTOR, HS, YEAR_INPUT, ...){ - - # check the input temporal resolution - if (missing(TempRes) || TempRes == "" || !TempRes %in% c("day", "month")) { - stop("Error: Please specify TempRes: 'day' or 'month'.") - } - - climate_vars <- list(...) - HS_arg_count <- length(formals(HS)) - if (length(climate_vars) != HS_arg_count) { - stop("The number of climate variables does not match the HS function's requirements.") - } - - # read in individual files of climate variables - for (i in seq_along(climate_vars)) { - assign(paste0("var", i,".stack"), terra::rast(climate_vars[[i]])) - } - - SECTOR_INDEX <- which(SECTOR_ALL == SECTOR) - sector_filter <- SECTOR_FLAG[[SECTOR_INDEX]] - - # ----- - # subset layers for year of interest - layer_dates <- terra::time(var1.stack) - YEAR <- format(layer_dates, "%Y") - - - assertthat::assert_that( - YEAR_INPUT %in% unique(YEAR), - msg = paste0("invalid YEAR_INPUT, make sure input files include data for ", YEAR_INPUT)) - - # subset layers falls into the year of interest - year_layer_index <- which(YEAR == YEAR_INPUT) - - # check number of layers - if (TempRes == "day") { - assertthat::assert_that( - length(year_layer_index) %in% c(365, 366), - msg = paste0("For daily input, expect 365 or 366 layers, but got ", length(year_layer_index), " layers.") - ) - } else if (TempRes == "month") { - assertthat::assert_that( - length(year_layer_index) == 12, - msg = paste0("For monthly input, expect 12 layers, but got ", length(year_layer_index), " layers.") - ) - } - - variable_list <- list() - for (i in 1:length(formals(HS))) { - # assign SECTOR grid filter raster - assign(paste0("var", i, ".SECTOR.y"), var.i.SECTOR.y <- get(paste0("var", i, ".stack"))[[year_layer_index]] * terra::rast(sector_filter)) - rm(list = paste0("var", i, ".stack")) - layer_dates.y <- terra::time(get(paste0("var", i, ".SECTOR.y"))) - assign(paste0("stack", i, ".SECTOR.y"), raster::brick(get(paste0("var", i, ".SECTOR.y")))) - rack <- get(paste0("stack", i, ".SECTOR.y")) - rm(list = paste0("stack", i, ".SECTOR.y")) - # names(rack) <- layer_dates.y - variable_list[[i]] <- rack - } - HS.stack.y <- do.call(overlay, c(variable_list, fun = HS)) - HS.stack.y <- stack(HS.stack.y) - names(HS.stack.y) <- layer_dates.y # name it so that DAY2MON function can work as expected - # check number of layers - if (TempRes == "day") { - assert_that( - nlayers(HS.stack.y) %in% c(365, 366), - msg = paste0("Error: For daily input, expect 365 or 366 layers, but got ", nlayers(HS.stack.y), " layers.") - ) - } else if (TempRes == "month") { - assert_that( - nlayers(HS.stack.y) == 12, - msg = paste0("Error: For monthly input, expect 12 layers, but got ", nlayers(HS.stack.y), " layers.") - ) - } - return(HS.stack.y) - } diff --git a/R/MON2ANN.R b/R/MON2ANN.R deleted file mode 100644 index 07f5f42..0000000 --- a/R/MON2ANN.R +++ /dev/null @@ -1,42 +0,0 @@ -#' MON2ANN -#' -#' @param input_rack Raster rack of monthly value -#' @param SECTOR choice of sector -#' -#' @importFrom raster stack overlay calc stackApply as.matrix as.data.frame ncell nlayers -#' @importFrom dplyr %>% -#' -#' @return A data frame of grid-level annual value -#' @export -#' -#' @examples -#' \dontrun{ -#' annual_PWC <- MON2ANN(input_rack = month_PWC, SECTOR = "MAIZ_I") -#' annual_HS <- MON2ANN(input_rack = month_HS, SECTOR = "MAIZ_I") -#' } - -MON2ANN <- function(input_rack, SECTOR){ - - if (SECTOR %in% c("WHEA_I", "RICE_I", "MAIZ_I", "SOYB_I", "BARL_I", "MILL_I", "PMIL_I", "SORG_I", "OCER_I", - "POTA_I", "SWPO_I", "YAMS_I", "CASS_I", "BEAN_I", "CHIC_I", "COWP_I", "PIGE_I", "LENT_I", - "GROU_I", "SUNF_I", "RAPE_I", "SESA_I", "SUGC_I", "SUGB_I", "COTT_I", - "WHEA_R", "RICE_R", "MAIZ_R", "SOYB_R", "BARL_R", "MILL_R", "PMIL_R", "SORG_R", "OCER_R", - "POTA_R", "SWPO_R", "YAMS_R", "CASS_R", "BEAN_R", "CHIC_R", "COWP_R", "PIGE_R", "LENT_R", - "GROU_R", "SUNF_R", "RAPE_R", "SESA_R", "SUGC_R", "SUGB_R", "COTT_R") ) { - SECTOR_INDEX <- which(SECTOR_GGCMI == SECTOR) - month_weight <- SECTOR_MONTH_WEIGHT[[SECTOR_INDEX]] - } else if (SECTOR %in% c("OFIB_I", "BANA_I", "PLNT_I", "CITR_I", "TROF_I", "TEMF_I", "TOMA_I", "ONIO_I", "VEGE_I", - "ORTS_I", "OPUL_I", "CNUT_I", "OILP_I", "OOIL_I", "COFF_I", "RCOF_I", "COCO_I", "RUBB_I", - "TEAS_I", "TOBA_I", "REST_I", - "OFIB_R", "BANA_R", "PLNT_R", "CITR_R", "TROF_R", "TEMF_R", "TOMA_R", "ONIO_R", "VEGE_R", - "ORTS_R", "OPUL_R", "CNUT_R", "OILP_R", "OOIL_R", "COFF_R", "RCOF_R", "COCO_R", "RUBB_R", - "TEAS_R", "TOBA_R", "REST_R", - "NONCROP") ) { - month_weight <- extended_weight_others - } - input_mtx <- as.matrix(input_rack) * month_weight - annual_mean <- apply(input_mtx, 1, function(row) if (all(is.na(row))) NA else sum(row, na.rm = TRUE)) - annual_output <- as.data.frame(input_rack, xy = T) %>% dplyr::select(x, y) - annual_output$value <- annual_mean - return(annual_output) -} diff --git a/R/cal_heat_stress.R b/R/cal_heat_stress.R new file mode 100644 index 0000000..de75888 --- /dev/null +++ b/R/cal_heat_stress.R @@ -0,0 +1,107 @@ +#' cal_heat_stress +#' +#' Calculate Heat Stress Levels Based on Climate Projections +#' +#' The `cal_heat_stress` function computes daily or monthly grid-level heat stress levels for specified +#' sectors, using a user-defined heat stress function and climate data. The function +#' supports both daily and monthly climate inputs and evaluates heat stress over a specific year. +#' +#' @param TempRes temporal resolution of input, "day" or "month" +#' @param SECTOR choice of sector +#' @param HS choice of heat stress function +#' @param YEAR_INPUT a vector of years of interest +#' @param ... individual .nc file name of climate variables in order +#' +#' @importFrom raster stack overlay calc stackApply as.matrix ncell nlayers +#' @importFrom terra rast +#' @importFrom dplyr %>% +#' @import assertthat +#' +#' @return A raster stack of daily grid-level heat stress level +#' @export +#' +#' @examples +#' \dontrun{ +#' GD_HS <- cal_heat_stress( +#' TempRes = "day", SECTOR = "MAIZ_I", HS = WBGT1, YEAR_INPUT = 2027, +#' "hurs_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4", +#' "tas_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4", +#' "ps_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4" +#' ) +#' } +cal_heat_stress <- function(TempRes, SECTOR, HS, YEAR_INPUT, ...) { + # check the input temporal resolution + if (missing(TempRes) || TempRes == "" || !TempRes %in% c("day", "month")) { + stop("Error: Please specify TempRes: 'day' or 'month'.") + } + + climate_vars <- list(...) + HS_arg_count <- length(formals(HS)) + if (length(climate_vars) != HS_arg_count) { + stop("The number of climate variables does not match the HS function's requirements.") + } + + # read in individual files of climate variables + for (i in seq_along(climate_vars)) { + assign(paste0("var", i, ".stack"), terra::rast(climate_vars[[i]])) + } + + SECTOR_INDEX <- which(SECTOR_ALL == SECTOR) + sector_filter <- SECTOR_FLAG[[SECTOR_INDEX]] + + # ----- + # subset layers for year of interest + layer_dates <- terra::time(var1.stack) + YEAR <- format(layer_dates, "%Y") + + + assertthat::assert_that( + YEAR_INPUT %in% unique(YEAR), + msg = paste0("invalid YEAR_INPUT, make sure input files include data for ", YEAR_INPUT) + ) + + # subset layers falls into the year of interest + year_layer_index <- which(YEAR == YEAR_INPUT) + + # check number of layers + if (TempRes == "day") { + assertthat::assert_that( + length(year_layer_index) %in% c(365, 366), + msg = paste0("For daily input, expect 365 or 366 layers, but got ", length(year_layer_index), " layers.") + ) + } else if (TempRes == "month") { + assertthat::assert_that( + length(year_layer_index) == 12, + msg = paste0("For monthly input, expect 12 layers, but got ", length(year_layer_index), " layers.") + ) + } + + variable_list <- list() + for (i in 1:length(formals(HS))) { + # assign SECTOR grid filter raster + assign(paste0("var", i, ".SECTOR.y"), var.i.SECTOR.y <- get(paste0("var", i, ".stack"))[[year_layer_index]] * terra::rast(sector_filter)) + rm(list = paste0("var", i, ".stack")) + layer_dates.y <- terra::time(get(paste0("var", i, ".SECTOR.y"))) + assign(paste0("stack", i, ".SECTOR.y"), raster::brick(get(paste0("var", i, ".SECTOR.y")))) + rack <- get(paste0("stack", i, ".SECTOR.y")) + rm(list = paste0("stack", i, ".SECTOR.y")) + # names(rack) <- layer_dates.y + variable_list[[i]] <- rack + } + HS.stack.y <- do.call(overlay, c(variable_list, fun = HS)) + HS.stack.y <- stack(HS.stack.y) + names(HS.stack.y) <- layer_dates.y # name it so that DAY2MON function can work as expected + # check number of layers + if (TempRes == "day") { + assert_that( + nlayers(HS.stack.y) %in% c(365, 366), + msg = paste0("Error: For daily input, expect 365 or 366 layers, but got ", nlayers(HS.stack.y), " layers.") + ) + } else if (TempRes == "month") { + assert_that( + nlayers(HS.stack.y) == 12, + msg = paste0("Error: For monthly input, expect 12 layers, but got ", nlayers(HS.stack.y), " layers.") + ) + } + return(HS.stack.y) +} diff --git a/R/PWC.R b/R/cal_pwc.R similarity index 62% rename from R/PWC.R rename to R/cal_pwc.R index 75b57ad..c061cc3 100644 --- a/R/PWC.R +++ b/R/cal_pwc.R @@ -1,4 +1,10 @@ -#' PWC +#' cal_pwc +#' +#' Calculate Physical Work Capacity (PWC) +#' +#' The `cal_pwc` function computes daily grid-level physical work capacity (PWC), +#' which range from 0 to 1, based on heat stress levels, a chosen labor heat response function, +#' and workload intensity. #' #' @param WBGT output from HeatStress function #' @param LHR choice of labor heat response function @@ -12,10 +18,9 @@ #' #' @examples #' \dontrun{ -#' GD_PWC <- PWC(HeatStress = HS.stack.y, LHR = LHR_Foster, workload = "high") +#' GD_PWC <- cal_pwc(HeatStress = HS.stack.y, LHR = LHR_Foster, workload = "high") #' } - -PWC <- function(WBGT, LHR, workload){ +cal_pwc <- function(WBGT, LHR, workload) { PWC.stack.y <- stack(lapply(1:nlayers(WBGT), function(nlay) { calc(WBGT[[nlay]], function(x) LHR(x, workload)) })) diff --git a/R/daily_to_monthly.R b/R/daily_to_monthly.R new file mode 100644 index 0000000..e0c0b8a --- /dev/null +++ b/R/daily_to_monthly.R @@ -0,0 +1,29 @@ +#' daily_to_monthly +#' +#' Convert Daily Raster Data to Monthly Averages +#' +#' This function takes a raster stack of daily values and computes the monthly averages for each pixel. +#' +#' @param input_rack Raster rack of daily value +#' +#' @importFrom raster stack overlay calc stackApply as.matrix ncell nlayers +#' @importFrom dplyr %>% +#' +#' @return A raster rack of monthly value +#' @export +#' +#' @examples +#' \dontrun{ +#' month_PWC <- daily_to_monthly(input_rack = PWC.stack.y) +#' month_HS <- daily_to_monthly(input_rack = HS.stack.y) +#' } +daily_to_monthly <- function(input_rack) { + # Create a vector of months corresponding to each layer + layer_names <- names(input_rack) + layer_dates <- as.Date(gsub("X", "", layer_names), format = "%Y.%m.%d") + MONTHS <- format(layer_dates, "%m") + # Calculate monthly mean using stackApply + monthly_mean <- stackApply(input_rack, indices = MONTHS, fun = mean) + monthly_mean <- stack(monthly_mean) + return(monthly_mean) +} diff --git a/R/grid_to_region.R b/R/grid_to_region.R new file mode 100644 index 0000000..1c8b690 --- /dev/null +++ b/R/grid_to_region.R @@ -0,0 +1,53 @@ +#' grid_to_region +#' +#' Aggregate Grid-Level Annual Values to Regional-Level +#' +#' The `grid_to_region` function aggregates grid-level annual values to regional scales using spatial mapping +#' weights and rasterized regional boundaries. This allows for the computation of regional annual +#' values for a specified sector. +#' +#' @param grid_annual_value data.frame of grid-level annual value +#' @param SECTOR choice of sector +#' @param rast_boundary rasterized regional boundaries, 0.5 degree, Set to global extent +#' +#' @importFrom dplyr %>% +#' @import assertthat +#' +#' @return A sf data frame of regional annual value +#' @export +#' +#' @examples +#' \dontrun{ +#' reg_annual_value <- grid_to_region(grid_annual_value = pwc.hothaps.ann, SECTOR = "MAIZ_R", rast_boundary = country_raster) +#' } +grid_to_region <- function(grid_annual_value, SECTOR, rast_boundary) { + # obtain spatial mapping weights information based on the SECTOR choice + SECTOR_INDEX <- which(HELPS::SECTOR_ALL == SECTOR) + smw <- HELPS::SECTOR_SMW[[SECTOR_INDEX]] + smw_df <- smw %>% + as.data.frame(xy = T) %>% + setNames(c("x", "y", "smw")) + + # transform the rasterized regional boundaries to data.frame + rast_boundary %>% as.data.frame(xy = T) -> rast_boundary_df + + # check if (x,y) are identical between grid_annual_value, smw_df and rast_boundary_df + assertthat::assert_that( + identical(round(grid_annual_value$x, 2), round(smw_df$x, 2), round(rast_boundary_df$x, 2)), + msg = paste0("longtitudes don't match between grid_annual_value and smw_df ") + ) + + assertthat::assert_that( + identical(round(grid_annual_value$y, 2), round(smw_df$y, 2), round(rast_boundary_df$y, 2)), + msg = paste0("latitudes don't match between grid_annual_value and smw_df ") + ) + + grid_annual_value$smw <- smw_df$smw + grid_annual_value$region_id <- rast_boundary_df[, 3] + + grid_annual_value %>% + dplyr::group_by(region_id) %>% + dplyr::summarise(value = weighted.mean(value, smw, na.rm = T)) -> + reg_annual_value + return(reg_annual_value) +} diff --git a/R/monthly_to_annual.R b/R/monthly_to_annual.R new file mode 100644 index 0000000..71155c3 --- /dev/null +++ b/R/monthly_to_annual.R @@ -0,0 +1,48 @@ +#' monthly_to_annual +#' +#' Calculate Annual Grid-Level Values from Monthly Raster Data +#' The `monthly_to_annual` function computes annual grid-level values from a raster stack of monthly data, +#' This function supports a wide range of agricultural sectors and accounts for variations in crop calendars. + +#' @param input_rack Raster rack of monthly value +#' @param SECTOR choice of sector +#' +#' @importFrom raster stack overlay calc stackApply as.matrix as.data.frame ncell nlayers +#' @importFrom dplyr %>% +#' +#' @return A data frame of grid-level annual value +#' @export +#' +#' @examples +#' \dontrun{ +#' annual_PWC <- monthly_to_annual(input_rack = month_PWC, SECTOR = "MAIZ_I") +#' annual_HS <- monthly_to_annual(input_rack = month_HS, SECTOR = "MAIZ_I") +#' } +monthly_to_annual <- function(input_rack, SECTOR) { + if (SECTOR %in% c( + "WHEA_I", "RICE_I", "MAIZ_I", "SOYB_I", "BARL_I", "MILL_I", "PMIL_I", "SORG_I", "OCER_I", + "POTA_I", "SWPO_I", "YAMS_I", "CASS_I", "BEAN_I", "CHIC_I", "COWP_I", "PIGE_I", "LENT_I", + "GROU_I", "SUNF_I", "RAPE_I", "SESA_I", "SUGC_I", "SUGB_I", "COTT_I", + "WHEA_R", "RICE_R", "MAIZ_R", "SOYB_R", "BARL_R", "MILL_R", "PMIL_R", "SORG_R", "OCER_R", + "POTA_R", "SWPO_R", "YAMS_R", "CASS_R", "BEAN_R", "CHIC_R", "COWP_R", "PIGE_R", "LENT_R", + "GROU_R", "SUNF_R", "RAPE_R", "SESA_R", "SUGC_R", "SUGB_R", "COTT_R" + )) { + SECTOR_INDEX <- which(SECTOR_GGCMI == SECTOR) + month_weight <- SECTOR_MONTH_WEIGHT[[SECTOR_INDEX]] + } else if (SECTOR %in% c( + "OFIB_I", "BANA_I", "PLNT_I", "CITR_I", "TROF_I", "TEMF_I", "TOMA_I", "ONIO_I", "VEGE_I", + "ORTS_I", "OPUL_I", "CNUT_I", "OILP_I", "OOIL_I", "COFF_I", "RCOF_I", "COCO_I", "RUBB_I", + "TEAS_I", "TOBA_I", "REST_I", + "OFIB_R", "BANA_R", "PLNT_R", "CITR_R", "TROF_R", "TEMF_R", "TOMA_R", "ONIO_R", "VEGE_R", + "ORTS_R", "OPUL_R", "CNUT_R", "OILP_R", "OOIL_R", "COFF_R", "RCOF_R", "COCO_R", "RUBB_R", + "TEAS_R", "TOBA_R", "REST_R", + "NONCROP" + )) { + month_weight <- extended_weight_others + } + input_mtx <- as.matrix(input_rack) * month_weight + annual_mean <- apply(input_mtx, 1, function(row) if (all(is.na(row))) NA else sum(row, na.rm = TRUE)) + annual_output <- as.data.frame(input_rack, xy = T) %>% dplyr::select(x, y) + annual_output$value <- annual_mean + return(annual_output) +} diff --git a/man/DAY2MON.Rd b/man/DAY2MON.Rd deleted file mode 100644 index 5657861..0000000 --- a/man/DAY2MON.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/DAY2MON.R -\name{DAY2MON} -\alias{DAY2MON} -\title{DAY2MON} -\usage{ -DAY2MON(input_rack) -} -\arguments{ -\item{input_rack}{Raster rack of daily value} -} -\value{ -A raster rack of monthly value -} -\description{ -DAY2MON -} -\examples{ -\dontrun{ -month_PWC <- DAY2MON(input_rack = PWC.stack.y) -month_HS <- DAY2MON(input_rack = HS.stack.y) -} -} diff --git a/man/G2R.Rd b/man/G2R.Rd deleted file mode 100644 index 79d7f16..0000000 --- a/man/G2R.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/G2R.R -\name{G2R} -\alias{G2R} -\title{G2R} -\usage{ -G2R(grid_annual_value, SECTOR, rast_boundary) -} -\arguments{ -\item{grid_annual_value}{data.frame of grid-level annual value} - -\item{SECTOR}{choice of sector} - -\item{rast_boundary}{rasterized regional boundaries, 0.5 degree, Set to global extent} -} -\value{ -A sf data frame of regional annual value -} -\description{ -G2R -} -\examples{ -\dontrun{ -reg_annual_value <- G2R(grid_annual_value = pwc.hothaps.ann, SECTOR = "MAIZ_R", rast_boundary = country_raster) -} -} diff --git a/man/HeatStress.Rd b/man/HeatStress.Rd deleted file mode 100644 index 26a65c2..0000000 --- a/man/HeatStress.Rd +++ /dev/null @@ -1,33 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/HeatStress.R -\name{HeatStress} -\alias{HeatStress} -\title{HeatStress} -\usage{ -HeatStress(TempRes, SECTOR, HS, YEAR_INPUT, ...) -} -\arguments{ -\item{TempRes}{temporal resolution of input, "day" or "month"} - -\item{SECTOR}{choice of sector} - -\item{HS}{choice of heat stress function} - -\item{YEAR_INPUT}{a vector of years of interest} - -\item{...}{individual .nc file name of climate variables in order} -} -\value{ -A raster stack of daily grid-level heat stress level -} -\description{ -HeatStress -} -\examples{ -\dontrun{ -GD_HS <- DAY2ANN(TempRes = "day", SECTOR = "MAIZ_I", HS = WBGT1, YEAR_INPUT = 2027, -"hurs_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4", -"tas_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4", -"ps_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4") -} -} diff --git a/man/MON2ANN.Rd b/man/MON2ANN.Rd deleted file mode 100644 index 2334464..0000000 --- a/man/MON2ANN.Rd +++ /dev/null @@ -1,25 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/MON2ANN.R -\name{MON2ANN} -\alias{MON2ANN} -\title{MON2ANN} -\usage{ -MON2ANN(input_rack, SECTOR) -} -\arguments{ -\item{input_rack}{Raster rack of monthly value} - -\item{SECTOR}{choice of sector} -} -\value{ -A data frame of grid-level annual value -} -\description{ -MON2ANN -} -\examples{ -\dontrun{ -annual_PWC <- MON2ANN(input_rack = month_PWC, SECTOR = "MAIZ_I") -annual_HS <- MON2ANN(input_rack = month_HS, SECTOR = "MAIZ_I") -} -} diff --git a/man/PWC.Rd b/man/PWC.Rd deleted file mode 100644 index d645c9c..0000000 --- a/man/PWC.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/PWC.R -\name{PWC} -\alias{PWC} -\title{PWC} -\usage{ -PWC(WBGT, LHR, workload) -} -\arguments{ -\item{WBGT}{output from HeatStress function} - -\item{LHR}{choice of labor heat response function} - -\item{workload}{choice of workload intensity, 'high', 'moderate', and, 'low'} -} -\value{ -A raster stack of daily grid-level physical work capacity multiplier: ranges from 0 to 1 -} -\description{ -PWC -} -\examples{ -\dontrun{ -GD_PWC <- PWC(HeatStress = HS.stack.y, LHR = LHR_Foster, workload = "high") -} -} diff --git a/man/cal_heat_stress.Rd b/man/cal_heat_stress.Rd new file mode 100644 index 0000000..c17a594 --- /dev/null +++ b/man/cal_heat_stress.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/cal_heat_stress.R +\name{cal_heat_stress} +\alias{cal_heat_stress} +\title{cal_heat_stress} +\usage{ +cal_heat_stress(TempRes, SECTOR, HS, YEAR_INPUT, ...) +} +\arguments{ +\item{TempRes}{temporal resolution of input, "day" or "month"} + +\item{SECTOR}{choice of sector} + +\item{HS}{choice of heat stress function} + +\item{YEAR_INPUT}{a vector of years of interest} + +\item{...}{individual .nc file name of climate variables in order} +} +\value{ +A raster stack of daily grid-level heat stress level +} +\description{ +Calculate Heat Stress Levels Based on Climate Projections +} +\details{ +The \code{cal_heat_stress} function computes daily or monthly grid-level heat stress levels for specified +sectors, using a user-defined heat stress function and climate data. The function +supports both daily and monthly climate inputs and evaluates heat stress over a specific year. +} +\examples{ +\dontrun{ +GD_HS <- cal_heat_stress( + TempRes = "day", SECTOR = "MAIZ_I", HS = WBGT1, YEAR_INPUT = 2027, + "hurs_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4", + "tas_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4", + "ps_day_GFDL-ESM2M_rcp60_r1i1p1_EWEMBI_20210101-20301231.nc4" +) +} +} diff --git a/man/cal_pwc.Rd b/man/cal_pwc.Rd new file mode 100644 index 0000000..65209ba --- /dev/null +++ b/man/cal_pwc.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/cal_pwc.R +\name{cal_pwc} +\alias{cal_pwc} +\title{cal_pwc} +\usage{ +cal_pwc(WBGT, LHR, workload) +} +\arguments{ +\item{WBGT}{output from HeatStress function} + +\item{LHR}{choice of labor heat response function} + +\item{workload}{choice of workload intensity, 'high', 'moderate', and, 'low'} +} +\value{ +A raster stack of daily grid-level physical work capacity multiplier: ranges from 0 to 1 +} +\description{ +Calculate Physical Work Capacity (PWC) +} +\details{ +The \code{cal_pwc} function computes daily grid-level physical work capacity (PWC), +which range from 0 to 1, based on heat stress levels, a chosen labor heat response function, +and workload intensity. +} +\examples{ +\dontrun{ +GD_PWC <- cal_pwc(HeatStress = HS.stack.y, LHR = LHR_Foster, workload = "high") +} +} diff --git a/man/daily_to_monthly.Rd b/man/daily_to_monthly.Rd new file mode 100644 index 0000000..17f1155 --- /dev/null +++ b/man/daily_to_monthly.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/daily_to_monthly.R +\name{daily_to_monthly} +\alias{daily_to_monthly} +\title{daily_to_monthly} +\usage{ +daily_to_monthly(input_rack) +} +\arguments{ +\item{input_rack}{Raster rack of daily value} +} +\value{ +A raster rack of monthly value +} +\description{ +Convert Daily Raster Data to Monthly Averages +} +\details{ +This function takes a raster stack of daily values and computes the monthly averages for each pixel. +} +\examples{ +\dontrun{ +month_PWC <- daily_to_monthly(input_rack = PWC.stack.y) +month_HS <- daily_to_monthly(input_rack = HS.stack.y) +} +} diff --git a/man/grid_to_region.Rd b/man/grid_to_region.Rd new file mode 100644 index 0000000..f8d8810 --- /dev/null +++ b/man/grid_to_region.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/grid_to_region.R +\name{grid_to_region} +\alias{grid_to_region} +\title{grid_to_region} +\usage{ +grid_to_region(grid_annual_value, SECTOR, rast_boundary) +} +\arguments{ +\item{grid_annual_value}{data.frame of grid-level annual value} + +\item{SECTOR}{choice of sector} + +\item{rast_boundary}{rasterized regional boundaries, 0.5 degree, Set to global extent} +} +\value{ +A sf data frame of regional annual value +} +\description{ +Aggregate Grid-Level Annual Values to Regional-Level +} +\details{ +The \code{grid_to_region} function aggregates grid-level annual values to regional scales using spatial mapping +weights and rasterized regional boundaries. This allows for the computation of regional annual +values for a specified sector. +} +\examples{ +\dontrun{ +reg_annual_value <- grid_to_region(grid_annual_value = pwc.hothaps.ann, SECTOR = "MAIZ_R", rast_boundary = country_raster) +} +} diff --git a/man/monthly_to_annual.Rd b/man/monthly_to_annual.Rd new file mode 100644 index 0000000..6949d5b --- /dev/null +++ b/man/monthly_to_annual.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/monthly_to_annual.R +\name{monthly_to_annual} +\alias{monthly_to_annual} +\title{monthly_to_annual} +\usage{ +monthly_to_annual(input_rack, SECTOR) +} +\arguments{ +\item{input_rack}{Raster rack of monthly value} + +\item{SECTOR}{choice of sector} +} +\value{ +A data frame of grid-level annual value +} +\description{ +Calculate Annual Grid-Level Values from Monthly Raster Data +The \code{monthly_to_annual} function computes annual grid-level values from a raster stack of monthly data, +This function supports a wide range of agricultural sectors and accounts for variations in crop calendars. +} +\examples{ +\dontrun{ +annual_PWC <- monthly_to_annual(input_rack = month_PWC, SECTOR = "MAIZ_I") +annual_HS <- monthly_to_annual(input_rack = month_HS, SECTOR = "MAIZ_I") +} +}