local variable 'r' referenced before assignment in ee_export_image_collection
camjay99 opened this issue · 2 comments
Environment Information
Wed Feb 08 08:39:21 2023 EST
OS | Linux | CPU(s) | 40 | Machine | x86_64
Architecture | 64bit | Environment | Jupyter
Python 3.9.7 (default, Sep 16 2021, 13:09:58) [GCC 7.5.0]
geemap | 0.19.6 | ee | 0.1.338 | ipyleaflet | 0.17.2
folium | 0.14.0 | jupyterlab | 3.5.3 | notebook | 6.5.2
ipyevents | 2.0.1 | | | |
Intel(R) oneAPI Math Kernel Library Version 2021.4-Product Build 20210904 for Intel(R) 64 architecture applications
Description
I am tried to download imagery that was preprocessed on Google Earth Engine using ee_export_image_collection, but I am occasionally running into an 'error' where the function stops downloading and prints the following message:
Exporting 7/323: 20190203T160459_20190203T160859_T18TUN.tif
Generating URL ...
Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/8d961ce67171185b859b027bd6cfd8c4-12ef7e4cf535293448fb4a6299ddc8b8:getPixels
Please wait ...
An error occurred while downloading.
local variable 'r' referenced before assignment
I am not sure what 'r' is, but it is not a part of my script.
What I Did
# Cloud masking algorithm for Sentinel-2 clouds
def maskS2clouds(image):
qa = image.select('QA60')
neighbor_size = 30 # 30 pixels or 300 meters
quality_threshold = 0.05 # above 5% of cloud or shadow would be masked
# Bits 10 and 11 are clouds and cirrus, respectively.
cloudBitMask = 1 << 10
cirrusBitMask = 1 << 11
# Both flags should be set to zero, indicating clear conditions.Both flags should be set to zero, indicating clear conditions.
mask = qa.bitwiseAnd(cloudBitMask).eq(0) \
.And(qa.bitwiseAnd(cirrusBitMask).eq(0))
"""
* Account for shadow effect and use neighborhood average
"""
# calculate the neighborhood average (NA) fraction of cloud (SCL 8-10)
cldPrbNA = image.select('SCL').gte(8).And(image.select('SCL').lte(10)) \
.reduceNeighborhood(**{
'reducer': ee.Reducer.mean(),
'kernel': ee.Kernel.circle(neighbor_size)
}).rename('cldPrbNA')
# calculate the neighborhood average (NA) fraction of shadow (SCL 3)
cldShadowNA = image.select('SCL').eq(3) \
.reduceNeighborhood(**{
'reducer': ee.Reducer.mean(),
'kernel': ee.Kernel.circle(neighbor_size)
}).rename('cldShadowNA')
# the NA fraction of clouds and shadow
cldTotalNA = cldPrbNA.add(cldShadowNA)
# Update the mask, cldTotalNA should be lower than quality_threshold defined above
mask = mask.And(cldTotalNA.lt(quality_threshold))
return image.updateMask(mask).divide(10_000) \
.addBands(cldPrbNA).addBands(cldShadowNA) \
.copyProperties(image, ['system:time_start'])
# Calculate EVI for the scene
def addVegetationIndices(image):
EVI = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED + 7.5 * BLUE + 1))',
{
'NIR': image.select('B8'),
'RED': image.select('B4'),
'BLUE': image.select('B2')
}).rename('EVI')
# Return the masked image with NIRv and EVI bands.
return image.addBands(EVI)
##################################################################
# Specify download region and prepare scene
##################################################################
region = ee.Geometry.Polygon(
[float(args.left), float(args.top),
float(args.left), float(args.bottom),
float(args.right), float(args.top),
float(args.right), float(args.bottom)], None, False)
# Setup Sentinel-2 image collection over study region with low(ish) cloud cover
site_images = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") \
.filterBounds(region) \
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 80)) \
.map(maskS2clouds).map(addVegetationIndices) \
.map(lambda x: x.clip(region)).select(['EVI'])
# Download data
geemap.ee_export_image_collection(site_images, out_dir='.')
It is likely that some results images are empty. You need to make sure every image contains some valid pixels. Otherwise, the export might fail. Try geemap.download_ee_image_collection()
to see if you get the same error.
https://geemap.org/common/#geemap.common.download_ee_image_collection
This appeared to be the issue. I added some additional code to remove images with no bands, and everything seems to be working now. Thank you!