Skip to content

Commit

Permalink
Merge pull request #72 from JGCRI/remove-savetog
Browse files Browse the repository at this point in the history
Remove save toggle, update default parameters, add custom emissions
  • Loading branch information
stephpenn1 authored May 30, 2024
2 parents 5a06b8c + 0e6d08b commit b3312db
Show file tree
Hide file tree
Showing 17 changed files with 4,866 additions and 149 deletions.
27 changes: 17 additions & 10 deletions h2/app.r
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ ui <- fluidPage(theme = shinythemes::shinytheme("readable"),
))
)
),
tabPanel(title = "Run Hector",
fluidRow(
run_ui("run_1")
)
),
tabPanel(title = "Custom Emissions",
fluidRow(
custom_ui("custom_1")
)
),
tabPanel(title = "Carbon Tracking",
fluidRow(
tracking_ui("tracking_1")
)
),
tabPanel(title = "Guides",
mainPanel(
style="vertical-align: middle;",
Expand All @@ -41,16 +56,6 @@ ui <- fluidPage(theme = shinythemes::shinytheme("readable"),
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/fBHXS7pjZcI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')

)
),
tabPanel(title = "Run Hector",
fluidRow(
run_ui("run_1"),
)
),
tabPanel(title = "Carbon Tracking",
fluidRow(
tracking_ui("tracking_1")
)
),
tabPanel(title = "About")
),
Expand All @@ -61,13 +66,15 @@ ui <- fluidPage(theme = shinythemes::shinytheme("readable"),
server <- function(input, output, session) {
r6 <- HectorInputs$new() # r6 class
r6_tracking <- HectorInputs$new() # separate r6 class for carbon tracking
r6_custom <- HectorInputs$new()
observeEvent(input$launch_scenario, updateTabsetPanel(session, "nav", selected = "Run Hector"), ignoreInit = TRUE)

run_server("run_1", r6 = r6)
summary_server("summary_1", r6 = r6)
graph_server("graph_1", r6 = r6)
download_server("download_1", r6 = r6)
tracking_server("tracking_1")
custom_server("custom_1", r6 = r6_custom)
}

# Run the application
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")
})
}
20 changes: 2 additions & 18 deletions h2/components/functions/func_graph_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,13 @@

graph_plots <- function(r6) {

if (r6$save == TRUE) {
#browser()

{ggplot(last(r6$output)) +
{ggplot(r6$output) +
geom_line(aes(x = year, y = value, color = Scenario)) +
labs(x = "Year", y = last(r6$output)$variable[1],
title = paste0("Run Name: ", last(r6$output)$run[1], "\n", "Variable: ", last(r6$output)$variable[1],"\nPermafrost: ",r6$permafrost)) +
title = paste0("Variable: ", last(r6$output)$variable[1],"\nPermafrost: ",r6$permafrost, "\n")) +
theme(legend.position = "bottom")} %>%
plotly::ggplotly()

} else if(r6$save == FALSE) {

{ggplot(r6$no_save_output) +
geom_line(aes(x = year, y = value, color = Scenario)) +
labs(x = "Year", y = r6$no_save_output$variable[1],
title = paste0("Run Name: Unsaved Run\n", "Variable: ", r6$no_save_output$variable[1],"\nPermafrost: ",r6$permafrost))} %>%
plotly::ggplotly() %>%
layout(
legend = list(
orientation = 'h', x = 0
)
)

}
}

8 changes: 8 additions & 0 deletions h2/components/layout/style copy.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ body
font-size: 14px;
}


.bootstrap-switch .bootstrap-switch-handle-off.bootstrap-switch-danger,
.bootstrap-switch .bootstrap-switch-handle-on.bootstrap-switch-danger {
background: #00699d;
color: white;
}


