heat_index <- function(data) {
  
  source("modules/Indicators/R/tmp/HeatIndex.R")
  
  ind.name <- "heat_index" 
  
  ## Find position of variables in data array
  pos_tas <- which(grepl("tas", data$obs$coords$var, ignore.case = TRUE))
  tas_var <- data$obs$coords$var[[pos_tas]]
  pos_hurs <- which(data$obs$coords$var == "hurs")

  HI_data <- data
  
  # browser()
  
  if (!is.null(data$obs)) {
    
    ## Check units of temperature, needs to be in Fahrenheit for calculation
    if (data$obs$attrs$Variable$metadata[[tas_var]]$units == 'K'){
      data$obs$data[,pos_tas,,,,,,,] <- (data$obs$data[,pos_tas,,,,,,,] - 273.15) * 9/5 + 32
    } else if (data$obs$attrs$Variable$metadata[[tas_var]]$units == 'C'){
      data$obs$data[,pos_tas,,,,,,,] <- (data$obs$data[,pos_tas,,,,,,,] * 9/5) + 32
    }
    HI_obs_aux <- HeatIndex(data$obs$data[,pos_tas,,,,,,,], data$obs$data[,pos_hurs,,,,,,,])
    ## Convert Fahrenheit to Celsius
    HI_obs_aux <- (HI_obs_aux - 32) * 5/9
    
    original_dims <- data$obs$dims 
    original_dims['var'] <- 1
    dim_names <- names(original_dims)
    
    HI_obs <- array(HI_obs_aux, dim = original_dims)
    HI_data$obs$data <- HI_obs
    
    HI_data$obs$dims['var'] <- 1
    HI_data$obs$coords$var <- tas_var #ind.name
    HI_data$obs$attrs$Variable$varName <- tas_var #ind.name
    HI_data$obs$attrs$Variable$metadata$hurs <- NULL

  }
  
  if (!is.null(data$hcst)) {
    
    if (data$hcst$attrs$Variable$metadata[[tas_var]]$units == 'K'){
      data$hcst$data[,pos_tas,,,,,,,] <- (data$hcst$data[,pos_tas,,,,,,,] - 273.15) * 9/5 + 32
    } else if (data$hcst$attrs$Variable$metadata[[tas_var]]$units == 'C'){
      data$hcst$data[,pos_tas,,,,,,,] <- (data$hcst$data[,pos_tas,,,,,,,] * 9/5) + 32
    }
    HI_hcst_aux <- HeatIndex(data$hcst$data[,pos_tas,,,,,,,], data$hcst$data[,pos_hurs,,,,,,,])
    HI_hcst_aux <- (HI_hcst_aux - 32) * 5/9
    
    original_dims <- data$hcst$dims 
    original_dims ['var'] <- 1
    
    HI_hcst <- array(HI_hcst_aux, dim = original_dims)
    HI_data$hcst$data <- HI_hcst
    
    HI_data$hcst$dims['var'] <- 1
    HI_data$hcst$coords$var <- tas_var #ind.name
    HI_data$hcst$attrs$Variable$varName <- tas_var #ind.name
    HI_data$hcst$attrs$Variable$metadata$hurs <- NULL
    
  }
  
  if (!is.null(data$fcst)) {
    
    if (data$fcst$attrs$Variable$metadata[[tas_var]]$units == 'K'){
      data$fcst$data[,pos_tas,,,,,,,] <- (data$fcst$data[,pos_tas,,,,,,,] - 273.15) * 9/5 + 32
    } else if (data$fcst$attrs$Variable$metadata[[tas_var]]$units == 'C'){
      data$fcst$data[,pos_tas,,,,,,,] <- (data$fcst$data[,pos_tas,,,,,,,] * 9/5) + 32
    }
    HI_fcst_aux <- HeatIndex(data$fcst$data[,pos_tas,,,,,,,], data$fcst$data[,pos_hurs,,,,,,,])
    HI_fcst_aux <- (HI_fcst_aux - 32) * 5/9
    
    original_dims <- data$fcst$dims 
    original_dims['var'] <- 1
    
    HI_fcst <- array(HI_fcst_aux, dim = original_dims)
    HI_data$fcst$data <- HI_fcst
    
    HI_data$fcst$dims['var'] <- 1
    HI_data$fcst$coords$var <- tas_var #ind.name
    HI_data$fcst$attrs$Variable$varName <- tas_var #ind.name
    HI_data$fcst$attrs$Variable$metadata$hurs <- NULL
    
  } else {
    HI_fcst <- NULL
  }

  return(HI_data)
  
}
