0

This code gives me the snow depth less than 0.0254 for each day in 2021, as 0 and 1 (0 means snow depth greater than 0.0254 and 1 means snow depth less than 0.0254) but, at the end, I want an image that sums up 20 days 1st of 2021 (the number of days each pixel has snow depth less than 0.0254). How can I do this?

var year = 2021
var fromDayOfYear = 1
var toDayOfYear = 20


var collection  = ee.ImageCollection('NASA/GLDAS/V021/NOAH/G025/T3H')
  .select('SnowDepth_inst')
  
sequence(fromDayOfYear, toDayOfYear) // Client side array
  .map(function (dayOfYear) {
    var date = ee.Date.fromYMD(year, 1, 1).advance(dayOfYear - 2, 'days')
    var image = collection
      .filter(ee.Filter.date(date.getRange('day')))
      .mean()
      .lte(0.0254)
    addWindspeedLayer(image, dayOfYear)
  })
  

function addWindspeedLayer(image, dayOfYear) {
  var visParams = {
    min: -0.28945971257313863,
    max: 7.197656994173444,
    palette: ['1303ff', '42fff6', 'f3ff40', 'ff5d0f'],
  }
  Map.addLayer(image.clip(table), visParams, 'Day ' + dayOfYear)
}
  
var sdVis = {
  min: 36.226610524397074,
  max: 2253.6926496799506,
  palette: ['1303ff', '42fff6', 'f3ff40', 'ff5d0f'],
}


function sequence(start, end) {
  return Array
    .apply(null, {length: 1 + end - start })
    .map(function (_, i) { return i + start })
} 

1 Answer 1

1

You already have a loop for daily snowdepth in your code. You additionally only need to create an Image Collection with all images added to the Canvas. As each image only have 0 and 1, the sum of all them in the Image Collection will have the sums up 20 days for each pixel. Following code is capable to do this for an arbitrary region in USA where can it snow.

var year = 2021;
var fromDayOfYear = 1;
var toDayOfYear = 20;

var collection  = ee.ImageCollection('NASA/GLDAS/V021/NOAH/G025/T3H')
  .select('SnowDepth_inst');
  
var list = ee.List([]);
  
sequence(fromDayOfYear, toDayOfYear) // Client side array
  .map(function (dayOfYear) {
    var date = ee.Date.fromYMD(year, 1, 1).advance(dayOfYear - 2, 'days');
    var image = collection
      .filter(ee.Filter.date(date.getRange('day')))
      .mean()
      .lte(0.0254);
    addSnowDepthLayer(image, dayOfYear);
  });
  

function addSnowDepthLayer(image, dayOfYear) {
  var visParams = {
    min: 0,
    max: 1,
    palette: ['blue', 'cyan'],
  };
  
  var new_img = image.clip(table);
  
  //Map.addLayer(new_img, visParams, 'Day ' + dayOfYear);
  
  list = list.add(new_img);
  
}

function sequence(start, end) {
  return Array
    .apply(null, {length: 1 + end - start })
    .map(function (_, i) { return i + start });
} 

var new_col = ee.ImageCollection(list);

var sum = new_col.sum();

//Map.addLayer(ee.Image(ee.List(list).get(3)));

Map.addLayer(sum, imageVisParam, 'sum');
Map.centerObject(table);

After running above code at GEE code editor, I got following result:

enter image description here

On above picture, I clicked on the pixel of right bottom corner where was summed until 16 days with the specified threshold (dark blue pixels have 0 value).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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