I am trying to get the PCA using following codes for sentinel 2A in GEE, but I get the message-computation time out. Is there a way to solve the issue?
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);
}
var dataset = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterDate('2022-1-01', '2023-01-30')
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',5))
.map(maskS2clouds);
var s2a=dataset.median().clip(nepal);
var bands=['B2','B3','B4','B5','B8','B11'];
var arrayImage = s2a.select(bands).toArray();
var covar = arrayImage.reduceRegion({
reducer: ee.Reducer.covariance(),
geometry:nepal,
maxPixels: 1e10,
scale:10,
bestEffort: true
});
var covarArray = ee.Array(covar.get('array'));
var eigens = covarArray.eigen();
var eigenVectors = eigens.slice(1, 1);
var principalComponents = ee.Image(eigenVectors)
.matrixMultiply(arrayImage.toArray(1));
var pcImage = principalComponents
.arrayProject([0])
.arrayFlatten([['pc1', 'pc2', 'pc3', 'pc4', 'pc5', 'pc6']]);
var pc1=pcImage.select('pc1');
print(pc1);