I am running into a problem I am trying to calculate MCARI and NDVI from the Sentinel 2 Level 2A dataset and it doesn't have any feature for the data range from 2017 to Dec 2018. Can anyone advise me how to resolve this?
My code is below:
// Define the ROI coordinates
var lat = 36.22095949;
var long = -115.534627;
var bound = ee.Geometry.Rectangle({
coords: [
long - 0.00045,
lat - 0.00045,
long + 0.00045,
lat + 0.00045
],
proj: 'EPSG:4326',
geodesic: false
});
// Add the ROI to the map
Map.addLayer(bound, {}, 'ROI');
// Define the start and end date for the time range of interest
var startDate = '2017-04-28';
var endDate = '2018-05-31';
// Load the Sentinel-2 surface reflectance collection and filter by the AOI and date range
var collection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")
.filterBounds(bound)
.filterDate(startDate, endDate);
// Filter the collection by cloud cover using 'CLOUDY_PIXEL_PERCENTAGE'
var filtered = collection.filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE', 5));
// Clip each image in the collection to the specified bounds
var clippedCollection = filtered.map(function(image) {
return image.clip(bound);
});
var mcariCollection = clippedCollection.map(function(image) {
var mcari = image.expression(
'1.5 * (2.5 * (B5 - B4) - 1.3 * (B5 - B3)) / sqrt((2 * B5 + 1)**2 - (6 * B5 - 5 * sqrt(B4)) - 0.5)',
{
'B5': image.select('B8'), // NIR band
'B4': image.select('B4'), // Red band
'B3': image.select('B3') // Green band
}
).rename('MCARI');
return image.addBands(mcari);
});
// Get the first image from the NDVI collection
var first = mcariCollection.first();
// Define visualization parameters for true color (optional)
var imageVisParam = {
bands: ['B4', 'B3', 'B2'],
min: 0,
max: 3000,
gamma: 1.4
};
// Add the first true color image to the map
Map.addLayer(first, imageVisParam, 'First Image');
// Define visualization parameters for NDVI
var ndviVisParam = {
min: -1,
max: 1,
palette: ["#3fff00", "#ff0000", "#ffff00", "#00c800", "#006400"]
};
// Add the NDVI layer of the first image to the map
Map.addLayer(first.select('MCARI'), ndviVisParam, 'MCARI');
// Calculate the mean NDVI for each image in the collection
var meanMCARICollection = mcariCollection.map(function(image) {
var meanMCARI = image.select('MCARI').reduceRegion({
reducer: ee.Reducer.mean(),
geometry: bound,
scale: 10,
maxPixels: 1e9
}).get('MCARI');
return image.set('mean_mcari', meanMCARI);
});
// Create a chart with date on the x-axis and mean NDVI on the y-axis
var chart = ui.Chart.feature.byFeature(meanMCARICollection, 'system:time_start', 'mean_mcari')
.setOptions({
title: 'Mean MCARI Over Time',
hAxis: {title: 'Date'},
vAxis: {title: 'Mean MCARI'},
lineWidth: 2,
pointSize: 4
});
// Print the chart to the console
print(chart);
// Display the map
Map.centerObject(bound, 15);