# Calculating yearly air temperature mean for selected region ERA5

Using ERA5 data, I am trying to extract the average annual temperature for a region. I am interested in a single value for each year. This is the code I tried to use:

``````//Define area
var area = ee.FeatureCollection("FAO/GAUL/2015/level2")

Map.addLayer(area, {color: 'green', width: 1}, 'area');

var startDate = ee.Date('1979-01-01');
var endDate = ee.Date('2021-12-31')

//Get ERA5 data
var era5_2mt =ee.ImageCollection("ECMWF/ERA5/MONTHLY")
.select('mean_2m_air_temperature')
.filterDate(startDate, endDate)
.filterBounds(area)
.distinct('year');

print(era5_2mt);

var era_2mt_mean = era5_2mt.mean().clip(area);
print(era_2mt_mean);
``````
• You had a tag for the ArcGIS Spatial Analyst but do not appear to be using it so I removed that.
– PolyGeo
Commented Sep 2, 2022 at 22:23

The trick to obtain the desired output is to `map` a function that gets the pixel mean value of each annual mean temperature raster for all the years of interest. So the first step is to construct a list of the years of interest and then map a function to those years.

``````var startYear = ee.Number.parse(startDate.format('Y'));
var endYear = ee.Number.parse(endDate.format('Y'));
var years = ee.List.sequence(startYear, endYear, 1);
print('years', years);

//Get ERA5 data
var era5_2mt = ee.FeatureCollection(years.map(function(year){
// Create start date using year value
var start = ee.Date.fromYMD(ee.Number(year), 1, 1);
var resul = ee.ImageCollection("ECMWF/ERA5/MONTHLY")
.filterBounds(area.geometry())
.select('mean_2m_air_temperature')
// Calculate mean raster
.mean()
// Calculate the mean of the pixel values
.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: area.geometry(),
// Resolution in m for the calculation
scale: 27830
});
// Return as Feature without geometry but with the value of interest as a property
return ee.Feature(null, resul);
}));

print('era5_2mt', era5_2mt);

// Export as csv to drive
Export.table.toDrive({
collection: era5_2mt,
description: 'mean_2m_air_temperature'
});
``````