Skip to content

Commit

Permalink
Merge pull request #34 from JGCRI/demo-prep
Browse files Browse the repository at this point in the history
Prepare for demo and fix bugs found while doing so.
  • Loading branch information
rplzzz authored Sep 20, 2017
2 parents bce3413 + 2f4f492 commit 9787a0c
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 45 deletions.
10 changes: 7 additions & 3 deletions R/electricity.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
#'
#' Produce electricity by region.
#'
#' Columns:
#' The raw table used by this module has columns:
#' \itemize{
#' \item{scenario}
#' \item{region}
#' \item{sector}
#' \item{subsector}
#' \item{technology}
#' \item{year}
#' \item{value}
#' \item{units}
#' \item{Units}
#' }
#'
#' @keywords internal
Expand All @@ -26,8 +30,8 @@ module.electricity <- function(mode, allqueries, aggkeys, aggfn, strtyr, endyr,
message('Function for processing variable: Electricity')

electricity <- allqueries$'Electricity'
electricity <- aggregate(electricity, aggfn, aggkeys)
electricity <- filter(electricity, strtyr, endyr, filters)
electricity <- aggregate(electricity, aggfn, aggkeys)
electricity <- unitconv_energy(electricity, ounit)
electricity
}
Expand Down
6 changes: 3 additions & 3 deletions R/mcl.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ generate <- function(scenctl,


if(fileformat == 'XLSX') {
output_xlsx(rslts, tabs, dirname)
output_xlsx(rslts, tabs, dbloc)
}
else if(fileformat == 'CSV') {
output_csv(rslts, tabs, dirname)
output_csv(rslts, tabs, dbloc)
}
else {
warning('Unknown file format ', fileformat, ' requested. ',
'Writing as CSV.')
output_csv(rslts, tabs, dirname)
output_csv(rslts, tabs, dbloc)
}

message('FIN.')
Expand Down
4 changes: 2 additions & 2 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ output_xlsx <- function(rslts, tabs, dirname)
#' @keywords internal
alternate_filename <- function(name)
{
name <- normalizePath(name)
name <- name
dir <- dirname(name)
filename <- basename(name)
np <- nameparse(filename)
Expand All @@ -96,7 +96,7 @@ alternate_filename <- function(name)
name <- file.path(dir, filename)
}

normalizePath(name)
normalizePath(name, mustWork=FALSE)
}


