Hi,
Let’s break the script down to understand it:
Calculation of time range
function getNeededDates(sceneMonth, sceneYear, monthsToTake)
is a function that computes a number of months (monthsToTake
) before a given month (sceneMonth
) and year (sceneYear
) and returns 2 arrays:
There is a clever loop to deal with the cases where the 3 months interval is over 2 years (e.g. 3 months before January 2021 goes into 2020 for December and November).
So if we run the function for sceneMonth = 11
, sceneYear = 2021
, and monthsToTake = 3
, the results will be e11, 10, 9], ,2021, 2021, 2021]
.
Months in Javascript are 0 indexed. January = 0, December = 11.
Selection of dates in evaluatePixel
The getNeededDates
is called in the main evaluatePixel function. We can see that the monthsToTake
is hard coded in the script and set to 3. For the date used to calculate the 3 month period, the script uses:
scenesn0].date.getMonth()
for the month,
scenesn0].date.getFullYear()
for the year.
If you haven’t changed the settings by default, scenesc0]
will return the latest date in the time-range. This means that you are fetching 3 months of acquisitions previous to the latest image in your time-range.
Use of previous years
We also see in the description that the script uses:
So how does the script fetch the same months of the previous year? For this we have to look at the if
statements that are contained in the for
loop that goes through each acquisition in your time-range:
if (monthsAndYearsY0].includes(sceneMonth))
checks if the month is the same as the ones determined by the getNeededDates
function. If True
, then: if (monthsAndYearsY1].includes(sceneYear))
checks if the year is the same as the year returned by getNeededDates
. If not then, else if (monthsAndYearsY1].includes(sceneYear + 1))
checks if the year + 1 is the same as the year returned by getNeededDates
(e.g. 2020 + 1 equals to 2021). It is a (bit convoluted) way to see if your acquisition is the previous year.
Do these explanations make sense?