2

I'm trying to normalize a collection of images, each to it's own mean value (over a certain region), but I keep getting an error inside the map() function that I can't find anywhere else in documentation or online.

Internal error: function argument not initialized.

Here is my code:

var col = ee.ImageCollection('COPERNICUS/S1_GRD')
  .select(['VV'])
  .filterBounds(PolyOfInterest)
  .filterDate(StartDate,EndDate);

var normalize = function(image) {
  var tmp = image.clip(PolyOfInterest);
  var stats = tmp.reduceRegion({
    geometry: roi,
    reducer: ee.Reducer.mean(),
    bestEffort: true,
  });
  print(stats); // I don't know how stats will be structured, so I haven't yet applied it to the image in the next line.

  return image; // Scale the image so that the mean value is mapped to 1 (values above the mean are clipped)
};

var scaledCollection = col.map(normalize);

Has anyone else encountered/solved this problem before, or could help me?

The problem persists even if the entirety of the normalize function is replaced with simply print(image);.

2 Answers 2

4

You cannot use print() (or addLayer or any other client side operation) inside of a map() function.

Please see Client vs. Server for more information.

3
  • Thank you! I can't believe I missed that. Still learning to keep the server/client distinction in mind. Commented Nov 20, 2019 at 19:50
  • Just wondering if you might be able to help with the second half, too. My normalization algorithm requires tmp.multiply(stats.getNumber('VV')). The multiply works during debugging on the client-side if I use getInfo(), but if I remove getInfo() then it fails on the server-side, complaining that the result of getNumber() is not a valid "value". Am I screwing up server-side protocol again? Commented Nov 20, 2019 at 20:01
  • @curiouserand Sorry, but more context is required to see what the problem is — and comments are not a good place for followup. Please post a new question with a complete code sample.
    – Kevin Reid
    Commented Nov 20, 2019 at 21:16
1

I have seen this problem before. The error comes from your line:

print(stats);

Substituting print(image); inside the function also won't help. I have tried exactly what you are doing and trying to print/access variables inside a function that is passed as a map-function will always give this error. I think that it is because GEE effectively evaulates 'backwards', which is the same reason why you see strange JSON descripters of Objects sometimes, instead of actual data. In otherwords, until it gets to the end of your script 'stats' does not exist and so it can't print it and gives you the unhelpful error that it is not initialized.

The best (only) thing is to run the function and then access the stats for a given image outside of the map-process.

Your Answer

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

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