Google Earth Engine – How to Export Clipped CHIRPS Gridded Data to CSV

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

This is what I have done so far,

Here's the code,
Link: https://code.earthengine.google.com/6844c4783cd11204e9754e027dd66c3d


var grid = ee.Geometry.Polygon(
        [[[-108.9998, 31.5002],
          [-108.9998, 31.7498],
          [-108.7495, 31.7498],
          [-108.7495, 31.5002]]]);
   
var chirps2021_1 = ee.ImageCollection('UCSB-CHG/CHIRPS/PENTAD')
            .filterDate('2021-01-01', '2021-02-28')
            .filter(ee.Filter.calendarRange(1,2,'month'))
            .filterBounds(grid);

var chirps2021_1_sum = ee.Image(chirps2021_1.sum())

var chirps2021_1_sum_grid = chirps2021_1_sum.clip(grid);

var precipitationVis = {
  min: 0.0,
  max: 112.0,
  palette: ['001137', '0aab1e', 'e7eb05', 'ff4a2d', 'e90000'],
};

Map.addLayer(chirps2021_1_sum_grid, precipitationVis, "Sum January & February 2021 Precipitation", true, 1.0);

Map.centerObject(grid, 0);

And here's the image,
enter image description here

As you can see, I clipped the sum of CHIRPS precipitation data to a 0.25-degree grid.
And inside the grid, you can observe a 5×5 grid, where each grid is 0.05 degrees, which is the CHIRPS gridded data. Each of the 25 grids has a different precipitation value. For example, the top left grid has a value of 21.11, and the bottom right has a value of 20.15.

What I want to do now is to export the data as a CSV file.
Here's the format I want to export; it should be a table with 25 rows corresponding to each CHIRPS grid. The headers for the table would be,

  • Grid Number
  • Precipitation Value
  • Polygon (the coordinates of the grid's polygon) – Optional

Best Answer

This is one way:

Add this to your code:

//  Create a featurecollection of  each pixel centroids of the image
var pixels = chirps2021_1_sum_13885.multiply(100).toInt().reduceToVectors({
  geometry: grid_13885,
  scale: 5566,
  geometryType: 'centroid',//you can change it to polygons but the result aren't optimal
  labelProperty: 'precipitation',
});

//get lat long
var proj = chirps2021_1_sum_13885.select([0]).projection()
var latlon = ee.Image.pixelLonLat()
chirps2021_1_sum_13885 = chirps2021_1_sum_13885.addBands(latlon.select('longitude','latitude'))

//extract lat, lon and precipitation
pixels=chirps2021_1_sum_13885.select('longitude').reduceRegions(pixels, ee.Reducer.first().setOutputs(['long']), 25)
pixels=chirps2021_1_sum_13885.select('latitude').reduceRegions(pixels, ee.Reducer.first().setOutputs(['lat']), 25);
pixels=chirps2021_1_sum_13885.select('precipitation').reduceRegions(pixels, ee.Reducer.first().setOutputs(['precipitation']), 25);


// Export the feature collection as a CSV file
Export.table.toDrive({
  collection: pixels,
  description: 'grid_13885',
  selectors: ['system:index','precipitation','lat','long'],
  fileFormat: 'CSV'
});

In the end you get the table with: system:id (grid number), lat, lon and precipitation values.

here the link of the code