I am trying to download set of MODIS LIA 8day products to my computer from Google Earth Engine. However, i have difficult time to drop masked images before downloading. Basically my code is taking set of aoi geometry from a file and then it build image collection and mask before create download url for every image in the collection.
# time window
start_dt = '2000-05-01'
end_dt = '2023-12-31'
# cloud mask for MODIS LAI
def build_cloud_mask(image):
# select the qc band
qc_lai = image.select(['FparLai_QC'])
# Define cloud bits (3-4 and 01 and 10)
cloud_bits = qc_lai.bitwiseAnd(0b00011000).rightShift(3).add(qc_lai.bitwiseAnd(0b00000011).rightShift(0))
# mask
cloud_mask = cloud_bits.eq(1).Or(cloud_bits.eq(2))
# Apply the mask to the image
return image.updateMask(cloud_mask.Not())
for index, row in ads_sample.iterrows():
# Extract corner coordinates
corners = list(row['geometry'].exterior.coords)
new_polygon = Polygon(corners)
# create ee geometry
#aoi = ee.Geometry.Polygon(new_polygon)
aoi = ee.Geometry.Polygon([list(coord) for coord in corners])
modis_collection = ee.ImageCollection("MODIS/061/MOD15A2H").filterBounds(aoi).filterDate(start_dt, end_dt)
# summer collection
summer_collection = modis_collection.filter(ee.Filter.calendarRange(6, 7, 'month'))
# masking the collection
modis_lai_masked = summer_collection.map(build_cloud_mask)
# filter images with any clouds
# images to a list
image_list = modis_lai_masked .toList(modis_lai_masked .size())
# take image list and gothrough each of them for download
# create folder for images
ads_id = row['id']
folder_path = './output/images/'+str(ads_id)
print(folder_path)
# create folders
try:
os.makedirs(folder_path)
except:
print('Folder in place already')
# number of images in collection for aoi
num_images = modis_lai_masked.size().getInfo()
for item in range(0, num_images):
# take images
filter_image = ee.Image(image_list.get(item))
# take band
filter_band = filter_image.select(['Lai_500m', 'FparLai_QC'])
# clip to aoi
filter_band = filter_band.clip(aoi).unmask()
# image name
im_name = str(filter_band.get('system:index').getInfo())
#print(im_name)
# take url
try:
url = filter_band.getDownloadURL({'scale':500,
'filePerBand':False,
'region':aoi,
'crs':'EPSG:26911',
'maxPixels': 1e13})
print(url)
I tried counting number of masked images, but it turns to be all images are cloudy, which is not true for these set of images.