Skip to main content
incode doc correction
Source Link
Rodrigo E. Principe
  • 10.2k
  • 1
  • 31
  • 37

After reading carefully your question, I've realized that what you need is a function I have incorporated in geetools

from geetools import tools

masked_collection = ee.ImageCollection('a masked col')
filled_collection = tools.imagecollection.fill_with_last(masked_collection)

The hardcoded version is:

def fill_with_last(collection):
    """ Fill masked values of each image pixel with the last available
    value
    :param selfcollection: the selfcollection that holds the images that will be filled
    :type selfcollection: ee.ImageCollection
    :rtype: ee.ImageCollection
    """

    new = collection.sort('system:time_start', True)
    collist = new.toList(new.size())
    first = ee.Image(collist.get(0)).unmask()
    rest = collist.slice(1)

    def wrap(img, ini):
        ini = ee.List(ini)
        img = ee.Image(img)
        last = ee.Image(ini.get(-1))
        mask = img.mask().Not()
        last_masked = last.updateMask(mask)
        last2add = last_masked.unmask()
        img2add = img.unmask()
        added = img2add.add(last2add) \
            .set('system:index', ee.String(img.id()))

        props = img.propertyNames()
        condition = props.contains('system:time_start')

        final = ee.Image(ee.Algorithms.If(condition,
                                          added.set('system:time_start',
                                                    img.date().millis()),
                                          added))

        return ini.add(final.copyProperties(img))

    newcol = ee.List(rest.iterate(wrap, ee.List([first])))
    return ee.ImageCollection.fromImages(newcol)

After reading carefully your question, I've realized that what you need is a function I have incorporated in geetools

from geetools import tools

masked_collection = ee.ImageCollection('a masked col')
filled_collection = tools.imagecollection.fill_with_last(masked_collection)

The hardcoded version is:

def fill_with_last(collection):
    """ Fill masked values of each image pixel with the last available
    value
    :param self: the self that holds the images that will be filled
    :type self: ee.ImageCollection
    :rtype: ee.ImageCollection
    """

    new = collection.sort('system:time_start', True)
    collist = new.toList(new.size())
    first = ee.Image(collist.get(0)).unmask()
    rest = collist.slice(1)

    def wrap(img, ini):
        ini = ee.List(ini)
        img = ee.Image(img)
        last = ee.Image(ini.get(-1))
        mask = img.mask().Not()
        last_masked = last.updateMask(mask)
        last2add = last_masked.unmask()
        img2add = img.unmask()
        added = img2add.add(last2add) \
            .set('system:index', ee.String(img.id()))

        props = img.propertyNames()
        condition = props.contains('system:time_start')

        final = ee.Image(ee.Algorithms.If(condition,
                                          added.set('system:time_start',
                                                    img.date().millis()),
                                          added))

        return ini.add(final.copyProperties(img))

    newcol = ee.List(rest.iterate(wrap, ee.List([first])))
    return ee.ImageCollection.fromImages(newcol)

After reading carefully your question, I've realized that what you need is a function I have incorporated in geetools

from geetools import tools

masked_collection = ee.ImageCollection('a masked col')
filled_collection = tools.imagecollection.fill_with_last(masked_collection)

The hardcoded version is:

def fill_with_last(collection):
    """ Fill masked values of each image pixel with the last available
    value
    :param collection: the collection that holds the images that will be filled
    :type collection: ee.ImageCollection
    :rtype: ee.ImageCollection
    """

    new = collection.sort('system:time_start', True)
    collist = new.toList(new.size())
    first = ee.Image(collist.get(0)).unmask()
    rest = collist.slice(1)

    def wrap(img, ini):
        ini = ee.List(ini)
        img = ee.Image(img)
        last = ee.Image(ini.get(-1))
        mask = img.mask().Not()
        last_masked = last.updateMask(mask)
        last2add = last_masked.unmask()
        img2add = img.unmask()
        added = img2add.add(last2add) \
            .set('system:index', ee.String(img.id()))

        props = img.propertyNames()
        condition = props.contains('system:time_start')

        final = ee.Image(ee.Algorithms.If(condition,
                                          added.set('system:time_start',
                                                    img.date().millis()),
                                          added))

        return ini.add(final.copyProperties(img))

    newcol = ee.List(rest.iterate(wrap, ee.List([first])))
    return ee.ImageCollection.fromImages(newcol)
Source Link
Rodrigo E. Principe
  • 10.2k
  • 1
  • 31
  • 37

After reading carefully your question, I've realized that what you need is a function I have incorporated in geetools

from geetools import tools

masked_collection = ee.ImageCollection('a masked col')
filled_collection = tools.imagecollection.fill_with_last(masked_collection)

The hardcoded version is:

def fill_with_last(collection):
    """ Fill masked values of each image pixel with the last available
    value
    :param self: the self that holds the images that will be filled
    :type self: ee.ImageCollection
    :rtype: ee.ImageCollection
    """

    new = collection.sort('system:time_start', True)
    collist = new.toList(new.size())
    first = ee.Image(collist.get(0)).unmask()
    rest = collist.slice(1)

    def wrap(img, ini):
        ini = ee.List(ini)
        img = ee.Image(img)
        last = ee.Image(ini.get(-1))
        mask = img.mask().Not()
        last_masked = last.updateMask(mask)
        last2add = last_masked.unmask()
        img2add = img.unmask()
        added = img2add.add(last2add) \
            .set('system:index', ee.String(img.id()))

        props = img.propertyNames()
        condition = props.contains('system:time_start')

        final = ee.Image(ee.Algorithms.If(condition,
                                          added.set('system:time_start',
                                                    img.date().millis()),
                                          added))

        return ini.add(final.copyProperties(img))

    newcol = ee.List(rest.iterate(wrap, ee.List([first])))
    return ee.ImageCollection.fromImages(newcol)