Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/bc3LC/rfasst
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Sampedro committed Mar 21, 2024
2 parents d18865d + 38e7d01 commit d5060cf
Show file tree
Hide file tree
Showing 29 changed files with 653 additions and 150 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export(m3_get_daly_o3)
export(m3_get_daly_pm25)
export(m3_get_mort_o3)
export(m3_get_mort_pm25)
export(m3_get_mort_pm25_ecoloss)
export(m3_get_o3_ecoloss_hcl)
export(m3_get_o3_ecoloss_vsl)
export(m3_get_pm25_ecoloss_gdpGrowth)
export(m3_get_pm25_ecoloss_vsl)
export(m3_get_yll_o3)
export(m3_get_yll_pm25)
export(m4_get_prod_loss)
Expand Down
62 changes: 34 additions & 28 deletions R/m1_emissions_rescale.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param query_path Path to the query file
#' @param db_name Name of the GCAM database
#' @param prj_name Name of the rgcam project. This can be an existing project, or, if not, this will be the name
#' @param prj rgcam loaded project
#' @param rdata_name Name of the RData file. It must contain the queries in a list
#' @param scen_name Name of the GCAM scenario to be processed
#' @param queries Name of the GCAM query file. The file by default includes the queries required to run rfasst
Expand All @@ -20,7 +21,7 @@
#' @importFrom magrittr %>%
#' @export

