Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ export(testCalculationFunctions)
export(testVisualizationFunctions)
export(writeModelMatrices)
export(writeModelforAPI)
export(writeModeltoJSON)
export(writeModeltoXLSX)
import(ggplot2)
115 changes: 90 additions & 25 deletions R/WriteModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@ writeModelforAPI <-function(model, basedir){
prepareWriteDirs(model, dirs)
writeModelMatrices(model,"bin",dirs$model)
writeModelDemandstoJSON(model,dirs$demands)
writeModelMetadata(model,dirs)
writeSectorCrosswalk(model, dirs$data)
writeModelMetadata(model,dirs,"csv")
writeSectorCrosswalk(model,dirs$data,"csv")
}

#' Writes all model data and metadata components to JSON
#' @param model A complete EEIO model: a list with USEEIO model components and attributes.
#' @param basedir Base directory to write the model components to
#' @description Writes all model data and metadata components to JSON
#' @export
writeModeltoJSON <- function(model, basedir) {
dirs <- setWriteDirs(model,basedir)
prepareWriteDirs(model, dirs)
writeModelMatrices(model,"json",dirs$model)
writeModelDemandstoJSON(model,dirs$demands)
writeModelMetadata(model,dirs,"json")
writeSectorCrosswalk(model,dirs$data,"json")
}

