0

I have a very large array containing lists, each with 364 values of daily cumulative rainfall for the years 1988-2018. I would like to calculate a mean for each day in this array, with the output being 364 values in an array, each value containing the mean for that day. Below is an example of the structure of my data and my desired outcome:

var array1 = [1,3,6,8,12,24,4,2,10,11,13]
var array2 = [5,9,2,18,10,2,6,8,4,3,3]

var arrays = [array1, array2]

var arrayMean = [3,6,4,13,11,13,5,5,7,7,8]

The arrayMean variable contains a list of values equally long to the input arrays, but with the mean for each value.

How do I perform this type of calculation?

1 Answer 1

2

What you need to do first is to define your arrays as earth engine objects. Then you can apply the reducers to the ee.Array.

// your arrays
var array1 = [1,3,6,8,12,24,4,2,10,11,13]
var array2 = [5,9,2,18,10,2,6,8,4,3,3]

var arrays = [array1, array2]

// your result
var arrayMean = [3,6,4,13,11,13,5,5,7,7,8]

// create earth engine arrays
var eeArray1 = ee.Array(array1)
var eeArray2 = ee.Array(array2)

// concatenate the arrays so that they are next to each other along the 1 axis
var eeArrays = ee.Array.cat([eeArray1,eeArray2],1)


// reduce the array along the 1 axis
var eeArrayMean = eeArrays.reduce({
  reducer: ee.Reducer.mean(),
  axes: [1]}).project([0])


print(eeArrayMean) // [3,6,4,13,11,13,5,5,7,7,8]

For more information on Arrays in earth engine see Nicholas Clinton's presentation on Arrays. https://youtu.be/-qo8L5GmKO0

2
  • Works exactly as intended, thanks a lot! One question though, what does the ".project([0])" do?
    – IskJon
    Mar 1, 2019 at 8:16
  • 1
    you can safely remove it and look at the result. It is an array, within an array. .project([0]) projects the array along the 0 axis. (removing a dimension). I imagine it like a 3 dimensional world "projected" on a 2 dimensional wall. Taking away a dimension. But if you watch Nicholas Clinton's presentation everything about arrays in Earth Engine is explained there (and then some). I've watched it several times, after brushing up on my math. Mar 1, 2019 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.