m1_emissions_rescale<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name,
m1_emissions_rescale<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name = NULL, prj = NULL,
rdata_name = NULL, scen_name, queries = "queries_rfasst.xml", final_db_year = 2100,
saveOutput = T, map = F, mapIndivPol = F, anim = T, recompute = F){

Expand All @@ -32,7 +33,8 @@ m1_emissions_rescale<-function(db_path = NULL, query_path = "./inst/extdata", db
#----------------------------------------------------------------------
# Assert that the parameters of the function are okay, or modify when necessary

#if (!endsWith(prj_name, '.dat')) prj_name = paste0(prj_name, '.dat')
if(!endsWith(prj_name, '.dat')) prj_name = paste0(prj_name, '.dat')
if(is.null(prj_name)) assertthat::assert_that(!is.null(prj), msg = 'Specify the project name or pass an uploaded project as parameter')

#----------------------------------------------------------------------
#----------------------------------------------------------------------
Expand Down Expand Up @@ -64,33 +66,37 @@ m1_emissions_rescale<-function(db_path = NULL, query_path = "./inst/extdata", db
dplyr::rename(`ISO 3`=subRegionAlt,
`FASST Region`=fasst_region)

# Load the rgcam project:
if (!is.null(db_path) & !is.null(db_name)) {
rlang::inform('Creating project ...')
conn <- rgcam::localDBConn(db_path,
db_name,migabble = FALSE)
prj <- rgcam::addScenario(conn,
prj_name,
scen_name,
paste0(query_path,"/",queries),
saveProj = F)
prj <- fill_queries(prj, db_path, db_name, prj_name, scen_name,
query_path, queries = 'queries_rfasst_nonCO2.xml')

rgcam::saveProject(prj, file = file.path('output',prj_name))

QUERY_LIST <- c(rgcam::listQueries(prj, c(scen_name)))
} else if (is.null(rdata_name)){
rlang::inform('Loading project ...')
prj <- rgcam::loadProject(prj_name)

QUERY_LIST <- c(rgcam::listQueries(prj, c(scen_name)))
} else {
rlang::inform('Loading RData ...')
if (!exists('prj_rd')) {
prj_rd = get(load(rdata_name))
QUERY_LIST <- names(prj_rd)
# Load the rgcam project if prj not passed as a parameter:
if (is.null(prj)) {
if (!is.null(db_path) & !is.null(db_name)) {
rlang::inform('Creating project ...')
conn <- rgcam::localDBConn(db_path,
db_name,migabble = FALSE)
prj <- rgcam::addScenario(conn,
prj_name,
scen_name,
paste0(query_path,"/",queries),
saveProj = F)
prj <- fill_queries(prj, db_path, db_name, prj_name, scen_name,
query_path, queries = 'queries_rfasst_nonCO2.xml')

rgcam::saveProject(prj, file = file.path('output',prj_name))

QUERY_LIST <- c(rgcam::listQueries(prj, c(scen_name)))
} else if (is.null(rdata_name)){
rlang::inform('Loading project ...')
prj <- rgcam::loadProject(prj_name)

QUERY_LIST <- c(rgcam::listQueries(prj, c(scen_name)))
} else {
rlang::inform('Loading RData ...')
if (!exists('prj_rd')) {
prj_rd = get(load(rdata_name))
QUERY_LIST <- names(prj_rd)
}
}
} else {
QUERY_LIST <- c(rgcam::listQueries(prj, c(scen_name)))
}
rlang::inform('Computing emissions ...')

Expand Down
62 changes: 49 additions & 13 deletions R/m2_conc.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#' m2_get_conc_pm25
#'
#'
#' Produce fine particulate matter (PM2.5) concentration levels for TM5-FASST regions based on re-scaled emission pathways.
#' @keywords module_2, concentration, PM2.5
#' @return Particulate matter (PM2.5) concentration levels for each TM5-FASST regions for all years (ug/m3).The list of countries that form each region and the full name of the region can be found in Table S2.2 in the TM5-FASST documentation paper: Van Dingenen, R., Dentener, F., Crippa, M., Leitao, J., Marmer, E., Rao, S., Solazzo, E. and Valentini, L., 2018. TM5-FASST: a global atmospheric source-receptor model for rapid impact analysis of emission changes on air quality and short-lived climate pollutants. Atmospheric Chemistry and Physics, 18(21), pp.16173-16211.
#' @param db_path Path to the GCAM database
#' @param query_path Path to the query file
#' @param db_name Name of the GCAM database
#' @param prj_name Name of the rgcam project. This can be an existing project, or, if not, this will be the name
#' @param prj rgcam loaded project
#' @param rdata_name Name of the RData file. It must contain the queries in a list
#' @param scen_name Name of the GCAM scenario to be processed
#' @param queries Name of the GCAM query file. The file by default includes the queries required to run rfasst
Expand All @@ -23,7 +23,7 @@
#' @importFrom magrittr %>%
#' @export

m2_get_conc_pm25<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name,
m2_get_conc_pm25<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name = NULL, prj = NULL,
rdata_name = NULL, scen_name, queries = "queries_rfasst.xml", final_db_year = 2100,
saveOutput = T, map = F, anim = T, recompute = F,
downscale = F, saveRaster_grid = F,
Expand All @@ -42,6 +42,7 @@ m2_get_conc_pm25<-function(db_path = NULL, query_path = "./inst/extdata", db_nam
if(agg_grid == "NUTS3") assertthat::assert_that(downscale == T, msg = 'Set `downscale` to TRUE to aggregate the downscaled PM2.5 to NUTS3')
if(save_AggGrid == T) assertthat::assert_that(agg_grid == "NUTS3" & downscale == T,
msg = 'Set `downscale` to TRUE and agg_grid to `NUTS3` to save the aggregated raster grid')
if(is.null(prj_name)) assertthat::assert_that(!is.null(prj), msg = 'Specify the project name or pass an uploaded project as parameter')


#----------------------------------------------------------------------
Expand Down Expand Up @@ -69,7 +70,7 @@ m2_get_conc_pm25<-function(db_path = NULL, query_path = "./inst/extdata", db_nam
dplyr::rename(subRegion=fasst_region) %>%
dplyr::mutate(subRegionAlt=as.factor(subRegionAlt))

em.list<-m1_emissions_rescale(db_path, query_path, db_name, prj_name, rdata_name, scen_name, queries, saveOutput = F, final_db_year, recompute = recompute)
em.list<-m1_emissions_rescale(db_path, query_path, db_name, prj_name, prj, rdata_name, scen_name, queries, saveOutput = F, final_db_year, recompute = recompute)

#----------------------------------------------------------------------
#----------------------------------------------------------------------
Expand Down Expand Up @@ -561,14 +562,14 @@ m2_get_conc_pm25<-function(db_path = NULL, query_path = "./inst/extdata", db_nam

#' m2_get_conc_o3
#'
#'
#' Produce ozone (O3) concentration levels based on re-scaled emission pathways.
#' @keywords module_2, concentration, O3
#' @return Produce ozone (O3) levels for each TM5-FASST regions for all years (ppb). The list of countries that form each region and the full name of the region can be found in Table S2.2 in the TM5-FASST documentation paper: Van Dingenen, R., Dentener, F., Crippa, M., Leitao, J., Marmer, E., Rao, S., Solazzo, E. and Valentini, L., 2018. TM5-FASST: a global atmospheric source-receptor model for rapid impact analysis of emission changes on air quality and short-lived climate pollutants. Atmospheric Chemistry and Physics, 18(21), pp.16173-16211.
#' @param db_path Path to the GCAM database
#' @param query_path Path to the query file
#' @param db_name Name of the GCAM database
#' @param prj_name Name of the rgcam project. This can be an existing project, or, if not, this will be the name
#' @param prj rgcam loaded project
#' @param rdata_name Name of the RData file. It must contain the queries in a list
#' @param scen_name Name of the GCAM scenario to be processed
#' @param queries Name of the GCAM query file. The file by default includes the queries required to run rfasst
Expand All @@ -581,13 +582,21 @@ m2_get_conc_pm25<-function(db_path = NULL, query_path = "./inst/extdata", db_nam
#' @importFrom magrittr %>%
#' @export

m2_get_conc_o3<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name,
m2_get_conc_o3<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name = NULL, prj = NULL,
rdata_name = NULL, scen_name, queries = "queries_rfasst.xml", final_db_year = 2100,
saveOutput = T, ch4_o3 = T, map = F, anim = T, recompute = F){

if (!recompute & exists('m2_get_conc_o3.output')) {
return(m2_get_conc_o3.output)
} else {
#----------------------------------------------------------------------
#----------------------------------------------------------------------
# Assert that the parameters of the function are okay, or modify when necessary

if (is.null(prj_name)) assertthat::assert_that(!is.null(prj), msg = 'Specify the project name or pass an uploaded project as parameter')

#----------------------------------------------------------------------
#----------------------------------------------------------------------

all_years<-all_years[all_years <= final_db_year]

Expand Down Expand Up @@ -810,6 +819,7 @@ m2_get_conc_o3<-function(db_path = NULL, query_path = "./inst/extdata", db_name
#' @param query_path Path to the query file
#' @param db_name Name of the GCAM database
#' @param prj_name Name of the rgcam project. This can be an existing project, or, if not, this will be the name
#' @param prj rgcam loaded project
#' @param rdata_name Name of the RData file. It must contain the queries in a list
#' @param scen_name Name of the GCAM scenario to be processed
#' @param queries Name of the GCAM query file. The file by default includes the queries required to run rfasst
Expand All @@ -821,14 +831,21 @@ m2_get_conc_o3<-function(db_path = NULL, query_path = "./inst/extdata", db_name
#' @importFrom magrittr %>%
#' @export


m2_get_conc_m6m<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name,
m2_get_conc_m6m<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name = NULL, prj = NULL,
rdata_name = NULL, scen_name, queries = "queries_rfasst.xml", final_db_year = 2100,
saveOutput = T, map = F, anim = T, recompute = F){

if (!recompute & exists('m2_get_conc_m6m.output')) {
return(m2_get_conc_m6m.output)
} else {
#----------------------------------------------------------------------
#----------------------------------------------------------------------
# Assert that the parameters of the function are okay, or modify when necessary

if(is.null(prj_name)) assertthat::assert_that(!is.null(prj), msg = 'Specify the project name or pass an uploaded project as parameter')

#----------------------------------------------------------------------
#----------------------------------------------------------------------

all_years<-all_years[all_years <= final_db_year]

Expand All @@ -852,7 +869,7 @@ m2_get_conc_m6m<-function(db_path = NULL, query_path = "./inst/extdata", db_name
dplyr::rename(subRegion=fasst_region) %>%
dplyr::mutate(subRegionAlt=as.factor(subRegionAlt))

em.list<-m1_emissions_rescale(db_path,query_path,db_name,prj_name,rdata_name,scen_name,queries,saveOutput = F, final_db_year = final_db_year, recompute = recompute)
em.list<-m1_emissions_rescale(db_path,query_path,db_name,prj_name,prj,rdata_name,scen_name,queries,saveOutput = F, final_db_year = final_db_year, recompute = recompute)

# First we load the base concentration and emissions, which are required for the calculations
base_conc<-raw.base_conc %>%
Expand Down Expand Up @@ -1076,6 +1093,7 @@ m2_get_conc_m6m<-function(db_path = NULL, query_path = "./inst/extdata", db_name
#' @param query_path Path to the query file
#' @param db_name Name of the GCAM database
#' @param prj_name Name of the rgcam project. This can be an existing project, or, if not, this will be the name
#' @param prj rgcam loaded project
#' @param rdata_name Name of the RData file. It must contain the queries in a list
#' @param scen_name Name of the GCAM scenario to be processed
#' @param queries Name of the GCAM query file. The file by default includes the queries required to run rfasst
Expand All @@ -1088,14 +1106,23 @@ m2_get_conc_m6m<-function(db_path = NULL, query_path = "./inst/extdata", db_name
#' @export


m2_get_conc_aot40<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name,
m2_get_conc_aot40<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name = NULL, prj = NULL,
rdata_name = NULL, scen_name, queries = "queries_rfasst.xml", final_db_year = 2100,
saveOutput = T, map = F, anim = T, recompute = F){

if (!recompute & exists('m2_get_conc_aot40.output')) {
return(m2_get_conc_aot40.output)
} else {

#----------------------------------------------------------------------
#----------------------------------------------------------------------
# Assert that the parameters of the function are okay, or modify when necessary

if(is.null(prj_name)) assertthat::assert_that(!is.null(prj), msg = 'Specify the project name or pass an uploaded project as parameter')

#----------------------------------------------------------------------
#----------------------------------------------------------------------

all_years<-all_years[all_years <= final_db_year]

# Create the directories if they do not exist:
Expand All @@ -1118,7 +1145,7 @@ m2_get_conc_aot40<-function(db_path = NULL, query_path = "./inst/extdata", db_na
dplyr::rename(subRegion=fasst_region) %>%
dplyr::mutate(subRegionAlt=as.factor(subRegionAlt))

em.list<-m1_emissions_rescale(db_path, query_path, db_name, prj_name, rdata_name, scen_name, queries, saveOutput = F, final_db_year = final_db_year, recompute = recompute)
em.list<-m1_emissions_rescale(db_path, query_path, db_name, prj_name, prj, rdata_name, scen_name, queries, saveOutput = F, final_db_year = final_db_year, recompute = recompute)

# First we load the base concentration and emissions, which are required for the calculations

Expand Down Expand Up @@ -1509,6 +1536,7 @@ m2_get_conc_aot40<-function(db_path = NULL, query_path = "./inst/extdata", db_na
#' @param query_path Path to the query file
#' @param db_name Name of the GCAM database
#' @param prj_name Name of the rgcam project. This can be an existing project, or, if not, this will be the name
#' @param prj rgcam loaded project
#' @param rdata_name Name of the RData file. It must contain the queries in a list
#' @param scen_name Name of the GCAM scenario to be processed
#' @param queries Name of the GCAM query file. The file by default includes the queries required to run rfasst
Expand All @@ -1520,15 +1548,23 @@ m2_get_conc_aot40<-function(db_path = NULL, query_path = "./inst/extdata", db_na
#' @importFrom magrittr %>%
#' @export


m2_get_conc_mi<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name,
m2_get_conc_mi<-function(db_path = NULL, query_path = "./inst/extdata", db_name = NULL, prj_name = NULL, prj = NULL,
rdata_name = NULL, scen_name, queries = "queries_rfasst.xml", final_db_year = 2100,
saveOutput = T, map = F, anim = T, recompute = F){

if (!recompute & exists('m2_get_conc_mi.output')) {
return(m2_get_conc_mi.output)
} else {

#----------------------------------------------------------------------
#----------------------------------------------------------------------
# Assert that the parameters of the function are okay, or modify when necessary

if(is.null(prj_name)) assertthat::assert_that(!is.null(prj), msg = 'Specify the project name or pass an uploaded project as parameter')

#----------------------------------------------------------------------
#----------------------------------------------------------------------

all_years<-all_years[all_years <= final_db_year]

# Create the directories if they do not exist:
Expand All @@ -1551,7 +1587,7 @@ m2_get_conc_mi<-function(db_path = NULL, query_path = "./inst/extdata", db_name
dplyr::rename(subRegion=fasst_region) %>%
dplyr::mutate(subRegionAlt=as.factor(subRegionAlt))

em.list<-m1_emissions_rescale(db_path,query_path, db_name, prj_name, rdata_name, scen_name, queries, saveOutput = F, final_db_year = final_db_year, recompute = recompute)
em.list<-m1_emissions_rescale(db_path,query_path, db_name, prj_name, prj, rdata_name, scen_name, queries, saveOutput = F, final_db_year = final_db_year, recompute = recompute)

# First we load the base concentration and emissions, which are required for the calculations

Expand Down
Loading

0 comments on commit d5060cf

Please sign in to comment.