forked from sentinel-hub/custom-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
80 lines (67 loc) · 2.02 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//VERSION=3
function setup() {
return {
input: ["B04", "B08"],
output: { bands: 1},
mosaicking: "ORBIT"
};
}
const msInDay = 24 * 60 * 60 * 1000;
const msInYear = 365.25 * msInDay;
const toleranceDays = 10;
const toleranceMs = toleranceDays * msInDay;
var metadata = undefined;
function filterScenes(scenes, inputMetadata) {
scenes = scenes.sort((s1, s2) => s2.date - s1.date);
const observed = scenes[0].date;
var newScenes = [scenes[0]];
for (var historical = observed - msInYear; historical >= inputMetadata.from - toleranceMs; historical -= msInYear) {
newScenes.push(findClosest(scenes, historical));
}
newScenes = newScenes.filter(scene => scene != null);
metadata = {
observed: observed.toISOString(),
historical: newScenes.slice(1).map(scene => scene.date.toISOString())
}
return newScenes;
}
function findClosest(scenes, date) {
var closestDt = toleranceMs + 1, closestScene = null;
for (var i = 0; i < scenes.length; i++) {
const dt = Math.abs(scenes[i].date - date);
if (dt < closestDt) {
closestDt = dt;
closestScene = scenes[i];
}
}
return closestScene;
}
function percentileOfScore (data, value) {
// Calculate the percentile rank of a value relative to a list of values.
if (!data.length) {return [0];}
data.sort();
let lowerCount = 0;
let sameCount = 0;
for (let i = 0; i < data.length; i++) {
if (data[i] < value) {
lowerCount++;
} else if (data[i] === value) {
sameCount++;
} else {
break;
}
}
return (lowerCount + 0.5 * sameCount) / data.length * 100;
}
function updateOutputMetadata(scenes, inputMetadata, outputMetadata) {
outputMetadata.userData = metadata;
}
function evaluatePixel(samples, scenes) {
const observed = index(samples[0].B08, samples[0].B04);
var hist_ndvi = [];
for (var i = 1; i < samples.length; i++) {
const ndvi = index(samples[i].B08, samples[i].B04);
hist_ndvi.push(ndvi);
}
return [percentileOfScore(hist_ndvi, observed) / 100];
}