1+ //VERSION=3 (auto-converted from 1)
2+ /*
3+ Source: @HarelDan - https://github.com/hareldunn/GIS_Repo/blob/master/Multi-Temporal%20NDVI%20for%20Sentinel%20Hub%20Custom%20Scripts
4+ Visualizing NDVI multi-temporal trends in Sentinel-2 imagery.
5+ will take the current image as baseline and calculate average NDVI for the previous 2 months
6+ Based on:
7+ https://twitter.com/sentinel_hub/status/922813457145221121
8+ https://twitter.com/sentinel_hub/status/1020755996359225344
9+ Script requires multi-temporal processing so parameter TEMPORAL=true should be added to the request.
10+ */
11+
12+ function setup ( ) {
13+ return {
14+ input : [
15+ {
16+ bands : [ "B04" , "B08" ] ,
17+ } ,
18+ ] ,
19+ output : { bands : 3 } ,
20+ mosaicking : "ORBIT" ,
21+ } ;
22+ }
23+
24+ function calcNDVI ( sample ) {
25+ var denom = sample . B04 + sample . B08 ;
26+ return denom != 0 ? ( sample . B08 - sample . B04 ) / denom : 0.0 ;
27+ }
28+ function stretch ( val , min , max ) {
29+ return ( val - min ) / ( max - min ) ;
30+ }
31+
32+ function evaluatePixel ( samples , scenes ) {
33+ var avg1 = 0 ;
34+ var count1 = 0 ;
35+ var avg2 = 0 ;
36+ var count2 = 0 ;
37+ var avg3 = 0 ;
38+ var count3 = 0 ;
39+ var endMonth = scenes [ 0 ] . date . getMonth ( ) ;
40+
41+ for ( var i = 0 ; i < samples . length ; i ++ ) {
42+ var ndvi = calcNDVI ( samples [ i ] ) ;
43+ if ( scenes [ i ] . date . getMonth ( ) == endMonth ) {
44+ avg3 = avg3 + ndvi ;
45+ count3 ++ ;
46+ } else if ( scenes [ i ] . date . getMonth ( ) == endMonth - 1 ) {
47+ avg2 = avg2 + ndvi ;
48+ count2 ++ ;
49+ } else {
50+ avg1 = avg1 + ndvi ;
51+ count1 ++ ;
52+ }
53+ }
54+ avg1 = avg1 / count1 ;
55+ avg2 = avg2 / count2 ;
56+ avg3 = avg3 / count3 ;
57+ avg1 = stretch ( avg1 , 0.1 , 0.7 ) ;
58+ avg2 = stretch ( avg2 , 0.1 , 0.7 ) ;
59+ avg3 = stretch ( avg3 , 0.1 , 0.7 ) ;
60+
61+ return [ avg1 , avg2 , avg3 ] ;
62+ }
63+
64+ function preProcessScenes ( collections ) {
65+ collections . scenes . orbits = collections . scenes . orbits . filter ( function (
66+ orbit
67+ ) {
68+ var orbitDateFrom = new Date ( orbit . dateFrom ) ;
69+ return (
70+ orbitDateFrom . getTime ( ) >=
71+ collections . to . getTime ( ) - 3 * 31 * 24 * 3600 * 1000
72+ ) ;
73+ } ) ;
74+ return collections ;
75+ }
0 commit comments