# data is a s2dv_cube object
# units as character
units_transform <- function(data, orig_units, user_units, var_name, freq, flux = FALSE,
                            ncores = NULL) {
  ## TODO: Change how argument 'flux' works
  for (i in 1:length(var_name)) {
    if (orig_units[i] == "degc") {
      orig_units[i] <- "c"
    }
    if (!(orig_units[i] == user_units[i])) {
      if (orig_units[i] %in% c("c", "k")) {
        if (user_units[i] %in% c("c", "k")) {
          data <- transform_units_temperature(data, orig_units[i], user_units[i],
                                              var_name[i], var_index = i)
        } else {
          stop("Transformation temperature units not available.")
        }
      } else if (orig_units[i] %in% c("ms-1", "kgm-2s-1", "mm", "m")) {
        if (user_units[i] %in% c("ms-1", "kgm-2s-1", "mm", "m")) {
          data <- transform_units_precipitation(data, orig_units[i], user_units[i],
                                                var_name[i], freq, flux, 
                                                ncores = ncores, var_index = i)
        } else {
          stop("Transformation precipitation units not available.")
        }
      } else if (orig_units[i] %in% c("pa", "hpa", "mb")) {
         if (user_units[i] %in% c("pa", "hpa", "mb")) {
          data <- transform_units_pressure(data, orig_units[i], user_units[i],
                                           var_name[i])
        } else {
          stop("Transformation precipitation units not available.")
        }
      } else {
        stop(paste("Transformation unknown from", orig_units[i], "to", user_units[i]))
      }
    }
  }
  return(data)
}

