2

enter image description hereI have been trying to create mosaic images from the region of japan from the MCD19A2.006 imagecollection (https://developers.google.com/earth-engine/datasets/catalog/MODIS_006_MCD19A2_GRANULES#description).

I wish to create a single mosaic image for each day of the year from the period 2013/03/01 to 2020/03/01 and be able to download it locally to my computer. I am able to create an image for one day but I am unsure how to create a loop to do this for each day and have it named differently using python. I would also like to know how I can mosaic all the bands together as currently I am only able to do this with one band at a time.

Below is test code that I did for one day (2020/01/01) using only the Optical_Depth_047 band.

roi= ee.Geometry.Polygon(
    [[[122.791488, 29.85464], 
      [122.791488, 45.462539], 
      [146.780501, 45.462539],
      [146.780501, 29.85464]]], None, False)

collection = ee.ImageCollection('MODIS/006/MCD19A2_GRANULES') \
    .filterDate('2020-01-01', '2020-01-02') \
    .filterBounds(roi) \
    .select('Optical_Depth_047')

mosaic = collection.mosaic()

Map.addLayer(mosaic, {'min': 0, 'max': 500, 'palette': ['black', 'blue', 'purple', 'cyan', 'green', 'yellow', 'red']}, 'mosaic')
0

if I have understood your question correctly. You want to merge all the images that have the same date into one image. So you combine all the satellite tiles together for a given date. The below code should help you.

'''
mergeByDate is a function that merges images together that have the same date.

@ imgCol: [ee.ImageCollection] mandatory value that specifies the image collection to merge by dates with.

Returns ee.ImageCollection
'''
def mergeByDate(imgCol):
    #Convert the image collection to a list.
    imgList = imgCol.toList(imgCol.size())
    
    # Driver function for mapping the unique dates
    def uniqueDriver(image):
        return ee.Image(image).date().format("YYYY-MM-dd")
    
    uniqueDates = imgList.map(uniqueDriver).distinct()

    # Driver function for mapping the moasiacs
    def mosaicDriver(date):
        date = ee.Date(date)
        
        image = (imgCol
               .filterDate(date, date.advance(1, "day"))
               .mosaic())
        
        return image.set(
                        "system:time_start", date.millis(), 
                        "system:id", date.format("YYYY-MM-dd"))
    
    mosaicImgList = uniqueDates.map(mosaicDriver)
    
    return ee.ImageCollection(mosaicImgList)

Then you call the function on your image collection where you store the satellite images. Like so:

collection = mergeByDate(collection)

This does it for all bands that the images contain. No need to loop. The map function can map a function over a collection.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.