transform_units_temperature <- function(data, original_units, new_units,
                                        var_name, var_index = 1) {

  data_arr <- data[[1]]$data
  data_list <- asplit(data_arr, which(names(dim(data_arr)) == "var"))
  if (original_units == 'c' & new_units == 'k') {
    data_list[[var_index]] <- data_list[[var_index]] + 273.15
    data[[1]]$attrs$Variable$metadata[[var_name]]$units <- "K"
  }
  if (original_units == 'k' & new_units == 'c') {
    data_list[[var_index]] <- data_list[[var_index]] - 273.15
    data[[1]]$attrs$Variable$metadata[[var_name]]$units <- "C"
  }

  # Combine list back to array
  data_arr <- array(unlist(data_list),
                    dim = c(dim(data_list[[1]]), var = length(data_list)))
  data[[1]]$data <- aperm(data_arr, match(names(dim(data[[1]]$data)), 
                          names(dim(data_arr))))

  return(data)
}
