Skip to content

Commit

Permalink
Update data handling - make cropland area data external
Browse files Browse the repository at this point in the history
  • Loading branch information
mengqi-z committed Oct 8, 2024
1 parent 21607cc commit 28338f9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 53 deletions.
2 changes: 1 addition & 1 deletion R/gcam_agprodchange.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ get_cropland_weight <- function(gcam_version = 'gcam7',


# for 67420 grid cells with country-glu-reg mapping
grid_ctry_glu_reg <- get_mirca_cropland(raster_brick = mirca_ras_brick,
grid_ctry_glu_reg <- get_mirca_cropland(raster_brick = gaia::mirca_ras_brick,
mapping = mp_rmap)


Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
64 changes: 17 additions & 47 deletions data-raw/DATASET.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,41 +107,6 @@ for(gcam_version in c('gcam6', 'gcam7')){
usethis::use_data(agprodchange_ni_gcam6, overwrite = TRUE)
usethis::use_data(agprodchange_ni_gcam7, overwrite = TRUE)

# For GCAM7
# agprodchange_ni_gcam7 <- dplyr::bind_rows(
# data.table::fread(file.path(agprodchange.dir, 'gcam7', 'L2052.AgProdChange_bio_irr_ref.csv')),
# data.table::fread(file.path(agprodchange.dir, 'gcam7', 'L2052.AgProdChange_ag_irr_ref.csv'))) %>%
# dplyr::mutate(year = paste0('X', year)) %>%
# dplyr::rename(AgProdChange_ni = AgProdChange)
#
# agprodchange_ni_gcam7 <- dplyr::bind_rows(
# agprodchange_ni_gcam7,
# agprodchange_ni_gcam7 %>%
# dplyr::select(-year, -AgProdChange_ni) %>%
# dplyr::distinct() %>%
# dplyr::mutate(year = 'X2015',
# AgProdChange_ni = 0)
# )
#
# usethis::use_data(agprodchange_ni_gcam7, overwrite = TRUE)
#
# # For GCAM6
# agprodchange_ni_gcam6 <- dplyr::bind_rows(
# data.table::fread(file.path(agprodchange.dir, 'gcam6', 'L2052.AgProdChange_bio_irr_ref.csv')),
# data.table::fread(file.path(agprodchange.dir, 'gcam6', 'L2052.AgProdChange_ag_irr_ref.csv'))) %>%
# dplyr::mutate(year = paste0('X', year)) %>%
# dplyr::rename(AgProdChange_ni = AgProdChange)
#
# agprodchange_ni_gcam6 <- dplyr::bind_rows(
# agprodchange_ni_gcam6,
# agprodchange_ni_gcam6 %>%
# dplyr::select(-year, -AgProdChange_ni) %>%
# dplyr::distinct() %>%
# dplyr::mutate(year = 'X2015',
# AgProdChange_ni = 0)
# )
#
# usethis::use_data(agprodchange_ni_gcam6, overwrite = TRUE)


#-------------------------------------------------------------------------------
Expand All @@ -164,6 +129,22 @@ for(crop_name in mapping_mirca_sage$crop_mirca){

usethis::use_data(coef_default, overwrite = TRUE)


#-------------------------------------------------------------------------------
# Raster of Cropland Area
#-------------------------------------------------------------------------------

# cropland area file list
crop_area_list <- list.files(
file.path(mirca.dir, 'harvested_area_grids_26crops_30mn'),
full.names = TRUE)

# convert ASCII files to raster bick
mirca_ras_brick <- raster::stack(crop_area_list)

usethis::use_data(mirca_ras_brick, overwrite = TRUE)


#===============================================================================
#'* Internal Data *
#===============================================================================
Expand Down Expand Up @@ -563,17 +544,6 @@ map_country <- sf_country %>%
sf::st_crs(map_country) <- 4326


#-------------------------------------------------------------------------------
# Raster of Cropland Area
#-------------------------------------------------------------------------------

# cropland area file list
crop_area_list <- list.files(
file.path(mirca.dir, 'harvested_area_grids_26crops_30mn'),
full.names = TRUE)

# convert ASCII files to raster bick
mirca_ras_brick <- raster::stack(crop_area_list)


#'*Save All Internal Data*
Expand All @@ -584,5 +554,5 @@ usethis::use_data(country_id, mapping_country, grid_fao_glu, mapping_fao_glu, ma
mirca_harvest_area, sage, fao_yield, fao_irr_equip, gdp,
waldhoff_formula, y_hat, reg_vars, weight_var, n_sig, fit_name,
col_scale_region, col_fill_region, theme_basic,
map_country, mirca_ras_brick,
map_country,
internal = TRUE, overwrite = TRUE )
Binary file added data/mirca_ras_brick.rda
Binary file not shown.
10 changes: 5 additions & 5 deletions tests/testthat/test-gaea.R → tests/testthat/test-gaia.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ data_dir_i <- gaia::get_example_data(
# Testing Outputs from Major Functions
# ------------------------------------

test_that("crop_calendars runs correctly", {
test_that("crop_calendars function test", {

out_crop_calendars <- run_crop_calendars()

Expand All @@ -26,7 +26,7 @@ test_that("crop_calendars runs correctly", {

})

test_that("data_aggregation runs correctly", {
test_that("data_aggregation function test", {

out_data_aggregation <- run_data_aggregation(data_dir = data_dir_i)

Expand All @@ -41,7 +41,7 @@ test_that("data_aggregation runs correctly", {
})


test_that("yield_regression runs correctly", {
test_that("yield_regression function test", {

out_yield_regression <- run_yield_regression()

Expand All @@ -55,7 +55,7 @@ test_that("yield_regression runs correctly", {
# testthat::test_path('output', 'figures', 'model_wheat_fit_lnyield_mmm_quad_noco2_nogdp.pdf'))
})

test_that("yield_shock_projection runs correctly", {
test_that("yield_shock_projection function test", {

out_yield_projections <- run_yield_shock_projection()

Expand All @@ -76,7 +76,7 @@ test_that("yield_shock_projection runs correctly", {
})


test_that("gcam_agprodchange runs correctly", {
test_that("gcam_agprodchange function test", {

out_yield_projections <- run_yield_shock_projection()
out_gcam_agprodchange <- run_gcam_agprodchange(data = out_yield_projections)
Expand Down

0 comments on commit 28338f9

Please sign in to comment.