# How to find the minimum band for several Features i

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);
``````

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), , 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);
``````