0

I'm trying to calculate the windspeed from a pixel many times changing the date range from an image collection. I Don't know how write loop for that:

var fc = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017')
  .filter('wld_rgn == "SE Asia"');

var Windspeed = ee.ImageCollection('NASA/GLDAS/V021/NOAH/G025/T3H')
  .filter(ee.Filter.date('2020-01-01'))
  .select('Wind_f_inst')
  .mean();

var Windspeed1 = ee.ImageCollection('NASA/GLDAS/V021/NOAH/G025/T3H')
  .filter(ee.Filter.date('2020-01-02'))
  .select('Wind_f_inst')
  .mean();

...

var Windspeed364 = ee.ImageCollection('NASA/GLDAS/V021/NOAH/G025/T3H')
  .filter(ee.Filter.date('2020-12-31'))
  .select('Wind_f_inst')
  .mean();

var wind = Windspeed.add(Windspeed1)..........add(windspeed364)

var WindVis = {
  min: -0.28945971257313863,
  max: 7.197656994173444,
  palette: ['1303ff', '42fff6', 'f3ff40', 'ff5d0f'],
};

Map.centerObject(fc);
Map.addLayer(wind.clip(fc), WindVis, 'U2');
1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Dec 6, 2022 at 10:00

1 Answer 1

1

You could for instance do client-side iteration over the days of year:

var year = 2020
var fromDayOfYear = 1
var toDayOfYear = 2 // Tweak this as you want
var fc = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017')
  .filter('wld_rgn == "SE Asia"')

Map.centerObject(fc)

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


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


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

https://code.earthengine.google.com/9f2c354df6a384b9977188dd005f5a4b

6
  • 1
    You are likely to find that this fails because you're generating gigantic requests on the client and trying to get them to the server. All those map() functions could just as easily be working on server side objects, thereby avoiding the scaling problems. Dec 7, 2022 at 9:57
  • 1
    Thanks for the insight on this, Noel. There need to be a client-side loop at one point or another, to add the image layers. So it would be better to get the mean() of the 8 images for each day server-side, then do a client-side loop to add each of the means to the map? Like this: code.earthengine.google.com/c65367e6cc58cc235459a4a5ac7b5e3b Dec 8, 2022 at 8:20
  • 1
    From the original question it looked like only one item (the sum of images) was being added to the map. Dec 8, 2022 at 14:05
  • Ah, you're probably right, I misunderstood that part! I was a bit confused about wanting to display 365 layers to begin with. About the mapping from my comment above. Would that make any difference in practice? Dec 8, 2022 at 14:32
  • For a small number of items, that way would be fine, although I'd probably use aggregate_array to get the list of items to loop over client-side rather than try to generate it again using a different (client-side) method. Dec 9, 2022 at 15:06

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.