Skip to content

Commit

Permalink
Merge pull request #183 from njwilson23/bqa-mask
Browse files Browse the repository at this point in the history
Proposed cloud/ice detection fixes
  • Loading branch information
Scisco committed May 13, 2016
2 parents 87873fc + 85e1b26 commit 362d5b0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion landsat/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def google_storage(self, scene, path):

self.remote_file_exists(url)

self.output('Source: Google Storge', normal=True, arrow=True)
self.output('Source: Google Storage', normal=True, arrow=True)
return self.fetch(url, path)

def amazon_s3(self, scene, bands):
Expand Down
24 changes: 11 additions & 13 deletions landsat/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,22 @@ def _percent_cut(self, color, low, high):
return numpy.percentile(color[numpy.logical_and(color > 0, color < 65535)], (low, high))

def _calculate_cloud_ice_perc(self):
""" Return the percentage of pixels that are either cloud or snow with
high confidence (> 67%).
"""
self.output('Calculating cloud and snow coverage from QA band', normal=True, arrow=True)

a = rasterio.open(join(self.scene_path, self._get_full_filename('QA'))).read_band(1)

count = 0
snow = [56320, 39936, 31744, 28590, 26656, 23552]
cloud = [61440, 59424, 57344, 53248, 28672, 36896, 36864, 24576]

for item in cloud:
count += numpy.extract(a == item, a).size

for item in snow:
count += numpy.extract(a == item, a).size * 2

perc = numpy.true_divide(count, a.size) * 100
cloud_high_conf = int('1100000000000000', 2)
snow_high_conf = int('0000110000000000', 2)
fill_pixels = int('0000000000000001', 2)
cloud_mask = numpy.bitwise_and(a, cloud_high_conf) == cloud_high_conf
snow_mask = numpy.bitwise_and(a, snow_high_conf) == snow_high_conf
fill_mask = numpy.bitwise_and(a, fill_pixels) == fill_pixels

perc = numpy.true_divide(numpy.sum(cloud_mask | snow_mask),
a.size - numpy.sum(fill_mask)) * 100.0
self.output('cloud/snow coverage: %s' % round(perc, 2), indent=1, normal=True, color='green')

return perc

def _filename(self, name=None, suffix=None, prefix=None):
Expand Down

0 comments on commit 362d5b0

Please sign in to comment.