AreaWeights <- function(listregs=NULL, datagrid='/cfu/autosubmit/con_files/mesh_mask_nemo.N3.2_O1L42.nc', datamask='/cfu/autosubmit/con_files/mask.regions.N3.2_O1L42.nc') {
  #
  # This function computes area weights of the selected regions in listsregs. 
  #
  # Args:
  #   listregs : the list of names of the selected regions/zones/points
  #   datagrid : specifies what NEMO or EC-Earth grid is being used with full path 
  #              and file name (containing all grid parameters), while for regular 
  #              1x1deg grid specify '1deg' (used by, e.g., HadISST sic, ..). 
  #              For NULL the output will be unit vector with the length of listregs.
  #   datamask : full path and file name with masks for all regions/zones/points 
  #              specificed in listregs (1 in region, 0 everywhere else) in datagrid.
  #
  # Return:
  #   areaweights : a vector of areas (10^9m^2) of the selected regions
  #
  # History:
  #   1.0  #  2014-10  (N.S. Fuckar, neven.fuckar@ic3.cat)  #  Original code
  #
###############################################################################

if ( is.null(listregs) ) { stop('list of sea ice regions/zones/points is not specified') }

if ( is.null(datagrid) ) { 
  areaweights <- rep(1, length(listregs)) 
} else {
  if ( datagrid=='1deg' ) {
    lat  <- seq(-89.5, 89.5, 1) 
    tmp1 <- InsertDim(cos(lat*pi/180),1,360)
    earthsurface <- 510072000000000 
    var1 <- tmp1*earthsurface/sum(tmp1) 
  } else {
    ncid1 <- open.ncdf(datagrid)
    e1t   <- get.var.ncdf(ncid1, 'e1t')
    e2t   <- get.var.ncdf(ncid1, 'e2t')
    var1  <- e1t*e2t 
    close.ncdf(ncid1)
  }

  ncid2 <- open.ncdf(datamask)

  areaweights <- array(dim=length(listregs))
  for ( ii in 1:length(listregs) ) {
    var2 <- get.var.ncdf(ncid2, listregs[ii])
    areaweights[ii] <- sum(var1*var2[,,1], na.rm=TRUE)/(10^9)
  }

  close.ncdf(ncid2)
}

invisible(areaweights)

}

