I am trying to use GEE to get temperature rasters. I am using the Daymet data, pulling every day of the year in 2011 in a county in New York, USA. I want to exclude temperatures less than 10 C and greater than 26 C. Daymet provides a layer for daily minimum temp. and max. temp.
The cold days can be tricky because the max. temp. for the day may be lower 10 C. I use updateMask()
multiple time to try to cover cases where: the daily max is > 26 C, the daily max is < 10C, and the daily min. is < 10 C. But I'm not sure if wrote this correctly.
I wrote the following:
var daymet = ee.ImageCollection("NASA/ORNL/DAYMET_V4")
.filterDate(start_date, end_date)
.map(function(im){return im.clip(tomp)});
// Set a threshold of temps for use in a later calculation
// Mask any pixle above 26 C
var thresholdhi = function(im) {
return im.where(im.gt(26.0), 0);
};
// Mask any pixel below 10 C
var thresholdlo = function(im) {
return im.where(im.lt(10.0), 0);
};
var daymean = daymet.map(function (image) {
var dayx = image.select('tmax');
var dayn = image.select('tmin');
// Apply threshold
// Hot days - max. temp. above 26
var dayxt = thresholdhi(dayx).rename('hotmax');
// Cold days - max. temp. (e.g. high temp on a very cold day)
var dayxt2 = thresholdlo(dayxt).rename('lowmax');
// Low temp. on cold day
var daynt = thresholdlo(dayn).rename('coldlow');
// Calculate mean temp.
var tavg = dayn.add(dayx).divide(2).rename('mean');
// Use the thresholded images as masks
// For non-winter days...
var tavg2 = tavg.updateMask(dayxt);
// For max. on winter days...
var tavg3 = tavg2.updateMask(dayxt2);
// For min. on any day...
var tavg4 = tavg3.updateMask(daynt).rename('lethal');
// Adding all these bands to compare values in the map Inspector
return image.addBands([dayxt,dayxt2,daynt,tavg,tavg4]);
});
I'm not sure if I used the updateMask correctly. I used the map Inspector with a mapped image, clicked on a random area within the map, and got these values:
tmax: 8.779999732971191
tmin: -0.25999999046325684
hotmax: 8.779999732971191
lowmax: 0
coldlow: 0
mean: 4.259999752044678
lethal: 16.674999237060547
I'm expecting the lethal value to be either the mean or 0, in the case the selected pixel is <10 C or > 26C. The value given for lethal here is greater than the max. temp. for the day, which is far greater than I expect. Can someone point out where I'm going wrong, please?
Code is here:
https://code.earthengine.google.com/73e816060c42e1ff5a3898b377c85f71
Best Answer
I removed the functions I created. I thought I had to create mask function and then use them with
map()
. Instead, I apply the.where()
directly to the means that are computed.The code environment: https://code.earthengine.google.com/44c7a3e19ea8dbdfd381efcde7296d40