AA <- function(data, lat, yes = NULL) {
  ycos <- cos((lat * pi)/180)
  nlat <- dim(data)[2]
  nlon <- dim(data)[1]
  ns <- nlon * nlat
  weight <- array(dim = c(nlon, nlat))
  for (i in 1:nlon) {
    weight[i, ] <- ycos
  }
  # weight=weight/sum(weight) #normalization
  
  data <- data * weight  #weightening
  
  miss.val <- is.null(yes)
  if (miss.val == "TRUE") {
    M <- array(dim = c(ns))
    weight2 <- array(dim = c(ns))
    for (i in 1:nlat) {
      M[(nlon * (i - 1) + 1):(nlon * (i - 1) + nlon)] <- data[, 
        i]
      weight2[(nlon * (i - 1) + 1):(nlon * (i - 1) + nlon)] <- weight[, 
        i]
    }
    not <- is.na(M)
    yes <- array(dim = c(ns))
    counter <- 0
    for (i in 1:ns) {
      if (not[i] == "FALSE") {
        counter <- counter + 1
        yes[counter] <- i
      }
    }
    yes <- yes[1:counter]
    tmp <- sum(M[yes])
    sweight <- sum(weight2[yes])
    data_ave <- tmp/sweight
    # data_ave=sum(M[yes])
  } else {
    yes <- yes
    M <- array(dim = c(ns))
    weight2 <- array(dim = c(ns))
    for (i in 1:nlat) {
      M[(nlon * (i - 1) + 1):(nlon * (i - 1) + nlon)] <- data[, 
        i]
      weight2[(nlon * (i - 1) + 1):(nlon * (i - 1) + nlon)] <- weight[, 
        i]
    }
    tmp <- sum(M[yes])
    sweight <- sum(weight2[yes])
    data_ave <- tmp/sweight
    # data_ave=sum(M[yes])
  }
  
  # outputs  
  data_ave 
}
