0

I would like to get a list of bandNames from an image after applying a function on a imageCollection for computing monthly mean. But I found that the bandNames is empty in the returned imagecollection. Here is the code:

PR_geometry = ee.Geometry.Rectangle([-67.32, 17.70, -65.19, 18.56])
imgcollection = ee.ImageCollection('COPERNICUS/S5P/OFFL/L3_NO2').\
                filterDate('2018/1/1', '2018/5/31').filterBounds(PR_geometry)

def make_datelist(n):
    return startdate.advance(n, 'month')

dates = dates.map(make_datelist)

# Get monthly mean using reducer
def getm(d1):
    d1 = ee.Date(d1)
    m = d1.get('month')
    y = d1.get('year')
    s = imgcollection.filter(ee.Filter.calendarRange(y, y, 'year'))\
        .filter(ee.Filter.calendarRange(m, m, 'month')).mean()
    return s\    #return image for each specific month and year
            .set('month', m)\
            .set('year', y)

MonthlyCollection=ee.ImageCollection.fromImages(dates.map(getm))

# print out the bandNames, but return []
print(MonthlyCollection.first().bandNames().getInfo())

However, if just try one image only, it can return full list of bandNames.

# Get images in single month
singleMonth = imgcollection.filter(ee.Filter.calendarRange(2018, 2018, 'year'))\
        .filter(ee.Filter.calendarRange(1, 1, 'month'))

# Return full list of bandNames
print('List of bands:',singleMonth.first().bandNames().getInfo())
reducedimg = singleMonth.reduce(ee.Reducer.mean())

# Return full list of bandNames
print('List of bands for reducedimg:',reducedimg.bandNames().getInfo())

So, why bandNames becomes empty after applying the function and how to get it back?

I also tried this, but still could not get the bandNames, what's wrong?

listOfImages = MonthlyCollection.toList(MonthlyCollection.size());
img0=ee.Image(listOfImages.get(2))
print(img0.propertyNames().getInfo())
print(img0.get('system:band_names').getInfo()) # return nothing
print(img0.get('system:band').getInfo())       # return nothing

TIA

1 Answers1

0

I finally find what happened. The reason that there is no bandName returned simply because there is no image available within specific time period. To cater this, I have added a count of bands value using .set function, and then filter the imageCollection by count of bands greater or equal to 1. Below is modified getm() function.

# Get monthly mean using reducer
def getm(d1):
    d1 = ee.Date(d1)
    m = d1.get('month')
    y = d1.get('year')
    s = imgcollection.filter(ee.Filter.calendarRange(y, y, 'year'))\
        .filter(ee.Filter.calendarRange(m, m, 'month')).mean()
    return s\    #return image for each specific month and year
            .set('month', m)\
            .set('year', y)\
            .set('count', s.bandNames().length())

listofimages = dates.map(getm)
monthlyCol = ee.ImageCollection.fromImages(listofimages)    
# filter out entry without any band
monthlyCol = monthlyCol.filter(ee.Filter.gte('count', 1))

# print out no. of images with bands 
print('no. of images with bands:', monthlyCol.size().getInfo())
# return bandNames successfully.
print('bandnames: ', monthlyCol.first().bandNames().getInfo())