.navbar-nav
{
float:none;
Expand Down
176 changes: 176 additions & 0 deletions h2/components/modules/mod_custom.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Run the model with custom emissions
custom_ui <- function(id) {
ns <- NS(id)
fluidRow(
sidebarPanel(
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,
choices = list("SSP 1-1.9"="input/hector_ssp119.ini",
"SSP 1-2.6"="input/hector_ssp126.ini",
"SSP 2-4.5"="input/hector_ssp245.ini",
"SSP 3-7.0"="input/hector_ssp370.ini",
"SSP 4-3.4"="input/hector_ssp434.ini",
"SSP 4-6.0"="input/hector_ssp460.ini",
"SSP 5-3.4OS"="input/hector_ssp534-over.ini",
"SSP 5-8.5"="input/hector_ssp585.ini"),
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 == 'input/hector_ssp119.ini'",
a(h6("Download SSP 1-1.9 Emissions File Template"),
href="inputs/ssp119_emiss-constraints_rf.csv"),
ns=NS(id)
),
conditionalPanel(
condition = "input.input_custom_SSP == 'input/hector_ssp126.ini'",
a(h6("Download SSP 1-2.6 Emissions File Template"),
href="inputs/ssp126_emiss-constraints_rf.csv"),
ns=NS(id)
),
conditionalPanel(
condition = "input.input_custom_SSP == 'input/hector_ssp245.ini'",
a(h6("Download SSP 2-4.5 Emissions File Template"),
href="inputs/ssp245_emiss-constraints_rf.csv"),
ns=NS(id)
),
conditionalPanel(
condition = "input.input_custom_SSP == 'input/hector_ssp370.ini'",
a(h6("Download SSP 3-7.0 Emissions File Template"),
href="inputs/ssp370_emiss-constraints_rf.csv"),
ns=NS(id)
),
conditionalPanel(
condition = "input.input_custom_SSP == 'input/hector_ssp434.ini'",
a(h6("Download SSP 4-3.4 Emissions File Template"),
href="inputs/ssp434_emiss-constraints_rf.csv"),
ns=NS(id)
),
conditionalPanel(
condition = "input.input_custom_SSP == 'input/hector_ssp460.ini'",
a(h6("Download SSP 4-6.0 Emissions File Template"),
href="inputs/ssp460_emiss-constraints_rf.csv"),
ns=NS(id)
),
conditionalPanel(
condition = "input.input_custom_SSP == 'input/hector_ssp534-over.ini'",
a(h6("Download SSP 5-3.4OS Emissions File Template"),
href="inputs/ssp534-over_emiss-constraints_rf.csv"),
ns=NS(id)
),
conditionalPanel(
condition = "input.input_custom_SSP == 'input/hector_ssp585.ini'",
a(h6("Download SSP 5-8.5 Emissions File Template"),
href="inputs/ssp585_emiss-constraints_rf.csv"),
ns=NS(id)
),
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")),
selectInput(ns("variable"), "Choose Output Variable:",
list("Carbon Cycle" = list("Atmospheric CO2" = CONCENTRATIONS_CO2(),
"FFI Emissions" = FFI_EMISSIONS(),
"LUC Emissions" = LUC_EMISSIONS()),
"Concentrations" = list("N2O Concentration" = CONCENTRATIONS_N2O()),
"Emissions" = list("Black Carbon Emissions" = EMISSIONS_BC(),
"Organic Carbon Emissions" = EMISSIONS_OC()),
"Forcings" = list("RF - Total" = RF_TOTAL(),
"RF - Albedo" = RF_ALBEDO(),
"RF - CO2" = RF_CO2(),
"RF - N2O" = RF_N2O(),
"RF - Black Carbon" = RF_BC(),
"RF - Organic Carbon" = RF_OC(),
"RF - Total SO2" = RF_SO2(),
"RF - Volcanic Activity" = RF_VOL(),
"RF - CH4" = RF_CH4())),
selected = "Atmospheric CO2", multiple = FALSE),
div(
class="paramDivs", actionButton(ns("input_load_emissions"), label="Create Scenario"))
)
),
mainPanel(
plotlyOutput(ns("graph")),
)
)
}

custom_server <- function(id, r6) {
moduleServer(id, function(input, output, session) {
observe({

# Require all inputs to exist
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)
}
r6$run_name <- input$input_custom_scenarioName

# Read in input data
inifile <- system.file(input$input_custom_SSP,package="hector")
emifile <- input$input_custom_emissions_file
emissions_data <- read.csv(file=emifile$datapath, header=TRUE, sep=",", skip = 5)
emissions_headers <- read.csv(file=emifile$datapath, header=FALSE, sep=",", skip = 4)
dates_col <- emissions_data$Date
r6$selected_var <- input$variable

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

# get data from base SSP run
base_output <- fetchvars(core, 1745:2300, vars = list(r6$selected_var)) %>%
mutate(run = names(which(scenarios == input$input_custom_SSP, arr.ind = FALSE)),
Scenario = names(which(scenarios == input$input_custom_SSP, arr.ind = FALSE)))
#browser()

# set vars and rerun core
for(i in c(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)

# get custom output
custom_output <- fetchvars(core, 1745:2300, vars = list(r6$selected_var)) %>%
mutate(run = r6$run_name, Scenario = names(which(scenarios == input$input_custom_SSP, arr.ind = FALSE)))
r6$output <- bind_rows(list(base_output,custom_output))

# Plot
# replace following with graph plots function in future
output$graph <- renderPlotly({
ggplot(r6$output) +
geom_line(aes(x = year, y = value, color = run)) +
labs(x = "Year", y = last(r6$output)$variable[1],
title = paste0("Variable: ", last(r6$output)$variable[1])) +
theme(legend.position = "bottom")
})

}) %>% bindEvent(input$input_load_emissions)

output$download_file <- downloadHandler(

)

})
}
Loading

0 comments on commit b3312db

Please sign in to comment.