-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_graph.json
More file actions
1078 lines (1078 loc) · 71.6 KB
/
process_graph.json
File metadata and controls
1078 lines (1078 loc) · 71.6 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"process_graph": {
"loadcollection1": {
"process_id": "load_collection",
"arguments": {
"bands": {
"from_parameter": "s2_data_bands"
},
"id": "SENTINEL2_L2A",
"properties": {
"eo:cloud_cover": {
"process_graph": {
"lte1": {
"process_id": "lte",
"arguments": {
"x": {
"from_parameter": "value"
},
"y": 95
},
"result": true
}
}
}
},
"spatial_extent": {
"from_parameter": "spatial_extent"
},
"temporal_extent": {
"from_parameter": "temporal_extent"
}
}
},
"apply1": {
"process_id": "apply",
"arguments": {
"data": {
"from_node": "loadcollection1"
},
"process": {
"process_graph": {
"add1": {
"process_id": "add",
"arguments": {
"x": {
"from_parameter": "x"
},
"y": 0
}
},
"multiply1": {
"process_id": "multiply",
"arguments": {
"x": {
"from_node": "add1"
},
"y": 0.0001
},
"result": true
}
}
}
}
},
"loadcollection2": {
"process_id": "load_collection",
"arguments": {
"bands": [
"SCL"
],
"id": "SENTINEL2_L2A",
"properties": {
"eo:cloud_cover": {
"process_graph": {
"lte2": {
"process_id": "lte",
"arguments": {
"x": {
"from_parameter": "value"
},
"y": 95
},
"result": true
}
}
}
},
"spatial_extent": {
"from_parameter": "spatial_extent"
},
"temporal_extent": {
"from_parameter": "temporal_extent"
}
}
},
"reducedimension1": {
"process_id": "reduce_dimension",
"arguments": {
"data": {
"from_node": "loadcollection2"
},
"dimension": "bands",
"reducer": {
"process_graph": {
"arrayelement1": {
"process_id": "array_element",
"arguments": {
"data": {
"from_parameter": "data"
},
"index": 0
}
},
"eq1": {
"process_id": "eq",
"arguments": {
"x": {
"from_node": "arrayelement1"
},
"y": 0
}
},
"eq2": {
"process_id": "eq",
"arguments": {
"x": {
"from_node": "arrayelement1"
},
"y": 3
}
},
"or1": {
"process_id": "or",
"arguments": {
"x": {
"from_node": "eq1"
},
"y": {
"from_node": "eq2"
}
}
},
"gt1": {
"process_id": "gt",
"arguments": {
"x": {
"from_node": "arrayelement1"
},
"y": 7
}
},
"or2": {
"process_id": "or",
"arguments": {
"x": {
"from_node": "or1"
},
"y": {
"from_node": "gt1"
}
}
},
"multiply2": {
"process_id": "multiply",
"arguments": {
"x": {
"from_node": "or2"
},
"y": 1.0
},
"result": true
}
}
}
}
},
"mask1": {
"process_id": "mask",
"arguments": {
"data": {
"from_node": "apply1"
},
"mask": {
"from_node": "reducedimension1"
}
}
},
"resamplespatial1": {
"process_id": "resample_spatial",
"arguments": {
"align": "upper-left",
"data": {
"from_node": "reducedimension1"
},
"method": "average",
"projection": null,
"resolution": 300
}
},
"apply2": {
"process_id": "apply",
"arguments": {
"data": {
"from_node": "resamplespatial1"
},
"process": {
"process_graph": {
"gte1": {
"process_id": "gte",
"arguments": {
"x": {
"from_parameter": "x"
},
"y": 0.05
},
"result": true
}
}
}
}
},
"applyneighborhood1": {
"process_id": "apply_neighborhood",
"arguments": {
"data": {
"from_node": "apply2"
},
"overlap": [
{
"dimension": "x",
"value": 32,
"unit": "px"
},
{
"dimension": "y",
"value": 32,
"unit": "px"
}
],
"process": {
"process_graph": {
"runudf1": {
"process_id": "run_udf",
"arguments": {
"data": {
"from_parameter": "data"
},
"runtime": "Python",
"udf": "from scipy.ndimage import distance_transform_edt\nimport numpy as np\nimport xarray as xr\nfrom openeo.udf import XarrayDataCube\n\n\ndef apply_datacube(cube: XarrayDataCube, context: dict) -> XarrayDataCube:\n \"\"\"\n Expects the cloud mask as input (in contrast to ``distance_transform_edt``).\n This is necessary, because ``apply_neighborhood`` pads the input with zeros and not ones.\n \"\"\"\n array = cube.get_array()\n # This special case appears to create some issues, so we skip it\n if not array.any():\n return XarrayDataCube(\n xr.DataArray(np.full_like(array, np.inf), dims=[\"t\", \"y\", \"x\"])\n )\n distance = distance_transform_edt(np.logical_not(array))\n return XarrayDataCube(xr.DataArray(distance, dims=[\"t\", \"y\", \"x\"]))\n"
},
"result": true
}
}
},
"size": [
{
"dimension": "t",
"value": "P1D"
},
{
"dimension": "x",
"value": 512,
"unit": "px"
},
{
"dimension": "y",
"value": 512,
"unit": "px"
}
]
}
},
"apply3": {
"process_id": "apply",
"arguments": {
"data": {
"from_node": "applyneighborhood1"
},
"process": {
"process_graph": {
"subtract1": {
"process_id": "subtract",
"arguments": {
"x": {
"from_parameter": "x"
},
"y": 1
}
},
"divide1": {
"process_id": "divide",
"arguments": {
"x": {
"from_node": "subtract1"
},
"y": 16.666666666666668
},
"result": true
}
}
}
}
},
"apply4": {
"process_id": "apply",
"arguments": {
"data": {
"from_node": "apply3"
},
"process": {
"process_graph": {
"clip1": {
"process_id": "clip",
"arguments": {
"max": 1,
"min": 0,
"x": {
"from_parameter": "x"
}
},
"result": true
}
}
}
}
},
"adddimension1": {
"process_id": "add_dimension",
"arguments": {
"data": {
"from_node": "apply4"
},
"label": "distance_score",
"name": "bands",
"type": "bands"
}
},
"resamplecubespatial1": {
"process_id": "resample_cube_spatial",
"arguments": {
"data": {
"from_node": "adddimension1"
},
"method": "bilinear",
"target": {
"from_node": "mask1"
}
}
},
"mergecubes1": {
"process_id": "merge_cubes",
"arguments": {
"cube1": {
"from_node": "mask1"
},
"cube2": {
"from_node": "resamplecubespatial1"
}
}
},
"dimensionlabels1": {
"process_id": "dimension_labels",
"arguments": {
"data": {
"from_node": "mask1"
},
"dimension": "t"
}
},
"loadcollection3": {
"process_id": "load_collection",
"arguments": {
"bands": {
"from_parameter": "s3_data_bands"
},
"featureflags": {
"reprojection_type": "binning",
"super_sampling": 2,
"flag_band": "CLOUD_flags",
"flag_bitmask": 255
},
"id": "SENTINEL3_SYN_L2_SYN",
"properties": {
"eo:cloud_cover": {
"process_graph": {
"lte3": {
"process_id": "lte",
"arguments": {
"x": {
"from_parameter": "value"
},
"y": 95
},
"result": true
}
}
}
},
"spatial_extent": {
"from_parameter": "spatial_extent"
},
"temporal_extent": {
"from_parameter": "temporal_extent"
}
}
},
"apply5": {
"process_id": "apply",
"arguments": {
"data": {
"from_node": "loadcollection3"
},
"process": {
"process_graph": {
"add2": {
"process_id": "add",
"arguments": {
"x": {
"from_parameter": "x"
},
"y": 0
}
},
"multiply3": {
"process_id": "multiply",
"arguments": {
"x": {
"from_node": "add2"
},
"y": 0.0001
},
"result": true
}
}
}
}
},
"filterlabels1": {
"process_id": "filter_labels",
"arguments": {
"condition": {
"process_graph": {
"neq2": {
"process_id": "neq",
"arguments": {
"x": {
"from_parameter": "value"
},
"y": "CLOUD_flags"
},
"result": true
}
}
},
"data": {
"from_node": "apply5"
},
"dimension": "bands"
}
},
"reducedimension2": {
"process_id": "reduce_dimension",
"arguments": {
"data": {
"from_node": "filterlabels1"
},
"dimension": "bands",
"reducer": {
"process_graph": {
"arrayelement2": {
"process_id": "array_element",
"arguments": {
"data": {
"from_parameter": "data"
},
"index": 0
},
"result": true
}
}
}
}
},
"apply6": {
"process_id": "apply",
"arguments": {
"data": {
"from_node": "reducedimension2"
},
"process": {
"process_graph": {
"isnodata1": {
"process_id": "is_nodata",
"arguments": {
"x": {
"from_parameter": "x"
}
},
"result": true
}
}
}
}
},
"applyneighborhood2": {
"process_id": "apply_neighborhood",
"arguments": {
"data": {
"from_node": "apply6"
},
"overlap": [
{
"dimension": "x",
"value": 32,
"unit": "px"
},
{
"dimension": "y",
"value": 32,
"unit": "px"
}
],
"process": {
"process_graph": {
"runudf2": {
"process_id": "run_udf",
"arguments": {
"data": {
"from_parameter": "data"
},
"runtime": "Python",
"udf": "from scipy.ndimage import distance_transform_edt\nimport numpy as np\nimport xarray as xr\nfrom openeo.udf import XarrayDataCube\n\n\ndef apply_datacube(cube: XarrayDataCube, context: dict) -> XarrayDataCube:\n \"\"\"\n Expects the cloud mask as input (in contrast to ``distance_transform_edt``).\n This is necessary, because ``apply_neighborhood`` pads the input with zeros and not ones.\n \"\"\"\n array = cube.get_array()\n # This special case appears to create some issues, so we skip it\n if not array.any():\n return XarrayDataCube(\n xr.DataArray(np.full_like(array, np.inf), dims=[\"t\", \"y\", \"x\"])\n )\n distance = distance_transform_edt(np.logical_not(array))\n return XarrayDataCube(xr.DataArray(distance, dims=[\"t\", \"y\", \"x\"]))\n"
},
"result": true
}
}
},
"size": [
{
"dimension": "t",
"value": "P1D"
},
{
"dimension": "x",
"value": 512,
"unit": "px"
},
{
"dimension": "y",
"value": 512,
"unit": "px"
}
]
}
},
"apply7": {
"process_id": "apply",
"arguments": {
"data": {
"from_node": "applyneighborhood2"
},
"process": {
"process_graph": {
"subtract2": {
"process_id": "subtract",
"arguments": {
"x": {
"from_parameter": "x"
},
"y": 1
}
},
"divide2": {
"process_id": "divide",
"arguments": {
"x": {
"from_node": "subtract2"
},
"y": 16.666666666666668
},
"result": true
}
}
}
}
},
"apply8": {
"process_id": "apply",
"arguments": {
"data": {
"from_node": "apply7"
},
"process": {
"process_graph": {
"clip2": {
"process_id": "clip",
"arguments": {
"max": 1,
"min": 0,
"x": {
"from_parameter": "x"
}
},
"result": true
}
}
}
}
},
"adddimension2": {
"process_id": "add_dimension",
"arguments": {
"data": {
"from_node": "apply8"
},
"label": "distance_score",
"name": "bands",
"type": "bands"
}
},
"mergecubes2": {
"process_id": "merge_cubes",
"arguments": {
"cube1": {
"from_node": "filterlabels1"
},
"cube2": {
"from_node": "adddimension2"
}
}
},
"applyneighborhood3": {
"process_id": "apply_neighborhood",
"arguments": {
"context": {
"temporal_extent_input": {
"from_parameter": "temporal_extent"
},
"temporal_extent_target": {
"from_parameter": "temporal_extent_target"
},
"interval_days": {
"from_parameter": "interval_days"
},
"sigma_doy": 10,
"use_stepwise_aggregation": false
},
"data": {
"from_node": "mergecubes2"
},
"process": {
"process_graph": {
"runudf3": {
"process_id": "run_udf",
"arguments": {
"context": {
"from_parameter": "context"
},
"data": {
"from_parameter": "data"
},
"runtime": "Python",
"udf": "import numpy as np\nimport pandas as pd\n\nimport xarray as xr\nfrom openeo.metadata import CubeMetadata\nfrom datetime import datetime, timezone\n\n\nEPS = 1e-5\n\n\ndef apply_datacube(cube: xr.DataArray, context: dict) -> xr.DataArray:\n \"\"\"\n Computes a composite time series. The input time series is converted to the time series passed\n as ``\"t_target\"`` in ``context``, by computing a weighted sum of input images for each time step in\n ``t_target``. The inputs are weighted by their temporal distance to the target time step and by the distance to\n cloud score (the ``\"distance_score\"`` band of the inputs).\n\n Expects ``cube`` to be an array of dimensions (t, bands, y, x)\n \"\"\"\n if context.get(\"use_stepwise_aggregation\", False):\n return apply_datacube_stepwise(cube, context)\n\n band_names = cube.get_index(\"bands\")\n assert \"bands\" in cube.dims, (\n f\"cube must have a 'bands' dimension, found '{cube.dims}'\"\n )\n assert \"distance_score\" in band_names, (\n f\"Input cube must have a band 'distance_score' in addition to the input bands. Found bands '{band_names}'\"\n )\n\n sigma_doy = context[\"sigma_doy\"]\n t_target = get_t_target_from_context(context)\n temporal_score = compute_temporal_score(cube.t, t_target, sigma_doy)\n distance_score = cube.sel(bands=\"distance_score\")\n data_bands = cube.sel(bands=[b for b in band_names if (b != \"distance_score\" and b != \"CLOUD_flags\")])\n\n composite = _compute_combined_score_no_intermediates(\n distance_score, temporal_score, data_bands\n )\n\n renamed = composite.rename({\"t_target\": \"t\"})\n dims = (\"t\", \"bands\", \"y\", \"x\")\n return renamed.transpose(*dims)\n\n\ndef apply_metadata(metadata: CubeMetadata, context: dict) -> CubeMetadata:\n t_target = get_t_target_from_context(context)\n t_target_str = [d.isoformat() for d in t_target.to_pydatetime()]\n\n metadata = metadata.rename_labels(dimension=\"t\", target=t_target_str)\n metadata = metadata.filter_bands(\n [\n band.name\n for band in metadata.band_dimension.bands\n if (band.name != \"distance_score\" and band.name != \"CLOUD_flags\")\n ]\n )\n return metadata\n\n\ndef compute_temporal_score(\n t: pd.DatetimeIndex, t_target: pd.DatetimeIndex, sigma_doy: float\n) -> xr.DataArray:\n \"\"\"\n Compute the temporal weight for each input and output time step.\n Generates a two-dimensional score, mapping input time steps to output time steps (``len(t) * len(t_target)`` entries).\n\n :param t: time stamps of the input time series\n :param t_target: target time stamps for which the composites are to be computed\n :param sigma_doy: standard deviation of the gaussian window used for temporal weighting\n \"\"\"\n t_values = t.values.astype(\"datetime64[D]\")\n t_target_values = t_target.values.astype(\"datetime64[D]\")\n difference_matrix = t_values[:, np.newaxis] - t_target_values[np.newaxis, :]\n\n arr = np.exp(-0.5 * np.square(difference_matrix.astype(int)) / np.square(sigma_doy))\n return xr.DataArray(\n arr,\n coords={\"t\": t, \"t_target\": t_target},\n dims=[\"t\", \"t_target\"],\n )\n\n\ndef compute_combined_score(\n distance_score: xr.DataArray, temporal_score: xr.DataArray\n) -> xr.DataArray:\n # equivalent to (distance_score * temporal_score).sum(dim=\"t\")\n # return xr.dot(distance_score, temporal_score, dim=\"t\")\n\n combined = distance_score * temporal_score\n # TODO remove EPS and use mask instead\n return combined / (combined.sum(dim=\"t\") + EPS)\n\n\ndef _compute_combined_score_no_intermediates(\n distance_score: xr.DataArray, temporal_score: xr.DataArray, bands: xr.DataArray\n) -> xr.DataArray:\n res = xr.apply_ufunc(\n _compute_normalized_composite,\n distance_score,\n temporal_score,\n bands,\n input_core_dims=[[\"t\", \"y\", \"x\"], [\"t_target\", \"t\"], [\"t\", \"bands\", \"y\", \"x\"]],\n output_core_dims=[[\"t_target\", \"bands\", \"y\", \"x\"]],\n vectorize=True,\n )\n return res\n\n\ndef _compute_normalized_composite(distance_score, temporal_score, bands, **kwargs):\n \"\"\"\n Compute the combined distance-to-cloud and temporal score and the weighted sum applying the score by pixel and\n input/target time stamp to generate the composites\n \"\"\"\n score = np.einsum(\"tyx,Tt->Ttyx\", distance_score, temporal_score)\n # consider pixels as not-observed if the first band has a nan value\n score_masked = np.where(np.isnan(bands[:, 0, ...]), 0, score)\n\n normalization_flat = np.sum(score_masked, axis=1)\n normalization = normalization_flat[:, np.newaxis, ...]\n score_normalized = score_masked / normalization\n\n finite_bands = np.where(np.isfinite(bands), bands, 0)\n weighted_composite = np.einsum(\n \"Ttyx,tbyx->Tbyx\", score_normalized, finite_bands\n ) # original\n\n no_data_mask = (normalization_flat == 0)[:, np.newaxis, ...] | (\n weighted_composite <= 0\n )\n weighted_composite_masked = np.where(no_data_mask, np.nan, weighted_composite)\n return weighted_composite_masked\n\n\ndef compute_t_target(temporal_extent, interval_days) -> pd.DatetimeIndex:\n t_target = xr.date_range(\n temporal_extent[0],\n temporal_extent[1],\n freq=f\"{interval_days}D\",\n inclusive=\"left\",\n )\n return t_target\n\n\ndef apply_datacube_stepwise(cube: xr.DataArray, context: dict) -> xr.DataArray:\n band_names = cube.get_index(\"bands\")\n assert \"bands\" in cube.dims, (\n f\"cube must have a 'bands' dimension, found '{cube.dims}'\"\n )\n assert \"distance_score\" in band_names, (\n f\"Input cube must have a band 'distance_score' in addition to the input bands. Found bands '{band_names}'\"\n )\n\n sigma_doy = context[\"sigma_doy\"]\n t_target = get_t_target_from_context(context)\n temporal_score = compute_temporal_score(cube.t, t_target, sigma_doy)\n distance_score = cube.sel(bands=\"distance_score\")\n data_bands = cube.sel(bands=[b for b in band_names if b != \"distance_score\"])\n\n composite = _compute_combined_score_ng(distance_score, temporal_score, data_bands)\n\n renamed = composite.rename({\"t_target\": \"t\"})\n dims = (\"t\", \"bands\", \"y\", \"x\")\n return renamed.transpose(*dims)\n\n\ndef _compute_combined_score_ng(distance_score, temporal_score, bands):\n # TODO make mosaic_days a parameter\n mosaic_days = 100\n composites = {}\n\n # TODO convert to \"rolling\"?\n for middle_date in temporal_score.t_target:\n window_start = middle_date - pd.Timedelta(days=mosaic_days / 2)\n window_end = middle_date + pd.Timedelta(days=mosaic_days / 2)\n\n windowed_bands = bands.sel(t=slice(window_start, window_end))\n windowed_bands = xr.where(\n np.abs(windowed_bands.mean(dim=\"t\")) < 5, windowed_bands, np.nan\n )\n windowed_distance_score = distance_score.sel(t=slice(window_start, window_end))\n windowed_temporal_score = temporal_score.sel(\n t=slice(window_start, window_end), t_target=middle_date\n )\n\n score = windowed_distance_score * windowed_temporal_score\n score_nan_masked = xr.where(np.isnan(windowed_bands.isel(bands=0)), 0, score)\n # score_nan_masked = score\n normalizing_coefficient = score_nan_masked.sum(dim=\"t\")# + 1e-5\n normalized_score = score_nan_masked / normalizing_coefficient\n\n weighted_bands = normalized_score * windowed_bands\n composite = weighted_bands.sum(skipna=True, dim=\"t\")\n composite = xr.where(normalized_score.sum(dim=\"t\") == 0 | (composite <= 0), np.nan, composite)\n composites[pd.to_datetime(middle_date.item()).strftime(\"%Y-%m-%d\")] = composite\n composite_da = xr.concat(\n [composites[t_target] for t_target in composites],\n dim=xr.IndexVariable(\"t_target\", temporal_score.t_target),\n )\n return composite_da\n\n\ndef get_t_target_from_context(context):\n if isinstance(context, dict): # from user parameters\n temporal_extent = context.get(\"temporal_extent_target\")\n if temporal_extent is None or len(temporal_extent) == 0: # use input temporal extent as a fallback if temporal extent target is not set\n temporal_extent = context[\"temporal_extent_input\"]\n interval_days = context[\"interval_days\"]\n t_target = compute_t_target(temporal_extent, interval_days)\n else:\n t_target = context\n\n return t_target\n"
},
"result": true
}
}
},
"size": [
{
"dimension": "x",
"value": 64,
"unit": "px"
},
{
"dimension": "y",
"value": 64,
"unit": "px"
}
]
}
},
"filterlabels2": {
"process_id": "filter_labels",
"arguments": {
"condition": {
"process_graph": {
"neq1": {
"process_id": "neq",
"arguments": {
"x": {
"from_parameter": "value"
},
"y": "distance_score"
},
"result": true
}
}
},
"data": {
"from_node": "applyneighborhood3"
},
"dimension": "bands"
}
},
"applykernel1": {
"process_id": "apply_kernel",
"arguments": {
"border": 0,
"data": {
"from_node": "filterlabels2"
},
"factor": 1.0,
"kernel": [
[
0.00296902,
0.01330621,
0.02193823,
0.01330621,
0.00296902
],
[
0.01330621,
0.0596343,
0.09832033,
0.0596343,
0.01330621
],
[
0.02193823,
0.09832033,
0.16210282,
0.09832033,
0.02193823
],
[
0.01330621,
0.0596343,
0.09832033,
0.0596343,
0.01330621
],
[
0.00296902,
0.01330621,
0.02193823,
0.01330621,
0.00296902
]
],
"replace_invalid": 0
}
},
"applydimension1": {
"process_id": "apply_dimension",
"arguments": {
"context": {
"from_node": "dimensionlabels1"
},
"data": {
"from_node": "applykernel1"
},
"dimension": "t",
"process": {
"process_graph": {
"runudf4": {
"process_id": "run_udf",
"arguments": {
"context": {
"from_parameter": "context"
},
"data": {
"from_parameter": "data"
},
"runtime": "Python",
"udf": "import pandas as pd\nimport xarray as xr\nfrom openeo.metadata import CubeMetadata\n\nfrom datetime import datetime, timezone\n\n\ndef apply_datacube(cube: xr.DataArray, context) -> xr.DataArray:\n \"\"\"\n Interpolate cube to the time series passed as context.\n Currently (2025-09-30), on the CDSE OpenEO backend, the target time series can only be passed as the complete context,\n when using the ``dimension_labels`` process. This is necessary to pass the S2 time series directly\n from an S2 cube with a user-defined temporal extent.\n This UDF supports two cases: passing the target time series ``t_target`` as a pd.Datetimeidex\n (this is what happens when chaining with the dimension_labels process) and passing dictionary as a context,\n which defines the time series through the parameters ``temporal_extent`` (left incl, right excl)\n and ``interval_days``.\n\n Expects ``cube`` to be an array of dimensions (t, bands, y, x)\n \"\"\"\n assert \"bands\" in cube.dims, (\n f\"cube must have a 'bands' dimension, found '{cube.dims}'\"\n )\n\n t_target = get_t_target_from_context(context)\n band_suffix = get_target_band_name_suffix_from_context(context)\n\n # The Wizard passes the temporal extent as a xr.IndexVariable which cannot be understood by xr.interp\n if isinstance(t_target, xr.IndexVariable) or isinstance(t_target, xr.DataArray):\n t_target = t_target.values\n t_target = pd.to_datetime(t_target)\n\n if getattr(t_target, \"tz\", None) is not None:\n t_target = t_target.tz_localize(None)\n\n band_names = cube.get_index(\"bands\")\n cube = cube.sel(bands=[b for b in band_names if (b != \"distance_score\" and b != \"CLOUD_flags\")])\n\n interpolated = cube.interp(t=t_target)\n interpolated = interpolated.assign_coords(\n bands=[f\"{b}{band_suffix}\" for b in interpolated.coords[\"bands\"].values]\n )\n dims = (\"t\", \"bands\", \"y\", \"x\")\n return interpolated.transpose(*dims)\n\n\ndef apply_metadata(metadata: CubeMetadata, context) -> CubeMetadata:\n t_target = get_t_target_from_context(context)\n band_suffix = get_target_band_name_suffix_from_context(context)\n\n metadata = metadata.rename_labels(dimension=\"t\", target=t_target)\n metadata = metadata.filter_bands(\n [\n band.name\n for band in metadata.band_dimension.bands\n if band.name != \"CLOUD_flags\"\n ]\n )\n metadata = metadata.rename_labels(dimension=\"bands\", target=[f\"{b}{band_suffix}\" for b in metadata.band_names])\n return metadata\n\ndef get_target_band_name_suffix_from_context(context):\n if isinstance(context, dict):\n return context.get(\"target_band_name_suffix\", \"\")\n return \"\"\n\ndef get_t_target_from_context(context):\n if isinstance(context, dict): # from user parameters\n temporal_extent = context.get(\"temporal_extent_target\")\n if temporal_extent is None or len(temporal_extent) == 0: # use input temporal extent as a fallback if temporal extent target is not set\n temporal_extent = context[\"temporal_extent_input\"]\n interval_days = context[\"interval_days\"]\n t_target = compute_t_target(temporal_extent, interval_days)\n else:\n t_target = context\n\n return t_target\n\n\ndef compute_t_target(temporal_extent, interval_days) -> pd.DatetimeIndex:\n t_target = xr.date_range(\n temporal_extent[0],\n temporal_extent[1],\n freq=f\"{interval_days}D\",\n inclusive=\"left\",\n )\n return t_target\n"
},
"result": true
}
}
}
}
},
"resamplecubespatial2": {
"process_id": "resample_cube_spatial",
"arguments": {
"data": {
"from_node": "applydimension1"
},
"method": "bilinear",
"target": {
"from_node": "mergecubes1"
}
}
},
"mergecubes3": {
"process_id": "merge_cubes",
"arguments": {
"cube1": {
"from_node": "mergecubes1"
},
"cube2": {
"from_node": "resamplecubespatial2"
}
}
},
"applyneighborhood4": {
"process_id": "apply_neighborhood",
"arguments": {
"context": {
"temporal_extent_input": {
"from_parameter": "temporal_extent"
},
"temporal_extent_target": {
"from_parameter": "temporal_extent_target"
},
"interval_days": {
"from_parameter": "interval_days"
},
"sigma_doy": {
"from_parameter": "temporal_score_stddev"
},
"use_stepwise_aggregation": false
},
"data": {
"from_node": "mergecubes3"
},
"process": {
"process_graph": {
"runudf5": {
"process_id": "run_udf",
"arguments": {
"context": {
"from_parameter": "context"
},
"data": {
"from_parameter": "data"
},
"runtime": "Python",
"udf": "import numpy as np\nimport pandas as pd\n\nimport xarray as xr\nfrom openeo.metadata import CubeMetadata\nfrom datetime import datetime, timezone\n\n\nEPS = 1e-5\n\n\ndef apply_datacube(cube: xr.DataArray, context: dict) -> xr.DataArray:\n \"\"\"\n Computes a composite time series. The input time series is converted to the time series passed\n as ``\"t_target\"`` in ``context``, by computing a weighted sum of input images for each time step in\n ``t_target``. The inputs are weighted by their temporal distance to the target time step and by the distance to\n cloud score (the ``\"distance_score\"`` band of the inputs).\n\n Expects ``cube`` to be an array of dimensions (t, bands, y, x)\n \"\"\"\n if context.get(\"use_stepwise_aggregation\", False):\n return apply_datacube_stepwise(cube, context)\n\n band_names = cube.get_index(\"bands\")\n assert \"bands\" in cube.dims, (\n f\"cube must have a 'bands' dimension, found '{cube.dims}'\"\n )\n assert \"distance_score\" in band_names, (\n f\"Input cube must have a band 'distance_score' in addition to the input bands. Found bands '{band_names}'\"\n )\n\n sigma_doy = context[\"sigma_doy\"]\n t_target = get_t_target_from_context(context)\n temporal_score = compute_temporal_score(cube.t, t_target, sigma_doy)\n distance_score = cube.sel(bands=\"distance_score\")\n data_bands = cube.sel(bands=[b for b in band_names if (b != \"distance_score\" and b != \"CLOUD_flags\")])\n\n composite = _compute_combined_score_no_intermediates(\n distance_score, temporal_score, data_bands\n )\n\n renamed = composite.rename({\"t_target\": \"t\"})\n dims = (\"t\", \"bands\", \"y\", \"x\")\n return renamed.transpose(*dims)\n\n\ndef apply_metadata(metadata: CubeMetadata, context: dict) -> CubeMetadata:\n t_target = get_t_target_from_context(context)\n t_target_str = [d.isoformat() for d in t_target.to_pydatetime()]\n\n metadata = metadata.rename_labels(dimension=\"t\", target=t_target_str)\n metadata = metadata.filter_bands(\n [\n band.name\n for band in metadata.band_dimension.bands\n if (band.name != \"distance_score\" and band.name != \"CLOUD_flags\")\n ]\n )\n return metadata\n\n\ndef compute_temporal_score(\n t: pd.DatetimeIndex, t_target: pd.DatetimeIndex, sigma_doy: float\n) -> xr.DataArray:\n \"\"\"\n Compute the temporal weight for each input and output time step.\n Generates a two-dimensional score, mapping input time steps to output time steps (``len(t) * len(t_target)`` entries).\n\n :param t: time stamps of the input time series\n :param t_target: target time stamps for which the composites are to be computed\n :param sigma_doy: standard deviation of the gaussian window used for temporal weighting\n \"\"\"\n t_values = t.values.astype(\"datetime64[D]\")\n t_target_values = t_target.values.astype(\"datetime64[D]\")\n difference_matrix = t_values[:, np.newaxis] - t_target_values[np.newaxis, :]\n\n arr = np.exp(-0.5 * np.square(difference_matrix.astype(int)) / np.square(sigma_doy))\n return xr.DataArray(\n arr,\n coords={\"t\": t, \"t_target\": t_target},\n dims=[\"t\", \"t_target\"],\n )\n\n\ndef compute_combined_score(\n distance_score: xr.DataArray, temporal_score: xr.DataArray\n) -> xr.DataArray:\n # equivalent to (distance_score * temporal_score).sum(dim=\"t\")\n # return xr.dot(distance_score, temporal_score, dim=\"t\")\n\n combined = distance_score * temporal_score\n # TODO remove EPS and use mask instead\n return combined / (combined.sum(dim=\"t\") + EPS)\n\n\ndef _compute_combined_score_no_intermediates(\n distance_score: xr.DataArray, temporal_score: xr.DataArray, bands: xr.DataArray\n) -> xr.DataArray:\n res = xr.apply_ufunc(\n _compute_normalized_composite,\n distance_score,\n temporal_score,\n bands,\n input_core_dims=[[\"t\", \"y\", \"x\"], [\"t_target\", \"t\"], [\"t\", \"bands\", \"y\", \"x\"]],\n output_core_dims=[[\"t_target\", \"bands\", \"y\", \"x\"]],\n vectorize=True,\n )\n return res\n\n\ndef _compute_normalized_composite(distance_score, temporal_score, bands, **kwargs):\n \"\"\"\n Compute the combined distance-to-cloud and temporal score and the weighted sum applying the score by pixel and\n input/target time stamp to generate the composites\n \"\"\"\n score = np.einsum(\"tyx,Tt->Ttyx\", distance_score, temporal_score)\n # consider pixels as not-observed if the first band has a nan value\n score_masked = np.where(np.isnan(bands[:, 0, ...]), 0, score)\n\n normalization_flat = np.sum(score_masked, axis=1)\n normalization = normalization_flat[:, np.newaxis, ...]\n score_normalized = score_masked / normalization\n\n finite_bands = np.where(np.isfinite(bands), bands, 0)\n weighted_composite = np.einsum(\n \"Ttyx,tbyx->Tbyx\", score_normalized, finite_bands\n ) # original\n\n no_data_mask = (normalization_flat == 0)[:, np.newaxis, ...] | (\n weighted_composite <= 0\n )\n weighted_composite_masked = np.where(no_data_mask, np.nan, weighted_composite)\n return weighted_composite_masked\n\n\ndef compute_t_target(temporal_extent, interval_days) -> pd.DatetimeIndex:\n t_target = xr.date_range(\n temporal_extent[0],\n temporal_extent[1],\n freq=f\"{interval_days}D\",\n inclusive=\"left\",\n )\n return t_target\n\n\ndef apply_datacube_stepwise(cube: xr.DataArray, context: dict) -> xr.DataArray:\n band_names = cube.get_index(\"bands\")\n assert \"bands\" in cube.dims, (\n f\"cube must have a 'bands' dimension, found '{cube.dims}'\"\n )\n assert \"distance_score\" in band_names, (\n f\"Input cube must have a band 'distance_score' in addition to the input bands. Found bands '{band_names}'\"\n )\n\n sigma_doy = context[\"sigma_doy\"]\n t_target = get_t_target_from_context(context)\n temporal_score = compute_temporal_score(cube.t, t_target, sigma_doy)\n distance_score = cube.sel(bands=\"distance_score\")\n data_bands = cube.sel(bands=[b for b in band_names if b != \"distance_score\"])\n\n composite = _compute_combined_score_ng(distance_score, temporal_score, data_bands)\n\n renamed = composite.rename({\"t_target\": \"t\"})\n dims = (\"t\", \"bands\", \"y\", \"x\")\n return renamed.transpose(*dims)\n\n\ndef _compute_combined_score_ng(distance_score, temporal_score, bands):\n # TODO make mosaic_days a parameter\n mosaic_days = 100\n composites = {}\n\n # TODO convert to \"rolling\"?\n for middle_date in temporal_score.t_target:\n window_start = middle_date - pd.Timedelta(days=mosaic_days / 2)\n window_end = middle_date + pd.Timedelta(days=mosaic_days / 2)\n\n windowed_bands = bands.sel(t=slice(window_start, window_end))\n windowed_bands = xr.where(\n np.abs(windowed_bands.mean(dim=\"t\")) < 5, windowed_bands, np.nan\n )\n windowed_distance_score = distance_score.sel(t=slice(window_start, window_end))\n windowed_temporal_score = temporal_score.sel(\n t=slice(window_start, window_end), t_target=middle_date\n )\n\n score = windowed_distance_score * windowed_temporal_score\n score_nan_masked = xr.where(np.isnan(windowed_bands.isel(bands=0)), 0, score)\n # score_nan_masked = score\n normalizing_coefficient = score_nan_masked.sum(dim=\"t\")# + 1e-5\n normalized_score = score_nan_masked / normalizing_coefficient\n\n weighted_bands = normalized_score * windowed_bands\n composite = weighted_bands.sum(skipna=True, dim=\"t\")\n composite = xr.where(normalized_score.sum(dim=\"t\") == 0 | (composite <= 0), np.nan, composite)\n composites[pd.to_datetime(middle_date.item()).strftime(\"%Y-%m-%d\")] = composite\n composite_da = xr.concat(\n [composites[t_target] for t_target in composites],\n dim=xr.IndexVariable(\"t_target\", temporal_score.t_target),\n )\n return composite_da\n\n\ndef get_t_target_from_context(context):\n if isinstance(context, dict): # from user parameters\n temporal_extent = context.get(\"temporal_extent_target\")\n if temporal_extent is None or len(temporal_extent) == 0: # use input temporal extent as a fallback if temporal extent target is not set\n temporal_extent = context[\"temporal_extent_input\"]\n interval_days = context[\"interval_days\"]\n t_target = compute_t_target(temporal_extent, interval_days)\n else:\n t_target = context\n\n return t_target\n"
},
"result": true
}
}
},
"size": [
{
"dimension": "x",
"value": 64,
"unit": "px"
},
{
"dimension": "y",
"value": 64,
"unit": "px"
}
]
}
},
"applydimension2": {
"process_id": "apply_dimension",
"arguments": {
"context": {
"temporal_extent_input": {
"from_parameter": "temporal_extent"
},
"temporal_extent_target": {
"from_parameter": "temporal_extent_target"
},
"interval_days": {
"from_parameter": "interval_days"
},
"target_band_name_suffix": "_interpolated"
},
"data": {
"from_node": "applykernel1"
},
"dimension": "t",
"process": {
"process_graph": {
"runudf6": {
"process_id": "run_udf",
"arguments": {
"context": {
"from_parameter": "context"
},
"data": {
"from_parameter": "data"
},
"runtime": "Python",
"udf": "import pandas as pd\nimport xarray as xr\nfrom openeo.metadata import CubeMetadata\n\nfrom datetime import datetime, timezone\n\n\ndef apply_datacube(cube: xr.DataArray, context) -> xr.DataArray:\n \"\"\"\n Interpolate cube to the time series passed as context.\n Currently (2025-09-30), on the CDSE OpenEO backend, the target time series can only be passed as the complete context,\n when using the ``dimension_labels`` process. This is necessary to pass the S2 time series directly\n from an S2 cube with a user-defined temporal extent.\n This UDF supports two cases: passing the target time series ``t_target`` as a pd.Datetimeidex\n (this is what happens when chaining with the dimension_labels process) and passing dictionary as a context,\n which defines the time series through the parameters ``temporal_extent`` (left incl, right excl)\n and ``interval_days``.\n\n Expects ``cube`` to be an array of dimensions (t, bands, y, x)\n \"\"\"\n assert \"bands\" in cube.dims, (\n f\"cube must have a 'bands' dimension, found '{cube.dims}'\"\n )\n\n t_target = get_t_target_from_context(context)\n band_suffix = get_target_band_name_suffix_from_context(context)\n\n # The Wizard passes the temporal extent as a xr.IndexVariable which cannot be understood by xr.interp\n if isinstance(t_target, xr.IndexVariable) or isinstance(t_target, xr.DataArray):\n t_target = t_target.values\n t_target = pd.to_datetime(t_target)\n\n if getattr(t_target, \"tz\", None) is not None:\n t_target = t_target.tz_localize(None)\n\n band_names = cube.get_index(\"bands\")\n cube = cube.sel(bands=[b for b in band_names if (b != \"distance_score\" and b != \"CLOUD_flags\")])\n\n interpolated = cube.interp(t=t_target)\n interpolated = interpolated.assign_coords(\n bands=[f\"{b}{band_suffix}\" for b in interpolated.coords[\"bands\"].values]\n )\n dims = (\"t\", \"bands\", \"y\", \"x\")\n return interpolated.transpose(*dims)\n\n\ndef apply_metadata(metadata: CubeMetadata, context) -> CubeMetadata:\n t_target = get_t_target_from_context(context)\n band_suffix = get_target_band_name_suffix_from_context(context)\n\n metadata = metadata.rename_labels(dimension=\"t\", target=t_target)\n metadata = metadata.filter_bands(\n [\n band.name\n for band in metadata.band_dimension.bands\n if band.name != \"CLOUD_flags\"\n ]\n )\n metadata = metadata.rename_labels(dimension=\"bands\", target=[f\"{b}{band_suffix}\" for b in metadata.band_names])\n return metadata\n\ndef get_target_band_name_suffix_from_context(context):\n if isinstance(context, dict):\n return context.get(\"target_band_name_suffix\", \"\")\n return \"\"\n\ndef get_t_target_from_context(context):\n if isinstance(context, dict): # from user parameters\n temporal_extent = context.get(\"temporal_extent_target\")\n if temporal_extent is None or len(temporal_extent) == 0: # use input temporal extent as a fallback if temporal extent target is not set\n temporal_extent = context[\"temporal_extent_input\"]\n interval_days = context[\"interval_days\"]\n t_target = compute_t_target(temporal_extent, interval_days)\n else:\n t_target = context\n\n return t_target\n\n\ndef compute_t_target(temporal_extent, interval_days) -> pd.DatetimeIndex:\n t_target = xr.date_range(\n temporal_extent[0],\n temporal_extent[1],\n freq=f\"{interval_days}D\",\n inclusive=\"left\",\n )\n return t_target\n"
},
"result": true
}
}
}
}
},
"resamplecubespatial3": {
"process_id": "resample_cube_spatial",
"arguments": {
"data": {
"from_node": "applydimension2"
},
"method": "bilinear",
"target": {
"from_node": "applyneighborhood4"
}
}
},
"mergecubes4": {
"process_id": "merge_cubes",
"arguments": {
"cube1": {
"from_node": "applyneighborhood4"
},
"cube2": {
"from_node": "resamplecubespatial3"
}
}
},
"applydimension3": {
"process_id": "apply_dimension",
"arguments": {
"context": {
"lr_mosaic_bands": {
"from_parameter": "s3_data_bands"
},
"hr_mosaic_bands": {
"from_parameter": "s2_data_bands"
},
"lr_interpolated_band_name_suffix": "_interpolated",
"output_ndvi": {
"from_parameter": "output_ndvi"
}
},
"data": {
"from_node": "mergecubes4"
},
"dimension": "bands",
"process": {
"process_graph": {
"runudf7": {
"process_id": "run_udf",
"arguments": {
"context": {
"from_parameter": "context"
},
"data": {
"from_parameter": "data"
},
"runtime": "Python",
"udf": "import xarray as xr\nimport numpy as np\nfrom openeo.metadata import CubeMetadata\n\n\ndef apply_datacube(cube: xr.DataArray, context: dict) -> xr.DataArray:\n \"\"\"\n Computes the main fusion procedure, equation (3) in [1].\n\n [1]: Senty, Paul, Radoslaw Guzinski, Kenneth Grogan, et al. \u201cFast Fusion of Sentinel-2 and Sentinel-3 Time Series over Rangelands.\u201d Remote Sensing 16, no. 11 (2024): 11. https://doi.org/10.3390/rs16111833.\n \"\"\"\n assert \"bands\" in cube.dims, (\n f\"cube must have a 'bands' dimension, found '{cube.dims}'\"\n )\n assert \"hr_mosaic_bands\" in context, (\n f\"The high resolution mosaic bands 'hr_mosaic_bands' must be provided in the 'context' dict. Found keys '{context.keys()}' in 'context'.\"\n )\n assert \"lr_mosaic_bands\" in context, (\n f\"The low resolution bands 'lr_mosaic_bands' must be provided in the 'context' dict. Found keys '{context.keys()}' in 'context'.\"\n )\n assert \"lr_interpolated_band_name_suffix\" in context, (\n \"The suffix differentiating low resolution interpolated bands from composited bands \"\n f\"'lr_interpolated_band_name_suffix' provided in the 'context' dict. Found keys '{context.keys()}' in 'context'.\"\n )\n\n hr_mosaic_bands = context[\"hr_mosaic_bands\"]\n lr_mosaic_bands = context[\"lr_mosaic_bands\"]\n if \"CLOUD_flags\" in lr_mosaic_bands:\n lr_mosaic_bands = list(set(lr_mosaic_bands) - {\"CLOUD_flags\"})\n interpolated_band_suffix = context[\"lr_interpolated_band_name_suffix\"]\n lr_interpolated_bands = [f\"{b}{interpolated_band_suffix}\" for b in lr_mosaic_bands]\n target_bands = context.get(\"target_bands\")\n if target_bands is None or len(target_bands) != len(hr_mosaic_bands):\n target_bands = hr_mosaic_bands\n output_ndvi = context.get(\"output_ndvi\", False)\n\n fused = fuse(\n cube, hr_mosaic_bands, lr_mosaic_bands, lr_interpolated_bands, target_bands\n )\n if output_ndvi:\n # TODO band selection breaks if target_bands is set\n nir = fused.sel(bands=\"B8A\")\n red = fused.sel(bands=\"B04\")\n ndvi = (nir - red) / (nir + red)\n # TODO may break on older version of xarray\n ndvi_formatted = ndvi.expand_dims({\"bands\": [\"ndvi\"]}, axis=fused.dims.index(\"bands\"))\n return ndvi_formatted\n\n return fused\n\n\ndef apply_metadata(metadata: CubeMetadata, context: dict) -> CubeMetadata:\n target_bands = context.get(\"target_bands\", context[\"hr_mosaic_bands\"])\n output_ndvi = context.get(\"output_ndvi\", False)\n if output_ndvi:\n target_bands = [\"ndvi\"]\n metadata = metadata.rename_labels(dimension=\"bands\", target=target_bands)\n return metadata\n\n\ndef fuse(cube, hr_mosaic_bands, lr_mosaic_bands, lr_interpolated_bands, target_bands):\n fused_list = [\n # Pixels where there is no data in either lr_m or lr_p are skipped, to avoid\n # only adding or only subtracting from the S2 composite, which would result in unusually high or low\n # (e.g. negative values).\n xr.where(\n (cube.sel(bands=lr_m) == 0)\n | np.isnan(cube.sel(bands=lr_m))\n | (cube.sel(bands=lr_p) == 0)\n | np.isnan(cube.sel(bands=lr_p)),\n cube.sel(bands=hr_m).squeeze(),\n cube.sel(bands=[hr_m, lr_p]).sum(dim=\"bands\")\n - cube.sel(bands=lr_m).squeeze(),\n )\n for (hr_m, lr_m, lr_p) in zip(\n hr_mosaic_bands, lr_mosaic_bands, lr_interpolated_bands\n )\n ]\n\n fused = xr.concat(fused_list, dim=\"bands\")\n fused = fused.assign_coords(bands=target_bands)\n return fused\n"
},
"result": true
}
}
}
},
"result": true
}
},
"id": "efast",
"summary": "The Efficient Fusion Algorithm Across Spatio-Temporal Scales (EFAST) is a method to create time-series with a fine resolution in space and time from a fine spatial but coarse temporal resolution source (Sentinel-2) and a coarse temporal but fine spatial resolution source (Sentinel-3).",
"description": "# Introduction\n\nThe [Efficient Fusion Algorithm Across Spatio-Temporal Scales (EFAST)](https://doi.org/10.3390/rs16111833) [1] is a method to create\ntime-series with a fine resolution in space and time from a fine spatial but coarse temporal resolution\nsource (Sentinel-2) and a coarse temporal but fine spatial resolution source (Sentinel-3).\n\nIn comparison to other methods (e.g. STARFM), EFAST aims to achieve results outperforming single-source Sentinel-2\ntime-series interpolation by exploiting change information from Sentinel-3 with minimal computational cost,\nassuming homogeneous temporal dynamics.\nEFAST was originally designed to accurately capture seasonal vegetation changes in homogeneous areas like range lands\nwhich present long temporal gaps in Sentinel-2 time series during the rainy season.\nDHI has published a [Python implementation of the algorithm](https://github.com/DHI-GRAS/efast).\nIn the context of the ESA funded [APEx initiative](https://apex.esa.int/), the algorithm has been ported to OpenEO\nby [Brockmann Consult GmbH](https://www.brockmann-consult.de/) and is implemented in this process graph.\n\nEFAST interpolates Sentinel-2 acquisitions, using a time (temporal distance to target time) and\ndistance-to-cloud weighted compositing scheme. The Sentinel-3 time-series is incorporated to locally update\nthe interpolated Sentinel-2 imagery to accurately track vegetation changes between cloud-free Sentinel-2\nacquisitions. The method is described in detail in [1].\n\n[1]: Senty, Paul, Radoslaw Guzinski, Kenneth Grogan, et al. \u201cFast Fusion of Sentinel-2 and Sentinel-3 Time Series over Rangelands.\u201d Remote Sensing 16, no. 11 (2024): 1833. https://doi.org/10.3390/rs16111833\n\n# Usage remarks\n\n- EFAST produces high temporal frequency time-series matching the Sentinel-2 L2A bands which have\ncorresponding Sentinel-3 OLCI bands with matching centre frequencies. Because the application of EFAST is\nfocused on NDVI time-series, the UDP includes a parameter (`output_ndvi`) to directly compute the NDVI\nfor a given temporal and spatial extent.\n\n- It should be noted that OpenEO provides bands from the SENTINEL_L2A collection in integer format. EFAST\nconverts the data to floating point values for interpolation. Therefore, output bands of EFAST have a\ndifferent data type (floating point) than the corresponding SENTINEL_L2A bands.\n\n- Please refer to the [Jupyter notebook example](https://esa-apex.github.io/apex_jupyterlite/lab/index.html?fromURL=https%3A%2F%2Fraw.githubusercontent.com%2Fbcdev%2Fefast-process-graph%2Frefs%2Fheads%2Fmain%2Fnotebooks%2FEFAST_example_jupyterlite.ipynb)\n for an interactive usage example and discussion of the parameters.\n A minimal example can be found in the process-graph repository's\n [readme](https://github.com/bcdev/efast-process-graph/).\n\n## Job Parameters for long time series and large areas of extent\n\nWhen applying EFAST to long time series and large areas, very large data volumes are processed.\nEFAST is used to generate time series of combined high spatial and high temporal resolution, which by nature\nresults in large volumes of data.\n\nThe EFAST process graph is configured by default to support one-year time series at 0.25\u00b0 by 0.25\u00b0 spatial tiles for the\nprocessing of 4 spectral bands. When time-series become longer, the `python-memory` job option may need to be increased.\nFor larger areas, the `executor-memory` job option may need to be increased.\nBy default, EFAST runs with 4 GB of Python memory and 11 GB of executor memory.\nIf you are running only small tests, you may want to reduce these values to save credits.\n\nBe careful when setting job options, as setting the executor memory too high or too low may both result in slower\nand more expensive jobs.\n\nWhen running EFAST from the Python client, you can set job options, when creating the job:\n```Python\nimport openeo\nconnection = openeo.connect(\"https://openeo.dataspace.copernicus.eu\").authenticate_oidc()\ncube = connection.datacube_from_process(\n \"efast\",\n # ...\n)\njob = cube.create_job(\n out_format=\"netcdf\",\n title=f\"EFAST with job options\",\n job_options={ # <----\n \"executor-memory\": \"14G\",\n \"python-memory\": \"4G\",\n # ...\n }\n)\njob.start_and_wait()\n```\n\nFor more on setting job options, see the [CDSE documentation on OpenEO job configuration](https://documentation.dataspace.copernicus.eu/APIs/openEO/job_config.html)\nand the Python client's documentation on [create_job](https://open-eo.github.io/openeo-python-client/api.html#openeo.rest.datacube.DataCube.create_job).",
"default_job_options": {
"executor-memory": "11G",
"python-memory": "4G"
},
"parameters": [
{
"name": "temporal_extent",
"description": "The date range of the Sentinel-2 L2A and Sentinel-3 SY_2_SYN inputs. The fused (output) time series can optionally be defined by the temporal_extent_target parameter. If temporal_extent_target is not set, the output time series will cover temporal_extent by default",
"schema": {
"type": "array",
"subtype": "temporal-interval"
}
},
{
"name": "temporal_extent_target",
"description": "The date range of the fused outputs. Must be completely contained in temporal_extent. This parameter is optional, if no temporal_extent_target is not set, the output will cover the temporal extent of the inputs (defined by the parameter temporal_extent). As per openeo convention, the start date of the temporal extent is included and the end date excluded if if no time of day is set.",
"schema": {
"type": "array",
"subtype": "temporal-interval",
"uniqueItems": true,
"minItems": 2,
"maxItems": 2,
"items": {
"anyOf": [
{
"type": "string",
"subtype": "date-time",
"format": "date-time"
},
{
"type": "string",
"subtype": "date",
"format": "date"
},
{