I am trying to calculate the mean pixel values of three Sentinel-2 images, all of which I have cloud masked separately. They all have small areas of cloud but in different places. I assumed that when I calculated the mean, the cloud masked pixels having no-data values would simply be excluded from the analysis and in cloudy patches I would get a mean of the other two images instead. However this is not the case and my final image is masked wherever there was cloud in any of the three images.
My goal is therefore to combine all three of these images, masked for cloud, to produce a cloud-free image of average pixel values.
I considered that calculating the mean directly (instead of adding separately and dividing by 3) might solve this, however since my images are separate assets and not a filtered image collection I do not know how to do this.
function maskS2clouds(collection) {
var qa = collection.select('QA60');
var cloudBitMask = 1 <<10;
var cirrusBitMask = 1 <<11;
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return collection.updateMask(mask).divide(10000);
}
//image collection is filtered
var dataset = ee.ImageCollection("COPERNICUS/S2")
.filterBounds(geometry)
.filterDate('2017-06-19', '2017-06-20')
.map(maskS2clouds);
var dataset2 = ee.ImageCollection("COPERNICUS/S2")
.filterBounds(geometry)
.filterDate('2017-08-23', '2017-08-24')
.map(maskS2clouds);
var dataset3 = ee.ImageCollection("COPERNICUS/S2")
.filterBounds(geometry)
.filterDate('2017-07-19', '2017-07-20')
.map(maskS2clouds);
//image with mask applied
var image1 = dataset.map(function(image) { return image.clip(geometry); });
var image2 = dataset2.map(function(image) { return image.clip(geometry); });
var image3 = dataset3.map(function(image) { return image.clip(geometry); });
var june = image1.mosaic();
var aug = image2.mosaic();
var july = image3.mosaic();
var junaug = june.add(aug);
var augjuly = junaug.add(july);
var avg = augjuly.divide(3);