colab Open in SageMaker Studio Lab Open in Planetary Computer

Getting the Closest Image to a Specific Date

Tutorial created by **David Montero Loaiza**: GitHub | Twitter

Let’s start!

If required, please uncomment:

[1]:
#!pip install eemont
#!pip install geemap

Import the required packages.

[2]:
import ee, eemont, datetime, geemap

Authenticate and Initialize Earth Engine and geemap.

[3]:
Map = geemap.Map()

Get and filter the image collection.

[4]:
MOD16A2 = ee.ImageCollection('MODIS/006/MOD16A2').select('ET')

Closest Image

In order to get the closest image to a specific date, you just have to use the closest() method.

[5]:
MOD16A2 = MOD16A2.closest('2020-10-16')

This method will return an ee.ImageCollection with all the closest images to that specific date. In this case, one image is retrieved.

[6]:
MOD16A2_date = MOD16A2.first().get('system:time_start').getInfo()

The date is retrieved as milliseconds, therefore, we have to convert it to a date object. We’ll use datetime here:

[7]:
datetime.datetime.fromtimestamp(MOD16A2_date/1000)
[7]:
datetime.datetime(2020, 10, 15, 2, 0)

Visualization

Let’s define the visualization parameters:

[8]:
visParams = {
  'min': 0.0,
  'max': 300.0,
  'palette': [
    'ffffff', 'fcd163', '99b718', '66a000', '3e8601', '207401', '056201',
    '004c00', '011301'
  ],
}

Use geemap to display results:

[9]:
Map.addLayer(MOD16A2,visParams,'ET')
Map.add_colorbar(visParams['palette'], vmax = 300.0, caption='ET')
Map