Skip to content

Commit

Permalink
Merge pull request #100 from developmentseed/pansharpen
Browse files Browse the repository at this point in the history
Pansharpen fixes
  • Loading branch information
Scisco committed Sep 30, 2015
2 parents 6c92e50 + 4dcd3c2 commit b9584d1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
12 changes: 9 additions & 3 deletions landsat/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,21 @@ def download(self, scenes, bands=None):

for scene in scenes:
# If bands are provided the image is from 2015 or later use Amazon
self.scene_interpreter(scene)

if (bands and int(scene[12]) > 4):
if isinstance(bands, list):
# Create a folder to download the specific bands into
path = check_create_folder(join(self.download_dir, scene))
try:
# Always grab MTL.txt if bands are specified
bands_plus = bands
bands_plus.append('MTL')
for band in bands_plus:
if 'BQA' not in bands:
bands.append('QA')

if 'MTL' not in bands:
bands.append('MTL')

for band in bands:
self.amazon_s3(scene, band, path)
output[scene] = 'aws'
except RemoteFileDoesntExist:
Expand Down
17 changes: 12 additions & 5 deletions landsat/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def _unzip(self, src, dst, scene, force_unzip=False):
try:
# check if file is already unzipped, skip
if isdir(dst) and not force_unzip:
self.output("%s is already unzipped." % scene, normal=True, arrow=True)
self.output('%s is already unzipped.' % scene, normal=True, color='green', indent=1)
return
else:
tar = tarfile.open(src, 'r')
Expand Down Expand Up @@ -253,8 +253,6 @@ def _write_to_file(self, new_bands, suffix=None, **kwargs):
return output_file

def _color_correction(self, band, band_id, low, coverage):
band = band.astype(numpy.uint16)

self.output("Color correcting band %s" % band_id, normal=True, color='green', indent=1)
p_low, cloud_cut_low = self._percent_cut(band, low, 100 - (coverage * 3 / 4))
temp = numpy.zeros(numpy.shape(band), dtype=numpy.uint16)
Expand Down Expand Up @@ -352,7 +350,7 @@ def run(self):
(String) the path to the processed image
"""

self.output("Image processing started for bands %s" % "-".join(map(str, self.bands)), normal=True)
self.output('Image processing started for bands %s' % '-'.join(map(str, self.bands)), normal=True, arrow=True)

bands = self._read_bands()
image_data = self._get_image_data()
Expand Down Expand Up @@ -397,7 +395,8 @@ def run(self):
(String) the path to the processed image
"""

self.output("PanSharpened Image processing started for bands %s" % "-".join(map(str, self.bands)), normal=True)
self.output('PanSharpened Image processing started for bands %s' % '-'.join(map(str, self.bands)),
normal=True, arrow=True)

bands = self._read_bands()
image_data = self._get_image_data()
Expand Down Expand Up @@ -431,6 +430,7 @@ def run(self):

return self._write_to_file(new_bands, pan, **rasterio_options)

@rasterio_decorator
def _write_to_file(self, new_bands, pan, **kwargs):

# Read coverage from QBA
Expand All @@ -450,6 +450,7 @@ def _write_to_file(self, new_bands, pan, **kwargs):
band = self._color_correction(band, self.bands[i], 0, coverage)

output.write_band(i+1, img_as_ubyte(band))

new_bands[i] = None

self.output("Writing to file", normal=True, color='green', indent=1)
Expand All @@ -476,3 +477,9 @@ def _rescale(self, bands):
bands[key] = (bands[key] * 65535).astype('uint16')

return bands

if __name__ == '__main__':

p = PanSharpen('/Users/ajdevseed/Desktop/LC81950282014159LGN00')

p.run()
8 changes: 3 additions & 5 deletions landsat/landsat.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def args_options():
help="Provide Full sceneID, e.g. LC81660392014196LGN00")