Expand Down
7 changes: 7 additions & 0 deletions R/runmodule.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ module.test_fun <- function(mode, allqueries, aggkeys, aggfn, strtyr, endyr,
#' in their input more readable. For example, the variable with canonical name
#' \code{gdp_mer_} can be represented as \code{GDP(MER)} in input files.
#'
#' Processing the data for a variable is done in a function called
#' module.varname, for example, the variable \code{gdp_mer_} is processed in
#' \code{\link{module.gdp_mer_}}. The documentation for these functions
#' contains a list of the columns in the raw table produced by the function,
#' prior to aggregation or filtering. This information is potentially useful
#' for planning filtering or aggregation operations. The module documentation
#' will also have any notes pertinent to the computation of the data.
#' @importFrom magrittr %>%
#' @export
listVariables <- function()
Expand Down
23 changes: 12 additions & 11 deletions R/socioeconomics.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#'
#' Produce population by region.
#'
#' Columns:
#' The raw table used by this module has columns:
#' \itemize{
#' \item{scenario}
#' \item{region}
#' \item{year}
#' \item{value}
#' \item{units}
#' \item{Units}
#' }
#'
#' @keywords internal
Expand All @@ -26,8 +27,8 @@ module.population <- function(mode, allqueries, aggkeys, aggfn, strtyr, endyr,
message('Function for processing variable: Population')

population <- allqueries$'Population'
population <- aggregate(population, aggfn, aggkeys)
population <- filter(population, strtyr, endyr, filters)
population <- aggregate(population, aggfn, aggkeys)
population <- unitconv_counts(population, ounit)
population
}
Expand All @@ -37,16 +38,16 @@ module.population <- function(mode, allqueries, aggkeys, aggfn, strtyr, endyr,
#'
#' Produce GDP MER by region.
#'
#' Columns:
#' The raw table used by this module has columns:
#' \itemize{
#' \item{scenario}
#' \item{region}
#' \item{year}
#' \item{value}
#' \item{units}
#' \item{Units}
#' }
#'
#' @keywords internal

module.gdp_mer_ <- function(mode, allqueries, aggkeys, aggfn, strtyr, endyr,
filters, ounit)
{
Expand All @@ -59,8 +60,8 @@ module.gdp_mer_ <- function(mode, allqueries, aggkeys, aggfn, strtyr, endyr,
message('Function for processing variable: GDP MER')

gdp_mer <- allqueries$'GDP(MER)'
gdp_mer <- filter(gdp_mer, strtyr, endyr, filters)
gdp_mer <- aggregate(gdp_mer, aggfn, aggkeys)
#gdp_mer <- filter(gdp_mer, strtyr, endyr, filters)
gdp_mer <- unitconv_usdollar(gdp_mer, ounit)
gdp_mer
}
Expand All @@ -70,16 +71,16 @@ module.gdp_mer_ <- function(mode, allqueries, aggkeys, aggfn, strtyr, endyr,
#'
#' Produce GDP PPP by region.
#'
#' Columns:
#' The raw table used by this module has columns:
#' \itemize{
#' \item{scenario}
#' \item{region}
#' \item{year}
#' \item{value}
#' \item{units}
#' \item{Units}
#' }
#'
#' @keywords internal

module.pcgdp_ppp_ <- function(mode, allqueries, aggkeys, aggfn, strtyr, endyr,
filters, ounit)
{
Expand All @@ -92,8 +93,8 @@ module.pcgdp_ppp_ <- function(mode, allqueries, aggkeys, aggfn, strtyr, endyr,
message('Function for processing variable: Per capita GDP PPP')

gdp_ppp <- allqueries$'pcGDP(PPP)'
gdp_ppp <- filter(gdp_ppp, strtyr, endyr, filters)
gdp_ppp <- aggregate(gdp_ppp, aggfn, aggkeys)
#gdp_ppp <- filter(gdp_ppp, strtyr, endyr, filters)
gdp_ppp <- unitconv_usdollar(gdp_ppp, ounit)
gdp_ppp
}
Expand Down
12 changes: 12 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
comment:
behavior: new
ignore: R/mcl.R

coverage:
precision: 1

status:
project:
default:
enabled: no

patch:
default:
enabled: no
13 changes: 7 additions & 6 deletions inst/extdata/default-queries.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
</aQuery>

<aQuery>
<supplyDemandQuery title="Electricity">
<axis1 name="technology">technology</axis1>
<axis2 name="Year">physical-output[@vintage]</axis2>
<xPath buildList="true" dataName="output" group="false" sumAll="false">*[@type = 'sector' (: collapse :) and (@name='electricity' or @name='elect_td_bld' or @name='industrial energy use')]//*[@type = 'technology' and not (@name='elect_td_bld' or @name='electricity')]/*[@type='output' (:collapse:) and (@name='electricity' or @name='elect_td_bld')]/physical-output/node()</xPath>
<comments/>
</supplyDemandQuery>
<supplyDemandQuery title="Electricity">
<axis1 name="region">region</axis1>
<axis2 name="Year">physical-output[@vintage]</axis2>
<xPath buildList="true" dataName="output" group="false" sumAll="false">*[@type = 'sector' and ((@name='electricity' or @name='elect_td_bld' or @name='industrial energy use'))]//*[@type = 'subsector']//*[@type = 'technology' and not (@name='elect_td_bld')]/*[@type='output' (:collapse:) and (@name='electricity' or @name='elect_td_bld')]/physical-output/node()</xPath>
<comments/>
</supplyDemandQuery>
</aQuery>

</queries>
File renamed without changes.
5 changes: 5 additions & 0 deletions inst/extdata/example-variable.ctl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GCAM variable,output variable,aggregation keys,aggregation function,start year,end year,filters,output units
Population,Population,,,1990,2020,,thous
pcGDP(PPP),Per-capita GDP (PPP),,,,,,Thous80US$/per
Electricity,Electricity by Technology,"region,technology",,1990,2050,,MWh
Electricity,Electricity by Subsector,"region, subsector",,1990,2050,(matches; sector; electricity),
2 changes: 0 additions & 2 deletions inst/extdata/scenario.ctl

This file was deleted.

4 changes: 0 additions & 4 deletions inst/extdata/variable.ctl

This file was deleted.

9 changes: 9 additions & 0 deletions man/listVariables.Rd

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

8 changes: 6 additions & 2 deletions man/module.electricity.Rd

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

5 changes: 3 additions & 2 deletions man/module.gdp_mer_.Rd

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

5 changes: 3 additions & 2 deletions man/module.pcgdp_ppp_.Rd

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

5 changes: 3 additions & 2 deletions man/module.population.Rd

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

15 changes: 9 additions & 6 deletions tests/testthat/test_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ test_that('alternate_filename does the right thing with xyz.csv.', {
on.exit(unlink(file.path(dir, '*.csv')))

expect_identical(alternate_filename(filename),
normalizePath(filename))
normalizePath(filename, mustWork=FALSE))

lastfilename <- filename
for(i in 1:3) {
fcon <- file(lastfilename, 'w')
close(fcon)
lastfilename <- alternate_filename(filename)
expect_identical(lastfilename,
normalizePath(file.path(dir,sprintf('xyz%03d.csv',i))))
normalizePath(file.path(dir,sprintf('xyz%03d.csv',i)),
mustWork=FALSE))
}
})

Expand All @@ -65,15 +66,16 @@ test_that('alternate_filename does the right thing with xyz.abc.csv.', {
on.exit(unlink(file.path(dir, '*.csv')))

expect_identical(alternate_filename(filename),
normalizePath(filename))
normalizePath(filename, mustWork=FALSE))

lastfilename <- filename
for(i in 1:3) {
fcon <- file(lastfilename, 'w')
close(fcon)
lastfilename <- alternate_filename(filename)
expect_identical(lastfilename,
normalizePath(file.path(dir,sprintf('xyz.abc%03d.csv',i))))
normalizePath(file.path(dir,sprintf('xyz.abc%03d.csv',i)),
mustWork=FALSE))
}
})

Expand All @@ -85,15 +87,16 @@ test_that('alternate_filename does the right thing with no file extension.', {
on.exit(unlink(file.path(dir, '*.csv')))

expect_identical(alternate_filename(filename),
normalizePath(filename))
normalizePath(filename, mustWork=FALSE))

lastfilename <- filename
for(i in 1:3) {
fcon <- file(lastfilename, 'w')
close(fcon)
lastfilename <- alternate_filename(filename)
expect_identical(lastfilename,
normalizePath(file.path(dir,sprintf('xyz%03d',i))))
normalizePath(file.path(dir,sprintf('xyz%03d',i)),
mustWork=FALSE))
}
})

Expand Down

0 comments on commit 9787a0c

Please sign in to comment.