#' Write model matrices as .csv or .bin files to output folder.
#' @param model A complete EEIO model: a list with USEEIO model components and attributes.
#' @param to_format A string specifying the format of write-to file, can be "csv" or "bin".
#' @param to_format A string specifying the format of write-to file, can be "csv", "bin", or "json".
#' @param outputfolder A directory to write matrices out to
#' @description Writes model matrices as .csv or .bin files to output folder.
#' @export
Expand All @@ -46,6 +60,13 @@ writeModelMatrices <- function(model, to_format, outputfolder) {
# Write x (Industry Output) or q (Commodity Output) to .bin files
writeMatrixasBinFile(as.matrix(model$q), paste0(modelfolder, "/q.bin"))
writeMatrixasBinFile(as.matrix(model$x), paste0(modelfolder, "/x.bin"))
} else if (to_format=="json") {
modelfolder <- paste0(outputfolder, "/matrix")
dir.create(modelfolder, showWarnings = FALSE)
for (matrix in c(matrix_ls, "q", "x")) {
writeMatrixtoJSON(as.matrix(model[[matrix]]),
paste0(modelfolder, "/", matrix, ".json"))
}
}
logging::loginfo(paste0("Model matrices written to ", modelfolder, "."))
}
Expand Down Expand Up @@ -83,7 +104,7 @@ writeModeltoXLSX <- function(model, outputfolder) {
dirs <- setWriteDirs(model, file.path(rappdirs::user_data_dir(), "useeior",
"Model_Builds", model$specs$Model))
prepareWriteDirs(model, dirs)
writeModelMetadata(model, dirs)
writeModelMetadata(model, dirs, "csv")
metadata_tabs <- c("demands", "flows", "indicators", "sectors")
if(is.null(model$SatelliteTables)){
metadata_tabs <- metadata_tabs[metadata_tabs != "flows"]
Expand Down Expand Up @@ -199,18 +220,27 @@ writeModelDemandstoJSON <- function(model, outputfolder) {
logging::loginfo(paste0("Model demand vectors for API written to ", outputfolder, "."))
}

#' Write model matrix as JSON files to output folder.
#' @param matrix A matrix to be written
#' @param outputpath A directory and file to write to.
#' @description Writes model objects.
writeMatrixtoJSON <- function(matrix, outputpath) {
write(jsonlite::toJSON(matrix, pretty = TRUE), outputpath)
}

#' Write model metadata (indicators and demands, sectors, and flows) as CSV files to output folder
#' format for file is here https://github.com/USEPA/USEEIO_API/blob/master/doc/data_format.md
#' @param model A complete EEIO model: a list with USEEIO model components and attributes.
#' @param dirs A named list of directories with model and data directory paths
#' @param to_format A string specifying the format of write-to file, can be "csv" or "json".
#' @description Writes model metadata, including indicators and demands.
writeModelMetadata <- function(model, dirs) {
writeModelMetadata <- function(model, dirs, to_format="csv") {
# Load metadata fields for API
fields <- configr::read.config(system.file("extdata/USEEIO_API_fields.yml",
package="useeior"))

# Write model description to models.csv
model_desc <- file.path(dirs$data, "models.csv")
model_desc <- file.path(dirs$data, paste0("models.", to_format))
ID <- model$specs$Model
Name <- model$specs$Model
Location <- model$specs$ModelRegionAcronyms[1]
Expand All @@ -232,13 +262,21 @@ writeModelMetadata <- function(model, dirs) {
if (!file.exists(model_desc)) {
df <- cbind.data.frame(model_fields)
} else {
df <- utils::read.table(model_desc, sep = ",", header = TRUE,
stringsAsFactors = FALSE, check.names = FALSE)
if(to_format == "csv") {
df <- utils::read.table(model_desc, sep = ",", header = TRUE,
stringsAsFactors = FALSE, check.names = FALSE)
} else if (to_format == "json") {
df <- jsonlite::fromJSON(model_desc)
}
if (!ID%in%df$ID) {
df <- rbind(df, cbind.data.frame(model_fields))
}
}
utils::write.csv(df, model_desc, na = "", row.names = FALSE, fileEncoding = "UTF-8")
if(to_format == "csv") {
utils::write.csv(df, model_desc, na = "", row.names = FALSE, fileEncoding = "UTF-8")
} else if(to_format == "json") {
write(jsonlite::toJSON(df, pretty = TRUE), file.path(dirs$data, "models.json"))
}

if(!is.null(model$Indicators)) {
# Write indicators to indicators.csv
Expand All @@ -250,16 +288,24 @@ writeModelMetadata <- function(model, dirs) {
indicators <- indicators[,fields$indicators]
checkNamesandOrdering(indicators$Name, rownames(model$C),
"code in indicators.csv and rows in C matrix")
utils::write.csv(indicators, paste0(dirs$model, "/indicators.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
if(to_format == "csv") {
utils::write.csv(indicators, paste0(dirs$model, "/indicators.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
} else if(to_format == "json") {
write(jsonlite::toJSON(indicators, pretty = TRUE), file.path(dirs$model, "indicators.json"))
}
}

# Write demands to demands.csv
demands <- model$DemandVectors$meta
demands$Index <- c(1:nrow(demands)-1)
demands <- demands[,fields$demands]
utils::write.csv(demands, paste0(dirs$model, "/demands.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
if(to_format == "csv") {
utils::write.csv(demands, paste0(dirs$model, "/demands.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
} else if(to_format == "json") {
write(jsonlite::toJSON(demands, pretty = TRUE), file.path(dirs$model, "demands.json"))
}

# Write sectors to csv
sectors <- model[[gsub("y", "ies", model$specs$CommodityorIndustryType)]]
Expand All @@ -270,8 +316,13 @@ writeModelMetadata <- function(model, dirs) {
sectors <- sectors[, fields$sectors]
checkNamesandOrdering(sectors$ID, rownames(model$L),
"code in sectors.csv and rows in L matrix")
utils::write.csv(sectors, paste0(dirs$model, "/sectors.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
if(to_format == "csv") {
utils::write.csv(sectors, paste0(dirs$model, "/sectors.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
} else if(to_format == "json") {
sectors$Description <- NULL
write(jsonlite::toJSON(sectors, pretty = TRUE), file.path(dirs$model, "sectors.json"))
}

if(!is.null(model$SatelliteTables)) {
# Write flows to csv
Expand All @@ -283,8 +334,12 @@ writeModelMetadata <- function(model, dirs) {
flows <- flows[, fields$flows]
#checkNamesandOrdering(flows$ID, rownames(model$B),
# "flows in flows.csv and rows in B matrix")
utils::write.csv(flows, paste0(dirs$model, "/flows.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
if(to_format == "csv") {
utils::write.csv(flows, paste0(dirs$model, "/flows.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
} else if(to_format == "json") {
write(jsonlite::toJSON(flows, pretty = TRUE), file.path(dirs$model, "flows.json"))
}
}

# Write years to csv
Expand All @@ -298,8 +353,13 @@ writeModelMetadata <- function(model, dirs) {
}
checkNamesandOrdering(years$ID, colnames(model$Rho),
"years in years.csv and cols in Rho matrix")
utils::write.csv(years, paste0(dirs$model, "/years.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
if(to_format == "csv") {
utils::write.csv(years, paste0(dirs$model, "/years.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
} else if(to_format == "json") {
#todo: is this needed for json?
write(jsonlite::toJSON(years, pretty = TRUE), file.path(dirs$model, "years.json"))
}

# Write session info to R sessioninfo.txt inside the model folder
writeSessionInfotoFile(dirs$model)
Expand All @@ -309,14 +369,19 @@ writeModelMetadata <- function(model, dirs) {
#' Write the model sector crosswalk as .csv file
#' @param model A complete EEIO model: a list with USEEIO model components and attributes.
#' @param outputfolder A directory to write model sector crosswalk
#' @description Writes the model sector crosswalk as .csv file
writeSectorCrosswalk <- function(model, outputfolder){
#' @param to_format A string specifying the format of write-to file, can be "csv" or "json".
#' @description Writes the model sector crosswalk as .csv or .json file
writeSectorCrosswalk <- function(model, outputfolder, to_format="csv"){
crosswalk <- prepareModelSectorCrosswalk(model)
crosswalk$ModelSchema <- ""
utils::write.csv(crosswalk, paste0(outputfolder, "/sectorcrosswalk.csv"),
na = "", row.names = FALSE, fileEncoding = "UTF-8")
logging::loginfo(paste0("Sector crosswalk written as sectorcrosswalk.csv to ",
outputfolder, "."))
if(to_format == "csv") {
utils::write.csv(crosswalk, paste0(outputfolder, "/sectorcrosswalk.csv"),
na = "", row.names = FALSE, fileEncoding = "UTF-8")
} else if (to_format == "json") {
write(jsonlite::toJSON(crosswalk, pretty = TRUE),
file.path(outputfolder, "sectorcrosswalk.json"))
}
logging::loginfo(paste0("Sector crosswalk written to ", outputfolder, "."))
}

#' Write out session information to a "Rsessioninfo.txt file in the given path
Expand Down
16 changes: 16 additions & 0 deletions man/writeMatrixtoJSON.Rd

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

2 changes: 1 addition & 1 deletion man/writeModelMatrices.Rd

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

4 changes: 3 additions & 1 deletion man/writeModelMetadata.Rd

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

16 changes: 16 additions & 0 deletions man/writeModeltoJSON.Rd

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

6 changes: 4 additions & 2 deletions man/writeSectorCrosswalk.Rd

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

2 changes: 2 additions & 0 deletions tests/test_model_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ writeModeltoXLSX(model, ".")
## USEEIOv2.3 Summary, commodity model with GHGs and Import Factors
m <- "USEEIOv2.3-s-GHG-19"
model <- buildModel(m)
writeModeltoJSON(model, ".")
printValidationResults(model)
testCalculationFunctions(model)
testVisualizationFunctions(model)
Expand All @@ -137,6 +138,7 @@ testVisualizationFunctions(model)
m <- "GAEEIOv1.0-GHG-19"
model <- buildModel(m)
printValidationResults(model)
writeModeltoJSON(model, ".")
writeModeltoXLSX(model, ".")
testCalculationFunctions(model)
testVisualizationFunctions(model)
Expand Down