1

I'm trying to exclude some specific dates from a GEE script. Here is the code I'm working on:

// Define the asset polygon as a feature collection
var asset_polygon = ee.FeatureCollection('projects/ee-user/assets/mask');

// Define the year of interest
var year = 2022;

// Define a list of dates to exclude
var excludeDates = ['2022-07-11'];

// Load a Sentinel-2 image collection and filter it by year and location
var sentinel2 = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterBounds(asset_polygon)
  .filterDate(year + '-01-01', year + '-12-31')
  .select(['B8', 'B4'])
  .filter(ee.Filter.date('2022-07-11').not())
  .map(function(image) {
    // Compute NDVI for each image
    var ndvi = image.normalizedDifference(['B8', 'B4']).rename('ndvi');
    // Add acquisition date as a band to the image
    var date = ee.Date(image.get('system:time_start')).format('YYYYMMdd');
    var dateBand = ee.Image(ee.Number.parse(date)).toInt().rename('date');
    return image.addBands(ndvi).addBands(dateBand);
  });

// Compute the maximum NDVI and acquisition date for each pixel in the collection
var maxNdvi = sentinel2.qualityMosaic('ndvi');
var acquisitionDate = maxNdvi.select('date');
var maxNdvi2 = sentinel2.qualityMosaic('ndvi').select('ndvi');

// Add the maximum NDVI layer to the map
Map.addLayer(maxNdvi2, {min: 0, max: 1, palette: ['white', 'green']}, 'Maximum NDVI');

// Export the rasters as single band images to Google Drive
Export.image.toDrive({
  image: maxNdvi2,
  description: 'S2_max_ndvi_' + year,
  folder: 'gee',
  scale: 10,
  region: asset_polygon.geometry()
});

Export.image.toDrive({
  image: acquisitionDate,
  description: 'S2_acq_date_' + year,
  folder: 'gee',
  scale: 10,
  region: asset_polygon.geometry()
});

Ideally I'd like to use a list of days to exclude (lines 7 and 8) but I don't know how to add it as a list and at the moment I'm not using this variable.

However, I tried filtering it directly using ee.Filter.not in line 15

  .filter(ee.Filter.date('2022-07-11').not())

This is not running into any error but it doesn't filter this date either. What I'm I doing wrong?

1 Answer 1

2

According to the ee.Filter.date() docs:

-start (Date|Number|String):

The start date (inclusive).

-end (Date|Number|String, optional):

The end date (exclusive). Optional. If not specified, a 1-millisecond range starting at 'start' is created.

So, when you only specify start, you get a 1-millisecond range. That's why ee.Filter.date('2022-07-11') doesn't match anything.

A convenient trick, that's not very obvious from the above docs, is to use ee.Date.getRange() as the only argument to the date filter. The below snippiet shows how you can combine a dynamic number of date filters.

var excludeDates = ['2022-07-11']
var dateFilters = excludeDates.map(function (date) {
  return ee.Filter.date(ee.Date(ee.String(date)).getRange('day')).not()
})
var filter = ee.Filter.and.apply(null, dateFilters)
var sentinel2 = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
  .filterBounds(asset_polygon)
  .filterDate(ee.Date.fromYMD(year, 1, 1).getRange('year'))
  .filter(filter)

A minor note, you might want to switch to COPERNICUS/S2_SR_HARMONIZED:

After 2022-01-25, Sentinel-2 scenes with PROCESSING_BASELINE '04.00' or above have their DN (value) range shifted by 1000. The HARMONIZED collection shifts data in newer scenes to be in the same range as in older scenes.

0

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.