Skip to content

Commit 4af7a2a

Browse files
committed
Remove the need to use filenames to refer to internal data.
1 parent d5ac8e4 commit 4af7a2a

12 files changed

+257
-37
lines changed

NAMESPACE

+4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ export(ORIENTATION_LA)
1010
export(ORIENTATION_NPOLE)
1111
export(ORIENTATION_SPOLE)
1212
export(addRegionID)
13+
export(basin235)
1314
export(ch_aea)
15+
export(chn)
1416
export(eck3)
1517
export(gcam14_colors)
1618
export(na_aea)
@@ -19,6 +21,8 @@ export(parse_mi_output)
1921
export(plot_GCAM)
2022
export(process_batch_q)
2123
export(qualPalette)
24+
export(rgn14)
25+
export(rgn32)
2226
export(robin)
2327
export(wintri)
2428
import(ggplot2)

R/Map_Functions.R

+97-26
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,44 @@ process_batch_q<-function(batchq, query, scen, filters, func=sum){
5757
#' Match GCAM ID to region using data from a lookup table.
5858
#'
5959
#' We match by ID number to avoid problems with variant spellings and the like.
60+
#' With the optional arguments you can also omit regions for which you don't
61+
#' want to plot the data for some reason, and you can translate the
62+
#' abbreviations used in subregion output.
63+
#'
64+
#' The \code{provincefile} and \code{drops} arguments are a little clunky. They
65+
#' are optional, but if you are using one of the built-in map sets, then you
66+
#' \emph{must not} specify them if they don't exist for the map set you are
67+
#' using. Currently, \code{rgn14} and \code{basin235} have neither drops nor
68+
#' province abbreviations. The \code{rgn32} set has drops, but not province
69+
#' abbreviations. Only the \code{chn} set (and the \code{usa} set, when it is
70+
#' finally implemented) has both.
6071
#' @param datatable A table of results produced by \code{\link{process_batch_q}}
61-
#' @param lookupfile File containing the region lookup table. XXX: we
62-
#' need to provide a mechanism for users to use the data installed
63-
#' internally in the package.
64-
#' @param provincefile File containing the province lookup table, if
65-
#' applicable. XXX: Same comment as above
66-
#' @param drops File containing a list of regions to drop, if
67-
#' applicable. XXX: Same comment as above.
72+
#' @param lookupfile Name of one of the predefined map sets, OR, if you're using
73+
#' a custom map set, the file containing the region lookup table
74+
#' @param provincefile Name of one of the predefined map sets, OR, if you're
75+
#' using a custom map set, file containing the province lookup table, if
76+
#' applicable.
77+
#' @param drops Name of one of the predefined map sets, OR, if you're using
78+
#' a custom map set, the file containing a list of regions to drop, if
79+
#' applicable.
6880
#' @return Input table modified to include a GCAM ID for reach region.
6981
#' @export
70-
addRegionID<-function(datatable, lookupfile, provincefile='none', drops='none') {
71-
if (provincefile != 'none'){
82+
addRegionID<-function(datatable, lookupfile=lut.rgn32, provincefile=NULL, drops=NULL) {
83+
if (!is.null(provincefile)){
7284
datatable<-translateProvince(datatable, provincefile)
7385
}
7486

75-
if (drops != 'none'){
87+
if (!is.null(drops)){
7688
datatable<-dropRegions(datatable, drops)
7789
}
7890

79-
lookuptable<-read.csv(lookupfile, strip.white=T, stringsAsFactors = F)
91+
lookuptable <-
92+
if(is.symbol(lookupfile)) {
93+
get.internal(lookupfile,'lut')
94+
}
95+
else {
96+
read.csv(lookupfile, strip.white=T, stringsAsFactors = F)
97+
}
8098

8199
#Differentiate region-Region issue
82100
if ("Region" %in% names(datatable)){
@@ -105,16 +123,23 @@ addRegionID<-function(datatable, lookupfile, provincefile='none', drops='none')
105123
return(finaltable)
106124
}
107125

126+
#' Replace subregion abbreviations with full subregion names
127+
#'
128+
#' Subregions are given two-letter abbreviations in GCAM output. This function
129+
#' uses a lookup table to restore the full names.
130+
#'
131+
#' @param datatable The table with the abbreviated names in it.
132+
#' @param provincefile Name of a defined mapset OR name of a file containing the
133+
#' lookup table.
108134
translateProvince<-function(datatable, provincefile){
109-
### Replace province abbreviations with full province names
110-
### to ensure matching with GCAM map names.
111-
### Inputs:
112-
### datatable - data frame of query from batch query CSV.
113-
### provincefile - string; path to file with abbreviations and full names of regions.
114-
### Outputs:
115-
### datatable - datatable modified so that abbreviations are now full names.
116135

117-
provincetable<-read.csv(provincefile, strip.white=T)
136+
provincetable <-
137+
if(is.symbol(provincefile)) {
138+
get.internal(provincefile,'prov')
139+
}
140+
else {
141+
read.csv(provincefile, strip.white=T)
142+
}
118143

119144
#Differentiate region-Region issue
120145
if ("Region" %in% names(datatable)){
@@ -142,16 +167,24 @@ dropRegions<-function(datatable, drops){
142167
### Outputs:
143168
### datatable - updated data frame with regions dropped.
144169

145-
dr<-read.csv(drops, strip.white=T, header=F)
146-
dr<-as.character(dr$V1)
147-
148-
regcols<-grepl("egion", names(datatable)) #Find instances of "region" or "Region" columns
170+
dr <-
171+
if(is.symbol(drops)) {
172+
get.internal(drops,'drop')
173+
}
174+
else {
175+
read.csv(drops, strip.white=T, header=F)
176+
}
177+
dr<-as.character(dr$V1)
149178

150-
datatable[regcols]<-lapply(datatable[regcols], function(x) replace(x, x %in% dr, NA)) #Replace drop col values with NA
179+
regcols<-grepl("egion", names(datatable)) #Find instances of "region" or "Region" columns
180+
## ^-- Technically this will also trigger on 'Legion' or 'legion'
151181

152-
datatable<-na.omit(datatable) #Remove rows containing NA
182+
## XXX: Do the next step with dplyr instead of this convoluted way
183+
datatable[regcols]<-lapply(datatable[regcols], function(x) replace(x, x %in% dr, NA)) #Replace drop col values with NA
153184

154-
return(datatable)
185+
datatable<-na.omit(datatable) #Remove rows containing NA
186+
187+
return(datatable)
155188
}
156189

157190
#---------------------------------------------------------------------------
@@ -535,3 +568,41 @@ plot_GCAM <- function(mapdata, col = NULL, proj=robin, extent=EXTENT_WORLD, orie
535568
return(mp)
536569
}
537570

571+
#' Get auxiliary data for a named mapset.
572+
#'
573+
#' We have several standard map sets. Each of them has several auxiliary tables
574+
#' associated with it. This function retrieves the auxiliary table associated
575+
#' with the requested. Right now this function understands \code{rgn14},
576+
#' \code{rgn32}, \code{basin235}, and \code{chn}.
577+
#'
578+
#' @param mapset The name of the mapset. Can be either a symbol or a string.
579+
#' @param type The type of table. Right now this is either 'lut', 'drop', or
580+
#' 'prov'
581+
get.internal <- function(mapset, type) {
582+
eval(as.symbol(paste(type,mapset,sep='.')))
583+
}
584+
585+
#' Designator for the rgn14 map set
586+
#'
587+
#' This symbol will select the rgn14 map set
588+
#' @export
589+
rgn14 <- quote(rgn14)
590+
591+
#' Designator for the rgn32 map set
592+
#'
593+
#' This symbol will select the rgn32 map set
594+
#' @export
595+
rgn32 <- quote(rgn32)
596+
597+
#' Designator for the basin235 map set
598+
#'
599+
#' This symbol will select the basin235 map set
600+
#' @export
601+
basin235 <- quote(basin235)
602+
603+
#' Designator for the chn map set
604+
#'
605+
#' This symbol will select the chn map set
606+
#' @export
607+
chn <- quote(chn)
608+

R/sysdata.rda

5.16 KB
Binary file not shown.

data-raw/gen-maps.R

+33-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,39 @@ gen.data <- function() {
1515
map.chn <- load.geojson('inst/extdata/rgnchn/GCAM_China.geojson', 'GCAM_ID')
1616
## TODO: add GCAM-USA dataset here
1717

18-
devtools::use_data(map.rgn14, map.rgn32, map.basin235, map.chn)
18+
devtools::use_data(map.rgn14, map.rgn32, map.basin235, map.chn, overwrite=TRUE)
1919
}
2020

21+
gen.internal <- function() {
22+
## Read the various region lookup tables, drop region lists, and province lists
23+
## 14-region has just a lookup
24+
lut.rgn14 <- read.csv('inst/extdata/rgn14/lookup.txt', strip.white=TRUE,
25+
stringsAsFactors=FALSE)
26+
27+
## 32-region has a lookup and a drop
28+
lut.rgn32 <- read.csv('inst/extdata/rgn32/lookup.txt', strip.white=TRUE,
29+
stringsAsFactors=FALSE)
30+
drop.rgn32 <- read.csv('inst/extdata/rgn32/drop-regions.txt', strip.white=TRUE,
31+
stringsAsFactors=FALSE)
32+
33+
## basins have just a lookup table
34+
lut.basin235 <- read.csv('inst/extdata/rgnbasin/lookup.txt', strip.white=TRUE,
35+
stringsAsFactors=FALSE)
36+
37+
## GCAM-china has all three
38+
lut.chn <- read.csv('inst/extdata/rgnchn/lookup.txt', strip.white=TRUE,
39+
stringsAsFactors=FALSE)
40+
drop.chn <- read.csv('inst/extdata/rgnchn/drop-regions.txt', strip.white=TRUE,
41+
stringsAsFactors=FALSE)
42+
prov.chn <- read.csv('inst/extdata/rgnchn/rgn-name-translation.csv', strip.white=TRUE,
43+
stringsAsFactors=FALSE)
44+
45+
## TODO: We need this data for GCAM-USA too
46+
47+
devtools::use_data(lut.rgn14, lut.rgn32, drop.rgn32, lut.basin235,
48+
lut.chn, drop.chn, prov.chn, internal=TRUE, overwrite=TRUE)
49+
}
50+
51+
2152
gen.data()
53+
gen.internal()

man/addRegionID.Rd

+22-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/basin235.Rd

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/chn.Rd

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get.internal.Rd

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/rgn14.Rd

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/rgn32.Rd

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/translateProvince.Rd

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/examples.Rmd

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ tables<-parse_mi_output(fn = gcam.datafile)
4646
4747
#Break out sample scenario
4848
prim_en<-process_batch_q(tables, "primary_energy", "Reference", c(fuel="a oil"))
49-
prim_en<-addRegionID(prim_en, system.file('extdata/rgn32','lookup.txt',package='gcammaptools'),
50-
drops=system.file('extdata/rgn32','drop-regions.txt',package='gcammaptools'))
49+
prim_en<-addRegionID(prim_en, lookupfile=rgn32, drops=rgn32)
5150
#Merge dataset with map data
5251
map_primen<-merge(map_32_wo_Taiwan.fort, prim_en, by="id")
5352
```

0 commit comments

Comments
 (0)