|
| 1 | +//VERSION=3 |
| 2 | + |
| 3 | +// Set defaultVis to false to scale and set color_min and color_max values. |
| 4 | +// LST has two observations per day: 1h30 and 13h30 solar local time |
| 5 | + |
| 6 | +const date = "2022-12-31"; // The date for which the backward average is calculated |
| 7 | +const nDays = 20; // The number of days to load data for |
| 8 | +const scaleFactor = 100; // The scale factor for the SWC values |
| 9 | +const color_min = 260; // The minimum value of the colormap. |
| 10 | +const color_max = 280; // The maximum value of the colormap. |
| 11 | +const sensing_time = "0130"; // Observation time: "0130" or "1330" or "" |
| 12 | +const variable = "LST"; // Variable of interest: "LST" or "LST_MaskedPixels" |
| 13 | + |
| 14 | +function setup() { |
| 15 | + return { |
| 16 | + input: [variable, "dataMask"], |
| 17 | + output: { id: "default", bands: 4 }, |
| 18 | + mosaicking: "TILE", |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +// Select files based on sensing time (0130 or 1330) and within the last nDays |
| 23 | +function preProcessScenes(collections) { |
| 24 | + var calculationDate = new Date(date); |
| 25 | + collections.scenes.tiles = collections.scenes.tiles.filter(function (tile) { |
| 26 | + var tileDate = new Date(tile.date); |
| 27 | + return ( |
| 28 | + tile.dataPath.includes("T" + sensing_time) && |
| 29 | + tileDate.getTime() >= calculationDate.getTime() - nDays * 24 * 3600 * 1000 |
| 30 | + ); |
| 31 | + }); |
| 32 | + return collections; |
| 33 | +} |
| 34 | + |
| 35 | +function get_mean_lst_value(samples) { |
| 36 | + // Get the sum of all LST values |
| 37 | + let n_valid_dates = 0; |
| 38 | + let sum = 0; |
| 39 | + for (let i = 0; i < samples.length; i++) { |
| 40 | + if (samples[i].dataMask) { |
| 41 | + sum += samples[i].LST / scaleFactor; |
| 42 | + n_valid_dates += 1; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Calculate the mean LST value |
| 47 | + let mean_lst_value = NaN; |
| 48 | + if (n_valid_dates > 0) { |
| 49 | + mean_lst_value = sum / n_valid_dates; |
| 50 | + } |
| 51 | + |
| 52 | + return mean_lst_value; |
| 53 | +} |
| 54 | + |
| 55 | +// Create color ramp 250 - 340 (full range) |
| 56 | +const cmap = [ |
| 57 | + [263, 0x000004], |
| 58 | + [266, 0x06051a], |
| 59 | + [270, 0x140e36], |
| 60 | + [274, 0x251255], |
| 61 | + [278, 0x3b0f70], |
| 62 | + [282, 0x51127c], |
| 63 | + [286, 0x641a80], |
| 64 | + [289, 0x782281], |
| 65 | + [293, 0x8c2981], |
| 66 | + [297, 0xa1307e], |
| 67 | + [301, 0xb73779], |
| 68 | + [305, 0xca3e72], |
| 69 | + [309, 0xde4968], |
| 70 | + [313, 0xed5a5f], |
| 71 | + [316, 0xf7705c], |
| 72 | + [320, 0xfc8961], |
| 73 | + [324, 0xfe9f6d], |
| 74 | + [328, 0xfeb77e], |
| 75 | + [332, 0xfecf92], |
| 76 | + [336, 0xfde7a9], |
| 77 | + [340, 0xfcfdbf], |
| 78 | +]; |
| 79 | + |
| 80 | +// Initialize the ColorRamp |
| 81 | +const visualizer = new ColorRampVisualizer(cmap, color_min, color_max); |
| 82 | + |
| 83 | +function evaluatePixel(samples) { |
| 84 | + // When there are no dates, return no data |
| 85 | + if (samples.length == 0) return [NaN, NaN, NaN, 0]; |
| 86 | + |
| 87 | + // Calculate mean LST value |
| 88 | + const mean_lst_val = get_mean_lst_value(samples); |
| 89 | + |
| 90 | + // Set opacity to 0 if there is no valid data |
| 91 | + let opacity = 1; |
| 92 | + if (isNaN(mean_lst_val)) { |
| 93 | + opacity = 0; |
| 94 | + } |
| 95 | + |
| 96 | + // Apply colormap |
| 97 | + imgVals = visualizer.process(mean_lst_val); |
| 98 | + return [...imgVals, opacity]; |
| 99 | +} |
0 commit comments