Google Earth Engine Image Processing – Replacing Image Collection Values Below a Threshold

google-earth-engine

Using two sets of bands from the Daymet Image Collections in Google Earth Engine, one for daily max. temperature and the other for daily min. temperature. I want to get daily mean temperature by averaging the two collections. First though, I want to set a lower limit to the two sets of bands before I take the mean. I thought the code in this question would be useful, but I'm getting an error and I'm new to GEE and don't understand exactly where I'm going wrong.

// Outline of lower 48 U.S.
var us = ee.Image("users/japolo/us_bounds");

var usbound = us.geometry();

// Set up Daymet collection from 2009 to 2020
var daymetb = ee.ImageCollection("NASA/ORNL/DAYMET_V4")
.filterDate('2009-01-01', '2020-12-31')
.map(function(im){return im.clip(usbound)});

// Set up projection for reproject of Daymet
var proj = ee.Projection('EPSG:4326');

// Reproject Daymet
var daymet = daymetb.map(function(im){ 
   return im.reproject(proj, null, 1000);
});

// Provide some kind of visualization palette for some test viz
var xTempVis = {
  min: -40.0,
  max: 40.0,
  palette: ['1621A2', 'white', 'cyan', 'green', 'yellow', 'orange', 'red'],
};

// Max. Temp. from Daymet 
var dayx = daymet.select('tmax');

// Min. Temp. from Daymet 
var dayn = daymet.select('tmin');

// Vapor pressure from Daymet
var dayv = daymet.select('vp');

// Set replacement value for thresholding in Image Collection
var replacen5 = ee.Image(-5.0);

// Set a threshold of temps for use in a later calculation
var conditional = function(im) {
  return im.where(im.lt(-5.0), replacen5);
};

// map the threshold over the two collections
var daynt = dayn.map(conditional);
var dayxt = dayx.map(conditional);


// Calculate mean temp. collection
var daysumb = daynt.map(function(im){return im.add(dayxt)});
var daymean = daysumb.map(function(im){return im.divide(2)});
//Map.addLayer(daymean, xTempVis)
print(daymean)

The error I get is

 ImageCollection (Error)
Image.add, argument 'image2': Invalid type.
Expected type: Image<unknown bands>.
Actual type: ImageCollection.

I have a feeling I should use something else in place of ee.Image(-5) for the value of the replacement, but I don't know what.

Best Answer

Your problem is this: im.add(dayxt). Here, im is an ee.Image, while dayxt is an ee.ImageCollection. Those cannot be added together. You can fix this by calculating the mean in a single map() operation:

var usbound = Map.getBounds(true) // You didn't share your asset, so using current map bounds instead

// Set up Daymet collection from 2009 to 2020
var daymet = ee.ImageCollection("NASA/ORNL/DAYMET_V4")
  .filterDate('2009-01-01', '2020-12-31')
  .filterBounds(usbound)

// Set a threshold of temps for use in a later calculation
var threshold = function(im) {
  return im.where(im.lt(-5.0), -5)
}

var daymean = daymet.map(function (image) {
  var dayx = image.select('tmax')
  var dayn = image.select('tmin')
  // Apply threshold
  var daynt = threshold(dayn)
  var dayxt = threshold(dayx)
  // Calculate mean temp.
  return daynt.add(dayxt).divide(2)
})

print(daymean)

https://code.earthengine.google.com/fa62da4be56f394bfda314cf79025b68

Related Question