Google Earth Engine – Grouping Weather Data by Region

google-earth-enginegoogle-earth-engine-javascript-api

I'm trying to extract daily weather parameters using https://developers.google.com/earth-engine/datasets/catalog/IDAHO_EPSCOR_GRIDMET. I'm trying to extract tmmx, tmmn and group them county-wise. Simply I want daily min and max temperatures for counties for a specified period of time. I'm new to Earth engine and I'm not sure how to go about the problem. Following is the code I have come up with. I'm struggling to figure out a way to aggregate the data.

var tmpCollection = ee.ImageCollection('IDAHO_EPSCOR/GRIDMET')
                    .select('tmmx', 'tmmn') 
                    .filterDate('2017-01-01', '2017-12-31');
                    
print(tmpCollection, 'weatherData');  

var nonCONUS = ['02','15','60','66','69','72','78']
var counties=ee.FeatureCollection('TIGER/2018/Counties').filter(ee.Filter.inList('STATEFP', nonCONUS).not());

print(counties.size(), 'counties');

// visualize        
Map.addLayer(counties,{},'counties');        
        
var countyTemp = tmpCollection.map.reduceRegions({
  collection: counties,
  reducer: ee.Reducer.mean(),
  scale: 4000
});

print(countyTemp);
  

// Export ---------------------------------------------------------------------
Export.table.toDrive({
  collection: countyTemp,
  description: 'weather_by_county',
  folder: 'weather_project',
  fileFormat: 'CSV'
});

Best Answer

It's a bit fiddly to do this. Here's one approach:

var days = endDate.difference(startDate, "days") // Number of days in date range
var dailyTempByCounty = ee.FeatureCollection(ee.List.sequence(0, days.subtract(1))
  .map(function(dayOffset) { // Calculate mean per day
    var date = startDate.advance(ee.Number(dayOffset), 'days')
    var dailyMean = tmpCollection
      .filterDate(date, date.advance(1, 'days'))
      .mean()
    return counties.map(function (county) {
       // Calculate mean for every county separately, 
       // so the date can be included
      var mean = dailyMean.reduceRegion({
        reducer: ee.Reducer.mean(), 
        geometry: county.geometry(), 
        scale: 4000
      })
      return county
        .set(mean)
        .set('date', date.format('yyyy-MM-dd'))
    })
  })
).flatten() // Flattens the collection of collection of features into a collection of features

https://code.earthengine.google.com/64937228d07d5c2d05cec1d5400aa914

It takes quite a lot of processing to do this. The linked script uses only data for a couple of days, and just two counties, to give a fast result that can be printed in the console.

Note that the end date is exclusive. Just like the date filter in collections - many people seem to get that wrong, and miss the last day of the year, just like in your question.

Related Question