2

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'
});

1 Answer 1

1

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.

2
  • Thanks for the answer, Daniel. This will give the mean temperatures for each county for the entire period I assume. What I want is daily temperature values for each county for a given period. Total records have to be Number_Of_Counties X No_of_days. I'm assuming I have to iterate over each image and reduce it to regions?
    – Shal
    Commented Mar 16, 2022 at 19:11
  • 1
    I've updated the answer. Commented Mar 17, 2022 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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