1

I have a dataset extracted from the MODIS daily snow cover dataset, combined with a DEM. It consists of seven bands with different height classes (name of the bands). These bands consist of ones and zeros, and I needed find the band with the minimum amount of ones, and save the height (name) of that band. I need to do this for several grid cells and several days. Eventually I would like to have a table with days on one axis, and grid cel on the other, and within it the height (minimum band) at that day. I am currently trying to find the minimum band but have not managed to do so. The code can be found here.

Is there a way to extract the name of the band with the minimum value, and save that in some data structure, for a certain day and grid cell?

Snippet of the code:

var snow = function(img){
  var e500 = img.expression('E + S',{'E':img.select(1),'S':img.select(0)}).remap([0,1,2],[0,1,0]).rename('0500');
  var e1000 = img.expression('E + S',{'E':img.select(2),'S':img.select(0)}).remap([0,1,2],[0,1,0]).rename('1000');
  var e1500 = img.expression('E + S',{'E':img.select(3),'S':img.select(0)}).remap([0,1,2],[0,1,0]).rename('1500');
  var e2000 = img.expression('E + S',{'E':img.select(4),'S':img.select(0)}).remap([0,1,2],[0,1,0]).rename('2000');
  var e2500 = img.expression('E + S',{'E':img.select(5),'S':img.select(0)}).remap([0,1,2],[0,1,0]).rename('2500');
  var e3000 = img.expression('E + S',{'E':img.select(6),'S':img.select(0)}).remap([0,1,2],[0,1,0]).rename('3000');
  var e3500 = img.expression('E + S',{'E':img.select(7),'S':img.select(0)}).remap([0,1,2],[0,1,0]).rename('3500');
return ee.Image.cat([e500,e1000,e1500,e2000,e2500,e3000,e3500])};
var B = A.map(snow);

// next a function is needed to sum and find the minimum

var unweighted = function(img){var uni = img.reduceRegions({
    reducer: ee.Reducer.sum().unweighted(),
    collection: grid,scale:500});
    var sum = ee.Feature(uni);
    return sum;
};

var sumA =B.map(unweighted);
var sumB = sumA.reduceColumns(ee.Reducer.min(7),['0500','1000','1500','2000','2500','3000','3500']);
print(sumB);
0

I guess your goal is to make this workable for more than one image, thus an image collection. Therefore, it needs to work inside a map. With one single image the function could be omitted.

This function should make a featurecollection with one feature for every grid. It appends the name of the minimal value-band (as BandName) and the min value (as minValue). As your input is one image, and the output a featurecollection, you will need to flatten() the outcome of the function.

// next a function is needed to sum and find the minimum
var unweighted = function(img){
  var uni = img.reduceRegions({
    reducer: ee.Reducer.sum().unweighted(),
    collection: grid,
    scale:500}).map(function(feat){
      var id = ee.String(feat.get('system:index')).cat('-').cat(ee.String(img.get('system:index')));
      var values = ee.Feature(feat).toDictionary(img.bandNames()).values();
      var bandNames = img.bandNames().map(function(name){return ee.Number.parse(name)});
      var array = ee.Array.cat([values, bandNames], 1);
      var min = array.reduce(ee.Reducer.min(2), [0], 1);
      var key = ee.String(min.get([0,1]).toInt());
      var value = min.get([0,0]);
      return ee.Feature(feat.setMulti(ee.Dictionary.fromLists(['BandName', 'minValue', 'id'], [key, value, id])));
    });
    return uni; 
};

var featureCollection = B.map(unweighted).flatten();
print('featureCollection output as the Grid', 'prop BandName is the band corresponding to', 
          '(first) minimum value "minValue"',featureCollection);

Link script

  • thank you, this works. However, it would be nice to eventually have a table with day on one axis, and grid cell on the other, only saving the height? – Muis de Kogel Mar 14 at 13:30
  • Just export it as CSV? code.earthengine.google.com/adab8254b66a5ca134dd97dc6aec2b97. in the Earth Engine, the structure would be best remained in a feature collection – Kuik Mar 14 at 13:46
  • Ah thank you. I did the export before, but did not get the selector part well. This helps a lot to reduce the CSV size for 20 years of data. – Muis de Kogel Mar 15 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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