I am very new to Google Earth Engine. I manipulated this code to my use from my class mini project. I am trying to export a date associated with the image along side the ET value after I performer a Reducer.mean() for a every feature. For now, I have accomplished this by adding a separate band for Year, Month and Day for each image. But I feel this is a crude way of performing this task. Ideally, I would like to export the Date as a string in a single column for each image. So I manipulated my code a little bit.
In order to accomplish this I added line 107 and 108 where I get the date of each image and then add that as a dictionary with name "date_as_string". Having done this on loop, I call this dictionary later while exporting (line 153). But this give me an Error (given below). I am not sure what I am doing wrong.
If my approach is wrong, is there a efficient way to export the date of each image as string in separate column?
Also, what is the best way to preview my analysis data I want to export before I export to CSV? Any suggestions regarding this? Following are the relevant information:
Asset: https://code.earthengine.google.com/?asset=users/memugal33/studyarea__pnw
Code link: https://code.earthengine.google.com/56e46e8769ab2e05183079e8ffa58df8
Error I get:
Error in map(ID=0): Image.date: Image '0' does not have a 'system:time_start' property.
Code:
var counties = ee.FeatureCollection("users/memugal33/studyarea__pnw"),
mod_ET = ee.ImageCollection("MODIS/006/MOD16A2");
var cnties = ee.FeatureCollection(counties);
//print(cnties)
var start_date = '2001-01-01';
var end_date = '2020-12-31';
////////////////////////////////////////////////
/// add Date to an image
function addDate_to_image(image){
var d = (image.date().getRelative('day','month'))
var d2 = ee.Image.constant(d).uint16().rename('day')
var m = (image.date().getRelative('month','year'))
var m2 = ee.Image.constant(m).uint16().rename('month')
var y = image.date().format('YYYY')
y = ee.Number.parse(y)
var y2 = ee.Image.constant(y).uint16().rename('year')
return image.addBands([d2,m2, y2]); /// The day, month and year is added as new bands
}
////////////////////////
///
/// add Date to an imageCollection/Time Series by using map(.) function
/// to recursively call the function addDate_to_image(.) defined above
//
//
function addDate_to_collection(collec){
var C = collec.map(addDate_to_image);
return C;
}
function extract_ET(a_feature){
var geom = a_feature.geometry();
var newDict = {'original_polygon_1': geom};
var imageC = mod_ET.filterDate(start_date, end_date)
.select('ET')
.filterBounds(geom)
// Clip(.) function is clipping the boundary of the given field.
.map(function(image){return image.clip(geom)})
// .filter('CLOUDY_PIXEL_PERCENTAGE < 90')
.sort('system:time_start', true);
imageC = addDate_to_collection(imageC);
imageC = imageC.map(function(im){return(im.set(newDict))});
imageC = imageC.set({ 'original_polygon': geom,
'cnty_feat':a_feature});
return imageC;
}
//
// The function below (mosaic_and_reduce_IC_mean(.)) stiches
// the tiles that cover a polygon to create a complete image of a field
// and then reduces (i.e. takes averages of all) ET pixels values into one value for
// the field.
//
function mosaic_and_reduce_IC_mean(an_IC){
an_IC = ee.ImageCollection(an_IC);
var reduction_geometry = ee.Feature(ee.Geometry(an_IC.get('original_polygon')));
var WSDA = an_IC.get('cnty_feat');
var start_date_DateType = ee.Date(start_date);
var end_date_DateType = ee.Date(end_date);
//######**************************************
// Difference in days between start and end_date
var diff = end_date_DateType.difference(start_date_DateType, 'day');
// Make a list of all dates
var range = ee.List.sequence(0, diff.subtract(1)).map(function(day){
return start_date_DateType.advance(day,'day')});
// Funtion for iteraton over the range of dates
function day_mosaics(date, newlist) {
// Cast
date = ee.Date(date);
newlist = ee.List(newlist);
// Filter an_IC between date and the next day
var filtered = an_IC.filterDate(date, date.advance(1, 'day'));
// Make the mosaic
var image = ee.Image(filtered.mosaic());
// Add the mosaic to a list only if the an_IC has images
return ee.List(ee.Algorithms.If(filtered.size(), newlist.add(image), newlist));
}
// Iterate over the range to make a new list, and then cast the list to an imagecollection
var newcol = ee.ImageCollection(ee.List(range.iterate(day_mosaics, ee.List([]))));
//print("newcol 1", newcol);
//######**************************************
var reduced = newcol.map(function(image){
var means = image.reduceRegions({
collection:reduction_geometry,
reducer:ee.Reducer.mean(),
scale: 10 //Does this scale make any difference???
});
//// This two lines is intended to add Date as string in the image property //////
///The idea is to name the dictionary name "date_as_string" and select
///"date_as_string" in the selectors while exporting to CSV
means = means.map(function(f){return f.set({date_as_string: image.date().format("yyyy/MM/DD")})})
return means.copyProperties(image);
////////////////////////////////////////////
//return means;
}
).flatten();
reduced = reduced.set({ 'original_polygon': reduction_geometry,
'cnties':WSDA
});
WSDA = ee.Feature(WSDA);
WSDA = WSDA.toDictionary();
// var newDict = {'WSDA':WSDA};
reduced = reduced.map(function(im){return(im.set(WSDA))});
return(reduced);
}
var Et_img_coll = cnties.map(extract_ET);
//print(Et_img_coll)
var allcnties = Et_img_coll.map(mosaic_and_reduce_IC_mean);
///////Visualizing For Preview purpose/////////
var allcnt = allcnties.first()
print(allcnt)
// var ET_chart = ui.Chart.feature.byFeature(allcnt, "ET")
// .setOptions({title: "ET MEAN",
// pointSize: 3});
// print(ET_chart);
////////////////////////////////////
var yourFileName = "ET_Final";
var FolderName = "rem_sens";
print(yourFileName)
Export.table.toDrive({
collection: allcnties.flatten(),
description: yourFileName,
folder: FolderName,
fileNamePrefix: yourFileName,
fileFormat: 'CSV',
// Provide the name of variables of interst to export.
// Some of these names come from shapefile that we have added to imagecollection
//
///Date_as_string is also selected
selectors:["JURISDIC_2","name","day","month","year", "ET", "date_as_string"]
});