transform_units_pressure <- 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 == 'pa') {
    data_list[[var_index]] <- data_list[[var_index]] / 100
    if (new_units == 'hpa') {
      data[[1]]$attrs$Variable$metadata[[var_name]]$units <- 'hPa'
    } else if (new_units == 'mb') {
      data[[1]]$attrs$Variable$metadata[[var_name]]$units <- 'mb'
    }
  } else if (original_units == 'hpa') {
    if (new_units == 'pa') {
      data_list[[var_index]] <- data_list[[var_index]] * 100
      data[[1]]$attrs$Variable$metadata[[var_name]]$units <- "Pa"
    } else if (new_units == "mb") {
      data[[1]]$attrs$Variable$metadata[[var_name]]$units <- "mb"
    }
  } else if (original_units == "mb") {
    if (new_units == 'pa') {
      data_list[[var_index]] <- data_list[[var_index]] * 100
      data[[1]]$attrs$Variable$metadata[[var_name]]$units <- "Pa"
    } else if (new_units == "hPa") {
      data[[1]]$attrs$Variable$metadata[[var_name]]$units <- "hPa"
    }
  }

  # 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)
}
