GEE Python API – Temporally Reduce Image Collection from Daily to Monthly Using map()

google-earth-engine-python-api

I have defined a class in BoundaryWiseStats.py that handles a daily ImageCollection,
and reduces it to a monthly collection

\\ BoundaryWiseStats.py

import ee
ee.Initialize()

class BoundaryWiseStats:
    """
    class to group a daily image collection, 
    on a monthly timescale and apply a given reducer
    then return the grouped image collection (12 images)
    """
    
    def __init__(self,year):
        self.iColl = ee.ImageCollection("users/cseicomms/rainfall_imd") #365 images/yr
        self.year = year

    def make_date_range_list(self):
        months = ee.List.sequence(1,12)
        self.drl = months.map(
            lambda m: ee.Date.fromYMD(self.year,m,1).getRange('month'))
        return self.drl
    
    def temp_reduce_image(self,dr):
        start = ee.DateRange(dr).start()  # get the start time of given date range
        end = ee.DateRange(dr).end()     # get the end time of given date range
        
        mimages = self.iColl.filter(ee.Filter.date(start,end)).select(0)  # filter a single month
        mimages_reduced = mimages.reduce(ee.Reducer.sum())             # apply reducer
        mimages_reduced = mimages_reduced.set(               # set start and end times to reduced image
            'system:time_start', start.millis()).set(
            'system:time_end', end.millis())
    
        return ee.Image(mimages_reduced)                     # return reduced image

    def temp_reduce_image_coll(self):
        self.iColl_reduced = self.drl.map(self.temp_reduce_image)
        print(self.iColl_reduced.size().getInfo())
        return self.iColl_reduced

the following code imports BoundaryWiseStats.py and runs uses it to reduce images from 2018. However it returns an error

import ee
ee.Initialize()
import BoundaryWiseStats as BWS
year = 2018
bws = BWS.BoundaryWiseStats(year)
drl = bws.make_date_range_list()
print(drl.size().getInfo())      # expect
iColl_reduced = bws.temp_reduce_image_coll()
TypeError: temp_reduce_image() takes 2 positional arguments but 3 were given

I'm unsure why it says 3 arguments were given, since I'm passing a date range to the temp_reduce_image method??

Best Answer

I don't think there's a way to call GEE's map() function with a class method. Since the functions that can be used in a map() can take only 1 argument, but class methods will have self passed on to it in addition.

You can re-structure your code to something like below

def temp_reduce_image_coll(self):
    
    def temp_reduce_image(dr):
        start = ee.DateRange(dr).start()  # get the start time of given date range
        end = ee.DateRange(dr).end()     # get the end time of given date range
    
        mimages = self.iColl.filter(ee.Filter.date(start,end)).select(0)  # filter a single month
        mimages_reduced = mimages.reduce(ee.Reducer.sum())             # apply reducer
        mimages_reduced = mimages_reduced.set(               # set start and end times to reduced image
            'system:time_start', start.millis()).set(
            'system:time_end', end.millis())

        return ee.Image(mimages_reduced)                     # return reduced image
        
    self.iColl_reduced = self.drl.map(temp_reduce_image)
    print(self.iColl_reduced.size().getInfo())
    return self.iColl_reduced
Related Question