Below is my code to build a time series collection of all ETM+ and OLI Landsat images intersecting my region of interest. Now I want to build an ImageCollection that represents the mean or median of all images within a year for all years. Can you provide an example of how to build intra-annual composites in Earth Engine.
var coefficients = {
itcps: ee.Image.constant([0.0003, 0.0088, 0.0061, 0.0412, 0.0254, 0.0172]).multiply(10000),
slopes: ee.Image.constant([0.8474, 0.8483, 0.9047, 0.8462, 0.8937, 0.9071]),
};
// Define function to get and rename bands of interest from OLI.
function renameOLI(img) {
return img.select(
['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'pixel_qa'],
['Blue', 'Green', 'Red', 'NIR', 'SWIR1', 'SWIR2', 'pixel_qa']
);
}
// Define function to get and rename bands of interest from ETM+.
function renameETM(img) {
return img.select(
['B1', 'B2', 'B3', 'B4', 'B5', 'B7', 'pixel_qa'],
['Blue', 'Green', 'Red', 'NIR', 'SWIR1', 'SWIR2', 'pixel_qa']
);
}
// Define function to apply harmonization transformation.
function etm2oli(img) {
return img.select(['Blue', 'Green', 'Red', 'NIR', 'SWIR1', 'SWIR2'])
.multiply(coefficients.slopes)
.add(coefficients.itcps)
.round()
.toShort()
.addBands(img.select('pixel_qa')
.copyProperties(img, ['system:time_start'])
);
}
// Define function to mask out clouds and cloud shadows.
function fmask(img) {
var cloudShadowBitMask = 1 << 3;
var cloudsBitMask = 1 << 5;
var qa = img.select('pixel_qa');
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0));
return img.updateMask(mask);
}
// Define function to calculate NDVI.
function calcNdvi(img) {
return img.normalizedDifference(['NIR', 'Red']).rename('NDVI');
}
// Define function to prepare OLI images.
function prepOLI(img) {
var orig = img;
img = renameOLI(img);
img = fmask(img);
img = calcNdvi(img);
return ee.Image(img.copyProperties(orig, orig.propertyNames()));
}
// Define function to prepare ETM+ images.
function prepETM(img) {
var orig = img;
img = renameETM(img);
img = fmask(img);
img = etm2oli(img);
img = calcNdvi(img);
return ee.Image(img.copyProperties(orig, orig.propertyNames()));
}
// Define a point on the study area
var aoi = ee.Geometry.Point([34.0049766165165,-6.137332241818918]);
// Define AOI on the map.
Map.centerObject(aoi, 10);
Map.addLayer(aoi, {color: 'f8766d'}, 'AOI');
Map.setOptions('HYBRID');
// Get Landsat surface reflectance collections for OLI, ETM+ and TM sensors.
var oliCol = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR');
var etmCol= ee.ImageCollection('LANDSAT/LE07/C01/T1_SR');
// Define a collection filter.
var colFilter = ee.Filter.and(
ee.Filter.bounds(aoi),
ee.Filter.calendarRange(1, 365, 'day_of_year'),
ee.Filter.lt('CLOUD_COVER', 50),
ee.Filter.lt('GEOMETRIC_RMSE_MODEL', 10),
ee.Filter.or(
ee.Filter.eq('IMAGE_QUALITY', 9),
ee.Filter.eq('IMAGE_QUALITY_OLI', 9)
)
);
// Filter collections and prepare them for merging.
oliCol = oliCol.filter(colFilter).map(prepOLI);
etmCol= etmCol.filter(colFilter).map(prepETM);
// Merge the collections.
var col = oliCol
.merge(etmCol);
Export.image.toAsset({image:col,
description:"collection",
assetId:"rungwa",
region:aoi.bounds(),
scale:10,
maxPixels:1e13});