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: (this funcation was tested with all three N3.2_O1L42 mask.regions in test13.R)
  #
###############################################################################
#
#  This is old tested way of specifying data1 that needs separate tmask for each datagrid
#
#  filename1 <- paste('/cfu/autosubmit/con_files/area_tmask_nemo.', datagrid, '.nc', sep='')
#  ncid1     <- open.ncdf(filename1)
#  var1      <- get.var.ncdf(ncid1,'AREA')
#  print(dim(var1))
#  close.ncdf(ncid1)

# 24/08: There should be here something like if (datagrid==HadISST | datagrid==NSIDC){
# 24/08: else and what is below pertains to the else part until the ++++
   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)
# 24/08: +++
# 24/08: The NSIDC and HadISST are 1deg grid so lat <- seq(-89.5, 89.5, 1) and
# 24/08: tmp1 <- Insertdim(cos(lat*pi/90),1,180)
# 24/08: earthsurface <- 510072000 (km2)
# 24/08: var1 <- tmp1*earthsurface/sum(tmp1) 
# 24/08: for NSIDC and HadISST

# 24/08: There should be only 1 file defining the regions below:
   filename2 <- paste('/cfu/autosubmit/con_files/mask.regions.',datagrid, '.i00v.nc' ,sep='')
#  filename2 <- paste('/cfu/autosubmit/con_files/mask.regions.',datagrid, '.b02s.nc' ,sep='')
#  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)

}

