You can do this with a join.
var collection = ee.ImageCollection("LANDSAT/LE07/C02/T1_L2")
.filterDate('2000-01-01', '2001-01-01') // End date is exclusive
.filter(ee.Filter.metadata('WRS_PATH', "greater_than", 171))
.filter(ee.Filter.metadata('WRS_PATH', "less_than", 206))
.filter(ee.Filter.metadata('WRS_ROW', "greater_than", 27))
.filter(ee.Filter.metadata('WRS_ROW', "less_than", 37))
var pathRowCollection = ee.ImageCollection(ee.Join.saveAll('images')
// Join images with same row/path together
.apply({
// Collection with one image for each row/path
primary: collection
.distinct(['WRS_PATH', 'WRS_ROW']),
secondary: collection,
condition: ee.Filter.and(
ee.Filter.equals({leftField: 'WRS_PATH', rightField: 'WRS_PATH'}),
ee.Filter.equals({leftField: 'WRS_ROW', rightField: 'WRS_ROW'})
)
})
.map(function (image) {
// All images for this row/path will be in the `images` property of the image
return ee.ImageCollection(ee.List(image.get('images')))
// Remove clouds in every image for this row/path
.map(maskClouds)
// Pick some approach to reduce the collection into a single image
// median(), mean(), mosaic(), qualityMosaic(), reduce(ee.Reducer)
.median()
// Rescaling to 0-10000, just out of habit
.multiply(0.0000275)
.subtract(0.2)
.multiply(10000)
.int16()
// Include row/path for the composite
.copyProperties(image, ['WRS_PATH', 'WRS_PATH'])
})
)
Map.addLayer(pathRowCollection.mosaic(), {bands: 'SR_B3,SR_B2,SR_B1', min: 0, max: 3000})
function maskClouds(image) {
var cloudFree = bitwiseExtract(image.select('QA_PIXEL'), 0, 5).eq(0)
return image
.updateMask(cloudFree)
}
function bitwiseExtract(value, fromBit, toBit) {
if (toBit === undefined)
toBit = fromBit
var maskSize = ee.Number(1).add(toBit).subtract(fromBit)
var mask = ee.Number(1).leftShift(maskSize).subtract(1)
return value.rightShift(fromBit).bitwiseAnd(mask)
}
https://code.earthengine.google.com/9f02891f699cab48345e0e99a4960d8d