knitr::opts_chunk$set(echo = TRUE)

library(plyr)
library(dplyr)
library(plotly)
library(reshape2)
library(rv)
library(stringr)
library(shiny)
library(shinydashboard)
library(tidyr)
library(viridis)

source("R/helper_fxns.R")
load("data/cost_surfaces_95.rdata")
#Read in table of total mitigation costs per eagle from ABT report for different durations & cost estimates
cost_table <- read.csv(file = 'data/ABT_REA_costs.csv', header = TRUE)

# Create different combinations of eagle rates and project sizes. 
# For testing purposes, assume all turbines are 200m tall w/80m blades
test_values <- expand.grid(erate = seq(0,3,0.05), size = seq(20, 500, 20)*expFac)

#test-values <- mutate(test_values, mrate = 15200, srate = 167)
effort_df <- data.frame(effort = seq(0, 500, 1))

# define annotation for FWS minimum survey effort
a <- list(
  x = 10,
  y = 10,
  text = 'FWS minimum',
  font = list(color = 'black', size = 12),
  xref = "x",
  yref = "y",
  xanchor = 'left',
  showarrow = TRUE,
  arrowhead = 7,
  ax = 20,
  ay = -40
)

plot_curves <- function(erate, nturb, mcost, scost){
  size <- nturb*expFac
  output <- plyr::mdply(effort_df, cost_curve, erate, size, mcost, scost)
  min_effort <- output$effort[output$T == min(output$T)]
  plot_ly(data = output, type = 'scatter', mode = 'lines')%>%
    add_trace(
      x = ~effort, y = ~T,
      #line = list(color = cols[(i-1)%/%6], width = ((i-1)%%6 +1)),
      line = list(color = 'orange'),
      showlegend = TRUE,
      name = 'Total'
      )%>%
    add_trace(
      x = ~effort, y = ~M,
      #line = list(color = cols[(i-1)%/%6], width = ((i-1)%%6 +1), dash = 'dash'),
      line = list(color = 'grey', dash = 'dash'),
      showlegend = TRUE,
      name = "Mitigation"
    )%>%
    add_trace(
      x = ~effort, y = ~S,
      #line = list(color = cols[(i-1)%/%6], width = ((i-1)%%6 +1), dash = 'dot'),
      line = list(color = 'blue', dash = 'dot'),
      showlegend = TRUE,
      name = 'Survey'
    )%>%
    add_trace(
      x = c(min_effort, min_effort), y = c(0, max(output$T)),
      line = list(color = 'black', width = 1),
      name = paste('Min cost effort (', min_effort, ' hrs)', sep = "")
    )%>%
    layout(
      xaxis = list(title = 'Survey effort (hr*km<sup>3</sup>)'),
      yaxis = list(title = 'Cost ($)'),
      annotations = a,
      legend = list(x = 0.2, y = 1)#,
      #title = paste('Costs at a site with', round(nturb, 0), 'turbines &', erate, 'eagle min/hr')
    )
}

#Estimated survey cost data from West Ecosystems Inc.
survey_costs <- list('annual_low_ppt' = 2000, 'annual_high_ppt' = 5000,
                     "Low" = 2000/12, 'High' = 5000/12,
              'annual_low_pMW' = 300, 'annual_high_pMW' = 600)

#Estimated powerpole retrofit costs and eagle electrocution rates (eagles/pole*yr) from Adt report
retro_costs <- list('Low' = 1040, 'High' = 2590)
electro_rates <- list('Low' = 0.0036, 'Median' = 0.0051, 'High' = 0.0066)
durations <- c(10, 20, 30, 40, 50)

#' Create a plotly heatmap of minimum cost survey efforts
#' @param df matrix storing minimum cost efforts for eagle rate x project size combos
#' @return plotly heatmap
plot_surface <- function(df){
  plot_ly(type = 'heatmap', z = acast(df, erate~size, value.var = "effort"),
        y = seq(0,2,0.05), x = seq(20,500,20),
        zmin = 0, zmax = 50,
        colorbar = list(title = 'Survey<br>Effort<br>(hr*km<sup>3</sup>)')
        )%>%
  layout(
    yaxis = list(title = 'Eagle Activity Rate (min/hr*km<sup>3</sup>)',
                 titlefont = list(color = 'black', size = 14),
                 tickfont = list(color = 'black', size = 12)),
    xaxis = list(title = 'Project Size (# Turbines)',
                 titlefont = list(color = 'black', size = 14),
                 tickfont = list(color = 'black', size = 12))
  )
}

select_scost <- selectInput('scost', 'Survey cost per hour',
                            list('Low ($167)' = 'Low',
                                 'High ($417)' = 'High'), selected = 'Low ($167/hr)')

select_mcost <- selectInput('mcost', 'Mitigation cost per retrofit',
                            list('Low ($1,040)' = 'Low',
                                 'High ($2,590)' = 'High'))

