#StackBounty: #r #google-earth-engine Write a for-loop in google earth engine

Bounty: 100

I have a grid shapefileof equal size polygons as shown below

library(tidyverse)
library(raster)

dat <- structure(list(ID = 758432:758443, 
                        lat = c(24.875, 24.875, 24.625, 24.625, 24.875, 24.875, 24.625, 24.625, 24.375, 24.375, 24.125, 24.125), 
                        lon = c(72.875, 72.625, 72.625, 72.875, 72.375, 72.125, 72.125, 72.375, 72.375, 72.125, 72.125, 72.375)), 
                  class = "data.frame", row.names = c(NA, -12L))


dat_rast <- rasterFromXYZ(dat[, c('lon', 'lat', 'ID')], crs = '+proj=longlat +datum=WGS84 +no_defs')
dat_poly <- rasterToPolygons(dat_rast, fun=NULL, na.rm=TRUE, dissolve=FALSE)

  

I want to process the NASA_NEX-GDDP data in the google earth engine

https://developers.google.com/earth-engine/datasets/catalog/NASA_NEX-GDDP

This data has 3 variables: pr, tasmin and tasmax and has a resolution of 0.25 arc degrees
and covers the period 1950-01-01 to 2099-12-31

For each polygon in dat_poly, I want to calculate mean daily pr, tasmin and tasmax

So far I can do this for a single lat long and a single variable using the following approach in the
code editor

 var startDate = ee.Date('1950-01-01');
 var endDate = ee.Date('2099-12-31');
 
 // select the variable to be processed: pr, tasmin, tasmax
 var dataset = ee.ImageCollection('NASA/NEX-GDDP')
 .filter(ee.Filter.date(startDate,endDate));
 var maximumAirTemperature = dataset.select('tasmax'); 
 
 // get projection information
 var proj = maximumAirTemperature.first().projection(); 
 
 // the lat lon for which I want to extract the data 
 var point = ee.Geometry.Point([72.875, 24.875]); 
 
 // calculate number of days to map and extract data for
 var n = endDate.difference(startDate,'day').subtract(1);
 
 var timeseries = ee.FeatureCollection(
   ee.List.sequence(0,n).map(function(i){
     var t1 = startDate.advance(i,'day');
     var t2 = t1.advance(1,'day');
     var feature = ee.Feature(point);
     var dailyColl = maximumAirTemperature.filterDate(t1, t2);
     var dailyImg = dailyColl.toBands();
     
    // rename bands to handle different names by date
     var bands = dailyImg.bandNames();
     var renamed = bands.map(function(b){
       var split = ee.String(b).split('_');
       return ee.String(split.get(0)).cat('_').cat(ee.String(split.get(1)));
     });
     
     // extract the data for the day and add time information
     var dict = dailyImg.rename(renamed).reduceRegion({
       reducer: ee.Reducer.mean(),
       geometry: point,
       scale: proj.nominalScale()
     }).combine(
       ee.Dictionary({'system:time_start':t1.millis(),'isodate':t1.format('YYYY-MM-dd')})
     );
     return ee.Feature(point,dict);
   })
 );
 
 Map.addLayer(point);
 Map.centerObject(point,6);
 
 // export feature collection to CSV
 Export.table.toDrive({
   collection: timeseries,
   description: 'my_file',
   fileFormat: 'CSV',
 });
 
 

Instead of extracting for a given lat lon, how can I calculate the mean daily pr, tasmin and tasmax of each polygon in my_poly for the given time period?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.