I have a JavaScript on GEE's code editor that allows me to download a collection of Landsat images with a cloud filter directly applied (meaning that I download only the images with less than a certain percentage of cloud cover).
I would like to translate the code to Python but I don't know how to deal with the custom cloud function:
In JavaScript, the cloud condition (less than x% of clouds to be in the collection) is called without any variable, directly in the ee.collection() command. In Python, I don't know how to make it work because there is no variable yet, it seems that I have to download the image first, and then apply the cloud percentage calculation (and then dump my image if it fails to meet the requirements).
Is there a way on Python to download Landsat images already filtered depending on their cloud cover ?
My JavaScript code is:
var geometry: Polygon, 4 vertices
type:
Polygon
coordinates:
List (1 element)
0:
List (5 elements)
0:
[-141.0073440277409,59.69885769939182]
1:
[-140.12569119570966,59.69885769939182]
2:
[-140.12569119570966,60.16125006608516]
3:
[-141.0073440277409,60.16125006608516]
4:
[-141.0073440277409,59.69885769939182]
0:
-141.0073440277409
1:
59.69885769939182
geodesic:
false
var cloudMaskL457 = function(image) {
var qa = image.select('pixel_qa');
// If the cloud bit (5) is set and the cloud confidence (7) is high
// or the cloud shadow bit is set (3), then it's a bad pixel.
var cloud = qa.bitwiseAnd(1 << 5)
.and(qa.bitwiseAnd(1 << 7))
.or(qa.bitwiseAnd(1 << 3));
// Remove edge pixels that don't occur in all bands
var mask2 = image.mask().reduce(ee.Reducer.min());
return image.updateMask(cloud.not()).updateMask(mask2);
};
var l5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
.filterDate('1984-01-01', '2012-05-05')
.map(cloudMaskL457)
.filterBounds(aoi)
.map(function(a){
return a.set('year', ee.Image(a).date().get('year'))
})
Ps: I already use code for Sentinel-2 downloading that has a specific built-in function. I failed to find something similar for Landsat.
S2_collection = ee.ImageCollection('COPERNICUS/S2').filterBounds(polytest).filter(ee.Filter.lessThanOrEquals('CLOUDY_PIXEL_PERCENTAGE', 10)).filter(ee.Filter.date(sdate, edate))