slide_erate <- sliderInput('erate', 'Eagle activity (min/hr)',
                           min = 0, max = 3, value = 0.1, step = 0.05)

slide_size <- sliderInput('size', 'Number of turbines',
                          min = 20, max = 500, value = 100, step = 10)

expand <- actionButton("show", "Click for details of the permitting process")

output$surfacePlot <- renderPlotly({
  selection <- paste(input$mcost, input$scost, sep = "_")
  df <- switch(selection,
               Low_Low = low_low,
               Low_High = low_high,
               High_Low = high_low,
               High_High = high_high)
  plot_surface(df)
})

output$curvePlot <- renderPlotly({
  mcost <- filter(cost_table, Cost == input$mcost, Duration == 30, Rate == "Median")%>%
    select(M)
  scost <- survey_costs[input$scost][[1]]
  erate <- input$erate
  size <- input$size
  plot_curves(erate, size, mcost, scost)
})

modal <- modalDialog(
        title = "Predicting Eagle Mortality at Wind Energy Sites",
        fluidRow(
          renderImage({
            list(
              src = 'www/turbines.jpg',
              contentType = 'image/jpg',
              width = 300,
              height = 300,
              alt = 'Eagle flying near wind turbines'
            )
          }),
                 p("The Bald and Golden Eagle Protection Act (BAGEPA) requires wind energy projects to obtain an incidental take permit for any bald or golden eagles that might be killed as a result of the project's operation. The ", tags$a(href = "https://www.fws.gov/", "U.S. Fish and Wildlife Service")," uses a Bayesian model to estimate how many eagles will be killed at a proposed project. The model combines eagle activity and collision rate data from existing wind farms with eagle activity data collected during surveys at the proposed site. To receive a permit, developers must mitigate for the incidental take of the predicted number of eagles."),
br(),
p("Mitigation often involves buying retrofits for power transmission lines that prevent eagles from being electrocuted, and greater predicted take requires more mitigation. Simultaneously, more survey effort produces eagle fataliy estimates that more closely reflect eagle activity levels at the proposed site (rather than existing sites). Therefore, the total cost of mitigation is related to four factors:"),
tags$ol(
  tags$li("Mitigation cost^: cost of powerpole retrofits to offset 1 eagle"),
  tags$li("Survey cost: costs of eagle surveys"),
  tags$li("Eagle activity: level of eagle activity at the project site"),
  tags$li("Project size*: number of turbines to be built.")
),
p("This app allows users to explore the relationship between these four factors under different scenarios to estimate costs associated with eagle surveying and mitigation for a given project."),
p("^Mitigation costs assume eagles are electrocuted at a rate of 0.0051 birds per pole per year, and that powerpole retrofits last for 20 years.",
  style = 'font-size:10px; font-style:italic;'),
p("*The project size parameter used to estimate eagle mortality is a product of turbine size, operating time, and number of turbines. For simplicity, we assume 200 m tall turbines with 80 m blades operating for 10 hr per day.",
  style = 'font-size:10px; font-style:italic;')
        ),
        easyClose = TRUE,
        size = 'l',
        footer = NULL
      )

observeEvent(input$show,
             {showModal(modal)})

Intro {.sidebar data-width=400}

Eagles & Wind Energy

#fluidPage(
#fluidRow(
p("The Bald and Golden Eagle Protection Act requires wind energy developers to survey and mitigate for eagles predicted to be killed at a proposed project.")
br()
div(expand, style = 'text-align: center;')
br()
p("This app lets a user explore how survey and mitigation costs change based on user-defined scenarios.")
#    )
#)

Parameters

#fluidPage(
  fluidRow(
    br(),
    column(1),
    column(10,
           select_mcost,
           select_scost,
           slide_erate,
           slide_size),
    column(1)
  )
#)

Row

Costs vs. Survey Effort

fillCol(flex = c(1, NA),
        plotlyOutput('curvePlot'),
        p(em("Costs associated with eagle surveying and mitigation as a function of survey effort. Greater survey effort generates predictions for eagle take that more closely reflect eagle activity at a given site. For sites with < 1.1 eagle min/hr, additional survey effort lowers mitigation costs. These curves are affected by changes in all four input parameters."),
          style = 'font-size:14px; margin: 10px 0px 0px 10px;')
)

Optimal Survey Effort

fillCol(flex = c(1, NA),
  plotlyOutput('surfacePlot', height = '100%'),
         p(em("Survey effort that will lead to minimum costs associated with eagle permitting. These values are shown for a range of possible eagle activity levels and project sizes for a given set of estimated survey and mitigation costs. The figure will change in response to different survey and mitigation cost estimates."),
           style = 'font-size:14px; margin: 10px 0px 0px 10px;'
           )
)


mjevans26/eaglesFWS documentation built on Dec. 29, 2021, 1:35 a.m.