I am trying to composite S2 images by different technique but the composites for the 25 and 33 percentiles (ee.Reducer.percentile([XX])) its giving an error :
p33: Layer error: Image.select: Pattern 'B2' did not match any bands.
p25: Layer error: Image.select: Pattern 'B2' did not match any bands.
This is the code I'm using:
//Se define la variable del CPC determinado y extent
var fires = table
//********************************************************************************************************//
//Extensión del raster en que se desea descargar los indices (metros a partir del CPC)
var extent_raster = 10000 ;
var b5km_fires = fires.geometry().buffer(extent_raster);
b5km_fires = b5km_fires.bounds();
//1.- Indicar Zona UTM a la que corresponde el CPC
var Zona_utm = '13'; // ---> INDICAR ZONA UTM EN DOS DIGITOS (p.e. 11,12,13,14,15,16)
//2.- Fechas de inicio y término en AAAA-MM-DD (AAAA = 4 digitos año, MM = 2 para el mes y DD = 2 para el dia)
var FECHA_INICIO = ee.Date(fires.aggregate_first('FECHA_INI'));// ---> INDICAR FECHA DE **INICIO** DEL INCENDIO
var FECHA_TERMINO = ee.Date(fires.aggregate_first('FECHA_TER')); // ---> INDICAR FECHA DE **TERMINO** DEL INCENDIO
//3.- Se define el periodo de análisis entre pre y post
var dw = 30; // ---> INDICAR NÚMERO DE DÍAS del COMPUESTO PRE Y POST (30,60,90)
//4.- Indicar el nombre de la carpeta donde se van a guardar los raster (se debe crear primero en su drive) (L204)
var carpeta = 'incendios';
//Fecha del periodo PRE a partir del atributo "FECHA_INI" en el CPC
var prefire_end = ee.Date(FECHA_INICIO);
var prefire_start = prefire_end.advance(-dw,'day');
//Fecha del periodo POST a partir del atributo "FECHA_TER" en el CPC
var postfire_start = ee.Date(FECHA_TERMINO);
var postfire_end = postfire_start.advance(dw,'day');
//Nombre del incendio
print(ee.String('Incendio: ').cat(ee.String(fires.aggregate_first("ID_CPC"))));
print(ee.String('Zona UTM seleccionada: ').cat(ee.String(Zona_utm)));
//Fechas en que ocurrio el incendio y días de los periodos pre y post
print(ee.String('Ocurrió entre: ').cat(prefire_end.format('YYYY-MM-dd', null)).cat(' y ').cat(postfire_start.format('YYYY-MM-dd', null)));
print(ee.String('Ventana de días (pre y post): ').cat(ee.Number(dw)).cat(' días'));
print(ee.String('Periodo pre: ').cat(prefire_start.format('YYYY-MM-dd', null)).cat(' a ').cat(prefire_end.format('YYYY-MM-dd', null)));
print(ee.String('Periodo post: ').cat(postfire_start.format('YYYY-MM-dd', null)).cat(' a ').cat(postfire_end.format('YYYY-MM-dd', null)));
//========================================================================================================
//Function to mask clouds using the Sentinel-2 QA band
function maskS2clouds(image) {
var qa = image.select('QA60');
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000)
.copyProperties(image, ['system:time_start']);
}
var s2_clear_sky = function(image){
// 1.Locate SCL product
var B1Band = image.select('B1');
var scl = image.select('SCL');
// 2.Apply mask to the image
// Select values:
var wantedPixels = scl.eq(4) //Vegetation
.or(scl.eq(5)) //Not vegetated
.or(scl.eq(7)) //Unclassified
.or(B1Band.lt(2000))
;
return image.updateMask(wantedPixels)
.copyProperties(image, ['system:time_start']);
};
// This is the Sentinel 2 collection (all the possible available Sentinel-2 imagery)
var S2SR = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterBounds(b5km_fires)
.filterDate(prefire_start, postfire_end)
.map(maskS2clouds);
var S2_collection = S2SR.map(s2_clear_sky);
// Bands that we want to be displayed
var S2_bands = ['B2','B3','B4','B5','B6','B7','B8','B8A','B11','B12'];
// This turns the whole S2 collection into one image, finding the middle value for each pixel
var S2pr_min = S2_collection.filterDate(prefire_start, prefire_end).sort('GENERATION_TIME', false)
.min().select(S2_bands).clip(b5km_fires);
var S2pr_ave = S2_collection.filterDate(prefire_start, prefire_end).sort('GENERATION_TIME', false)
.mean().select(S2_bands).clip(b5km_fires);
var S2pr_p33 = S2_collection.filterDate(prefire_start, prefire_end).sort('GENERATION_TIME', false).reduce(ee.Reducer
.percentile([33])).select(S2_bands).clip(b5km_fires);
var S2pr_p25 = S2_collection.filterDate(prefire_start, prefire_end).sort('GENERATION_TIME', false).reduce(ee.Reducer
.percentile([25])).select(S2_bands).clip(b5km_fires);
// This controls how we want the S2 image to be displayed
var visualization = {
min: 0.0,
max: 0.3,
bands: ['B8', 'B4', 'B3'],
};
Map.addLayer(S2pr_min, visualization,'min');
Map.addLayer(S2pr_ave, visualization,'ave', false);
Map.addLayer(S2pr_p33, visualization,'p33', false);
Map.addLayer(S2pr_p25, visualization,'p25', false);
//Establecer parametros de simbología para el CPC
var cpcVis = fires.style({
color: '0000FF',
width: 5,
fillColor: '00000000', // relleno transparente
lineType: 'solid'
});
// This automatically pans the map to the middle of our area of interest
Map.centerObject(b5km_fires);
Map.addLayer(cpcVis, null, 'CPC'); //Agregar a "layers" en el visualizador el CPC (var "fires")