I want to map a function to count how many pixels in my study area have been detected as a hotspot at least once, by month, for a given time period. I want the output to be a list of hotspot counts for each month. I'm using MODIS Aqua and Terra data, and I've merged them together. When I run the function, I get the error: "Image.gte: If one image has no bands, the other must also have no bands. Got 0 and 1." I filtered the merged data to make sure there were no images with empty bands, but I still get this error. Does this have to do with the fact that I merged Aqua and Terra data? How can I fix it?
Code is here: https://code.earthengine.google.com/c4db2521035b3f46af7e810b3c1889e8 The relevant section is reproduced below:
var terra = ee.ImageCollection("MODIS/061/MOD14A1").select('FireMask','MaxFRP')
.filterDate(start, end);
var aqua = ee.ImageCollection("MODIS/061/MYD14A1").select('FireMask','MaxFRP')
.filterDate(start, end);
// Merge Terra and Aqua hotspots to include more overpass times
var hotspots_all = ee.ImageCollection(terra.merge(aqua)).sort("system:time_start");
// Clip imagery to area
var hotspots = hotspots_all.map(function(image) { return image.clip(geometry).reproject('EPSG:4326', null, 1000); });
// Find the images with missing bands, and get rid of them (these are data outages)
var filteredHotspots = hotspots.filter(ee.Filter.listContains('band_names', 'MaxFRP'));
// Display Hotspots on the map.
var fire_viz = {min: 1, max: 10, palette: ['lightsalmon','red','darkred']}; // show range for number of times the same pixel has been detected
Map.addLayer(hotspots_masked.select('MaxFRP').count().clip(geometry), fire_viz, 'Hotspots Count (masked)'); // for plain color: use {palette: 'orange'}
print(hotspots_masked, 'MODIS Hotspots (Terra + Aqua, masked)');
///////////////////////////////////////////////////////////////////////
// Get Monthly hotspot counts (burn days): map over years and months
var years = ee.List.sequence(start_year, end_year);
var months = ee.List.sequence(start_month, end_month);
var hotfrp_mask = filteredHotspots.select('MaxFRP'); // or use hotspots_masked
var monthlyCount = years.map(function(y) {
var yearCollection = hotfrp_mask.filter(ee.Filter.calendarRange(y, y, 'year')); //using the masked QC'ed imagery
var byYear = months.map(function (m){
var totalCount = yearCollection.filter(ee.Filter.calendarRange(m, m, 'month')) // get all images in a month time period (there are multiple observations every day)
// Count the number of non-masked in the stack at each pixel
.count(); // Output: One image where each pixel is the number of unmasked pixels (hotspots) in the input stack.
// Sum up all the hotspot pixel counts for each month for the geometry
var getBurnCount = totalCount.updateMask(totalCount.gte(1)) // Update mask: keep all counts at or above 1
.reduceRegion({
reducer: ee.Reducer.sum().unweighted(), // Get region-wide sum of the pixel counts, unweighted.
geometry: geometry,
scale: 1000, // This is the scale of the hotspot data product
maxPixels: 1e13
});
// reduceRegion() returns a dictionary with the sum of unmasked pixels for each month for the geometry
//var countValue = totalCount.get('count');
var countValue = ee.Dictionary(getBurnCount).get('MaxFRP'); // get value associated with dict key - is this what the band will be called?
var f = ee.Feature(null, { //originally ee.Feature(null, {})
'year': y,
'month': m,
'hotspot_count': countValue
});
return f;
});
return byYear.flatten();
});
// The result is the total number of times a hotspot was detected ONE OR MORE times for each month.
// This solves the problem of double-counting hotspots detected by both Terra and Aqua. However,
// it reduces the total hotspot count for each month.
// Print to see output
print("monthlyCount", monthlyCount);
print("monthly Count flat", monthlyCount.flatten());
var monthlyList = monthlyCount.flatten(); // convert to list of all months (from list of lists)