Bug in WeightedMean function
Hi @nperez,
I have found a bug in the WeightedMean function. I have been using this function to calculate area weighted means of data with latitudes and longitudes with regular grids, for example the global mean. I have realised that the output of this function gives different results depending on the order of the longitudes, for example, if the data goes from 0 to 360 vs is the data is from 180 to 360 and then from 1 to 180. There shouldn't be any effect from this, and in fact, the longitude in regular grids should always have equal weights.
I believe the problem may be in lines 123-125:
dlon <- abs(c(lon[2 : nblon] - lon[1 : nblon - 1])) * pi / 180
dlon <- c(dlon, dlon[1])
dlon <- array(dlon, dim = c(nblon, nblat))
If there is a discontinuity in the latitudes, as in the case of the lons going from 180 to 360 and then from 1 to 180 then the weight dlon calculated when the lon goes from 360 to 1 is wrongly given a large weight. I also don't see why the longitude should be weighted like the latitudes, even though it doesn't have an effect.
This could be solved by giving the longitudes equal weights all the time, which typically is the case in regular grids.