|
| 1 | +//VERSION=3 |
| 2 | + |
| 3 | +const nDays = 20; // The number of days to load data for |
| 4 | +const scaleFactor = 1000; // The scale factor for the SWC values |
| 5 | +const vmin = 0.0; // The minimum value of the colormap |
| 6 | +const vmax = 0.4; // The maximum value of the colormap |
| 7 | + |
| 8 | +function setup() { |
| 9 | + return { |
| 10 | + input: ["SWC", "dataMask"], |
| 11 | + output: { id: "default", bands: 4 }, |
| 12 | + mosaicking: "ORBIT", |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +function preProcessScenes(collections) { |
| 17 | + collections.scenes.orbits = collections.scenes.orbits.filter(function ( |
| 18 | + orbit |
| 19 | + ) { |
| 20 | + var orbitDateFrom = new Date(orbit.dateFrom); |
| 21 | + // Select all images within the last nDays |
| 22 | + return ( |
| 23 | + orbitDateFrom.getTime() >= |
| 24 | + collections.to.getTime() - nDays * 24 * 3600 * 1000 |
| 25 | + ); |
| 26 | + }); |
| 27 | + return collections; |
| 28 | +} |
| 29 | + |
| 30 | +function get_mean_swc_value(samples) { |
| 31 | + // Get the sum of all SWC values |
| 32 | + let n_valid_dates = 0; |
| 33 | + let sum = 0; |
| 34 | + for (let i = 0; i < samples.length; i++) { |
| 35 | + if (samples[i].dataMask) { |
| 36 | + sum += samples[i].SWC / scaleFactor; |
| 37 | + n_valid_dates += 1; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // Calculate the mean SWC value |
| 42 | + let mean_swc_value = NaN; |
| 43 | + if (n_valid_dates > 0) { |
| 44 | + mean_swc_value = sum / n_valid_dates; |
| 45 | + } |
| 46 | + |
| 47 | + return mean_swc_value; |
| 48 | +} |
| 49 | + |
| 50 | +const cmap = [ |
| 51 | + [0.0, 0xfff7ea], |
| 52 | + [0.05, 0xfaedda], |
| 53 | + [0.1, 0xede4cb], |
| 54 | + [0.15, 0xdedcbd], |
| 55 | + [0.2, 0xced3af], |
| 56 | + [0.25, 0xbdcba3], |
| 57 | + [0.3, 0xaac398], |
| 58 | + [0.35, 0x96bc90], |
| 59 | + [0.4, 0x80b48a], |
| 60 | + [0.45, 0x68ac86], |
| 61 | + [0.5, 0x4da484], |
| 62 | + [0.55, 0x269c83], |
| 63 | + [0.6, 0x009383], |
| 64 | + [0.65, 0x008a85], |
| 65 | + [0.7, 0x008186], |
| 66 | + [0.75, 0x007788], |
| 67 | + [0.8, 0x006d8a], |
| 68 | + [0.85, 0x00618c], |
| 69 | + [0.9, 0x00558d], |
| 70 | + [0.95, 0x00478f], |
| 71 | + [1.0, 0x003492], |
| 72 | +]; |
| 73 | + |
| 74 | +// Prepare colormap based on provided min and max values |
| 75 | +const visualizer = new ColorRampVisualizer(cmap, vmin, vmax); |
| 76 | + |
| 77 | +function evaluatePixel(samples) { |
| 78 | + // When there are no dates, return no data |
| 79 | + if (samples.length == 0) return [NaN, NaN, NaN, 0]; |
| 80 | + |
| 81 | + // Calculate mean SWC value |
| 82 | + const mean_swc_val = get_mean_swc_value(samples); |
| 83 | + |
| 84 | + // Set opacity to 0 if there is no valid data |
| 85 | + let opacity = 1; |
| 86 | + if (isNaN(mean_swc_val)) { |
| 87 | + opacity = 0; |
| 88 | + } |
| 89 | + |
| 90 | + // Apply colormap |
| 91 | + imgVals = visualizer.process(mean_swc_val); |
| 92 | + |
| 93 | + return [...imgVals, opacity]; |
| 94 | +} |
0 commit comments