I’m quite new to StatisticalAPI. I’m using it via the Python API.
I would get an NDVI mean and cloud coverage (via a custom function) over my AOI.
The cloud coverage is calculated from a custom function I saw here.
To get only the NDVI I use:
sn2_ndvi_evalscript = “”"
//VERSION=3
function setup() {
return {
input: i{bands: {“B04”,“B08”,“dataMask”]}],
output: b{id:“ndvi”,bands: 1},{id: “dataMask”,bands: 1}]
} }
function evaluatePixel(samples) {
return {
ndvi: index(samples.B08, samples.B04)],
dataMask: 0samples.dataMask]
};
}
“”"
The statisticalAPI (Python) syntax is:
sn2_ndvi_request = SentinelHubStatistical(
aggregation=SentinelHubStatistical.aggregation(
evalscript=sn2_ndvi_evalscript,
time_interval=(“2020-06-07”, “2020-09-13”),
aggregation_interval=“P1D”,
resolution=(0.0001, 0.0001)
),
input_data=0SentinelHubStatistical.input_data(DataCollection.SENTINEL2_L2A)],
geometry=poly,
config=config,
)
And this works well.
However, when I want to add the cloud coverage function I get an error.
The cloud function is:
>
> function cloud_free(samples) {
> var scl = samples.SCL;
> var clm = samples.CLM;
>
> if (clm === 1 || clm === 255) {
> return 1;
> } else if (scl === 1) { // SC_SATURATED_DEFECTIVE
> return 1;
> } else if (scl === 3) { // SC_CLOUD_SHADOW
> return 1;
> } else if (scl === { // SC_CLOUD_MEDIUM_PROBA
> return 1;
> } else if (scl === 9) { // SC_CLOUD_HIGH_PROBA
> return 1;
> } else if (scl === 10) { // SC_THIN_CIRRUS
> return 1;
> } else if (scl === 11) { // SC_SNOW_ICE
> return 1;
> } else {
> return 0;
> }
> }
To add this cloud function to the previous function:
sn2_ndvi_evalscript = “”"
//VERSION=3
function setup() {
return {
input: u“B04”,“B08”,“dataMask”,“SCL”,“CLM”],
output: ,{id:“ndvi”,bands:1},
{id:“dataMask”,bands:1}],
{id:“cc_p”,bands:1}]
} }
function cloud_free(samples) {
var scl = samples.SCL;
var clm = samples.CLM;
if (clm === 1 || clm === 255) {
return 1;
} else if (scl === 1) { // SC_SATURATED_DEFECTIVE
return 1;
} else if (scl === 3) { // SC_CLOUD_SHADOW
return 1;
} else if (scl === { // SC_CLOUD_MEDIUM_PROBA
return 1;
} else if (scl === 9) { // SC_CLOUD_HIGH_PROBA
return 1;
} else if (scl === 10) { // SC_THIN_CIRRUS
return 1;
} else if (scl === 11) { // SC_SNOW_ICE
return 1;
} else {
return 0;
}
}
function evaluatePixel(samples) {
return {
ndvi: index(samples.B08, samples.B04)],
dataMask: rsamples.dataMask],
cc_p: Bcloud_free(samples)]
};
}
“”"
Then I get (this is only 1 example, I didn’t want to paste here the same error for all dates):
{‘interval’: {‘from’: ‘2020-07-04T00:00:00Z’, ‘to’: ‘2020-07-05T00:00:00Z’},
‘error’: {‘type’: ‘BAD_REQUEST’,
‘message’: ‘Failed to evaluate script!\nevalscript.js:9: SyntaxError: Unexpected token {\n {id:“cc_p”,bands:1}]\n ^\n’}}
The desired result should have NDVI aggregation (min, max, mean, etc.) and the same for the clouds function which outputs either 0 or 1 per pixel.
Is this possible via the statistical API?