I would like to take the median of NDVI per month for each pixel, then for each field, reduce those medians to mean by for each region.
This question answers how to take medians per month. So, after I take the median of each pixel and I pass that to my other function to do spatial mosaic and clip, the output has nothing in it.
Here is my code. I try to take medians for each pixel per month
in the line that says print ("line 98", imageC);.
when I pass the result of extract_satellite_IC(.) to mosaic_and_reduce_IC_mean(.)
to mosaic and reduce by region, in line 160 (print ("reduced from 160, reduction function", reduced);) my data disappears.
I think I will be fine also with first mosaic and reduce by region, and then take median of the time series data over each month. So, I tried to use the function median_by_month(.) at line 161 instead. That does not works neither.
The answers in these other questions are slightly different from my question.
for example this is one of them.
var SF = ee.FeatureCollection(shapeFilem); // shapeFilem is a shapefile with several polygons in it located in PNW. They all are contained within the big_rectangle.
var outfile_name_prefix = 'L5_T1C2L2_rangeLand_';
var wstart_date = '2000-01-01'; // YYYY-MM-DD
var wend_date = '2001-01-01'; // YYYY-MM-DD
var start_year = 2000;
var end_year = 2001; //
var outfile_name = outfile_name_prefix + wstart_date + "_" + wend_date;
//////////////////////////////////////////////////////
///////
/////// Functions
///////
//////////////////////////////////////////////////////
//////
////// Median of each pixel across a month.
//////
function median_by_month(ICC){
var months = ee.List.sequence(1, 12);
var years = ee.List.sequence(start_year, end_year);
var byMonthYear = ee.ImageCollection.fromImages(
years.map(function(y) {
return months.map(function (m) {
return ICC.filter(ee.Filter.calendarRange(y, y, 'year'))
.filter(ee.Filter.calendarRange(m, m, 'month'))
.median()
.set('month', m).set('year', y);
});
}).flatten());
return byMonthYear;
}
var cloudMaskL578_C2L2 = function(image) { /// Function to clear clouds in Landsat-7
var qa = image.select('QA_PIXEL');
var cloud = qa.bitwiseAnd(1 << 3).and(qa.bitwiseAnd(1 << 9))
.or(qa.bitwiseAnd(1 << 4));
return image.updateMask(cloud.not());
};
/// add Date of image to an imageCollection
function add_system_start_time_image(image) {
image = image.addBands(image.metadata('system:time_start').rename("system_start_time"));
return image;
}
function add_system_start_time_collection(colss){
var c = colss.map(add_system_start_time_image);
return c;
}
/// NDVI - Landsat-5
function addNDVI_to_image(image) {
var ndvi = image.normalizedDifference(['SR_B4', 'SR_B3']).rename('NDVI');
return image.addBands(ndvi);
}
function add_NDVI_collection(image_IC){
var NDVI_IC = image_IC.map(addNDVI_to_image);
return NDVI_IC;
}
////////////////////////////////////////////////
///
/// scale the bands
///
function scale_the_bands(image){
var NIR = image.select('SR_B4').multiply(0.0000275).add(-0.2);
var red = image.select('SR_B3').multiply(0.0000275).add(-0.2);
var blue = image.select('SR_B1').multiply(0.0000275).add(-0.2);
return image.addBands(NIR, null, true)
.addBands(red, null, true)
.addBands(blue, null, true);
}
////////////////////////////////////////////////
///
/// Do the Job
///
function extract_satellite_IC(a_feature, start_date, end_date){
var geom = a_feature.geometry(); // a_feature is a feature collection
var newDict = {'original_polygon_1': geom};
var imageC = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2')
.filterDate(start_date, end_date)
.filterBounds(geom)
.map(function(image){return image.clip(geom)})
.sort('system:time_start', true);
// toss out cloudy pixels
imageC = imageC.map(cloudMaskL578_C2L2);
// scale the bands
imageC = imageC.map(scale_the_bands);
// imageC = imageC.map(clean_clouds_from_one_image_landsat);
imageC = add_NDVI_collection(imageC); // add NDVI as a band
imageC = add_system_start_time_collection(imageC);
// print ("line 98", imageC);
imageC=median_by_month(imageC);
// print ("100", imageC);
imageC=imageC.map(function(im){return(im.addBands(im.metadata('month')))});
imageC=imageC.map(function(im){return(im.addBands(im.metadata('year')))});
// print ("103", imageC);
// add original geometry to each image. We do not need to do this really:
imageC = imageC.map(function(im){return(im.set(newDict))});
// add original geometry and WSDA data as a feature to the collection
imageC = imageC.set({ 'original_polygon': geom});
// print ("109", imageC);
return imageC;
}
function mosaic_and_reduce_IC_mean(an_IC,a_feature, start_date, end_date){
an_IC = ee.ImageCollection(an_IC);
print ("line 115", an_IC);
var reduction_geometry = a_feature;
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'));
var image = ee.Image(filtered.mosaic()); // Make the 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([]))));
//######**************************************
var reduced = newcol.map(function(image){
return image.reduceRegions({
collection:reduction_geometry,
reducer:ee.Reducer.mean(),
scale: 10
});
}
).flatten();
reduced = reduced.set({ 'original_polygon': reduction_geometry});
reduced=ee.ImageCollection(reduced);
// print ("lin 159", reduced);
print ("reduced from 160, reduction function", reduced);
// reduced=median_by_month(reduced);
// reduced=reduced.map(function(im){return(im.addBands(im.metadata('month')))});
// reduced=reduced.map(function(im){return(im.addBands(im.metadata('year')))});
// print ("reduced from 164", reduced);
return(reduced);
}
// remove geometry on each feature before printing or exporting
var myproperties=function(feature){
feature=ee.Feature(feature).setGeometry(null);
return feature;
};
//////////////////////////////////////////////////////
///////
/////// Body
///////
//////////////////////////////////////////////////////
var xmin = -123.5;
var xmax = -111.3;
var xmed1 = (xmin + xmax) / 2.0;
var ymax = 49.0;
var ymin = 42.2;
var ymed1 = (ymin + ymax) / 2.0;
var box1 = ee.Geometry.Polygon([[xmin, ymed1], [xmin, ymax], [xmed1, ymax], [xmed1, ymed1], [xmin, ymed1]]);
var box2 = ee.Geometry.Polygon([[xmed1, ymed1], [xmed1, ymax], [xmax, ymax], [xmax, ymed1], [xmed1, ymed1]]);
var box3 = ee.Geometry.Polygon([[xmin, ymin], [xmin, ymed1], [xmed1, ymed1], [xmed1, ymin], [xmin, ymin]]);
var box4 = ee.Geometry.Polygon([[xmed1, ymin], [xmed1, ymed1], [xmax, ymed1], [xmax, ymin], [xmed1, ymin]]);
var big_rectangle = [box1, box2, box3, box4];
var SF_regions = ee.FeatureCollection(big_rectangle);
var reduction_geometry = ee.FeatureCollection(SF);
Map.addLayer(SF_regions, {color: 'red'}, 'WA');
Map.addLayer(SF, {color: 'blue'}, 'SF');
var ymed = (ymin + ymax)/2.0;
Map.setCenter(xmed1, ymed, 2);
var imageC = extract_satellite_IC(SF_regions, wstart_date, wend_date);
var reduced = mosaic_and_reduce_IC_mean(imageC, reduction_geometry, wstart_date, wend_date);
Export.table.toDrive({
collection: reduced,
description:outfile_name,
folder: "savingFolder",
fileNamePrefix: outfile_name,
fileFormat: 'CSV',
selectors:["long", 'lat', 'NDVI', "system_start_time"]
});