Please find attached basic architecture for my data-extraction procedure of NDVI values per pixel, for all images taken in a certain ROI in given dates:
The code below represent the architecture on a small scale of date-range and applied only for SEN2 images (to be done later - the same for SEN3 images):
//Dates of Interest
var start = ee.Date("2018-04-01");
var finish = ee.Date("2018-05-01");
///--------------------- SENTINEL2 Collection ---------------------------------------///
var sen2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate(start, finish)
.filterBounds(region)
.map(function(image){
return image
.clipToCollection(region)
.updateMask(image.select('QA60').not())
})
///--------------------- Unique list creation ---------------------------------------///
sen2 = sen2.map(function(image){
return image.set('simpleDateMillis', ee.Date(image.date().format('YYYY-MM-dd')).millis())
});
var listMillisSimple = ee.List(sen2.aggregate_array('simpleDateMillis'));
var uniqueDatesSimple = listMillisSimple.distinct();
var SEN2_day0 = uniqueDatesSimple.map(function(date){
return ee.Date(date);
});
print("SEN2 day0:",SEN2_day0);
//------------------------------ AOI ------------------------------//
//define all AOI
var field1 = ee.FeatureCollection(region.filterMetadata("system:index","equals","00000000000000000000"));
//-----------------------NDVI CALC-----------------------//
//Get the ndvi for the specific field and time
function get_ndvi_sen2(subRegion, image) {
var ndvi_s2 = image
.normalizedDifference(['B8', 'B4'])
.rename('ndvi_s2');
var ndvi_s2_Features = ee.FeatureCollection(ee.List(ndvi_s2
.reduceRegion({
reducer: ee.Reducer.toList(),
geometry: subRegion,
scale: 10
})
.get('ndvi_s2'))
.map(function (ndvi_s2) {
return ee.Feature(null, {ndvi_s2: ndvi_s2});
})
);
return ndvi_s2_Features;
}
//------------------ CSV Export ---------------------//
var sen2_disc, sen2_selc = 'ndvi_s2';
var sen2_folder = 'GOOD/SEN2'
function write_to_drive(col, disc, selc, folder, field){
print("Col: ",col, "Disc: ",disc, "Selc: ",selc, "Folder: ",folder, "Field: ",field, date);
Export.table.toDrive({
collection: col,
fileNamePrefix: disc+' '+field,
selectors: [selc],
folder: folder+'/'+field
})
}
var feat_per_img = function(sngl_img){
return get_ndvi_sen2(field1, sngl_img)
}
function write_to_drive_front(feat){
write_to_drive(feat, 'NDVI_Sen2', sen2_selc, sen2_folder, 'field1');
}
//------ test run for SEN2_day0 list -----//
var allImagesInRange = sen2.filter(ee.Filter.inList("system:time_start", SEN2_day0))
write_to_drive_front(ee.FeatureCollection(allImagesInRange.map(feat_per_img)));
I hope it is not too messy. As of now - the problem is that I get empty feature collection that needs to be later passed to the function that writes it to CSV file. So I get an empty CSV file.
My main question is - why doesn't the FeatureCollection pass on properly so the Export.table.toDrive could write it to CSV?
Another question is why isn't the code extracting as many CSV files as the number of dates on the list? I have mapped the image collection so the procedure of calculating and extracting ndvi values should be on every image of the ImageCollection.
Any ideas?