3

I am trying to adapt script from Daniel Wiell on accessing GFS data and add the DateSlider on it. Unfortunately no success.

My goal is can access 24-hour forecast data from every date that I chosed from the slider. My code is below:

var region = ee.Geometry(Map.getBounds(true))

// Strip time off current date/time
var today = ee.Date(new Date().toISOString().split('T')[0])
var start_period = ee.Date('2015-07-01');

var collection = ee.ImageCollection('NOAA/GFS0P25')
  .select('total_precipitation_surface')
  // Might not have date for today, so include yesterday too
  .filterDate(today.advance(-1, 'day'), today.advance(1, 'day'))
  // Sort so latest can be picked by taking first
  .sort('system:time_start', false)


function forecast(hours) {
  var image = collection
    .filterMetadata('forecast_hours', 'equals', hours)
    // Since colleciton is sorted descending, if there are forecasts 
    // for both yesterday and today, first() will give today.
    .first()
  var date = image.date().advance(hours, 'hours')
  return image
    .set('date', date.format())
}

// UI widgets needs client-side data. evaluate()
// to get client-side values of start and end period
ee.Dictionary({start: start_period, end: today.advance(1, 'day')})
  .evaluate(renderSlider) 

function renderSlider(dates) {
  var slider = ui.DateSlider({
    start: dates.start.value, 
    end: dates.end.value, 
    period: 1, // Every 5 days
    onChange: renderDateRange
  })
  Map.add(slider)
}

function renderDateRange(dateRange) {
  var gfs1day = ee.ImageCollection([24].map(forecast))
      .filterDate(dateRange.start(), dateRange.end());
      
  var vis = {min: 0, max: 200, palette: [
      'cccccc','f9f3d5','dce2a8','a8c58d','77a87d','ace8f8',
      '4cafd9','1d5ede','001bc0','9131f1','e983f3','f6c7ec'
    ]}
  var layer = ui.Map.Layer(gfs1day, vis, 'GFS 1-day')
  Map.layers().reset([layer])
}

Link to GEE code

Any idea where my error is?

1 Answer 1

2

Not sure what you try to achieve with the function forecast... I think that is not necessary. I would recommend to filter the complete collection first on band and metadata:

var collection = ee.ImageCollection('NOAA/GFS0P25')
  .select('total_precipitation_surface')
  .filterMetadata('forecast_hours', 'equals', 24)

Then you can simply filter that collection with the daterange returned from the ui.DateSlider. You can then add the collection to the map, eventually by forcing a type of aggregation (I chose median()):

function renderDateRange(dateRange) {
  var gfs1day = collection
      .filterDate(dateRange.start(), dateRange.end())
      .median(); // make a composite of the collection
      
  var vis = {min: 0, max: 200, palette: [
      'cccccc','f9f3d5','dce2a8','a8c58d','77a87d','ace8f8',
      '4cafd9','1d5ede','001bc0','9131f1','e983f3','f6c7ec'
    ]}
  var layer = ui.Map.Layer(gfs1day, vis, 'GFS 1-day')
  Map.layers().reset([layer])
}

Link code

3
  • Thank you, its worked! Btw, is it possible whenever I open the link, the forecast automatically show for current date (without move the slider to other date and back to current date)?
    – user97103
    Aug 28, 2020 at 16:18
  • link
    – Kuik
    Aug 29, 2020 at 8:45
  • Thank you @Kuik you made my day.
    – user97103
    Aug 29, 2020 at 9:32

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.