Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ic_get_dates() function #216

Closed
ErikSeras opened this issue Dec 13, 2020 · 1 comment
Closed

Add ic_get_dates() function #216

ErikSeras opened this issue Dec 13, 2020 · 1 comment
Assignees
Labels
Feature Request New feature or request
Projects

Comments

@ErikSeras
Copy link
Contributor

  • geemap version: 0.8.4
  • Python version: 3.6.9
  • Operating System: Google Colab

Description

I would like geemap to have a function that allows getting dates from an ee.ImageCollection. I tried it this way.

What I Did

import ee
import geemap
from datetime import datetime

try:
    ee.Initialize()
except Exception as e:
    ee.Authenticate()
    ee.Initialize()

# Function
def ic_get_dates(imgcol):
    def iter_func(image, newlist):
        date = ee.Number.parse(image.date().format("YYYYMMddHHmmss"));
        newlist = ee.List(newlist);
        return ee.List(newlist.add(date).sort())
    date_ee = imgcol.iterate(iter_func, ee.List([]))

    date_list = list(ee.List(date_ee).reduce(ee.Reducer.frequencyHistogram()).getInfo().keys())

    def date_gee(date):
        return str(datetime.strptime(date, '%Y%m%d%H%M%S')).replace(" ", "T")

    return list(map(date_gee, date_list))

# IC
ic = ee.ImageCollection("NOAA/GOES/16/MCMIPF") \
    .filterDate("2020-08-18T17:00:00", "2020-08-18T18:00:00")

# Get dates
dates = ic_get_dates(ic)

print(dates)

# ['2020-08-18T17:00:20', '2020-08-18T17:10:20', '2020-08-18T17:20:20', '2020-08-18T17:30:20', '2020-08-18T17:40:20', '2020-08-18T17:50:20']
@giswqs giswqs self-assigned this Dec 13, 2020
@giswqs giswqs added the Feature Request New feature or request label Dec 13, 2020
@giswqs giswqs added this to To do in geemap via automation Dec 13, 2020
@giswqs giswqs moved this from To do to Done in geemap Dec 13, 2020
@giswqs
Copy link
Member

giswqs commented Dec 13, 2020

The function can be simplified as two lines. See geemap.image_dates()

def image_dates(img_col, date_format='YYYY-MM-dd'):

    dates = img_col.aggregate_array('system:time_start')
    new_dates = dates.map(lambda d: ee.Date(d).format(date_format))
    return new_dates

@giswqs giswqs closed this as completed Dec 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request New feature or request
Projects
geemap
  
Done
Development

No branches or pull requests

2 participants