I am new to Google Earth Engine and SO but here goes: I am trying to compute a new type of normalized difference using bands MODIS bands 1 and 11. They are 500m and 1km spatial resolutions and come from from two different MODIS Image Collections (sur_refl_b01 from MOD09GA at 500m resolution and sur_refl_b11 from MODOCGA at 1km resolution).
I first resampled the 1km data to 500m. I can correctly display both the sur_refl_b01 and the resampled sur_refl_b11 data. I then merged the two image collections into a single image collection that I'm calling "modis_combined" It has the correct number of bands but it doesn't display correctly. The Inspector indicates the following error: "Expected a homogeneous image collection, but an image with incompatible bands was encountered: First image type: 2 bands ([sur_refl_b01, sur_refl_b01_1]). Current image type: 2 bands ([sur_refl_b11, sur_refl_b11_1]). Image ID: 2_2_2001_02_28 Some bands might require explicit casts."
I get this error related to calling the cci function in which I use normalizedDifference:
ImageCollection (Error) Error in map(ID=1_2001_03_01): Image.normalizedDifference: No band named 'sur_refl_b11'. Available band names: [sur_refl_b01].
I am guessing that when I merged the image collections that the band names didn't stick. How do I label the bands in the merged imageCollection so that the normalizedDifference function will recognize them? Or what other mess is happening?
// Define a sample Region-of-Interest
var roi = ee.Geometry.Polygon(
[[[-122.0, 45.0],
[-122.1, 32.0],
[-108.9, 32.0],
[-108.9, 45.0]]]);
// Load a collection of MODIS land surface reflectance data (500m)
// Select band 1 (red)
// Filter by a date range
// Subset by the region of interest
var modis_b01_500m = ee.ImageCollection('MODIS/006/MOD09GA')
.select(['sur_refl_b01'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in the collection
print('modis_b01_500m', modis_b01_500m);
print('Number of images in modis_b01_500m:', modis_b01_500m.size());
// Define a collection of MODIS ocean color reflectance data (1km)
var modis_b11_1km = ee.ImageCollection('MODIS/006/MODOCGA')
.select(['sur_refl_b11'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in this collection.
print('modis_b11_1km', modis_b11_1km);
print('Number of images in modis_b11_1km:', modis_b11_1km.size());
//Resample the MODIS 1km collection to 500m spatial resolution
var modis_b11_resampled = modis_b11_1km.map(function(image) {
var modis_pro = image.projection();
var image_resampled = image.reproject(modis_pro, null, 500)
.copyProperties(image);
return image_resampled
})
//Check on the number of bands in this resampled collection.
print('modis_b11_resampled',modis_b11_resampled);
print('Number of images in modis_b11_resampled:', modis_b11_resampled.size());
//combine the resampled collection with the modis 500m collection
var modis_combined = modis_b01_500m.merge(modis_b11_resampled);
//Check on the number of bands in this combined collection.
print('modis_combined', modis_combined);
print('Number of images in modis_combined:', modis_combined.size());
//Display the MODIS combined image collection
//Display the layers
Map.addLayer(modis_b01_500m,{min:0,max:10000},'modis_b01_500m');
Map.addLayer(modis_b11_1km,{min:0,max:2000},'modis_b11_1km');
Map.addLayer(modis_b11_resampled, {min:0, max:10000}, 'modis_resampled');
Map.addLayer(modis_combined, {min:0, max:10000}, 'MODIS combined');
var cci = modis_combined.map(function(image){
var cci = image.normalizedDifference(['sur_refl_b11','sur_refl_b01']);
return cci;
})
print('cci', cci);
print('Number of images in cci:', cci.size());
In your code, you use the .merge()
command to combine the two ImageCollections. The two ImageCollections are combined, but the images from both collections remain seperate from each other, and therefore a .normalizedDifference
function where you need images from both bands cannot be executed.
What you need to do is Join both collections based on the date, so that images from both collections with the same date are combined into one image with 2 bands. This will allow you to do .normalizedDifference
function. Here's how it goes:
// Define a sample Region-of-Interest
var roi = ee.Geometry.Polygon(
[[[-122.0, 45.0],
[-122.1, 32.0],
[-108.9, 32.0],
[-108.9, 45.0]]]);
// Load a collection of MODIS land surface reflectance data (500m)
// Select band 1 (red)
// Filter by a date range
// Subset by the region of interest
var modis_b01_500m = ee.ImageCollection('MODIS/006/MOD09GA')
.select(['sur_refl_b01'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in the collection
print('modis_b01_500m', modis_b01_500m);
print('Number of images in modis_b01_500m:', modis_b01_500m.size());
// Define a collection of MODIS ocean color reflectance data (1km)
var modis_b11_1km = ee.ImageCollection('MODIS/006/MODOCGA')
.select(['sur_refl_b11'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in this collection.
print('modis_b11_1km', modis_b11_1km);
print('Number of images in modis_b11_1km:', modis_b11_1km.size());
//Resample the MODIS 1km collection to 500m spatial resolution
var modis_b11_resampled = modis_b11_1km.map(function(image) {
var modis_pro = image.projection();
var image_resampled = image.reproject(modis_pro, null, 500)
.copyProperties(image);
return image_resampled
})
//Check on the number of bands in this resampled collection.
print('modis_b11_resampled',modis_b11_resampled);
print('Number of images in modis_b11_resampled:', modis_b11_resampled.size());
//combine the resampled collection with the modis 500m collection
// 1. Join datasets. First, define filter. This is based on the date.
var filter = ee.Filter.equals({
leftField: 'system:time_start',
rightField: 'system:time_start'
});
// 2. Create the join.
var innerJoin = ee.Join.inner('primary', 'secondary');
// 3. Apply the join.
var merge = innerJoin.apply(modis_b01_500m, modis_b11_resampled, filter)
// 4. Merge both collections.
var merged = merge.map(function(f){
var b01 = ee.Image(f.get('primary')).rename('b01')
var b11 = ee.Image(f.get('secondary')).rename('b11')
return b01.addBands(b11).copyProperties(b01)
})
print(merged, 'merged')
// 5. Add NormalizedDifference band
var normalizedBand = merged.map(function(image) {
var normalize = ee.Image(image).normalizedDifference(["b01", "b11"]).rename("normalizedBand")
return ee.Image(image).addBands(normalize).copyProperties(image)
})
print(normalizedBand, 'normalized band added')