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
[11, 10, 9], [2021, 2021, 2021].
Selection of dates in evaluatePixel
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:
scenes.date.getMonth() for the month,
scenes.date.getFullYear() for the year.
If you haven’t changed the settings by default,
scenes 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 (monthsAndYears.includes(sceneMonth)) checks if the month is the same as the ones determined by the
getNeededDates function. If
if (monthsAndYears.includes(sceneYear)) checks if the year is the same as the year returned by
getNeededDates. If not then,
else if (monthsAndYears.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?