generated from JGCRI/rtemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update paper, meta data. Update test_gaea. Improve vignettes
- Loading branch information
Showing
14 changed files
with
361 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
library(dplyr) | ||
|
||
# ------------------------------- | ||
# Prepare example data | ||
# ------------------------------- | ||
|
||
# output directory | ||
output_dir_i <- file.path(getwd(), 'output') | ||
|
||
# download data | ||
data_dir <- gaea::get_example_data( | ||
download_url = 'https://zenodo.org/records/13179630/files/weighted_climate.zip?download=1', | ||
data_dir = output_dir_i) | ||
|
||
# setup variables | ||
climate_hist_dir_i <- file.path(data_dir, 'canesm5_hist') | ||
climate_impact_dir_i <- file.path(data_dir, 'canesm5') | ||
|
||
climate_model_i <- 'canesm5' | ||
climate_scenario_i <- 'gcam-ref' | ||
member_i = 'r1i1p1f1' | ||
bias_adj_i = 'w5e5' | ||
|
||
cfe_i = 'no-cfe' | ||
gcam_version_i = 'gcam7' | ||
|
||
base_year_i = 2015 | ||
start_year_i = 2015 | ||
end_year_i = 2100 | ||
smooth_window_i = 20 | ||
|
||
diagnostics_i <- T | ||
use_default_coeff_i <- F | ||
|
||
|
||
# ------------------------------- | ||
# Functions | ||
# ------------------------------- | ||
|
||
# Step 1: weighted_climate | ||
|
||
# Step 2: crop_calendar | ||
run_crop_calendars <- function(output_dir = output_dir_i){ | ||
|
||
output <- gaea::crop_calendars(output_dir = output_dir) | ||
|
||
return(output) | ||
} | ||
|
||
# Step 3: data_aggregation | ||
run_data_aggregation <- function(climate_hist_dir = climate_hist_dir_i, | ||
climate_impact_dir = climate_impact_dir_i, | ||
climate_model = climate_model_i, | ||
climate_scenario = climate_scenario_i, | ||
output_dir = output_dir_i){ | ||
|
||
output <- gaea::data_aggregation(climate_hist_dir = climate_hist_dir, | ||
climate_impact_dir = climate_impact_dir, | ||
climate_model = climate_model, | ||
climate_scenario = climate_scenario, | ||
output_dir = output_dir) | ||
|
||
return(output) | ||
} | ||
|
||
|
||
# Step 4: yield_regression | ||
run_yield_regression <- function(diagnostics = diagnostics_i, | ||
output_dir = output_dir_i){ | ||
|
||
gaea::yield_regression(diagnostics = diagnostics, | ||
output_dir = output_dir) | ||
|
||
} | ||
|
||
|
||
# Step 5: yield_projections | ||
|
||
run_yield_projections <- function(use_default_coeff = use_default_coeff_i, | ||
climate_model = climate_model_i, | ||
climate_scenario = climate_scenario_i, | ||
base_year = base_year_i, | ||
start_year = start_year_i, | ||
end_year = end_year_i, | ||
smooth_window = smooth_window_i, | ||
diagnostics = diagnostics_i, | ||
output_dir = output_dir_i){ | ||
|
||
output <- gaea::yield_projections(use_default_coeff = use_default_coeff, | ||
climate_model = climate_model, | ||
climate_scenario = climate_scenario, | ||
base_year = base_year, | ||
start_year = start_year, | ||
end_year = end_year, | ||
smooth_window = smooth_window, | ||
diagnostics = diagnostics, | ||
output_dir = output_dir) | ||
|
||
return(output) | ||
|
||
} | ||
|
||
# Step 6: gcam_agprodchange | ||
|
||
run_gcam_agprodchange <- function(data = NULL, | ||
climate_model = climate_model_i, | ||
climate_scenario = climate_scenario_i, | ||
member = member_i, | ||
bias_adj = bias_adj_i, | ||
cfe = cfe_i, | ||
gcam_version = gcam_version_i, | ||
diagnostics = diagnostics_i, | ||
output_dir = output_dir_i){ | ||
|
||
output <- gaea::gcam_agprodchange(data = df_yield_projection, | ||
climate_model = climate_model, | ||
climate_scenario = climate_scenario, | ||
member = member, | ||
bias_adj = bias_adj, | ||
cfe = cfe, | ||
gcam_version = gcam_version, | ||
diagnostics = diagnostics, | ||
output_dir = output_dir) | ||
|
||
return(output) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,71 @@ | ||
library(gaea); library(testthat); | ||
library(sf) | ||
|
||
timestep = 'monthly' | ||
# climate_model = 'canesm5' | ||
# climate_scenario = 'gcam-ref' | ||
# time_periods = seq(2015, 2020, 1) | ||
output_dir = file.path(getwd(), 'output') | ||
testthat::skip_on_cran() | ||
testthat::skip_on_travis() | ||
testthat::skip_on_ci() | ||
|
||
# Run tests for each function | ||
test_that("weighted_climate runs correctly", { | ||
testthat::expect_error(gaea::weighted_climate(timestep = NULL)) | ||
# ------------------------------------ | ||
# Testing Outputs from Major Functions | ||
# ------------------------------------ | ||
# | ||
# test_that("crop_calendars runs correctly", { | ||
# | ||
# out_crop_calendars <- run_crop_calendars() | ||
# | ||
# testthat::expect_snapshot_file( | ||
# testthat::test_path('output', 'data_processed', 'crop_calendar.csv')) | ||
# }) | ||
# | ||
# test_that("data_aggregation runs correctly", { | ||
# | ||
# out_data_aggregation <- run_data_aggregation() | ||
# | ||
# testthat::expect_snapshot_file( | ||
# testthat::test_path('output', 'data_processed', 'weather_canesm5_gcam-ref_wheat.csv')) | ||
# testthat::expect_snapshot_file( | ||
# testthat::test_path('output', 'data_processed', 'historic_vars_wheat.csv')) | ||
# }) | ||
# | ||
# | ||
# test_that("yield_regression runs correctly", { | ||
# | ||
# out_yield_regression <- run_yield_regression() | ||
# | ||
# testthat::expect_snapshot_file( | ||
# testthat::test_path('output', 'data_processed', 'reg_out_wheat_fit_lnyield_mmm_quad_noco2_nogdp.csv')) | ||
# testthat::expect_snapshot_file( | ||
# testthat::test_path('output', 'data_processed', 'weather_yield_wheat.csv')) | ||
# testthat::expect_snapshot_file( | ||
# testthat::test_path('output', 'figures', 'model_wheat_fit_lnyield_mmm_quad_noco2_nogdp.pdf')) | ||
# }) | ||
|
||
test_that("yield_projections runs correctly", { | ||
|
||
out_yield_projections <- run_yield_projections() | ||
|
||
testthat::expect_snapshot_file( | ||
testthat::test_path('output', 'yield_impacts_annual', 'yield_canesm5_gcam-ref_wheat.csv')) | ||
testthat::expect_snapshot_file( | ||
testthat::test_path('output', 'yield_impacts_smooth', 'yield_canesm5_gcam-ref_wheat.csv')) | ||
testthat::expect_snapshot_file( | ||
testthat::test_path('output', 'data_processed', 'format_yield_change_rel2015.csv')) | ||
testthat::expect_snapshot_file( | ||
testthat::test_path('output', 'figures', 'annual_projected_climate_impacts_canesm5_gcam-ref_wheat_fit_lnyield_mmm_quad_noco2_nogdp.pdf')) | ||
testthat::expect_snapshot_file( | ||
testthat::test_path('output', 'figures', 'smooth_projected_climate_impacts_canesm5_gcam-ref_wheat_fit_lnyield_mmm_quad_noco2_nogdp.pdf')) | ||
testthat::expect_snapshot_file( | ||
testthat::test_path('output', 'maps', 'map_canesm5_gcam-ref_wheat_2090.pdf')) | ||
}) | ||
|
||
# Run tests for each function | ||
test_that("crop_calendars runs correctly", { | ||
crop_cal <- gaea::crop_calendars(output_dir = output_dir) | ||
|
||
test_that("gcam_agprodchange runs correctly", { | ||
|
||
out_gcam_agprodchange <- run_gcam_agprodchange(data = out_yield_projections) | ||
|
||
testthat::expect_snapshot_file( | ||
testthat::test_path('output', 'gcam7_agprodchange_no-cfe', 'agyield_impact_canesm5_r1i1p1f1_w5e5v2_gcam-ref.xml')) | ||
testthat::expect_snapshot_file( | ||
testthat::test_path('output', 'gcam7_agprodchange_no-cfe', 'figures_yield_impacts', 'Wheat.png')) | ||
testthat::expect_snapshot_file( | ||
testthat::test_path('output', 'data_processed', 'crop_calendar.csv')) | ||
testthat::test_path('output', 'gcam7_agprodchange_no-cfe', 'figures_agprodchange', 'Wheat.png')) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
## ----eval=FALSE--------------------------------------------------------------- | ||
# | ||
# # load gaea | ||
# library(gaea) | ||
# | ||
# # NOTE: please change `data_dir` to your desired location for downloaded data | ||
# data_dir <- gaea::get_example_data( | ||
# download_url = 'https://zenodo.org/records/13179630/files/gaea_example_climate.zip?download=1', | ||
# data_dir = 'path/to/desired/location' | ||
# ) | ||
# | ||
# # Path to the climate NetCDF files | ||
# # NOTE: Each variable can have more than one file | ||
# # historical climate data | ||
# pr_historical_file <- file.path(data_dir, 'pr_monthly_canesm5_w5e5_rcp7_1950_2014.nc') | ||
# tas_historical_file <- file.path(data_dir, 'tas_monthly_canesm5_w5e5_rcp7_1950_2014.nc') | ||
# | ||
# # projected climate data | ||
# pr_projection_file <- file.path(data_dir, 'pr_monthly_canesm5_w5e5_rcp7_2015_2100.nc') | ||
# tas_projection_file <- file.path(data_dir, 'tas_monthly_canesm5_w5e5_rcp7_2015_2100.nc') | ||
# | ||
# # Run gaea | ||
# # The full run with raw climate data can take up to an hour | ||
# gaea::yield_impact( | ||
# pr_hist_ncdf = pr_historical_file, | ||
# tas_hist_ncdf = tas_historical_file, | ||
# pr_proj_ncdf = pr_projection_file, | ||
# tas_proj_ncdf = tas_projection_file, | ||
# timestep = 'monthly', # specify the time step of the NetCDF data (monthly or daily) | ||
# historical_periods = c(1950:2014), # vector of historical years selected for fitting | ||
# climate_model = 'canesm5', # label of climate model name | ||
# climate_scenario = 'gcam-ref', # label of climate scenario name | ||
# member = 'r1i1p1f1', # label of ensemble member name | ||
# bias_adj = 'w5e5', # label of climate data for bias adjustment | ||
# cfe = 'no-cfe', # label of CO2 fertilization effect in the formula (default is no CFE) | ||
# gcam_version = 'gcam7', # output is different depending on the GCAM version (gcam6 or gcam7) | ||
# use_default_coeff = FALSE, # set to TRUE when there is no historical climate data available | ||
# base_year = 2015 # GCAM base year | ||
# start_year = 2015, # start year of the projected climate data | ||
# end_year = 2100, # end year of the projected climate data | ||
# smooth_window = 20, # number of years as smoothing window | ||
# co2_hist = NULL, # historical annual CO2 concentration. If NULL, will use default value | ||
# co2_proj = NULL, # projected annual CO2 concentration. If NULL, will use default value | ||
# diagnostics = TRUE, # set to TRUE to output diagnostic plots | ||
# output_dir = 'path/to/output/folder' # path to the output folder | ||
# ) | ||
# | ||
|
Oops, something went wrong.