Skip to content

Commit

Permalink
Update testthat and vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
mengqi-z committed Aug 4, 2024
1 parent 88d5ee8 commit e227ff3
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 64 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Imports:
stringr (>= 1.5.0),
tibble (>= 3.2.1),
tidyr (>= 1.3.0),
utils
utils,
xml2 (>= 1.3.6)
Suggests:
knitr,
rmarkdown (>= 2.16),
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<p align="center">
<a href="https://jgcri.github.io/gaea/" target="_blank"><img src="https://github.com/JGCRI/jgcricolors/blob/main/vignettes/button_user_guide.PNG?raw=true"
alt="https://jgcri.github.io/gaea/" height="60"/></a>
alt="https://jgcri.github.io/gaea/articles/vignette.html" height="60"/></a>
<img src="https://github.com/JGCRI/jgcricolors/blob/main/vignettes/button_divider.PNG?raw=true" height="40"/>
</p>

Expand Down Expand Up @@ -50,11 +50,29 @@ alt="https://jgcri.github.io/gaea/" height="60"/></a>
install.packages("devtools")
devtools::install_github("JGCRI/gaea")
```

or

```r
install.packages("remotes")
remotes::install_github("JGCRI/gaea")
```

Additional steps for UBUNTU from a terminal

```
sudo add-apt-repository ppa:ubuntugis/ppa
sudo apt-get update
sudo apt-get install libudunits2-dev libgdal-dev libgeos-dev libproj-dev libmagick++-dev
```

Additional steps for MACOSX from a terminal

```
brew install pkg-config
brew install gdal
```

<br/>


Expand Down
11 changes: 8 additions & 3 deletions inst/extras/devTests.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ gaea::weighted_climate(pr_ncdf = 'C:/WorkSpace/GCIMS/GCIMS_Yield/climate_process

if(T){

pr_proj_ncdf = 'C:/WorkSpace/GCIMS/GCIMS_Yield/climate_process/data/climate/pr_mon_basd_CanESM5_W5E5v2_GCAM_ref_2015-2100.nc'
tas_proj_ncdf = 'C:/WorkSpace/GCIMS/GCIMS_Yield/climate_process/data/climate/tas_mon_basd_CanESM5_W5E5v2_GCAM_ref_2015-2100.nc'
# pr_proj_ncdf = 'C:/WorkSpace/GCIMS/GCIMS_Yield/climate_process/data/climate/pr_mon_basd_CanESM5_W5E5v2_GCAM_ref_2015-2100.nc'
# tas_proj_ncdf = 'C:/WorkSpace/GCIMS/GCIMS_Yield/climate_process/data/climate/tas_mon_basd_CanESM5_W5E5v2_GCAM_ref_2015-2100.nc'

pr_hist_ncdf = 'C:/WorkSpace/GCIMS/GCIMS_Yield/climate_process/data/climate/gaea_example_climate/pr_monthly_canesm5_w5e5_rcp7_1950_2014.nc'
tas_hist_ncdf = 'C:/WorkSpace/GCIMS/GCIMS_Yield/climate_process/data/climate/gaea_example_climate/tas_monthly_canesm5_w5e5_rcp7_1950_2014.nc'
pr_proj_ncdf = 'C:/WorkSpace/GCIMS/GCIMS_Yield/climate_process/data/climate/gaea_example_climate/pr_monthly_canesm5_w5e5_rcp7_2015_2100.nc'
tas_proj_ncdf = 'C:/WorkSpace/GCIMS/GCIMS_Yield/climate_process/data/climate/gaea_example_climate/tas_monthly_canesm5_w5e5_rcp7_2015_2100.nc'
timestep = 'monthly'
climate_model = 'canesm5'
climate_scenario = 'gcam-ref'
Expand All @@ -46,7 +51,7 @@ if(T){
co2_hist = NULL
co2_proj = NULL
diagnostics <- TRUE
output_dir = 'C:/WorkSpace/github/test_scripts/gaea/output_test'
output_dir = 'C:/WorkSpace/github/test_scripts/gaea/output_joss_test'

}

Expand Down
Binary file modified paper/workflow.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ start_year_i = 2015
end_year_i = 2100
smooth_window_i = 20

diagnostics_i <- T
diagnostics_i <- F
use_default_coeff_i <- F


Expand Down Expand Up @@ -112,7 +112,7 @@ run_gcam_agprodchange <- function(data = NULL,
diagnostics = diagnostics_i,
output_dir = output_dir_i){

output <- gaea::gcam_agprodchange(data = df_yield_projection,
output <- gaea::gcam_agprodchange(data = data,
climate_model = climate_model,
climate_scenario = climate_scenario,
member = member,
Expand Down
114 changes: 64 additions & 50 deletions tests/testthat/test-gaea.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,84 @@ library(sf)

testthat::skip_on_cran()
testthat::skip_on_travis()
testthat::skip_on_ci()


# ------------------------------------
# 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("crop_calendars runs correctly", {

out_crop_calendars <- run_crop_calendars()

testthat::expect_snapshot(out_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(out_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(out_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'))
testthat::expect_snapshot(out_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'))
})


test_that("gcam_agprodchange runs correctly", {

out_yield_projections <- run_yield_projections()
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', 'gcam7_agprodchange_no-cfe', 'figures_agprodchange', 'Wheat.png'))
testthat::expect_snapshot(out_gcam_agprodchange)

# 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', 'gcam7_agprodchange_no-cfe', 'figures_agprodchange', 'Wheat.png'))
})
56 changes: 55 additions & 1 deletion vignettes/vignette.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## ----eval=FALSE---------------------------------------------------------------
## ----eval=F-------------------------------------------------------------------
#
# # load gaea
# library(gaea)
Expand All @@ -19,6 +19,12 @@
# 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')
#

## ----eval=FALSE---------------------------------------------------------------
#
# # load gaea
# library(gaea)
#
# # Run gaea
# # The full run with raw climate data can take up to an hour
# gaea::yield_impact(
Expand Down Expand Up @@ -46,3 +52,51 @@
# )
#

## ----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/weighted_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
# climate_hist_dir <- file.path(data_dir, 'canesm5_hist')
#
# # projected climate data
# climate_impact_dir <- file.path(data_dir, 'canesm5')
#

## ----eval=FALSE---------------------------------------------------------------
#
# # load gaea
# library(gaea)
#
# # Run gaea
# gaea::yield_impact(
# climate_hist_dir = climate_hist_dir,
# climate_impact_dir = climate_impact_dir,
# timestep = 'monthly', # specify the time step of the NetCDF data (monthly or daily)
# 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
# )
#

Loading

0 comments on commit e227ff3

Please sign in to comment.