parser_download.add_argument('-b', '--bands', help='If you specify bands, landsat-util will try to download '
'the band from S3. If the band does not exist, an error is returned')
'the band from S3. If the band does not exist, an error is returned', default='432')
parser_download.add_argument('-d', '--dest', help='Destination path')
parser_download.add_argument('-p', '--process', help='Process the image after download', action='store_true')
parser_download.add_argument('--pansharpen', action='store_true',
Expand Down Expand Up @@ -221,7 +221,7 @@ def args_options():
'Example: --clip -346.06658935546875,49.93531194616915,-345.4595947265625,' +
'50.2682767372753')
parser_process.add_argument('-b', '--bands', help='specify band combinations. Default is 432'
'Example: --bands 321')
'Example: --bands 321', default='432')
parser_process.add_argument('-v', '--verbose', action='store_true',
help='Turn on verbosity')
parser_process.add_argument('-u', '--upload', action='store_true',
Expand Down Expand Up @@ -344,9 +344,7 @@ def main(args):
return ["Connection timeout. Probably the region parameter is incorrect", 1]
u.run(args.bucket, get_file(stored), stored)

v.output("The output is stored at %s" % stored, normal=True, arrow=True)

return ['Image Processing Completed', 0]
return ['The output is stored at %s' % stored, 0]
else:
return ['Download Completed', 0]
except IncorrectSceneId:
Expand Down
20 changes: 10 additions & 10 deletions tests/test_landsat.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_download_process_continuous(self, mock_downloader, mock_process):
mock_downloader.assert_called_with(['LC80010092015051LGN00', 'LC80010092014051LGN00'], [4, 3, 2])
mock_process.assert_called_with('path/to/folder/LC80010092014051LGN00', '432',
False, False, False, False, bounds=None)
self.assertEquals(output, ["Image Processing Completed", 0])
self.assertEquals(output, ["The output is stored at image.TIF", 0])

# Call with force unzip flag
args = ['download', 'LC80010092015051LGN00', 'LC80010092014051LGN00', '-b', '432', '-d',
Expand All @@ -107,7 +107,7 @@ def test_download_process_continuous(self, mock_downloader, mock_process):
mock_downloader.assert_called_with(['LC80010092015051LGN00', 'LC80010092014051LGN00'], [4, 3, 2])
mock_process.assert_called_with('path/to/folder/LC80010092014051LGN00', '432', False, False, False,
True, bounds=None)
self.assertEquals(output, ["Image Processing Completed", 0])
self.assertEquals(output, ["The output is stored at image.TIF", 0])

# Call with pansharpen
args = ['download', 'LC80010092015051LGN00', 'LC80010092014051LGN00', '-b', '432', '-d',
Expand All @@ -116,7 +116,7 @@ def test_download_process_continuous(self, mock_downloader, mock_process):
mock_downloader.assert_called_with(['LC80010092015051LGN00', 'LC80010092014051LGN00'], [4, 3, 2, 8])
mock_process.assert_called_with('path/to/folder/LC80010092014051LGN00', '432', False, True, False,
False, bounds=None)
self.assertEquals(output, ["Image Processing Completed", 0])
self.assertEquals(output, ["The output is stored at image.TIF", 0])

# Call with pansharpen and clipping
args = ['download', 'LC80010092015051LGN00', 'LC80010092014051LGN00', '-b', '432', '-d',
Expand All @@ -125,7 +125,7 @@ def test_download_process_continuous(self, mock_downloader, mock_process):
mock_downloader.assert_called_with(['LC80010092015051LGN00', 'LC80010092014051LGN00'], [4, 3, 2, 8])
mock_process.assert_called_with('path/to/folder/LC80010092014051LGN00', '432', False, True, False,
False, bounds=[-180.0, -180.0, 0.0, 0.0])
self.assertEquals(output, ["Image Processing Completed", 0])
self.assertEquals(output, ["The output is stored at image.TIF", 0])

# Call with ndvi
args = ['download', 'LC80010092015051LGN00', 'LC80010092014051LGN00', '-b', '432', '-d',
Expand All @@ -134,7 +134,7 @@ def test_download_process_continuous(self, mock_downloader, mock_process):
mock_downloader.assert_called_with(['LC80010092015051LGN00', 'LC80010092014051LGN00'], [4, 5])
mock_process.assert_called_with('path/to/folder/LC80010092014051LGN00', '432', False, False, True,
False, bounds=None)
self.assertEquals(output, ["Image Processing Completed", 0])
self.assertEquals(output, ["The output is stored at image.TIF", 0])

@mock.patch('landsat.landsat.Uploader')
@mock.patch('landsat.landsat.process_image')
Expand All @@ -153,7 +153,7 @@ def test_download_process_continuous_with_upload(self, mock_downloader, mock_pro
False, bounds=None)
mock_upload.assert_called_with('somekey', 'somesecret', 'this')
mock_upload.return_value.run.assert_called_with('mybucket', 'image.TIF', 'image.TIF')
self.assertEquals(output, ["Image Processing Completed", 0])
self.assertEquals(output, ['The output is stored at image.TIF', 0])

@mock.patch('landsat.landsat.process_image')
@mock.patch('landsat.landsat.Downloader.download')
Expand All @@ -178,7 +178,7 @@ def test_process_correct(self, mock_process):
args = ['process', 'path/to/folder/LC80010092015051LGN00']
output = landsat.main(self.parser.parse_args(args))

mock_process.assert_called_with('path/to/folder/LC80010092015051LGN00', None,
mock_process.assert_called_with('path/to/folder/LC80010092015051LGN00', '432',
False, False, False, False, False, None)
self.assertEquals(output, ["The output is stored at image.TIF"])

Expand All @@ -190,7 +190,7 @@ def test_process_correct_with_clipping(self, mock_process):
args = ['process', 'path/to/folder/LC80010092015051LGN00', '--clip', '"-180,-180,0,0"']
output = landsat.main(self.parser.parse_args(args))

mock_process.assert_called_with('path/to/folder/LC80010092015051LGN00', None,
mock_process.assert_called_with('path/to/folder/LC80010092015051LGN00', '432',
False, False, False, False, False, [-180.0, -180.0, 0.0, 0.0])
self.assertEquals(output, ["The output is stored at image.TIF"])

Expand All @@ -202,7 +202,7 @@ def test_process_correct_pansharpen(self, mock_process):
args = ['process', '--pansharpen', 'path/to/folder/LC80010092015051LGN00']
output = landsat.main(self.parser.parse_args(args))

mock_process.assert_called_with('path/to/folder/LC80010092015051LGN00', None, False, True, False, False,
mock_process.assert_called_with('path/to/folder/LC80010092015051LGN00', '432', False, True, False, False,
False, None)
self.assertEquals(output, ["The output is stored at image.TIF"])

Expand All @@ -214,7 +214,7 @@ def test_process_correct_ndvi(self, mock_process):
args = ['process', '--ndvi', 'path/to/folder/LC80010092015051LGN00']
output = landsat.main(self.parser.parse_args(args))

mock_process.assert_called_with('path/to/folder/LC80010092015051LGN00', None, False, False, True, False,
mock_process.assert_called_with('path/to/folder/LC80010092015051LGN00', '432', False, False, True, False,
False, None)
self.assertEquals(output, ["The output is stored at image.TIF"])

Expand Down

0 comments on commit b9584d1

Please sign in to comment.