Applying .mean()
to the collection will return a single image that represents the mean of all monthly means; essentially mean temperature 1979 to 2019.
To calculate a collection of mean annual temperature images (one image per year), you can use the code below. It:
- Reduces the image collection to a set of distinct year representatives.
- Performs a join between the distinct year collection and the complete collection to group images by year.
- Maps a function over the distinct year collection to calculate mean annual temperature
from the list of images belonging to each year in the time series.
Note that I also included code to a make a time series chart for a region or interest.
Code Editor script
// Import image collection.
var era5_complete = ee.ImageCollection("ECMWF/ERA5/MONTHLY");
// #############################################################################
// ### Make mean annual temperature image collection ###
// #############################################################################
// Reduce the collection to a set of unique year representatives
// to serve as the primary collection in a join with the complete
// collection.
var distinctYear = ee.ImageCollection("ECMWF/ERA5/MONTHLY").distinct('year');
// Define a filter that identifies which images from the complete collection
// match the 'year' from the distinctYear collection.
var filter = ee.Filter.equals({leftField: 'year', rightField: 'year'});
// Define a join.
var join = ee.Join.saveAll('sameYear');
// Apply the join and convert the resulting FeatureCollection to an
// ImageCollection.
var joinCol = ee.ImageCollection(
join.apply(distinctYear, era5_complete, filter)
);
// Apply mean reduction among same year collections.
var annualMonthlyMeanCol = joinCol.map(function(img) {
var yearCol = ee.ImageCollection.fromImages(
img.get('sameYear')
);
return yearCol.mean()
.set('Year', img.get('year'));
});
// #############################################################################
// ### Regional mean annual temperature time series chart ###
// #############################################################################
// Define an area of interest to calculate regional mean annual temperature for.
var aoi = ee.Geometry.Polygon(
[[[-122.14909071938769, 37.21291770869186],
[-122.14909071938769, 37.01360552399172],
[-121.82224745766894, 37.01360552399172],
[-121.82224745766894, 37.21291770869186]]], null, false);
// Display AOI on map.
Map.centerObject(aoi, 10);
Map.addLayer(aoi, null, 'AOI');
// Map over the mean annual temperature collection - for each image calculate
// regional mean and return a feature. Result is a collection that can be
// plotted with ui.Chart.feature charts.
var regionalTemp = annualMonthlyMeanCol.map(function(img) {
var regionReduce = img.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: aoi,
scale: 10000});
return ee.Feature(aoi, regionReduce)
.set('Year', img.get('Year'));
});
// Chart mean annual temperature time series.
var chart = ui.Chart.feature.byFeature({
features:regionalTemp,
xProperty: 'Year',
yProperties: ['mean_2m_air_temperature']})
.setOptions({
title: 'Mean annual temperature',
legend: {position: 'none'},
hAxis: {title: 'Year'},
vAxis: {title: 'Kelvin'},
});
print(chart);
