Skip to content

Commit 33193d4

Browse files
committed
add existing examples
0 parents  commit 33193d4

4 files changed

+397
-0
lines changed

Diff for: multitemporal-datafusion-2dates.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//VERSION=3
2+
3+
// This is an example of how to get the data for 2 different dates from one input dataset.
4+
// This example contains:
5+
// - a function which checks if the provided dates are the same day: sameDay()
6+
// - a function which retrieves the index of the sample (from samples variable) for the requested date (from inputData variable): getDataIndexForDate()
7+
8+
9+
function setup (){
10+
return {
11+
input: [
12+
{datasource: "s1grd", bands:["VV", "VH"], mosaicking: "ORBIT"},
13+
{datasource: "s2l2a", bands:["B03", "B04", "B08", "B11", "B12"], mosaicking: "ORBIT"}
14+
],
15+
output: [
16+
{id: "default", bands: 3, sampleType: SampleType.AUTO}
17+
]
18+
};
19+
}
20+
21+
function sameDay(d1, d2) {
22+
return d1.getFullYear() === d2.getFullYear() &&
23+
d1.getMonth() === d2.getMonth() &&
24+
d1.getDate() === d2.getDate();
25+
}
26+
27+
function getDataIndexForDate(dataset, dateString){
28+
29+
for(const scene of dataset.scenes){
30+
// throw new Error(scene.date + " | " + new Date(dateString))
31+
if(sameDay(scene.date, new Date(dateString))){
32+
return scene.idx;
33+
}
34+
}
35+
36+
// You can throw an error or just return null (depending on what you want to do with that further on).
37+
throw new Error(JSON.stringify(dataset))
38+
// return null;
39+
}
40+
41+
function evaluatePixel(samples, inputData, inputMetadata, customData, outputMetadata) {
42+
43+
// get indices for the correct samples in samples variable
44+
// for the dates from inputData variable
45+
var indexS1_T1 = getDataIndexForDate(inputData.s1grd, '2020-07-07');
46+
var indexS1_T2 = getDataIndexForDate(inputData.s1grd, '2020-07-07');
47+
var indexS2L2A = getDataIndexForDate(inputData.s2l2a, '2020-07-07');
48+
49+
if(!indexS1_T1 || !indexS1_T2 || !indexS2L2A){
50+
throw new Error("some dataset has no data on the selected date");
51+
}
52+
53+
var s1_1 = samples.s1grd[indexS1_T1]; // Assigns S1 data from t1
54+
var s1_2 = samples.s1grd[indexS1_T2]; // Assigns S1 data from t2
55+
var s2_1 = samples.s2l2a[indexS2L2A]; // Assigns S2 data from t1
56+
57+
// ... your code ...
58+
}

