I am trying to create a cloudless mosaic using the following script as adapted from the custom script repo with minor modification
//VERSION=3
// adapted from here:
// https://github.com/sentinel-hub/custom-scripts/blob/master/sentinel-2/cloudless_mosaic/L2A-first_quartile_4bands.js
function setup() {
return {
input: [{
bands: [
"B04", // red
"B03", // green
"B02", // blue
"B08", // nir
"B11", // swir
"SCL" // pixel classification
],
units: "DN"
}],
output: {
bands: 5,
sampleType: SampleType.UINT16
},
mosaicking: "ORBIT"
};
}
function updateOutputMetadata(scenes,inputMetadata, outputMetadata) {
outputMetadata.userData = { "scenes": scenes.orbits }
}
function filterScenes(availableScenes, inputMetadata) {
return availableScenes.filter(function(scene) {
var m = scene.date.getMonth();
var y = scene.date.getFullYear();
var years = [2020]; #year
var months = [6, 7]; // 0 indexed for months
return months.includes(m) && years.includes(y);
});
}
function getValue(values) {
values.sort(function (a, b) {
return a - b;
});
return getMedian(values);
}
// function for pulling first quartile of values
function getFirstQuartile(sortedValues) {
var index = Math.floor(sortedValues.length / 4);
return sortedValues[index];
}
// function for pulling median (second quartile) of values
function getMedian(sortedValues) {
var index = Math.floor(sortedValues.length / 2);
return sortedValues[index];
}
function validate(samples) {
var scl = samples.SCL;
if (scl === 3) { // SC_CLOUD_SHADOW
return false;
} else if (scl === 9) { // SC_CLOUD_HIGH_PROBA
return false;
} else if (scl === 8) { // SC_CLOUD_MEDIUM_PROBA
return false;
} else if (scl === 7) { // SC_CLOUD_LOW_PROBA
// return false;
} else if (scl === 10) { // SC_THIN_CIRRUS
return false;
} else if (scl === 11) { // SC_SNOW_ICE
return false;
} else if (scl === 1) { // SC_SATURATED_DEFECTIVE
return false;
} else if (scl === 2) { // SC_DARK_FEATURE_SHADOW
// return false;
}
return true;
}
function evaluatePixel(samples, scenes) {
var clo_b02 = [];
var clo_b03 = [];
var clo_b04 = [];
var clo_b08 = [];
var clo_b11 = [];
var clo_b02_invalid = [];
var clo_b03_invalid = [];
var clo_b04_invalid = [];
var clo_b08_invalid = [];
var clo_b11_invalid = [];
var a = 0;
var a_invalid = 0;
for (var i = 0; i < samples.length; i++) {
var sample = samples[i];
if (sample.B02 > 0 && sample.B03 > 0 && sample.B04 > 0 && sample.B08 > 0 && sample.B11 > 0) {
var isValid = validate(sample);
if (isValid) {
clo_b02[a] = sample.B02;
clo_b03[a] = sample.B03;
clo_b04[a] = sample.B04;
clo_b08[a] = sample.B08;
clo_b11[a] = sample.B11;
a = a + 1;
} else {
clo_b02_invalid[a_invalid] = sample.B02;
clo_b03_invalid[a_invalid] = sample.B03;
clo_b04_invalid[a_invalid] = sample.B04;
clo_b08_invalid[a_invalid] = sample.B08;
clo_b11_invalid[a_invalid] = sample.B11;
a_invalid = a_invalid + 1;
}
}
}
var rValue;
var gValue;
var bValue;
var nValue;
var sValue;
if (a > 0) {
rValue = getValue(clo_b04);
gValue = getValue(clo_b03);
bValue = getValue(clo_b02);
nValue = getValue(clo_b08);
sValue = getValue(clo_b11);
} else if (a_invalid > 0) {
rValue = getValue(clo_b04_invalid);
gValue = getValue(clo_b03_invalid);
bValue = getValue(clo_b02_invalid);
nValue = getValue(clo_b08_invalid);
sValue = getValue(clo_b11_invalid);
} else {
rValue = 0;
gValue = 0;
bValue = 0;
nValue = 0;
sValue = 0;
}
return [rValue, gValue, bValue, nValue, sValue];
}
It works pretty well, however, I get an image for the range of two months and the image (RGB) that appears to be bright and enhanced, although I did not increase the brightness or use visualiser. What I wanted is to get raw values (median of the pixels ranging across the months of interest).
And I ran the simple script to get raw values as I use to:
//VERSION=3
function setup() {
return {
input: [{
bands: ["B02","B03","B04","B08"],
units: "DN"
}],
output: {
bands: 4,
sampleType: "INT16"
}
};
}
function evaluatePixel(sample) {
return [sample.B02,
sample.B03,
sample.B04,
sample.B08];
}
Note: The input data is same for both the scripts
"data": [
{
"dataFilter": {
"timeRange": {
"from": "2020-06-01T00:00:00Z",
"to": "2020-07-26T23:59:59Z"
},
"maxCloudCoverage": 50,
"mosaickingOrder": "leastRecent"
},
"type": "sentinel-2-l2a"
}
]
},
The RGB image I construct from the raw values is less bright than the former. I am not sure how I would explain this difference to myself. I was thinking maybe it was because of the mosaicking type.