davemlz / eemont

A python package that extends Google Earth Engine.
https://eemont.readthedocs.io/
MIT License
417 stars 69 forks source link

Handling invalid methods for ee.Image.maskClouds with Sentinel-2 #39

Closed aazuspan closed 3 years ago

aazuspan commented 3 years ago

Hi @davemlz, I came across a minor issue when cloud masking Sentinel-2 imagery: If you pass an invalid method to ee.Image.maskClouds or ee.ImageCollection.maskClouds (like accidentally using method=prob instead of method=cloud_prob) it throws an unhelpful error that's tough to debug. If it could raise an exception that warns you about the bad method and/or gives you a list of valid methods that would be awesome :)

There's also a line in the docstring that mentions using the method prob instead of cloud_prob that's a little confusing.

Recreating

Run:

# Use any invalid "method" with Sentinel-2
ee.Image("COPERNICUS/S2_SR/20170328T083601_20170328T084228_T35RNK").maskClouds(method="prob")

Raises:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
/tmp/ipykernel_10788/3049107977.py in <module>
----> 1 ee.Image("COPERNICUS/S2_SR/20170328T083601_20170328T084228_T35RNK").maskClouds(method="prob")

~/anaconda3/envs/gee/lib/python3.9/site-packages/eemont/image.py in maskClouds(self, method, prob, maskCirrus, maskShadows, scaledImage, dark, cloudDist, buffer, cdi)
    965     >>> S2 = ee.ImageCollection('COPERNICUS/S2_SR').first().maskClouds(prob = 75,buffer = 300,cdi = -0.5)
    966     """
--> 967     return _maskClouds(
    968         self,
    969         method,

~/anaconda3/envs/gee/lib/python3.9/site-packages/eemont/common.py in _maskClouds(self, method, prob, maskCirrus, maskShadows, scaledImage, dark, cloudDist, buffer, cdi)
    967     else:
    968         if isinstance(self, ee.image.Image):
--> 969             masked = lookup[platformDict["platform"]](self)
    970         elif isinstance(self, ee.imagecollection.ImageCollection):
    971             if platformDict["platform"] == "COPERNICUS/S2_SR":

~/anaconda3/envs/gee/lib/python3.9/site-packages/eemont/common.py in S2(args)
    797                 S2Masked = CDI(S2Masked)
    798             if maskShadows:
--> 799                 S2Masked = get_shadows(S2Masked)
    800             S2Masked = apply_mask(clean_dilate(S2Masked))
    801         elif isinstance(self, ee.imagecollection.ImageCollection):

UnboundLocalError: local variable 'S2Masked' referenced before assignment
davemlz commented 3 years ago

Hi, @aazuspan!

Thank you for pointing this out, I'll work on it! :rocket:

davemlz commented 3 years ago

I corrected it!

It will be available in the next version or now in the dev version :)

Thank you, @aazuspan!