Diff for: multitemporal-datafusion-example.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//VERSION=3
2+
// ADVANCED multitemporal data fusion example
3+
4+
// NOTE: values and dates in this example are NOT CORRECT - they ARE MADE UP
5+
6+
var setup = () => ({
7+
input: [
8+
{datasource: "s2l1c", bands:["B02", "B03", "B04", "B08", "B11"], units: "REFLECTANCE", mosaicking: "ORBIT"},
9+
{datasource: "s1grd", bands:["VV", "VH"], units: "REFLECTANCE", mosaicking: "ORBIT"},
10+
{datasource: "s2l2a", bands:["B02", "B03", "B04"], units: "REFLECTANCE", mosaicking: "ORBIT"}
11+
],
12+
output: [
13+
{ id: "default", bands: 3, sampleType: SampleType.AUTO }
14+
],
15+
})
16+
17+
18+
function evaluatePixel(samples, inputData, inputMetadata, customData, outputMetadata) {
19+
/*
20+
For each processed pixel (each time the evaluatePixel is run), the parameters have the following structure
21+
22+
Structure of samples:
23+
samples:{
24+
"s2l2a":[
25+
{"B02": 123, "B03": 123, "B04": 123, ...}, // has index 0 (from inputData: date = 2020-01-01)
26+
{"B02": 123, "B03": 123, "B04": 123, ...}, // has index 1 (from inputData: date = 2020-01-03)
27+
// one for each date
28+
],
29+
"s2l1c":[
30+
{"B02": 123, "B03": 123, "B04": 123, ...}, // has index 0 (from inputData: date = 2020-01-01)
31+
{"B02": 123, "B03": 123, "B04": 123, ...}, // has index 1 (from inputData: date = 2020-01-03)
32+
// one for each date
33+
],
34+
"s1grd":[
35+
{"VV":123,"VH":123}, // has index 0 (from inputData: date = 2020-01-02)
36+
{"VV":123,"VH":123}, // has index 1 (from inputData: date = 2020-01-05)
37+
// one for each date
38+
]
39+
},
40+
41+
// CAUTION: THE DATES IN THE SCENES CAN DIFFER BETWEEN DATASETS
42+
// idx-es in the scenes of the dataset correspond to the indexes of the samples of that dataset (but not the other datasets)
43+
44+
Structure of inputData:
45+
inputData:{
46+
"s2l2a":{
47+
"scenes":[
48+
{"date": "2020-01-01", "idx": 0, ...}
49+
{"date": "2020-01-03", "idx": 1, ...}
50+
// for each date one
51+
],
52+
"normalizationFactor": 0.0001
53+
},
54+
"s2l1c":{
55+
"scenes":[
56+
{"date": "2020-01-01", "idx": 0, ...}
57+
{"date": "2020-01-03", "idx": 1, ...}
58+
// for each date one
59+
],
60+
"normalizationFactor": 0.0001
61+
},
62+
"s1grd":{
63+
"scenes":[
64+
{"date": "2020-01-02", "idx": 0, ...}
65+
{"date": "2020-01-05", "idx": 1, ...}
66+
// for each date one
67+
],
68+
"normalizationFactor": 0.0001
69+
},
70+
}
71+
72+
Structure of inputMetadata, customData, outputMetadata: NOT IMPORTANT; NOT USED IN THIS EXAMPLE
73+
74+
*/
75+
76+
// IF YOU NEED THE DATA (SAMPLES) FROM ALL DATASETS, HERE IS HOW TO ONLY USE THE DATA ON THE SAME DATES
77+
// Not all dates from one dataset are in the other, so we need to filter the scenes and samples
78+
// plan: go through all the scenes of one dataset (since scenes have the dates and the indexes of the elements in the samples)
79+
// and find the scenes from other datasets on the same dates as for the selected dataset
80+
// we also need to fix the arrays of samples
81+
82+
// something like
83+
84+
var S2L1CSamples = samples.s2l1c; // has value [{"B02": 123, "B03": 123, "B04": 123, ...}, {one for each date}],
85+
var S2L2ASamples = samples.s2l2a; // has value [{"B02": 123, "B03": 123, "B04": 123, ...}, {one for each date}],
86+
var S1Samples = samples.s1grd; // has value [{"VV": 123, "VH": 123, ...}, {one for each date}],
87+
88+
var S2L1CScenes = scenes.s2l1c.scenes; // has value [{"date": "2020-01-01", "idx": 0, ...}, {one for each date}]
89+
var S2L2AScenes = scenes.s2l2a.scenes; // has value [{"date": "2020-01-01", "idx": 0, ...}, {one for each date}]
90+
var S1Scenes = scenes.s1.scenes; // has value [{"date": "2020-01-02", "idx": 0, ...}, {one for each date}]
91+
92+
var usableDates = []; // we can use one variable that holds the dates that are common to all the datasets
93+
var usableS2L1CSamples = []; // holds S2 L1C samples on the common dates
94+
var usableS2L2ASamples = []; // holds S2 L2A samples on the common dates
95+
var usableS1Samples = []; // holds S1 samples on the common dates
96+
97+
for(S2L1Cscene of S2L1CScenes){
98+
const date = S2L1Cscene.date;
99+
const S2L2Ascene = S2L2AScenes.find(scene => scene.date === date);
100+
const S1scene = S1Scenes.find(scene => scene.date === date);
101+
102+
if(S2L2Ascene !== undefined && S1scene != undefined){
103+
usableDates.push(date);
104+
usableS2L1CSamples.push(S2L1CSamples[S2L1Cscene.idx]);
105+
usableS2L2ASamples.push(S2L2ASamples[S2L2Ascene.idx]);
106+
usableS1Samples.push(S1Samples[S1scene.idx]);
107+
}
108+
}
109+
110+
// work with usableDates, usableS2L1CSamples, usableS2L2ASamples, usableS1Samples from here on
111+
112+
// ... your code ...
113+
114+
115+
}

