In order to study the growth pattern of crops, I tried to smoothen the Sentinel-1 time series graph using the following code but got the error.
Error:
ImageCollection (Error)
ImageCollection.reduce: Error in map(ID=null):
Image.select: Can't add a band named 'VV' to image because a band with this name already exists. Existing bands: [VV].
Code:
Map.addLayer(Bhagwanpur, {}, 'Bhagwanpur', false)
Map.centerObject(Bhagwanpur, 12)
Map.addLayer(GT, {}, 'March GT', false)
var CreateBuffer = function(feature, buffer_size) {
return feature.buffer(buffer_size)
}
var buffer_size= 20
var GT_Buffer = GT.map(function(ft){return CreateBuffer(ft, buffer_size)});
Map.addLayer(GT_Buffer, {}, 'GT_Buffer')
Map.centerObject(GT_Buffer, 15)
print('GT_Buffer',GT_Buffer)
/*----------------------------------------------------------------------------------------------------------*/
// Importing the Sentinel-1 Images:
var Sen= ee.ImageCollection("COPERNICUS/S1_GRD")
.filterDate('2021-05-01', '2022-07-15')
.filterBounds(Bhagwanpur)
.select (['VV','VH'])
.filter(ee.Filter.eq('instrumentMode', 'IW'))
print('Sen',Sen)
var addVVbyVH = function(image) {
var vv = image.select('VV');
var vh = image.select('VH');
var VVbyVH = (vv).divide(vh).rename('VVbyVH');
return image.addBands(VVbyVH);
};
var Sen = Sen.map(addVVbyVH);
print('Sen',Sen)
/*-------------------------------------------------------------------------------------------------------------*/
//Visualization of Images:
Map.addLayer(Sen.first().clip(Bhagwanpur), {'min': [-25, -20, 0], 'max': [5, -10, 5]}, 'S1 RGB', false)
/*------------------------------------------Smoothen the Sentinel-1 graph------------------------------------------------------------------*/
function smoother(t){
// helper function to apply linear regression equation
function applyFit(img){
return img.select('VV').multiply(fit.select('scale')).add(fit.select('offset'))
.set('system:time_start',img.get('system:time_start')).rename('VV');
}
t = ee.Date(t);
var window = Sen.filterDate(t.advance(-windowSize,'month'),t.advance(windowSize,'month'));
var fit = window.select(['VV','VV'])
.reduce(ee.Reducer.linearFit());
return window.map(applyFit).toList(5);
}
// function to reduce time stacked linear regression results
// requires that a variable 'fitIC' exists from the smooter function
function reduceFits(t){
t = ee.Date(t);
return fitIC.filterDate(t.advance(-windowSize,'day'),t.advance(windowSize,'day'))
.mean().set('system:time_start',t.millis()).rename('VV');
}
var dates = Sen.aggregate_array('system:time_start');
var windowSize = 1; //days on either sides
var fitIC = ee.ImageCollection(dates.map(smoother).flatten());
var smoothed = ee.ImageCollection(dates.map(reduceFits));
// print('smoothed',smoothed)
// var smooth_mos = smoothed.mosaic()
// merge original and smoothed data into one image collection for plotting
// var joined = ee.ImageCollection(smoothed.select(['vv'],['smoothed'])
// .merge(Sen.select(['vv'],['original'])));
/*------------------------------------// Plot the Graph----------------------------------------*/
print(smoothed);
var scale = smoothed.first().projection().nominalScale();
print("scale", scale);
var chartvv = ui.Chart.image.seriesByRegion(
Sen,
GT_Buffer.filter(ee.Filter.eq('Field_ID', 5)),
ee.Reducer.mean(),
'VV',
10)
var chartv2 = chartvv.setSeriesNames(['VV'])
.setChartType('LineChart')
.setOptions({
title: 'Sentinel-1 time series',
hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true}},
vAxis: {
title: 'dB values',
titleTextStyle: {italic: false, bold: true}
},
lineWidth: 5,
colors: ['#fc0303', '#17fc03','#0324fc'],
curveType: 'function'
});
print(chartv2)
Code link: https://code.earthengine.google.co.in/6f7665b85f5fe82319ebdb9e04141c74