I have written a code for calculating GDD(Growing Degree Days) using the ERA-5 hourly 2m temperature data in Google Earth Engine. The code uses the max and min temperature of that day to calculate the average temperature which is then subtracted by the base temperature (which is a constant value). The code works fine for a single date data, however when I try to replicate it for a month, I either get a single value for all days or hourly values which is incorrect. I have to carry out this analysis day wise for an entire year, hence I cannot run the code for each day individually. I have tried many methods to replicate the code for a longer time period but none of them are giving me results.
To summarize following are the objectives I wish to achieve with my code:
- Replicate the GDD formula for each day in a month
- Extract GDD value and export in .csv format with the date and GDD value
How can I achieve these objectives?
// Create a geometry representing an export region.
var StudyArea = ee.Geometry.Rectangle([76.134,28.7472,76.394, 28.486]);
Map.addLayer(StudyArea)
Map.centerObject(StudyArea)
var roi = ee.Geometry.Rectangle([76.302,28.602,76.317,28.593]);
Map.addLayer(roi)
//////////Adding Data///////////////////////////////////////////////////////////
var image = ee.ImageCollection("ECMWF/ERA5_LAND/HOURLY")
.filter(ee.Filter.date('2021-01-01','2021-01-02'))
//.filter(ee.Filter.eq('hour', 17))
.filterBounds(StudyArea)
.select('temperature_2m');
///////////////////////Calculation of GDD///////////////////////////////////////
var cgdd = function(image){
var min = image.min()
var min1 = min.clip(StudyArea)
Map.addLayer(min1)
var max = image.max()
var max1 = max.clip(StudyArea)
Map.addLayer(max1)
var a = ee.Image(2)
var sum = max1.add(min1)
var avg = sum.divide(a)
var b = ee.Image(273)
var celcius= avg.subtract(b)
Map.addLayer(avg)
Map.addLayer(celcius)
var c = ee.Image(17.9)
var gdd = celcius.subtract(c).rename('GDD');
//Map.addLayer(gdd)
return gdd;
};
// Test the function on a single image.
var test = cgdd(image);
Map.addLayer(test)
print(test)
// Extracting the values for ROI
var extract = test.reduceRegions({
collection: roi,
reducer: ee.Reducer.mean(),
scale: 10,
});
print(extract)