Diff for: multitemporal-datafusion-example.txt

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//VERSION=3
2+
// ADVANCED multitemporal data fusion example
3+
4+
// NOTE: values and dates in this example are NOT CORRECT - they ARE MADE UP
5+
6+
var setup = () => ({
7+
input: [
8+
{datasource: "s2l1c", bands:["B02", "B03", "B04", "B08", "B11"], units: "REFLECTANCE", mosaicking: "ORBIT"},
9+
{datasource: "s1grd", bands:["VV", "VH"], units: "REFLECTANCE", mosaicking: "ORBIT"},
10+
{datasource: "s2l2a", bands:["B02", "B03", "B04"], units: "REFLECTANCE", mosaicking: "ORBIT"}
11+
],
12+
output: [
13+
{ id: "default", bands: 3, sampleType: SampleType.AUTO }
14+
],
15+
})
16+
17+
18+
function evaluatePixel(samples, inputData, inputMetadata, customData, outputMetadata) {
19+
/*
20+
For each processed pixel (each time the evaluatePixel is run), the parameters have the following structure
21+
22+
Structure of samples:
23+
samples:{
24+
"s2l2a":[
25+
{"B02": 123, "B03": 123, "B04": 123, ...}, // has index 0 (from inputData: date = 2020-01-01)
26+
{"B02": 123, "B03": 123, "B04": 123, ...}, // has index 1 (from inputData: date = 2020-01-03)
27+
// one for each date
28+
],
29+
"s2l1c":[
30+
{"B02": 123, "B03": 123, "B04": 123, ...}, // has index 0 (from inputData: date = 2020-01-01)
31+
{"B02": 123, "B03": 123, "B04": 123, ...}, // has index 1 (from inputData: date = 2020-01-03)
32+
// one for each date
33+
],
34+
"s1grd":[
35+
{"VV":123,"VH":123}, // has index 0 (from inputData: date = 2020-01-02)
36+
{"VV":123,"VH":123}, // has index 1 (from inputData: date = 2020-01-05)
37+
// one for each date
38+
]
39+
},
40+
41+
// CAUTION: THE DATES IN THE SCENES CAN DIFFER BETWEEN DATASETS
42+
// idx-es in the scenes of the dataset correspond to the indexes of the samples of that dataset (but not the other datasets)
43+
44+
Structure of inputData:
45+
inputData:{
46+
"s2l2a":{
47+
"scenes":[
48+
{"date": "2020-01-01", "idx": 0, ...}
49+
{"date": "2020-01-03", "idx": 1, ...}
50+
// for each date one
51+
],
52+
"normalizationFactor": 0.0001
53+
},
54+
"s2l1c":{
55+
"scenes":[
56+
{"date": "2020-01-01", "idx": 0, ...}
57+
{"date": "2020-01-03", "idx": 1, ...}
58+
// for each date one
59+
],
60+
"normalizationFactor": 0.0001
61+
},
62+
"s1grd":{
63+
"scenes":[
64+
{"date": "2020-01-02", "idx": 0, ...}
65+
{"date": "2020-01-05", "idx": 1, ...}
66+
// for each date one
67+
],
68+
"normalizationFactor": 0.0001
69+
},
70+
}
71+
72+
Structure of inputMetadata, customData, outputMetadata: NOT IMPORTANT; NOT USED IN THIS EXAMPLE
73+
74+
*/
75+
76+
// IF YOU NEED THE DATA (SAMPLES) FROM ALL DATASETS, HERE IS HOW TO ONLY USE THE DATA ON THE SAME DATES
77+
// Not all dates from one dataset are in the other, so we need to filter the scenes and samples
78+
// plan: go through all the scenes of one dataset (since scenes have the dates and the indexes of the elements in the samples)
79+
// and find the scenes from other datasets on the same dates as for the selected dataset
80+
// we also need to fix the arrays of samples
81+
82+
// something like
83+
84+
var S2L1CSamples = samples.s2l1c; // has value [{"B02": 123, "B03": 123, "B04": 123, ...}, {one for each date}],
85+
var S2L2ASamples = samples.s2l2a; // has value [{"B02": 123, "B03": 123, "B04": 123, ...}, {one for each date}],
86+
var S1Samples = samples.s1grd; // has value [{"VV": 123, "VH": 123, ...}, {one for each date}],
87+
88+
var S2L1CScenes = scenes.s2l1c.scenes; // has value [{"date": "2020-01-01", "idx": 0, ...}, {one for each date}]
89+
var S2L2AScenes = scenes.s2l2a.scenes; // has value [{"date": "2020-01-01", "idx": 0, ...}, {one for each date}]
90+
var S1Scenes = scenes.s1.scenes; // has value [{"date": "2020-01-02", "idx": 0, ...}, {one for each date}]
91+
92+
var usableDates = []; // we can use one variable that holds the dates that are common to all the datasets
93+
var usableS2L1CSamples = []; // holds S2 L1C samples on the common dates
94+
var usableS2L2ASamples = []; // holds S2 L2A samples on the common dates
95+
var usableS1Samples = []; // holds S1 samples on the common dates
96+
97+
for(S2L1Cscene of S2L1CScenes){
98+
const date = S2L1Cscene.date;
99+
const S2L2Ascene = S2L2AScenes.find(scene => scene.date === date);
100+
const S1scene = S1Scenes.find(scene => scene.date === date);
101+
102+
if(S2L2Ascene !== undefined && S1scene != undefined){
103+
usableDates.push(date);
104+
usableS2L1CSamples.push(S2L1CSamples[S2L1Cscene.idx]);
105+
usableS2L2ASamples.push(S2L2ASamples[S2L2Ascene.idx]);
106+
usableS1Samples.push(S1Samples[S1scene.idx]);
107+
}
108+
}
109+
110+
// work with usableDates, usableS2L1CSamples, usableS2L2ASamples, usableS1Samples from here on
111+
112+
// ... your code ...
113+
114+
115+
}

0 commit comments

Comments
 (0)