Skip to content

Commit

Permalink
Custom emissions input files
Browse files Browse the repository at this point in the history
Emissions templates built into Hector v3 have a few issues (incorrect units, HFC245fa named incorrectly, and HFC365 constrain and emissions can't be set). These files are corrected and will be used as templates instead
  • Loading branch information
ciara-donegan committed May 8, 2024
1 parent 5a06b8c commit db34966
Show file tree
Hide file tree
Showing 12 changed files with 4,654 additions and 1 deletion.
7 changes: 6 additions & 1 deletion h2/app.r
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ ui <- fluidPage(theme = shinythemes::shinytheme("readable"),
),
tabPanel(title = "Run Hector",
fluidRow(
run_ui("run_1"),
run_ui("run_1")
)
),
tabPanel(title = "Custom Emissions",
fluidRow(
custom_ui("custom_1")
)
),
tabPanel(title = "Carbon Tracking",
Expand Down
54 changes: 54 additions & 0 deletions h2/components/functions/func_custom_emissions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Function to load in custom emissions

loadCustomEmissions <- function() {
print("in load custom")

if (is.null(input$input_custom_emissions_file) | (is.na(input$input_custom_scenarioName) | is.null(input$input_custom_scenarioName) | (input$input_custom_scenarioName == "")))
{
shinyalert::shinyalert("Missing Information", "Please name the scenario and load an emissions file before attempting to load the scenario.", type = "warning")
return(NULL)
}

scenarioName <- input$input_custom_scenarioName

tryCatch(
{
# Load scenario and custom emissions
inifile <- system.file(input$input_custom_SSP, package='hector', mustWork=TRUE)
emissions_file <- input$input_custom_emissions_file$datapath
emissions_data <- read.csv(file=emissions_file, header=TRUE, sep=",", skip = 5)
emissions_headers <- read.csv(file=emissions_file, header=FALSE, sep=",", skip = 4)
dates_col <- emissions_data$Date

withProgress(message = paste('Creating Custom Scenario ', scenarioName, "...\n"), value = 1/2,
{
core <<- newcore(inifile, suppresslogging=TRUE, name=scenarioName)
run(core, 2100)
incProgress(1/1, detail = paste("Load complete."))
Sys.sleep(0.2)
})

# Set custom emissions here
for(i in 2:ncol(emissions_data))
{
setvar(core = core, dates = emissions_data[, 1],var = colnames(emissions_data)[i], values = emissions_data[, i], unit = as.character(emissions_headers[[paste0("V",i)]][[1]]))
}

reset(core)
run(core, 2100)
#updateSelectInput(session, inputId = "mapCore", choices = names(hcores))
#loadGraph()
},
warning = function(war)
{
showModal(modalDialog(
title = "Warning",
paste("Details: ",war
)
))
},
error = function(err)
{
shinyalert::shinyalert("Custom Scenario Error",print(paste('Error attempting to load custom scenario: ',err)), type = "error")
})
}
562 changes: 562 additions & 0 deletions h2/components/inputs/ssp119_emiss-constraints_rf.csv

Large diffs are not rendered by default.

562 changes: 562 additions & 0 deletions h2/components/inputs/ssp126_emiss-constraints_rf.csv

Large diffs are not rendered by default.

562 changes: 562 additions & 0 deletions h2/components/inputs/ssp245_emiss-constraints_rf.csv

Large diffs are not rendered by default.

562 changes: 562 additions & 0 deletions h2/components/inputs/ssp370_emiss-constraints_rf.csv

Large diffs are not rendered by default.

562 changes: 562 additions & 0 deletions h2/components/inputs/ssp434_emiss-constraints_rf.csv

Large diffs are not rendered by default.

562 changes: 562 additions & 0 deletions h2/components/inputs/ssp460_emiss-constraints_rf.csv

Large diffs are not rendered by default.

562 changes: 562 additions & 0 deletions h2/components/inputs/ssp534-over_emiss-constraints_rf.csv

Large diffs are not rendered by default.

562 changes: 562 additions & 0 deletions h2/components/inputs/ssp585_emiss-constraints_rf.csv

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions h2/components/modules/mod_custom.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Run the model with custom emissions
custom_ui <- function(id) {
ns <- NS(id)
fluidRow(
sidebarPanel(
tabsetPanel(
tabPanel(class = "params", "Custom Scenarios",
div
(
h5("Custom Emissions Pathway"),
tags$hr(class="hrNav"),
p("Steps to run your own scenario with custom emissions:"),
tags$ol(
tags$li("Choose a baseline SSP scenario as your starting point"),
tags$li("Give your new custom scenario a name"),
tags$li("Download the emissions file template for that scenario and enter your own emissions"),
tags$li("Upload the new customized emissions file")
),
p(tags$strong("Do not edit any field names or change the CSV file in any way other than changing the data")),
tags$table(
tags$tr(width = "100%",
tags$td(width = "145", "Baseline Scenario:"),
tags$td(width = "155", selectInput(ns("input_custom_SSP"), label = NULL, list("SSP 1-1.9" = "input/tables/ssp119_emiss-constraints_rf.csv",
"SSP 1-2.6" = "input/tables/ssp126_emiss-constraints_rf.csv",
"SSP 2-4.5" = "input/tables/ssp245_emiss-constraints_rf.csv",
"SSP 3-7.0" = "input/tables/ssp370_emiss-constraints_rf.csv",
"SSP 4-3.4" = "input/tables/ssp434_emiss-constraints_rf.csv",
"SSP 4-6.0" = "input/tables/ssp460_emiss-constraints_rf.csv",
"SSP 5-3.4OS" = "input/tables/ssp534-over_emiss-constraints_rf.csv",
"SSP 5-8.5" = "input/tables/ssp585_emiss-constraints_rf.csv"),
width=150, selected = "SSP 2-4.5"))
),
tags$tr(width = "100%",
tags$td(width = "145", "Your Scenario Name:"),
tags$td(width = "200", textInput(ns("input_custom_scenarioName"), label = NULL, width=195, value = ""))
)
),
div(
conditionalPanel(
condition = "input.input_custom_SSP == 'SSP 1-1.9'",
a(h6("Download SSP 1-1.9 Emissions File Template"),
href=system.file("input/tables/ssp119_emiss-constraints_rf.csv",package="hector"))
),
conditionalPanel(
condition = "input.input_custom_SSP == 'SSP 1-2.6'",
a(h6("Download SSP 1-2.6 Emissions File Template"),
href=system.file("input/tables/ssp126_emiss-constraints_rf.csv",package="hector"))
),
conditionalPanel(
condition = "input.input_custom_SSP == 'SSP 2-4.5'",
a(h6("Download SSP 2-4.5 Emissions File Template"),
href=system.file("input/tables/ssp245_emiss-constraints_rf.csv",package="hector"))
),
conditionalPanel(
condition = "input.input_custom_SSP == 'SSP 3-7.0'",
a(h6("Download SSP 3-7.0 Emissions File Template"),
href=system.file("input/tables/ssp370_emiss-constraints_rf.csv",package="hector"))
),
conditionalPanel(
condition = "input.input_custom_SSP == 'SSP 4-3.4'",
a(h6("Download SSP 4-3.4 Emissions File Template"),
href=system.file("input/tables/ssp434_emiss-constraints_rf.csv",package="hector"))
),
conditionalPanel(
condition = "input.input_custom_SSP == 'SSP 4-6.0'",
a(h6("Download SSP 4-6.0 Emissions File Template"),
href=system.file("input/tables/ssp460_emiss-constraints_rf.csv",package="hector"))
),
conditionalPanel(
condition = "input.input_custom_SSP == 'SSP 5-3.4OS'",
a(h6("Download SSP 5-3.4OS Emissions File Template"),
href=system.file("input/tables/ssp534-over_emiss-constraints_rf.csv",package="hector"))
),
conditionalPanel(
condition = "input.input_custom_SSP == 'SSP 5-8.5'",
a(h6("Download SSP 5-8.5 Emissions File Template"),
href=system.file("input/tables/ssp585_emiss-constraints_rf.csv",package="hector"))
),
fileInput(ns("input_custom_emissions_file"), "Upload Custom Emissions File:", width=275, buttonLabel = "Choose File", accept = c("text/csv", ".csv", "text/comma-separated-values,text/plain")),
div(
class="paramDivs", actionButton(inputId=ns("input_load_emissions"), label="Create Scenario"))
)
) # End Div
) # End Custom Scenarios Tab Panel
)
)
)
}

custom_server <- function(id, r6) {
moduleServer(id, function(input, output, session) {
observe({
print("test")
}) %>% bindEvent(input$input_load_emissions)
})
}
2 changes: 2 additions & 0 deletions h2/global.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ library(svglite)

source("./components/modules/mod_graph.r")
source("./components/modules/mod_run.r")
source("./components/modules/mod_custom.r")
source("./components/modules/mod_summary.r")
source("./components/modules/mod_download.r")
source("./components/modules/mod_tracking.r")
source("./components/functions/func_graph_plots.R")
source("./components/functions/func_custom_emissions.R")

theme_set(theme_minimal())

Expand Down

0 comments on commit db34966

Please sign in to comment.