TransWikia.com

Reformatting Dictionary Objects to rows and columns in Google Earth Engine

Geographic Information Systems Asked by Cece on January 16, 2021

I’m new to GEE and trying to figure out how to create a table with 4 columns (month, Tmaxmin Tmaxmean, and Tmaxmax) with 444 entries for each. Any ideas on how to do this?? Here’s my code.

// Load and define the region of interest 

var subwatershed = ee.FeatureCollection("users/cherratc/subwatershed-polygon")

// Load the TerraClimate image collection and filter it by the date range and variable 

var dataset = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE')
  .filterBounds(subwatershed)
  .filterDate("1979-01-01","2016-01-01")
  .select("tmmx");


// Convert dataset from ImageCollection to Image

var image = dataset.toBands();

// Divide Image to obtain results in degrees celsius

var scaledImage = image.divide(10);

//Compute mean minimum temperature across ROI for each month

var subwatershedMean = scaledImage.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: subwatershed.geometry(),
  scale: 30,
  maxPixels: 1e11
});

print('TMaxMean',subwatershedMean);

// Compute upper bound for maximum temperature for each month

var subwatershedMax = scaledImage.reduceRegion({
  reducer: ee.Reducer.max(),
  geometry: subwatershed.geometry(),
  scale: 30,
  maxPixels: 1e12
});
print('TMaxMax',subwatershedMax);

// Compute lower bound for maximum temperature for each month

var subwatershedMin = scaledImage.reduceRegion({
  reducer: ee.Reducer.min(),
  geometry: subwatershed.geometry(),
  scale: 30,
  maxPixels: 1e12
});
print('TMaxMin',subwatershedMin)

// Create table with all three variables and export it to an excel spreadsheet

https://code.earthengine.google.com/d49bb64c187418ae7d405821c50c99a7

One Answer

There's two rules of efficient Earth Engine programming that will not only make your script run better on large data sets, but will also make it simpler to get the result you want.

  1. Instead of using reduceRegion several times on the same region with different reducers, construct one combined reducer that computes all the results together (in one output dictionary).
  2. Process collections as collections; avoid toList and toBands. This way, the results for each month are separate features in the output collection, not entries in a dictionary. Since feature collections are tables, this means you don't have to do any data conversion to export them.
var dataset = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE')
  .filterBounds(subwatershed)
  .filterDate("1979-01-01","2016-01-01")
  .select("tmmx");

var results = dataset.map(function (image) {
  var scaledImage = image.divide(10);
  var reduction = scaledImage.reduceRegion({
    reducer: ee.Reducer.minMax().combine({
      reducer2: ee.Reducer.mean(),
      sharedInputs: true,
    }),
    geometry: subwatershed.geometry(),
    scale: 30,
  });
  // Attach the reduction results and month as properties to the features.
  return image.setMulti(reduction)
    .set('month', image.date().get('month'));
});

// Preview
print(results.limit(10));

// Export CSV
Export.table.toDrive({
  collection: results,
  selectors: ['month', 'tmmx_mean', 'tmmx_min', 'tmmx_max'],
  fileFormat: 'csv',
  description: 'tmmx_statistics',
});

https://code.earthengine.google.com/1f7dec81dcbd6ebb52157cf0ee423779

This will produce table output like the following. (Note that your ROI asset wasn't public, so I drew a polygon just for testing and the numbers won't match the ones you want.)

month tmmx_mean tmmx_min tmmx_max
1 -13.14891635 -13.2 -13.1
2 -9.392522791 -9.5 -9.3
3 -0.3491377597 -0.5 -0.3
4 1.396253722 1.2 1.5
5 20.09761134 20 20.2
6 16.42754677 16.3 16.6
7 22.17167321 22 22.4
8 20.77410891 20.7 20.9
9 13.18549459 13.1 13.4
10 2.718732402 2.6 2.8
11 -2.205163972 -2.3 -2.1
12 -5.427413822 -5.5 -5.4
1 -12.55723209 -12.6 -12.5
2 -8.499013843 -8.6 -8.4
3 -4.435080324 -4.6 -4.3
... ... ... ...

Answered by Kevin Reid on January 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP