Different results every time I calculate a weighted area average

72 views
Skip to first unread message

JackB

unread,
Nov 21, 2019, 11:11:33 AM11/21/19
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Hi,

I'm trying to calculate the Niño 3.4 index from an netCDF file of ERA5 monthly SSTs from 1979 to 2019, but I'm getting quite a baffling problem. 

Part of the calculation requires a weighted area average to be found for the grid box [-5°N, 5°N, 170°W, 120°W]. However, every time I run this code, I get a different result for the weighted average with time. I'm really not sure what's going on as the grid area is always the same - the only bit that changes every time is the final weighted_avg cube. 

I've attached the code used below. The cube that is loaded in is just the ERA5 monthly SSTs from 1979 to 2019 (file size is 3.8 GB). I would be very grateful for any advice.

import iris
import iris.analysis
import iris.coord_categorisation

cube = iris.load_cube('.../ERA5/Monthly_SSTs.nc')

iris.coord_categorisation.add_month(cube, 'time', name='month')
iris.coord_categorisation.add_year(cube, 'time', name='year')

lat = cube.extract(iris.Constraint(latitude=lambda x: -5 <= x <= 5))
latlon = lat.intersection(longitude=(-170,-120))
    
latlon.coord('latitude').guess_bounds()
latlon.coord('longitude').guess_bounds()
grid_areas = iris.analysis.cartography.area_weights(latlon)
    
weighted_avg = latlon.collapsed(['latitude', 'longitude'], iris.analysis.MEAN, weights=grid_areas)

print (weighted_avg[0:10].data)  # This gives a different result every time I run the code
Reply all
Reply to author
Forward
0 new messages