1

I am using the TerraClim data in Earth Engine to obtain location and year specific temperature values. I have 12 districts and TerraClim has data from 1958 to 2015. For each district, I want to get the mean of max temperature (during January - April) for each year. I was able to get the mean max temperature (Jan-Apr) for each year. But, for some reason, the reduceRegion() or reduceRegions() is not working. Need help to ultimately create a table with: District attribute, mean temperature for each year between 1958 and 2015.

//Image and shapefile import
var images = ee.ImageCollection("IDAHO_EPSCOR/TERRACLIMATE"),
var districts = ee.FeatureCollection("users/asifishti/IGP_dist");

//Selecting maximum temperature band 
var image_maxtmp = images.select ("tmmx");
print (image_maxtmp);

//Filtering image for Jan - April and 1958 and 2015
var maxtmp_janapr = image_maxtmp.filter(ee.Filter.calendarRange(1958,2015,'year'))
.filter(ee.Filter.calendarRange(1,4,'month'))
print (maxtmp_janapr);

//Getting yearwise max temperature image
var year = ee.List.sequence (1958,2015);
var mean_func = function (i) {
  return maxtmp_janapr.mean().set("Year", i)
};

var yearwise_maxtmp = ee.ImageCollection(year.map(mean_func));
print (yearwise_maxtmp);

//Calculating max temp for each district (NOT WORKING)
var dist_maxtmp = function(feature){
  var mean_maxtmp = yearwise_maxtmp.reduceRegions({
    reducer: ee.Reducer.mean(),
    geometry: feature.geometry(),
    scale: 4000, //The image has 2.5 arc-second/~4km resolution
    maxPixels: 1e12
  });
  var maxtmp_dist = feature.set("Max Temp", mean_maxtmp.get("tmmx"));
  return maxtmp_dist;
};

var distwise = districts.map(dist_maxtmp);
print (distwise);
1
  • Additional information: The districts are non-contiguous.
    – Evan
    Apr 17 '20 at 5:08
3

The problem with your code is that you are calling reduceRegions() on yearwise_maxtmp, which is an ee.ImageCollection. It can only be called on an ee.Image. Here's one way to do this:

// var districts = ee.FeatureCollection("users/asifishti/IGP_dist")
// You didn't share your districts
var districts = ee.FeatureCollection([
  ee.Feature(ee.Geometry.Point([0, 10]).buffer(10000), {'District': 'District A'}),
  ee.Feature(ee.Geometry.Point([50, 10]).buffer(10000), {'District': 'District B'})
])

var startYear = 1958
var endYear = 2015
var startMonth = 1
var endMonth = 4

var yearlyMeans = ee.FeatureCollection(
  ee.List.sequence(startYear, endYear)
    .map(calculateYearlyMeans)
).flatten()

print(yearlyMeans)  
print(
  ui.Chart.feature.groups({
    features: yearlyMeans, 
    xProperty: 'Year', 
    yProperty: 'Max Temp', 
    seriesProperty: 'District'
  })
)

Export.table.toDrive({
  collection: yearlyMeans, 
  description: 'yearlyMeans',
  selectors: ['District', 'Year', 'Max Temp']
})


function calculateYearlyMeans(year) {
  year = ee.Number(year)
  var mean = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE')
    .select("tmmx")
    .filter(ee.Filter.calendarRange(year, year.add(1), 'year'))
    .filter(ee.Filter.calendarRange(startMonth, endMonth, 'month'))      
    .mean()
  return districts.map(function (district) {
    return calculateDistrictMean(district, year, mean)
  })
}

function calculateDistrictMean(district, year, mean) {
  var districtMean = mean.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: district.geometry(),
    scale: 4000,
    maxPixels: 1e12
  })
  return district
    .set('Max Temp', districtMean.get('tmmx'))  
    .set('Year', year)
}

https://code.earthengine.google.com/bfccec9c678a65aa3de98b26d36abe4c

1
  • Thank you so much, Daniel! This is really a nice solution.
    – Evan
    Apr 17 '20 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.