I am trying to create monthly composites of NDVI. But I am getting error.
I have this code:
def create_composites(ndvi, vis, band, view, layer_year, year):
def monthlyComposite(year, month):
start = ee.Date.fromYMD(year, month, 1)
end = start.advance(1, 'month')
composite = ndvi.filterDate(start, end).median()
return composite.set('year', year).set('month', month).set('system:time_start', start.millis())
composites = []
layer_names = []
visual = view
for month in range(1, 13):
composite = monthlyComposite(year, month)
layer_name = '{}-{:02d}'.format(year, month)
if visual == 1:
Map.addLayer(composite.select(band), vis, layer_name+layer_year)
composites.append(composite)
layer_names.append(layer_name)
return composites, layer_names
Here is the NDVI code:
def NDVI_band(image):
ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
return image.addBands(ndvi)
NDVI_bands = image.map(NDVI_band)
def ndvi_clip(img):
return img.clip(canada)
clip = with_NDVI_bands.map(ndvi_clip)
ndvi_composites, ndvi_layers = create_composites(clip, ndviVis, 'NDVI', view, layer_year, year)
I am getting this error:
---------------------------------------------------------------------------
HttpError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/ee/data.py in _execute_cloud_call(call, num_retries)
401 try:
--> 402 return call.execute(num_retries=num_retries)
403 except googleapiclient.errors.HttpError as e:
11 frames
HttpError: <HttpError 400 when requesting https://earthengine.googleapis.com/projects/project/maps?fields=name&alt=json returned "Image.select: Band pattern 'NDVI' was applied to an Image with no bands. See https://developers.google.com/earth-engine/guides/debugging#no-bands". Details: "Image.select: Band pattern 'NDVI' was applied to an Image with no bands. See https://developers.google.com/earth-engine/guides/debugging#no-bands">
During handling of the above exception, another exception occurred:
EEException Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/ee/data.py in _execute_cloud_call(call, num_retries)
402 return call.execute(num_retries=num_retries)
403 except googleapiclient.errors.HttpError as e:
--> 404 raise _translate_cloud_exception(e) # pylint: disable=raise-missing-from
405
406
EEException: Image.select: Band pattern 'NDVI' was applied to an Image with no bands. See https://developers.google.com/earth-engine/guides/debugging#no-bands
I have tried to debug to find out in which part of the code, the bands are empty/not NDVI and I figured out that in the create_composites
function, composite
is returning empty bands list. I have successfully run this code a few months ago but now I am facing this error.
Can someone please suggest a solution?