Forest Cut Temporal Detection Script - select time


I’m trying to understand better the very interesting code for forest cuy temporal detection Nicolas Karasiak and I wanted to ask regard the year and month selection.

I’m trying to use it as evaluate script, but then I get confused about how the years and months are selected. I saw that there is function “getNeededDates” which seems to take 3 years, but i’m not sure how to month is selected and what is the sceneMonth and sceneyear parameters, where are they taken from and how.

the orignal script of Nicolas is here and explaination is here.

Hi Reut,

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:

  • the first array contains the months

  • the second array contains the years

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].

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:

scenes[0].date.getMonth() for the month,
scenes[0].date.getFullYear() for the year.

If you haven’t changed the settings by default, scenes[0] 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[0].includes(sceneMonth)) checks if the month is the same as the ones determined by the getNeededDates function. If True, then:
  • if (monthsAndYears[1].includes(sceneYear)) checks if the year is the same as the year returned by getNeededDates. If not then,
  • else if (monthsAndYears[1].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?

1 Like