Google Earth Engine – Using Multiple Masks

google-earth-enginemasking

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.


var cou_t = ['Tompkins'];

var st_y = ['New York'];

var cobo = ee.FeatureCollection("TIGER/2016/Counties").filter(ee.Filter.inList('NAME', cou_t)).geometry();

var st_ny = ee.FeatureCollection("TIGER/2018/States").filter(ee.Filter.inList('NAME', st_y));

var tomp = cobo.intersection(st_ny);

// Played around with different dates, like January, March, July
var start_date = ee.Date('2012-01-01');                 // <<<<----       
var end_date = ee.Date('2012-01-02');                   // <<<<----    

var daymet = ee.ImageCollection("NASA/ORNL/DAYMET_V4")
.filterDate(start_date, end_date)
.map(function(im){return im.clip(tomp)});

var daymean = daymet.map(function (image) {
var dayx = image.select('tmax');
  var dayn = image.select('tmin');
  // Apply threshold
  // Hot temp on max. temp.
  var dayxt = dayx.where(dayx.lt(10.0),0).rename('hotmax');
  // Low temp. on max. temp. (e.g. high temp on a very cold day)
  var dayxt2 = dayxt.where(dayxt.gt(26.0),0).rename('lowmax');
  // Low temp. on cold day
  var daynt = dayxt2.where(dayn.lt(10.0),0).rename('coldlow');
  // Calculate mean temp.
  var tavg = dayx.add(dayn).divide(2).rename('avg');
  var tavghc = tavg.where(dayx.lt(10.0),0).rename('meanhotcol');
  var tavgcc = tavghc.where(dayx.gt(26.0),0).rename('meanhihot');
  var tavgcc2 = tavgcc.where(dayn.lt(10.0),0).rename('meanfinal');
  return image.addBands([dayxt,dayxt2,daynt,tavg,tavghc,tavgcc,tavgcc2]);
});

var start_date2 = ee.Date('2012-03-15');                 // <<<<----       
var end_date2 = ee.Date('2012-03-16');                   // <<<<----    

var daymet2 = ee.ImageCollection("NASA/ORNL/DAYMET_V4")
.filterDate(start_date2, end_date2)
.map(function(im){return im.clip(tomp)});

var daymean2 = daymet2.map(function (image) {
var dayx = image.select('tmax');
  var dayn = image.select('tmin');
  // Apply threshold
  // Hot temp on max. temp.
  var dayxt = dayx.where(dayx.lt(10.0),0).rename('hotmax');
  // Low temp. on max. temp. (e.g. high temp on a very cold day)
  var dayxt2 = dayxt.where(dayxt.gt(26.0),0).rename('lowmax');
  // Low temp. on cold day
  var daynt = dayxt2.where(dayn.lt(10.0),0).rename('coldlow');
  // Calculate mean temp.
  var tavg = dayx.add(dayn).divide(2).rename('avg');
  var tavghc = tavg.where(dayx.lt(10.0),0).rename('meanhotcol');
  var tavgcc = tavghc.where(dayx.gt(26.0),0).rename('meanhihot');
  var tavgcc2 = tavgcc.where(dayn.lt(10.0),0).rename('meanfinal');
  return image.addBands([dayxt,dayxt2,daynt,tavg,tavghc,tavgcc,tavgcc2]);
});

var start_date3 = ee.Date('2012-07-15');                 // <<<<----       
var end_date3 = ee.Date('2012-07-16');                   // <<<<----    

var daymet3 = ee.ImageCollection("NASA/ORNL/DAYMET_V4")
.filterDate(start_date3, end_date3)
.map(function(im){return im.clip(tomp)});

var daymean3 = daymet3.map(function (image) {
var dayx = image.select('tmax');
  var dayn = image.select('tmin');
  // Apply threshold
  // Hot temp on max. temp.
  var dayxt = dayx.where(dayx.lt(10.0),0).rename('hotmax');
  // Low temp. on max. temp. (e.g. high temp on a very cold day)
  var dayxt2 = dayxt.where(dayxt.gt(26.0),0).rename('lowmax');
  // Low temp. on cold day
  var daynt = dayxt2.where(dayn.lt(10.0),0).rename('coldlow');
  // Calculate mean temp.
  var tavg = dayx.add(dayn).divide(2).rename('avg');
  var tavghc = tavg.where(dayx.lt(10.0),0).rename('meanhotcol');
  var tavgcc = tavghc.where(dayx.gt(26.0),0).rename('meanhihot');
  var tavgcc2 = tavgcc.where(dayn.lt(10.0),0).rename('meanfinal');
  return image.addBands([dayxt,dayxt2,daynt,tavg,tavghc,tavgcc,tavgcc2]);
});

Map.setCenter(-76,42.4);
Map.setZoom(9);
Map.addLayer(daymean,null,"one");
Map.addLayer(daymean2,null,"two");
Map.addLayer(daymean3,null,"three");

The code environment: https://code.earthengine.google.com/44c7a3e19ea8dbdfd381efcde7296d40

Related Question