Skip to content

Commit

Permalink
Fixed crop naming issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hahsan1 committed Apr 7, 2024
1 parent d2ea94f commit a68371f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
58 changes: 28 additions & 30 deletions R/grid_to_basin_yield.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @param region_id Default = NULL. Filters to specified GCAM region (1-32), otherwise no filter
#' @param gridded_yield_dir Default = NULL.
#' @param write_dir Default = "step2_grid_to_basin_yield". Output Folder
#' @param crops Default = c("maize", "rice", "soy", "spring wheat", "winter wheat")
#' @param crops Default = c("Corn", "Rice", "Soy", "Spring Wheat", "Winter Wheat")
#' @param esm_name Default = 'WRF'
#' @param cm_name Default = 'LPJmL'
#' @param scn_name Default = 'rcp8p5_hotter'
Expand All @@ -39,7 +39,7 @@ grid_to_basin_yield <- function(carbon = NULL,
region_id = NULL,
gridded_yield_dir = NULL,
write_dir = "step2_grid_to_basin_yield",
crops = c("maize", "rice", "soy", "spring wheat", "winter wheat"),
crops = c("Corn", "Rice", "Soy", "Spring Wheat", "Winter Wheat"),
esm_name = 'WRF',
cm_name = 'LPJmL',
scn_name = 'rcp8p5_hotter',
Expand Down Expand Up @@ -95,10 +95,10 @@ grid_to_basin_yield <- function(carbon = NULL,
# This matches yield data to harvested area data
# ... both of which come in different files.
match_crop <- function(CROP){
croplist <- c("crop01" = "wheat",
"crop02" = "maize",
"crop03" = "rice",
"crop08" = "soy",
croplist <- c("crop01" = "Wheat",
"crop02" = "Corn",
"crop03" = "Rice",
"crop08" = "Soy",
"crop11" = "cas",
"crop25" = "mgr",
"crop06" = "mil",
Expand Down Expand Up @@ -280,10 +280,10 @@ grid_to_basin_yield <- function(carbon = NULL,
## Then you'll do the aggregation with MIRCA harvested area data below:
aggregate_halfdeg_yield2basin <- function(halfdeg_yield){

if(unique(halfdeg_yield$crop) == 'Corn'){
area_crop_name <- 'maize'
if(unique(halfdeg_yield$crop) %in% c('Spring Wheat', 'Winter Wheat')){
area_crop_name <- 'Wheat'
}else{
area_crop_name <- tolower(unique(halfdeg_yield$crop))
area_crop_name <- unique(halfdeg_yield$crop)
}

if(unique(halfdeg_yield$irr) == 'IRR'){
Expand Down Expand Up @@ -472,14 +472,8 @@ grid_to_basin_yield <- function(carbon = NULL,


# read in climate data
if(crop == 'maize'){
rf_tp <- utils::read.csv(inputlist[grepl('corn', inputlist) & grepl('rfd', inputlist)], stringsAsFactors = F)
ir_tp <- utils::read.csv(inputlist[grepl('corn', inputlist) & grepl('irr', inputlist)], stringsAsFactors = F)
}else{
rf_tp <- utils::read.csv(inputlist[grepl(crop, inputlist) & grepl('rfd', inputlist)], stringsAsFactors = F)
ir_tp <- utils::read.csv(inputlist[grepl(crop, inputlist) & grepl('irr', inputlist)], stringsAsFactors = F)
}

rf_tp <- utils::read.csv(inputlist[grepl(tolower(crop), inputlist) & grepl('rfd', inputlist)], stringsAsFactors = F)
ir_tp <- utils::read.csv(inputlist[grepl(tolower(crop), inputlist) & grepl('irr', inputlist)], stringsAsFactors = F)

# Combine C, N, T and P data with ir and rfd params to get ir, rf yields in each
# grid cell in each year
Expand Down Expand Up @@ -599,11 +593,11 @@ grid_to_basin_yield <- function(carbon = NULL,

# Save gridded yield data (as well as deltaT and delatP) by crop and irr
ir_yields %>%
utils::write.csv(., paste0(gridded_yield_dir, '/', cm_name, "_", esm_name, '_', scn_name,'_', crop, '_irr_gridded_yield_deltaT_deltaP.csv'),
utils::write.csv(., paste0(gridded_yield_dir, '/', cm_name, "_", esm_name, '_', scn_name,'_', tolower(crop), '_irr_gridded_yield_deltaT_deltaP.csv'),
row.names = F)

rf_yields %>%
utils::write.csv(., paste0(gridded_yield_dir, '/', cm_name, "_", esm_name, '_', scn_name,'_', crop, '_rfd_gridded_yield_deltaT_deltaP.csv'),
utils::write.csv(., paste0(gridded_yield_dir, '/', cm_name, "_", esm_name, '_', scn_name,'_', tolower(crop), '_rfd_gridded_yield_deltaT_deltaP.csv'),
row.names = F)
}

Expand All @@ -612,27 +606,31 @@ grid_to_basin_yield <- function(carbon = NULL,
# we load from file. This helps to deal with the spring and winter wheat aggregation.
# We deal with wheat separately first. Then the rest of the crops are done in a loop.

if (any(grepl("wheat", crops))) {
if (any(grepl("Wheat", crops))) {

crop <- "wheat"
crop <- "Wheat"
rlang::inform(paste0("Generating basin yield for ", crop))

griddedlist <- list.files(path=gridded_yield_dir, pattern = paste0(esm_name, "_", scn_name), full.names=TRUE, recursive=FALSE)

# Read in spring and winter wheat gridded yield outputs
ir_yield_swheat <- utils::read.csv(griddedlist[grepl('spring wheat', griddedlist) & grepl('irr', griddedlist)], stringsAsFactors = F) %>%
dplyr::select(-c(deltaT, deltaP)) %>%
dplyr:: rename(swheat_yield = yield)
dplyr::rename(swheat_yield = yield) %>%
dplyr::mutate(crop = "Wheat")
rf_yield_swheat <- utils::read.csv(griddedlist[grepl('spring wheat', griddedlist) & grepl('rfd', griddedlist)], stringsAsFactors = F)%>%
dplyr::select(-c(deltaT, deltaP)) %>%
dplyr:: rename(swheat_yield = yield)
dplyr::rename(swheat_yield = yield) %>%
dplyr::mutate(crop = "Wheat")

ir_yield_wwheat <- utils::read.csv(griddedlist[grepl('winter wheat', griddedlist) & grepl('irr', griddedlist)], stringsAsFactors = F)%>%
dplyr::select(-c(deltaT, deltaP)) %>%
dplyr:: rename(wwheat_yield = yield)
dplyr::rename(wwheat_yield = yield) %>%
dplyr::mutate(crop = "Wheat")
rf_yield_wwheat <- utils::read.csv(griddedlist[grepl('winter wheat', griddedlist) & grepl('rfd', griddedlist)], stringsAsFactors = F)%>%
dplyr::select(-c(deltaT, deltaP)) %>%
dplyr:: rename(wwheat_yield = yield)
dplyr::rename(wwheat_yield = yield) %>%
dplyr::mutate(crop = "Wheat")


# Function for adding rows with NA (we want to keep NA if summing two NA, otherwise
Expand Down Expand Up @@ -704,22 +702,22 @@ grid_to_basin_yield <- function(carbon = NULL,


utils::write.csv(yieldByBasin, paste0(write_dir, "/", cm_name, "_",
esm_name, "_", scn_name, "_", crop, "_",
esm_name, "_", scn_name, "_", tolower(crop), "_",
min(years), "_", max(years),".csv"),
row.names = F)

}


# Run basin yield aggregation for remaining crops (not including wheat)
for(crop in crops[!grepl("wheat", crops)]){
for(crop in crops[!grepl("Wheat", crops)]){

rlang::inform(paste0("Generating basin yield for ", crop))

# Read in gridded yield outputs by crop
ir_yields <- utils::read.csv(griddedlist[grepl(crop, griddedlist) & grepl('irr', griddedlist)], stringsAsFactors = F) %>%
ir_yields <- utils::read.csv(griddedlist[grepl(tolower(crop), griddedlist) & grepl('irr', griddedlist)], stringsAsFactors = F) %>%
dplyr::select(-c(deltaT, deltaP))
rf_yields <- utils::read.csv(griddedlist[grepl(crop, griddedlist) & grepl('rfd', griddedlist)], stringsAsFactors = F)%>%
rf_yields <- utils::read.csv(griddedlist[grepl(tolower(crop), griddedlist) & grepl('rfd', griddedlist)], stringsAsFactors = F)%>%
dplyr::select(-c(deltaT, deltaP))


Expand Down Expand Up @@ -759,7 +757,7 @@ grid_to_basin_yield <- function(carbon = NULL,


utils::write.csv(yieldByBasin, paste0(write_dir, "/", cm_name, "_",
esm_name, "_", scn_name, "_", crop, "_",
esm_name, "_", scn_name, "_", tolower(crop), "_",
min(years), "_", max(years),".csv"),
row.names = F)

Expand Down
4 changes: 2 additions & 2 deletions man/grid_to_basin_yield.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a68371f

Please sign in to comment.