AreaWeights <- function( listseas, datagrid ) {
  #
  # This function computes area weights of the selected regions in listseas 
  #
  # Args:
  #   listseas is the list of names of the selected regions
  #   datagrid sepcifies the used NEMO or EC-Earth version or obs dataset
  #
  # Return:
  #   areaweights : 1-dimensional array of areas (10^9m^2) of the selected regions
  #
  # History: 
  #
###############################################################################
#

if ( datagrid=='HadISST' | datagrid=='NSIDC' ) {
   lat <- seq(-89.5, 89.5, 1) 
   tmp1 <- InsertDim(cos(lat*pi/180),1,360)
   earthsurface <- 510072000000000 
   var1 <- tmp1*earthsurface/sum(tmp1) 
} else {
  filename1 <- paste('/cfu/autosubmit/con_files/mesh_mask_nemo.', datagrid, '.nc', sep='')
  ncid1     <- open.ncdf(filename1)
  e1t       <- get.var.ncdf(ncid1, 'e1t')
  e2t       <- get.var.ncdf(ncid1, 'e2t')
  var1      <- e1t*e2t 
  close.ncdf(ncid1)
}

filename2 <- paste('/cfu/autosubmit/con_files/mask.regions.',datagrid, '.nc' ,sep='')
ncid2     <- open.ncdf(filename2)

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

close.ncdf(ncid2)

invisible(areaweights)

}

