PlotStereoMap <- function(var, lon, lat, latlims = c(60,90), toptitle = '',  
                          sizetit = 1, units = '', brks = NULL, cols = NULL,
                          filled.continents = FALSE, dots = NULL, intlat=10, 
                          drawleg = TRUE, subsampleg = 1, colNA='white', 
                          fileout = NULL, ...) {
  # Process the user graphical parameters that may be passed in the call
  ## Graphical parameters to exclude
  excludedArgs <- c("cex", "cex.main", "col", "fin", "lab", "las", "lwd", "mai", "mar", "mgp", "new", "pch", "ps")
  userArgs <- .FilterUserGraphicArgs(excludedArgs, ...)

  # If there is any filenames to store the graphics, process them
  # to select the right device 
  if (!is.null(fileout)) {
    deviceInfo <- .SelectDevice(fileout)
    saveToFile <- deviceInfo$fun
    fileout <- deviceInfo$files
  }

  #
  #  Input arguments 
  # ~~~~~~~~~~~~~~~~~
  #
  dims <- dim(var)
  if (length(dims) > 2) {
    stop("Only 2 dimensions expected for var : (lon,lat) ")
  }
  if (dims[1] != length(lon) | dims[2] != length(lat)) {
    if (dims[1] == length(lat) & dims[2] == length(lon)) {
      var <- t(var)
      dims <- dim(var)
    } else {
      stop("Inconsistent var dimensions / longitudes +  latitudes")
    }
  }
  dlon <- median(lon[2:dims[1]] - lon[1:(dims[1] - 1)])/2
  dlat <- median(lat[2:dims[2]] - lat[1:(dims[2] - 1)])/2
  colorbar <- colorRampPalette(c("dodgerblue4", "dodgerblue1", 
              "forestgreen", "yellowgreen", "white", "white", 
              "yellow", "orange", "red", "saddlebrown"))
  if (is.null(brks) == TRUE) {
    ll <- signif(min(var, na.rm = TRUE), 4)
    ul <- signif(max(var, na.rm = TRUE), 4)
    if (is.null(cols) == TRUE) {
      cols <- colorbar(10)
    }
    nlev <- length(cols)
    brks <- signif(seq(ll, ul, (ul - ll) / nlev), 4)
  } else {
    if (is.null(cols) == TRUE) {
      nlev <- length(brks) - 1
      cols <- colorbar(nlev)
    } else {
      if (length(cols) != (length(brks) - 1)) {
        stop("Inconsistent colour levels / list of colours")
      }
    }
  }
  #
  #  Defining the layout
  # ~~~~~~~~~~~~~~~~~~~~~
  # 
  xmargin <- 0
  ymargin <- 0
  topmargin <- 0
  ymargin2 <- 0 
  if (toptitle != '') {
    topmargin <- 1.5 
  }
  #
  #  Plotting the map
  # ~~~~~~~~~~~~~~~~~~
  #

  # Open connection to graphical device
  if (!is.null(fileout)) saveToFile(fileout, width = 11, height = 8)
  # Load the user parameters
  par(userArgs)

  if (drawleg) {
    layout(matrix(1:2, ncol = 2, nrow = 1), widths = c(9, 1))
  }
  par(mar = c(xmargin, ymargin, topmargin, ymargin2), mgp = c(1, 1, 0) ,cex = 1.4)
  map("world", projection = "stereographic", orientation = c(sign(latlims[1])*90, 0, 0),
       xlim = c(-180,180), ylim = latlims, interior = FALSE, wrap = TRUE, lwd = 1)
  for (jx in 1:dim(var)[1]) { 
    for (jy in 1:dim(var)[2]) {
      if ( lat[jy] >= latlims[1] & latlims[2] >= lat[jy] ) {
        coord=mapproject(c(lon[jx]-dlon,lon[jx]+dlon,lon[jx]+dlon,lon[jx]-dlon),
                         c(lat[jy]-dlat,lat[jy]-dlat,lat[jy]+dlat,lat[jy]+dlat))
        ind=which(brks[-1]>var[jx,jy] & var[jx,jy]>brks[-length(brks)])
        if (is.na(var[jx,jy])) {
          polygon(coord, col=colNA, border=NA)
        }else{
          polygon(coord, col=cols[ind], border=NA)
        }
        if (is.null(dots) == FALSE) {
          if (dots[jx, jy] == TRUE) {
            coord=mapproject(lon[jx],lat[jy])
            points(coord, col = 'black', cex = 0.02, pch = 4)
          }
        }
      }
    }
  }
  if (filled.continents == TRUE) {
    map("world", projection = "stereographic", orientation = c(sign(latlims[1])*90, 0, 0), 
        xlim = c(-180,180), ylim = latlims, interior = FALSE, wrap = TRUE, lwd = 0.7, 
        col = gray(0.5), fill = TRUE, add = TRUE, border = gray(0.5))
  }else{
    map("world", projection = "stereographic", orientation = c(sign(latlims[1])*90, 0, 0),
       xlim = c(-180,180), ylim = latlims, interior = FALSE, wrap = TRUE, lwd = 1, add = TRUE)
  }
  map.grid(lim = c(-180, 180, latlims), nx = 18, ny = (latlims[2]-latlims[1])/intlat,
           col = 'lightgrey', labels = FALSE) 
  title(toptitle, cex.main=sizetit)
  #
  #  Colorbar
  # ~~~~~~~~~~
  #
  if (drawleg) {
    par(mar = c(xmargin+0.3, 0, topmargin, 2.5), mgp = c(1, 1, 0),
        las = 1, cex = 1.2)
    image(1, c(1:length(cols)), t(c(1:length(cols))), axes = F, col = cols,
          xlab = '', ylab = '', main = units, cex.main = 1.1)
    box()
    axis(4, at = seq(0.5, length(brks) - 0.5, subsampleg),
         labels = brks[seq(1, length(brks), subsampleg)])
  }

  # If the graphic was saved to file, close the connection with the device
  if(!is.null(fileout)) dev.off()
}




