Skip to content

Commit 59d4b9d

Browse files
AmberMulderAmberMulderPlanet
andauthoredNov 5, 2024··
Add LST Backward Average script (#333)
* Initial setup LST Backwar Average scripts and docs * Create and update color ramp in one command --------- Co-authored-by: Amber Mulder <amber.mulder@planet.com>
1 parent ba2159c commit 59d4b9d

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed
 

‎planetary-variables/land-surface-temperature/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ Planet's LST product provides near real-time measurements twice a day at 1:30 an
1313

1414
- [Land Surface Temperature Visualization]({% link planetary-variables/land-surface-temperature/land-surface-temperature-visualization/index.md %})
1515
- [Land Surface Temperature Anomaly]({% link planetary-variables/land-surface-temperature/land-surface-temperature-anomaly/index.md %})
16+
- [Land Surface Temperature Backward Average]({% link planetary-variables/land-surface-temperature/land-surface-temperature-backward-average/index.md %})
1617
- [Land Surface Temperature Quality Flags]({% link planetary-variables/land-surface-temperature/land-surface-temperature-quality-flags/index.md %})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: Land Surface Temperature Backward Average
3+
grand_parent: Planetary Variables
4+
parent: Land Surface Temperature
5+
layout: script
6+
nav_exclude: false
7+
scripts:
8+
- [Visualization, script.js]
9+
- [Raw Values, raw.js]
10+
examples:
11+
- zoom: '11'
12+
lat: '44.8398'
13+
lng: '-0.5294'
14+
datasetId: '8d977093-cf9e-4351-8159-90f2522c29c1'
15+
fromTime: '2022-12-01T00:00:00.000Z'
16+
toTime: '2022-12-30T23:59:59.999Z'
17+
platform:
18+
- EOB
19+
evalscripturl: https://custom-scripts.sentinel-hub.com/custom-scripts/planetary-variables/land-surface-temperature/land-surface-temperature-backward-average/script.js
20+
additionalQueryParams:
21+
- - themeId
22+
- PLANET_SANDBOX
23+
---
24+
## General description
25+
The Land Surface Temperature Backward Average is a method to reduce data gaps and measurement noise in the Land Surface Temperature (LST) data. Depending on the requirements, we can choose a lookback period, for example 20 days. The 20-day backward average of LST for day n is the average of LST over the 20 days preceding day n. We compute the backward average using all available measurements within this 20-day period, and therefore, we do have a valid value for every day, except in case of prolonged data unavailability, such as during long frost and snow periods.
26+
27+
## Why it is useful
28+
The Land Surface Temperature Backward Average is suitable for applications where long-term temperatures are more relevant than daily fluctuations. The moving average operation reduces day-to-day variations and in the resulting time series, seasonal and longer-term changes can be easily detected. It can be used for monitoring drought risk, yield forecasting and analysis of climate change.
29+
30+
## Useful links
31+
- [Product specifications](https://planet.widen.net/s/tltwk6hnps)
32+
- [Data sheet](https://planet.widen.net/s/ttvp2rvwzd)
33+
- [Sentinel Hub documentation about Land Surface Temperature](https://docs.sentinel-hub.com/api/latest/data/planetary-variables/land-surface-temp/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//VERSION=3
2+
3+
// LST has two observations per day: 1h30 and 13h30 solar local time
4+
5+
const date = "2022-12-31"; // The date for which the backward average is calculated
6+
const nDays = 20; // The number of days to load data for
7+
const scaleFactor = 100; // The scale factor for the SWC values
8+
const sensing_time = "0130"; // Observation time: "0130" or "1330" or ""
9+
const variable = "LST"; // Variable of interest: "LST" or "LST_MaskedPixels"
10+
11+
function setup() {
12+
return {
13+
input: [variable, "dataMask"],
14+
output: { bands: 1, sampleType: "FLOAT32" },
15+
mosaicking: "TILE",
16+
};
17+
}
18+
19+
// Select files based on sensing time (0130 or 1330) and within the last nDays
20+
function preProcessScenes(collections) {
21+
var calculationDate = new Date(date);
22+
collections.scenes.tiles = collections.scenes.tiles.filter(function (tile) {
23+
var tileDate = new Date(tile.date);
24+
return (
25+
tile.dataPath.includes("T" + sensing_time) &&
26+
tileDate.getTime() >= calculationDate.getTime() - nDays * 24 * 3600 * 1000
27+
);
28+
});
29+
return collections;
30+
}
31+
32+
function get_mean_lst_value(samples) {
33+
// Get the sum of all LST values
34+
let n_valid_dates = 0;
35+
let sum = 0;
36+
for (let i = 0; i < samples.length; i++) {
37+
if (samples[i].dataMask) {
38+
sum += samples[i].LST / scaleFactor;
39+
n_valid_dates += 1;
40+
}
41+
}
42+
43+
// Calculate the mean LST value
44+
let mean_lst_value = NaN;
45+
if (n_valid_dates > 0) {
46+
mean_lst_value = sum / n_valid_dates;
47+
}
48+
49+
return mean_lst_value;
50+
}
51+
52+
function evaluatePixel(samples) {
53+
// When there are no dates, return no data
54+
if (samples.length == 0) return [NaN];
55+
56+
// Calculate mean LST value
57+
const mean_lst_val = get_mean_lst_value(samples);
58+
59+
return [mean_lst_val];
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)
Please sign in to comment.