0

I get an error when trying to concatenate 3 arrays along the 1-axis.

I obtained an array from a NDVI image collection using .toArrayPerBand(0) Then I created two other arrays using .arraySlice(), which give me the values of 1 and 2 days before. I want to concatenate these three arrays using .arrayCat() along the 1 axis, but I get this error:

Image (Error) Image.arrayCat: Incompatible type dimensions, found 'Type<Float<-1.0, 1.0, dimensions=2>>' and 'Type<Float<-1.0, 1.0, dimensions=1>>'.

I need the three arrays to be concatenated so that I can compute for each date the maximum in the last 3 dates using .arrayReduce().

// 3 PREVIOUS VALUES MAXIMUM NDVI

//define area of interest and the image collection
var aoi = 
      ee.Geometry.Polygon(
        [[[11.111455811313702, 46.3205838600638],
          [11.111455811313702, 46.31527834569152],
          [11.11800040131004, 46.31527834569152],
          [11.11800040131004, 46.3205838600638]]], null, false);
var ndvi_IC=ee.ImageCollection("LANDSAT/LC08/C01/T1_8DAY_NDVI")
    .filterDate('2020-04-01','2020-10-01')
    .filter(ee.Filter.bounds(aoi))
//transform the image collection in an array
var array=ndvi_IC.toArrayPerBand(0)


//define the function to obtain for each date the maximum values of the 3 previous dates
var computemax3previous = function(image) {
//I want to create 3 arrays: 
//ar_original with values in order      :0,1,2,3,4,5,6,7,8,9,10
//ar_1torightfilled with values in order:0,0,1,2,3,4,5,6,7,8,9
//ar_2torightfilled with values in order:0,0,0,1,2,3,4,5,6,7,8
var arpos0= image.arraySlice({axis: 0, start: 0, end: 1})//the first value of the array(position0)
var ar_original=image.arraySlice({axis: 0, start: 0})
        var ar_1toright=image.arraySlice({axis: 0, start: 0, end: -1})
var ar_1torightfilled=arpos0.arrayCat(ar_1toright,0)
        var ar_2toright=image.arraySlice({axis: 0, start: 0, end: -2})
var ar_2torightfilled=arpos0.arrayCat(arpos0,0).arrayCat(ar_2toright,0)

//Concatenate the 3 arrays on the 1-axis
var A3dates= ar_original.arrayCat(ar_1torightfilled,1).arrayCat(ar_2torightfilled,1)
print(A3dates)
// reduce along the 1-axis to obtain the 3previous dates maximum
 var arraymax = array.arrayReduce(ee.Reducer.max(), [1])
                .arrayProject([0])//retain just the 0-axis

  return arraymax}

var   max3previous=(computemax3previous(array))

https://code.earthengine.google.com/4a8a10a6e285d19d7ce62ffc1406c828

1 Answers1

0

Error origins from here: var A3dates = ar_original.arrayCat(ar_1torightfilled, 1).arrayCat(ar_2torightfilled, 1). In your version, ar_original and ar_1torightfilled are 1D array. But after arrayCat, ar_original.arrayCat(ar_1torightfilled, 1) will be 2D.

//define area of interest and the image collection
var aoi =
    ee.Geometry.Polygon(
        [[[11.111455811313702, 46.3205838600638],
        [11.111455811313702, 46.31527834569152],
        [11.11800040131004, 46.31527834569152],
        [11.11800040131004, 46.3205838600638]]], null, false);
var imgcol = ee.ImageCollection("LANDSAT/LC08/C01/T1_8DAY_NDVI")
    .filterDate('2020-04-01', '2020-10-01')
    .filter(ee.Filter.bounds(aoi));
// transform the image collection in an array

// The result will be masked if any input is masked.
imgcol = imgcol.map(function(img) { return img.unmask(-1.0); });
var array = imgcol.toArray();
print(array);

Map.centerObject(aoi, 12);
// Map.addLayer(imgcol);
// Map.addLayer(array);

//define the function to obtain for each date the maximum values of the 3 previous dates
var computemax3previous = function (image) {
    //I want to create 3 arrays: 
    //ar_original with values in order      :0,1,2,3,4,5,6,7,8,9,10
    //ar_1torightfilled with values in order:0,0,1,2,3,4,5,6,7,8,9
    //ar_2torightfilled with values in order:0,0,0,1,2,3,4,5,6,7,8
    var arpos0 = image.arraySlice({ axis: 0, start: 0, end: 1 });//the first value of the array(position0)
    var ar_original = image.arraySlice({ axis: 0, start: 0 });
    var ar_1toright = image.arraySlice({ axis: 0, start: 0, end: -1 });
    var ar_1torightfilled = arpos0.arrayCat(ar_1toright, 0);
    var ar_2toright = image.arraySlice({ axis: 0, start: 0, end: -2 });
    var ar_2torightfilled = arpos0.arrayCat(arpos0, 0).arrayCat(ar_2toright, 0);
  
    //Concatenate the 3 arrays on the 1-axis
    var A3dates = ar_original.arrayCat(ar_1torightfilled, 1).arrayCat(ar_2torightfilled, 1);
    // reduce along the 1-axis to obtain the 3previous dates maximum
    var arraymax = array.arrayReduce(ee.Reducer.max(), [1])
        .arrayProject([0]);//retain just the 0-axis
    return arraymax;
};

var max3previous = computemax3previous(array);
print(max3previous);
Map.addLayer(max3previous, {}, 'max3previous')
Dongdong Kong
  • 361
  • 2
  • 13