Skip to content

Commit 65721e0

Browse files
author
Jonas Viehweger
committed
Merge branch 'main' of github.com:sentinel-hub/custom-scripts into main
2 parents 66a5be9 + 59d4b9d commit 65721e0

File tree

14 files changed

+468
-1
lines changed

14 files changed

+468
-1
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +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 %})
17+
- [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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: Land Surface Temperature Quality Flags
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-04-26T00:00:00.000Z'
16+
toTime: '2022-04-26T23: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-quality-flags/script.js
20+
additionalQueryParams:
21+
- - themeId
22+
- PLANET_SANDBOX
23+
---
24+
## General description
25+
Land Surface Temperature (LST) products include [quality flag assets](https://developers.planet.com/docs/planetary-variables/land-surface-temperature-technical-specification/#quality-flags) that provide quality metadata for each pixel using a bitwise flag system. These flags help identify the reliability of the LST values for each pixel. Here we show how these quality flags can be easily displayed using custom scripts. For a complete list of all possible quality flags and their corresponding bits, please refer to [these tables](https://developers.planet.com/docs/planetary-variables/land-surface-temperature-technical-specification/#quality-flags).
26+
27+
## Notes on usage
28+
Users can customize the script by adding the bit numbers corresponding to the quality flag(s) of interest to the `bits_to_check` list within the script. By default, the provided scripts are set to check all critical flags (indicating unreliable data, with corresponding LST pixels set to the no data value), but this can be updated to include or exclude specific flags as needed.
29+
30+
Planet’s LST product provides near real-time measurements twice a day at 1:30 and 13:30 solar local time. The `sensing_time` variable in the scripts can be used to select the sensing time of interest
31+
32+
The provided Visualization script highlights pixels for which specific quality flags of interest are set. This allows users to visually inspect areas of concern or interest.
33+
34+
The Raw Values script retrieves a binary raster where:
35+
36+
- `1` indicates pixels for which the quality flag(s) of interest are set
37+
- `0` indicates pixels where the quality flag(s) of interest are not set
38+
39+
## Description of representative images
40+
The 'Open water' (bit 15\) quality flag in Flevoland, The Netherlands, on 2022-12-31.
41+
42+
![The water quality flag](fig/qf_lst_100m_water_netherlands_2022-12-31.png)
43+
44+
## Useful links
45+
- [Product specifications](https://planet.widen.net/s/tltwk6hnps)
46+
- [Data sheet](https://planet.widen.net/s/ttvp2rvwzd)
47+
- [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,47 @@
1+
//VERSION=3
2+
3+
const bits_to_check = [8, 9, 11, 13, 14, 15]; // Bits to check if they are set
4+
const sensing_time = "0130"; // "0130" or "1330" or ""
5+
6+
function setup() {
7+
return {
8+
input: ["QF", "dataMask"],
9+
output: { bands: 1, sampleType: "UINT8" },
10+
mosaicking: "TILE",
11+
};
12+
}
13+
14+
//Select files based on sensing time (0130 or 1330)
15+
function preProcessScenes(collections) {
16+
collections.scenes.tiles = collections.scenes.tiles.filter(function (tile) {
17+
return tile.dataPath.includes("T" + sensing_time);
18+
});
19+
collections.scenes.tiles.sort((a, b) => new Date(b.date) - new Date(a.date));
20+
return collections;
21+
}
22+
23+
function areBitsSet(qf) {
24+
// Check if the bits are set
25+
for (let idx in bits_to_check) {
26+
const bit = 1 << (bits_to_check[idx] - 1);
27+
if ((qf & bit) !== 0) {
28+
return true;
29+
}
30+
}
31+
return false;
32+
}
33+
34+
function evaluatePixel(sample) {
35+
// When there are no dates, return no data
36+
if (sample.length == 0) return [NaN];
37+
38+
// Return NaN for no data pixels
39+
if (sample[0].dataMask == 0) {
40+
return [NaN];
41+
}
42+
43+
// Check if the bits are set
44+
const bit_set = areBitsSet(sample[0].QF);
45+
46+
return [bit_set ? 1 : 0];
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//VERSION=3
2+
3+
const bits_to_check = [8, 9, 11, 13, 14, 15]; // Bits to check if they are set
4+
const sensing_time = "0130"; // "0130" or "1330" or ""
5+
6+
function setup() {
7+
return {
8+
input: ["QF", "dataMask"],
9+
output: { id: "default", bands: 4 },
10+
mosaicking: "TILE",
11+
};
12+
}
13+
14+
//Select files based on sensing time (0130 or 1330)
15+
function preProcessScenes(collections) {
16+
collections.scenes.tiles = collections.scenes.tiles.filter(function (tile) {
17+
return tile.dataPath.includes("T" + sensing_time);
18+
});
19+
collections.scenes.tiles.sort((a, b) => new Date(b.date) - new Date(a.date));
20+
return collections;
21+
}
22+
23+
function areBitsSet(qf) {
24+
// Check if the bits are set
25+
for (let idx in bits_to_check) {
26+
const bit = 1 << (bits_to_check[idx] - 1);
27+
if ((qf & bit) !== 0) {
28+
return true;
29+
}
30+
}
31+
return false;
32+
}
33+
34+
function evaluatePixel(sample) {
35+
// When there are no dates, return no data
36+
if (sample.length == 0) return [NaN, NaN, NaN, 0];
37+
38+
// Return NaN for no data pixels
39+
if (sample[0].dataMask == 0) {
40+
return [NaN, NaN, NaN, 0];
41+
}
42+
43+
// Check if the bits are set
44+
const bit_set = areBitsSet(sample[0].QF);
45+
46+
// Ensure only flagged pixels are displayed
47+
let opacity = 0;
48+
let imgVals = [1, 0, 0];
49+
if (bit_set) {
50+
opacity = 0.5;
51+
}
52+
53+
return [...imgVals, opacity];
54+
}

planetary-variables/soil-water-content/index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ Planet's SWC product provides near-daily measurements at spatial resolutions of
1414
- [Soil Water Content Visualization]({% link planetary-variables/soil-water-content/soil-water-content-visualization/index.md %})
1515
- [Soil Water Content Anomaly]({% link planetary-variables/soil-water-content/soil-water-content-anomaly/index.md %})
1616
- [Derived Root-Zone Soil Water Content]({% link planetary-variables/soil-water-content/derived-root-zone-soil-water-content/index.md %})
17-
- [Soil Water Content Backward Average]({% link planetary-variables/soil-water-content/soil-water-content-backward-average/index.md %})
17+
- [Soil Water Content Backward Average]({% link planetary-variables/soil-water-content/soil-water-content-backward-average/index.md %})
18+
- [Soil Water Content Quality Flags]({% link planetary-variables/soil-water-content/soil-water-content-quality-flags/index.md %})

0 commit comments

Comments
 (0)