0

I am trying to download time series data of AVI using Sentinel 2 also I want to make a time series chart?

This is my code:

var batch = require('users/fitoprincipe/geetools:batch')
var avi = function(image) {
  return image.select().addBands(image.expression(
                          'NIR * ((1 - RED) * (NIR - RED))**(1/3)', {
                        'NIR': image.select('B8'),
                        'RED': image.select('B4')
                    }).float()).rename('AVI')};

// Import all available Sentinel 2 surface reflectance imagery as an ee.ImageCollection
// and filter for images within your study area
var S2_SR = ee.ImageCollection('COPERNICUS/S2_SR').filter(ee.Filter.date('2015-10-01', '2021-12-31')).select([
      // Make this list whichever bands you want to view or analyze
      'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8','B8A','B9','B11','B12'
    ]).map(avi)
var clippedSAR= S2_SR.map(function(im){ 
  return im.clip(geometry);
});     
// Center your map at your study area
var visParams = {
  min: 0,
  max: 20000,
  gamma: 1.2,
  bands: ['B4', 'B8', 'B2'],
};


Map.addLayer(geometry);
Map.centerObject(geometry);
var chart = Chart.image.series(avi, geometry, ee.Reducer.mean(),1000, 'system:time_start').setOptions({
          title: 'AVI',
          vAxis: {title: 'NIR & RED'},
});
print(chart)
batch.Download.ImageCollection.toDrive(avi, 'work',
                {scale: 30,
                 region: geometry,
                 type: 'float',
                  description: 'Landsat8',
                  maxPixels: 1e13,
                  fileFormat: 'GeoTIFF',
                })
1

1 Answer 1

0

You're missing a quite important step. You never actually have any imagery to work with, and simply pass the avi function instead of an ee.ImageCollection to the chart. The below adds a Sentinel 2 SR image collection, calculates AVI and removes clouds in each image within a date range, for some geometry. Note about the image collection from the EE data-set catalog:

WARNING: ESA did not produce L2 data for all L1 assets, and earlier L2 coverage is not global.

If you want your time-series to go further back, you might want to switch to the COPERNICUS/S2_HARMONIZED collection instead.

var avi = function(image) {
  image = image.divide(10000) // Rescale the bands from 0-10000 to 0-1, which the expression expects
  return image.select().addBands(image.expression('NIR * ((1 - RED) * (NIR - RED))**(1/3)', {
    'NIR': image.select('B8'),
    'RED': image.select('B4')
  }).float()).rename('AVI')
}

var geometry = ee.Geometry(Map.getBounds(true))
var startDate = '2019-01-01'
var endDate = '2022-04-01'

var cloudThreshold = 30
var filter = ee.Filter.and(
  ee.Filter.bounds(geometry),
  ee.Filter.date(startDate, endDate)
)
var s2 = ee.ImageCollection(
  ee.Join.saveFirst('cloudProbability').apply({
    primary: ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED').filter(filter),
    secondary: ee.ImageCollection('COPERNICUS/S2_CLOUD_PROBABILITY').filter(filter),
    condition: ee.Filter.equals({
      leftField: 'system:index',
      rightField: 'system:index'
    })
  })
).map(function(image) {
  var cloudFree = ee.Image(image.get('cloudProbability')).lt(cloudThreshold)
  return image
    .addBands(avi(image))
    .updateMask(cloudFree)
})

https://code.earthengine.google.com/3a6b71fb3e34d32a5336fdbbb01e7706

Your Answer

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

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