Skip to content

Commit

Permalink
write sector cw to json, #294
Browse files Browse the repository at this point in the history
  • Loading branch information
bl-young committed Nov 11, 2024
1 parent 33d122d commit 948e39b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions R/WriteModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ writeModelforAPI <-function(model, basedir){
writeModelMatrices(model,"bin",dirs$model)
writeModelDemandstoJSON(model,dirs$demands)
writeModelMetadata(model,dirs,"csv")
writeSectorCrosswalk(model, dirs$data)
writeSectorCrosswalk(model,dirs$data,"csv")
}

#' Writes all model data and metadata components to JSON
Expand All @@ -30,7 +30,7 @@ writeModeltoJSON <- function(model, basedir) {
writeModelMatrices(model,"json",dirs$model)
writeModelDemandstoJSON(model,dirs$demands)
writeModelMetadata(model,dirs,"json")
# writeSectorCrosswalk(model, dirs$data)
writeSectorCrosswalk(model,dirs$data,"json")
}

#' Write model matrices as .csv or .bin files to output folder.
Expand Down Expand Up @@ -365,14 +365,19 @@ writeModelMetadata <- function(model, dirs, to_format="csv") {
#' 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

0 comments on commit 948e39b

Please sign in to comment.