// Function to mask clouds based on the pixel_qa band of Landsat 5
function maskL5clouds(image) {
var qa = image.select('pixel_qa')
// Bits 5, 7, 3 are clouds, Cloud, Cloud Confidence and Clouds Shadow, respectively.
var cloudBitMask = 1 << 5
var cloudconfBitsMask = 1 << 7
var cloudShadowBitMask = 1 << 3
//clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(
qa.bitwiseAnd(cloudconfBitsMask).eq(0)).and(
qa.bitwiseAnd(cloudShadowBitMask).eq(0))
// Return the masked and scaled data, without the QA bands.
// Devuelve los datos enmascarados y escalados, sin las bandas de control de calidad.
return image.updateMask(mask).divide(10000)
.select("B.*")
.copyProperties(image, ["system:time_start"])
}
// Variable Global.
var countries = ee.FeatureCollection('USDOS/LSIB/2013')
// use Two-letter FIPS country code Perú = PE
var PE = countries.filter(ee.Filter.eq('cc', 'PE'));
// Image Collection Jan 1, 1984 - May 5, 2012
var l5 = ee.ImageCollection("LANDSAT/LT05/C01/T1_SR")
.filterBounds(PE)
.filterDate('2000-01-01', '2005-01-01')
.filter(ee.Filter.lt('CLOUD_COVER', 20))
.map(maskL5clouds)
.median()
// Show.
Map.centerObject(PE)
Map.addLayer(l5, {bands: 'B5,B4,B3', min:0, max:0.5},'Landsat 5')