Skip to content

Commit

Permalink
allow multiple states and years in DownloadandSaveModel, improve log …
Browse files Browse the repository at this point in the history
…messages, resolve #28
  • Loading branch information
bl-young committed Nov 12, 2024
1 parent 7bb389d commit df5f1e9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ download_model_RDS <- function(modelname) {
# Download file
utils::download.file(file_url,file_path,
mode = "wb", quiet = TRUE)
print(paste0("Downloading model for ", substr(modelname,0,2), " in 20", year, " to ", file_path, "."))
print(paste0("Downloaded model for ", substr(modelname,0,2), " in 20", year, " to ", file_path, "."))
} else {
print(paste("File already exists at", file_path))
}
Expand Down
4 changes: 2 additions & 2 deletions examples/BuildandSaveModelsLocally.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ params:
input: select
choices: ["v1.0","v1.1-GHG","v1.1-GHGc"]
years:
label: "Model Year"
label: "Model Year(s)"
value: 2012
input: select
multiple: TRUE
choices: [2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
states:
label: "State"
label: "State(s)"
value: "AL"
input: select
multiple: TRUE
Expand Down
61 changes: 32 additions & 29 deletions examples/DownloadandSaveModelLocally.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ params:
value: "v1.0"
input: select
choices: ["v1.0", "v1.1-GHG", "v1.1-GHGc"]
year:
label: "Model Year"
years:
label: "Model Year(s)"
value: 2012
input: select
multiple: FALSE
multiple: TRUE
choices: [2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
state:
label: "State"
states:
label: "State(s)"
value: ""
input: select
multiple: TRUE
choices: ["AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL",
"IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT",
"NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI",
Expand All @@ -31,43 +32,45 @@ params:
```{r setup, include=FALSE}
source("../R/utils.R")
# Stop execution of Rmd if params not selected
if (params$state=="") {
stop("You must select a state and a year.")
}
## Select model build parameters
state <- params$state
year <- params$year
states <- params$states
years <- params$years
spec <- params$model
if (spec == "v1.0") {
modelstub <- 'EEIOv1.0-s-'
} else if (spec == "v1.1-GHG") {
modelstub <- 'EEIOv1.1-GHG-'
} else if (spec == "v1.1-GHGc") {
modelstub <- 'EEIOv1.1-GHGc-'
}
modelname <- paste0(state, modelstub,substr(year,3,4))
```

```{r load-model, echo=FALSE}
if (params$write) {
install_useeior()
# prepare output directory
output_dir <- file.path('../output')
dir.create(output_dir, showWarnings = FALSE)
}
file_path <- download_model_RDS(modelname)
model <- readRDS(file_path) # Test that it loads correctly
```


```{r write_model, eval=params$write, echo=FALSE}
install_useeior()
# write model to Excel
output_dir <- file.path('../output')
dir.create(output_dir, showWarnings = FALSE)
for(state in states) {
if((modelstub == 'EEIOv1.1-GHGc-') & (!state %in% c("VT", "NY", "ME"))) {
stop(paste0(state, " not available with a customized GHG Inventory, please use v1.1-GHG."))
} else if ((modelstub == 'EEIOv1.1-GHG-') & (state %in% c("VT", "NY", "ME"))) {
stop(paste0(state, " is only available with a customized GHG Inventory, please use v1.1-GHGc."))
}
for(year in years) {
modelname <- paste0(state, modelstub,substr(year,3,4))
file_path <- download_model_RDS(modelname)
useeior::writeModeltoXLSX(model, output_dir)
model <- readRDS(file_path) # Test that it loads correctly
if(params$write) {
useeior::writeModeltoXLSX(model, output_dir)
print(paste0("Model for ", state, " in ", year, " written to Excel at ", output_dir, "."))
}
}
}
print(paste0("Model for ", state, " in ", year, " written to Excel at ", output_dir, "."))
```

0 comments on commit df5f1e9

Please sign in to comment.