From ee00f5e6263c546cb34b506370634e5ac5ca3ac2 Mon Sep 17 00:00:00 2001 From: julesair Date: Fri, 17 Jul 2015 08:37:11 +0200 Subject: [PATCH 01/14] excluded venv from commits --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 37c53b8..b03131b 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ eva.py test.sh _build htmlcov +venv/ From c881bb2780bd39674e36853da28f03dbb246ffa2 Mon Sep 17 00:00:00 2001 From: julesair Date: Fri, 17 Jul 2015 09:45:57 +0200 Subject: [PATCH 02/14] added NDVI grey feature --- landsat/landsat.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/landsat/landsat.py b/landsat/landsat.py index bb034df..7fd1553 100755 --- a/landsat/landsat.py +++ b/landsat/landsat.py @@ -175,6 +175,9 @@ def args_options(): parser_download.add_argument('--pansharpen', action='store_true', help='Whether to also pansharpen the process ' 'image. Pansharpening requires larger memory') + parser_process.add_argument('--ndvi', type=str, choices=['color','grey'], + help='Create an NDVI map. Specify if output should be GTIFF in grayscale (grey) ' + 'or a .PNG with a colormap (color)') parser_download.add_argument('-u', '--upload', action='store_true', help='Upload to S3 after the image processing completed') parser_download.add_argument('--key', help='Amazon S3 Access Key (You can also be set AWS_ACCESS_KEY_ID as ' @@ -340,13 +343,20 @@ def process_image(path, bands=None, verbose=False, pansharpen=False, force_unzip """ try: bands = convert_to_integer_list(bands) - p = Process(path, bands=bands, verbose=verbose, force_unzip=force_unzip) + + if isinstance(ndvi, str): + p = Process(path, bands=[4,5], verbose=verbose, force_unzip=force_unzip) + else: + p = Process(path, bands=bands, verbose=verbose, force_unzip=force_unzip) except IOError: exit("Zip file corrupted", 1) except FileDoesNotExist as e: exit(e.message, 1) - - return p.run(pansharpen) + if isinstance(ndvi, str): + out=[p.run_ndvi(pansharpen,mode=ndvi)] + else: + out=[p.run_rgb(pansharpen)] + return out def __main__(): From 3698776e65ccb6db77753a41123d3c241c0304e4 Mon Sep 17 00:00:00 2001 From: julesair Date: Fri, 17 Jul 2015 09:49:04 +0200 Subject: [PATCH 03/14] addes NDVI greay feature --- landsat/image.py | 158 ++++++++++++++++++++++++++++++++++++++++++++- landsat/landsat.py | 11 ++-- 2 files changed, 164 insertions(+), 5 deletions(-) diff --git a/landsat/image.py b/landsat/image.py index 0a7d928..a2c7c9a 100644 --- a/landsat/image.py +++ b/landsat/image.py @@ -81,7 +81,7 @@ class Process(VerbosityMixin): for band in self.bands: self.bands_path.append(join(self.scene_path, self._get_full_filename(band))) - def run(self, pansharpen=True): + def run_rgb(self, pansharpen=True): """ Executes the image processing. :param pansharpen: @@ -205,6 +205,162 @@ class Process(VerbosityMixin): self.output("Writing to file", normal=True, color='green', indent=1) return output_file + def run_ndvi(self, pansharpen=True, mode='grey'): + """ Executes the image processing. + + :param pansharpen: + Whether the process should also run pansharpenning. Default is True + :type pansharpen: + boolean + + :returns: + (String) the path to the processed image + """ + + self.output("* Image processing started for NDVI", normal=True) + + # Read radiance conversion factors from mtl file + try: + with open(self.scene_path + '/' + self.scene + '_MTL.txt', 'rU') as mtl: + lines = mtl.readlines() + for line in lines: + if 'REFLECTANCE_ADD_BAND_3' in line: + add_B3 = float(line.replace('REFLECTANCE_ADD_BAND_3 = ', '')) + elif 'REFLECTANCE_MULT_BAND_3' in line: + mult_B3 = float(line.replace('REFLECTANCE_MULT_BAND_3 = ', '')) + elif 'REFLECTANCE_ADD_BAND_4' in line: + add_B4 = float(line.replace('REFLECTANCE_ADD_BAND_4 = ', '')) + elif 'REFLECTANCE_MULT_BAND_4' in line: + mult_B4 = float(line.replace('REFLECTANCE_MULT_BAND_4 = ', '')) + except IOError: + pass + + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + with rasterio.drivers(): + bands = [] + + # Add band 8 for pansharpenning + if pansharpen: + self.bands.append(8) + + bands_path = [] + + for band in self.bands: + bands_path.append(join(self.scene_path, self._get_full_filename(band))) + + try: + for i, band in enumerate(self.bands): + bands.append(self._read_band(bands_path[i])) + except IOError as e: + exit(e.message, 1) + + src = rasterio.open(bands_path[-1]) + + # Get pixel size from source + self.pixel = src.affine[0] + + # Only collect src data that is needed and delete the rest + src_data = { + 'transform': src.transform, + 'crs': src.crs, + 'affine': src.affine, + 'shape': src.shape + } + del src + + crn = self._get_boundaries(src_data) + + dst_shape = src_data['shape'] + dst_corner_ys = [crn[k]['y'][1][0] for k in crn.keys()] + dst_corner_xs = [crn[k]['x'][1][0] for k in crn.keys()] + y_pixel = abs(max(dst_corner_ys) - min(dst_corner_ys)) / dst_shape[0] + x_pixel = abs(max(dst_corner_xs) - min(dst_corner_xs)) / dst_shape[1] + + dst_transform = (min(dst_corner_xs), + x_pixel, + 0.0, + max(dst_corner_ys), + 0.0, + -y_pixel) + # Delete crn since no longer needed + del crn + + new_bands = [] + for i in range(0, 3): + new_bands.append(numpy.empty(dst_shape, dtype=numpy.float32)) + + if pansharpen: + bands[:3] = self._rescale(bands[:3]) + new_bands.append(numpy.empty(dst_shape, dtype=numpy.float32)) + + self.output("Projecting", normal=True, arrow=True) + for i, band in enumerate(bands): + self.output("band %s" % self.bands[i], normal=True, color='green', indent=1) + reproject(band, new_bands[i], src_transform=src_data['transform'], src_crs=src_data['crs'], + dst_transform=dst_transform, dst_crs=self.dst_crs, resampling=RESAMPLING.nearest) + + # Bands are no longer needed + del bands + + if pansharpen: + new_bands = self._pansharpenning(new_bands) + del self.bands[3] + + self.output("Final Steps", normal=True, arrow=True) + + + output_file = '%s_NDVI' % (self.scene) + + if pansharpen: + output_file += '_pan' + mask=(new_bands[1]+new_bands[0])==0 + + + new_bands[0]=new_bands[0]*mult_B3+add_B3 + new_bands[1]=new_bands[1]*mult_B4+add_B4 + ndvi=numpy.true_divide((new_bands[1]-new_bands[0]),(new_bands[1]+new_bands[0])) + ndvi[mask]=-1 + + if mode=='grey': + ndvi=((ndvi+1)*255 / 2).astype(numpy.uint8) + output_file += '.TIF' + output_file = join(self.dst_path, output_file) + + output = rasterio.open(output_file, 'w', driver='GTiff', + width=dst_shape[1], height=dst_shape[0], + count=1, dtype=numpy.uint8, + nodata=0, transform=dst_transform, + crs=self.dst_crs) + + output.write_band(1, ndvi) + + elif mode=='color': +# import matplotlib +# matplotlib.use('Agg') +# import matplotlib.pyplot as plt +# plt.imshow(ndvi) +# image = plt.gcf() +# image.set_size_inches(8, 8) +# plt.savefig('myfig',dpi=1000) +# output_file += '.PNG' +# output_file = join(self.dst_path, output_file) +# +# rgb=self._index2rgb(index_matrix=ndvi) +# +# output = rasterio.open(output_file, 'w', driver='GTiff', +# width=dst_shape[1], height=dst_shape[0], +# count=3, dtype=numpy.uint8, +# nodata=0, transform=dst_transform, photometric='RGB', +# crs=self.dst_crs) +# +# for i in range(0, 3): +# output.write_band(i+1, rgb[i]) + + + self.output("Writing to file", normal=True, color='green', indent=1) + return output_file + def _pansharpenning(self, bands): self.output("Pansharpening", normal=True, arrow=True) diff --git a/landsat/landsat.py b/landsat/landsat.py index 7fd1553..95eeec5 100755 --- a/landsat/landsat.py +++ b/landsat/landsat.py @@ -175,7 +175,7 @@ def args_options(): parser_download.add_argument('--pansharpen', action='store_true', help='Whether to also pansharpen the process ' 'image. Pansharpening requires larger memory') - parser_process.add_argument('--ndvi', type=str, choices=['color','grey'], + parser_download.add_argument('--ndvi', type=str, choices=['color','grey'], help='Create an NDVI map. Specify if output should be GTIFF in grayscale (grey) ' 'or a .PNG with a colormap (color)') parser_download.add_argument('-u', '--upload', action='store_true', @@ -194,6 +194,9 @@ def args_options(): parser_process.add_argument('--pansharpen', action='store_true', help='Whether to also pansharpen the process ' 'image. Pansharpening requires larger memory') + parser_process.add_argument('--ndvi', type=str, choices=['color','grey'], + help='Create an NDVI map. Specify if output should be GTIFF in grayscale (grey) ' + 'or a .PNG with a colormap (color)') parser_process.add_argument('-b', '--bands', help='specify band combinations. Default is 432' 'Example: --bands 321') parser_process.add_argument('-v', '--verbose', action='store_true', @@ -234,7 +237,7 @@ def main(args): if args.subs == 'process': verbose = True if args.verbose else False force_unzip = True if args.force_unzip else False - stored = process_image(args.path, args.bands, verbose, args.pansharpen, force_unzip) + stored = process_image(args.path, args.bands, verbose, args.pansharpen, force_unzip, args.ndvi) if args.upload: u = Uploader(args.key, args.secret, args.region) @@ -298,7 +301,7 @@ def main(args): if src == 'google': path = path + '.tar.bz' - stored = process_image(path, args.bands, False, args.pansharpen, force_unzip) + stored = process_image(path, args.bands, False, args.pansharpen, force_unzip, args.ndvi) if args.upload: try: @@ -318,7 +321,7 @@ def main(args): return ['The SceneID provided was incorrect', 1] -def process_image(path, bands=None, verbose=False, pansharpen=False, force_unzip=None): +def process_image(path, bands=None, verbose=False, pansharpen=False, force_unzip=None, ndvi=False): """ Handles constructing and image process. :param path: From f4c0ac8d15ac9f2adfb38d0df25efc50d47731bd Mon Sep 17 00:00:00 2001 From: julesair Date: Fri, 17 Jul 2015 14:58:09 +0200 Subject: [PATCH 04/14] addes latest search. Could by enhanced by minimizing server requests --- landsat/image.py | 1 + landsat/landsat.py | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/landsat/image.py b/landsat/image.py index a2c7c9a..9437bd5 100644 --- a/landsat/image.py +++ b/landsat/image.py @@ -336,6 +336,7 @@ class Process(VerbosityMixin): output.write_band(1, ndvi) elif mode=='color': + print('dummie') # import matplotlib # matplotlib.use('Agg') # import matplotlib.pyplot as plt diff --git a/landsat/landsat.py b/landsat/landsat.py index 95eeec5..c71fde6 100755 --- a/landsat/landsat.py +++ b/landsat/landsat.py @@ -9,6 +9,8 @@ import json from os.path import join from urllib2 import URLError +from datetime import datetime +from dateutil.relativedelta import relativedelta from dateutil.parser import parse import pycurl from boto.exception import NoAuthHandlerFound @@ -152,9 +154,12 @@ def args_options(): parser_search.add_argument('-e', '--end', help='End Date - Most formats are accepted ' 'e.g. Jun 12 2014 OR 06/12/2014') - parser_search.add_argument('-c', '--cloud', type=float, default=20.0, + parser_search.add_argument('--latest',default=-1,type=int, + help='search the latest landsat image. When number is given, the result will be the image' + 'with least cloudcover of the n latest images.') + parser_search.add_argument('-c', '--cloud', type=float, default=100.0, help='Maximum cloud percentage ' - 'default is 20 perct') + 'default is 100 perct') parser_search.add_argument('-p', '--pathrow', help='Paths and Rows in order separated by comma. Use quotes ("001").' 'Example: path,row,path,row 001,001,190,204') @@ -252,8 +257,13 @@ def main(args): args.start = reformat_date(parse(args.start)) if args.end: args.end = reformat_date(parse(args.end)) + if args.latest>0: + end = datetime.now() + start = end-relativedelta(days=+args.latest*16-1) + args.end = end.strftime("%Y-%m-%d") + args.start = start.strftime("%Y-%m-%d") except (TypeError, ValueError): - return ["You date format is incorrect. Please try again!", 1] + return ["Your date format is incorrect. Please try again!", 1] s = Search() @@ -262,15 +272,33 @@ def main(args): lon = float(args.lon) if args.lon else None except ValueError: return ["The latitude and longitude values must be valid numbers", 1] - - result = s.search(paths_rows=args.pathrow, + + sign=0 #keeps information if start date should be in- or decreased + while True: + result = s.search(paths_rows=args.pathrow, lat=lat, lon=lon, limit=args.limit, start_date=args.start, end_date=args.end, cloud_max=args.cloud) - + + if result['status'] == 'SUCCESS': + diff=args.latest-result['total'] + else: + diff=1 #When no image was found, decrease start date + + # if option "latest" not called OR #desiredimages==#foundimages OR sign of diff alternates + # The least was the case with tile 110,025, where one image was found twice in the database... + #...around March 2015 + if args.latest==-1 or diff==0 or diff*sign<0: + break + else: + # adapt starting date and redo the search + sign=cmp(diff,0) + start = start-relativedelta(days=+sign*16) + args.start = start.strftime("%Y-%m-%d") + if result['status'] == 'SUCCESS': v.output('%s items were found' % result['total'], normal=True, arrow=True) if result['total'] > 100: From 2b3db79aae97e7e50a162469b5c6ded437176b90 Mon Sep 17 00:00:00 2001 From: julesair Date: Fri, 17 Jul 2015 16:28:36 +0200 Subject: [PATCH 05/14] Enhanced latest search, limited to 365 days back in time --- landsat/landsat.py | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/landsat/landsat.py b/landsat/landsat.py index c71fde6..e6d44b1 100755 --- a/landsat/landsat.py +++ b/landsat/landsat.py @@ -145,9 +145,9 @@ def args_options(): help='Search Landsat metdata') # Global search options - parser_search.add_argument('-l', '--limit', default=10, type=int, + parser_search.add_argument('-l', '--limit', default=0, type=int, help='Search return results limit\n' - 'default is 100') + 'default is 10') parser_search.add_argument('-s', '--start', help='Start Date - Most formats are accepted ' 'e.g. Jun 12 2014 OR 06/12/2014') @@ -258,8 +258,9 @@ def main(args): if args.end: args.end = reformat_date(parse(args.end)) if args.latest>0: + args.limit=100 end = datetime.now() - start = end-relativedelta(days=+args.latest*16-1) + start = end-relativedelta(days=+365) args.end = end.strftime("%Y-%m-%d") args.start = start.strftime("%Y-%m-%d") except (TypeError, ValueError): @@ -271,34 +272,31 @@ def main(args): lat = float(args.lat) if args.lat else None lon = float(args.lon) if args.lon else None except ValueError: - return ["The latitude and longitude values must be valid numbers", 1] - - sign=0 #keeps information if start date should be in- or decreased - while True: - result = s.search(paths_rows=args.pathrow, + return ["The latitude and longitude values must be valid numbers", 1] + + result = s.search(paths_rows=args.pathrow, lat=lat, lon=lon, limit=args.limit, start_date=args.start, end_date=args.end, cloud_max=args.cloud) + + if args.latest>0: + datelist=[] + for i in range(0,result['total_returned']): + datelist.append((result['results'][i]['date'],result['results'][i])) + + datelist.sort(key=lambda tup: tup[0], reverse=True) + datelist = datelist[:args.latest] - if result['status'] == 'SUCCESS': - diff=args.latest-result['total'] - else: - diff=1 #When no image was found, decrease start date - - # if option "latest" not called OR #desiredimages==#foundimages OR sign of diff alternates - # The least was the case with tile 110,025, where one image was found twice in the database... - #...around March 2015 - if args.latest==-1 or diff==0 or diff*sign<0: - break - else: - # adapt starting date and redo the search - sign=cmp(diff,0) - start = start-relativedelta(days=+sign*16) - args.start = start.strftime("%Y-%m-%d") - + result['results']=[] + for i in range(0,len(datelist)): + result['results'].append(datelist[i][1]) + result['total_returned']=len(datelist) + result['total']=len(datelist) + + if result['status'] == 'SUCCESS': v.output('%s items were found' % result['total'], normal=True, arrow=True) if result['total'] > 100: From cf9c9cf67d0b7f41861d8bb7854f44c822a5a955 Mon Sep 17 00:00:00 2001 From: julesair Date: Mon, 20 Jul 2015 08:30:13 +0200 Subject: [PATCH 06/14] searchlatest improved --- landsat/landsat.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/landsat/landsat.py b/landsat/landsat.py index e6d44b1..439570c 100755 --- a/landsat/landsat.py +++ b/landsat/landsat.py @@ -145,7 +145,7 @@ def args_options(): help='Search Landsat metdata') # Global search options - parser_search.add_argument('-l', '--limit', default=0, type=int, + parser_search.add_argument('-l', '--limit', default=10, type=int, help='Search return results limit\n' 'default is 10') parser_search.add_argument('-s', '--start', @@ -258,7 +258,7 @@ def main(args): if args.end: args.end = reformat_date(parse(args.end)) if args.latest>0: - args.limit=100 + args.limit=25 end = datetime.now() start = end-relativedelta(days=+365) args.end = end.strftime("%Y-%m-%d") @@ -281,29 +281,30 @@ def main(args): start_date=args.start, end_date=args.end, cloud_max=args.cloud) - - if args.latest>0: - datelist=[] - for i in range(0,result['total_returned']): - datelist.append((result['results'][i]['date'],result['results'][i])) - - datelist.sort(key=lambda tup: tup[0], reverse=True) - datelist = datelist[:args.latest] - - result['results']=[] - for i in range(0,len(datelist)): - result['results'].append(datelist[i][1]) - result['total_returned']=len(datelist) - result['total']=len(datelist) - if result['status'] == 'SUCCESS': - v.output('%s items were found' % result['total'], normal=True, arrow=True) + if args.latest>0: + datelist=[] + for i in range(0,result['total_returned']): + datelist.append((result['results'][i]['date'],result['results'][i])) + + datelist.sort(key=lambda tup: tup[0], reverse=True) + datelist = datelist[:args.latest] + + result['results']=[] + for i in range(0,len(datelist)): + result['results'].append(datelist[i][1]) + result['total_returned']=len(datelist) + + else: + v.output('%s items were found' % result['total'], normal=True, arrow=True) + if result['total'] > 100: return ['Over 100 results. Please narrow your search', 1] else: v.output(json.dumps(result, sort_keys=True, indent=4), normal=True, color='green') - return ['Search completed!'] + return ['Search completed!'] + elif result['status'] == 'error': return [result['message'], 1] elif args.subs == 'download': From bc597ab2ce6cfd5aa296db890f034723b78746aa Mon Sep 17 00:00:00 2001 From: julesair Date: Mon, 20 Jul 2015 16:38:19 +0200 Subject: [PATCH 07/14] working on colorbar plot, the rest is fine --- landsat/colormap_cubehelix | 273 +++++++++++++++++++++++++++++++++++++ landsat/image.py | 138 ++++++++++++------- landsat/landsat.py | 2 +- landsat/settings.py | 3 + 4 files changed, 364 insertions(+), 52 deletions(-) create mode 100644 landsat/colormap_cubehelix diff --git a/landsat/colormap_cubehelix b/landsat/colormap_cubehelix new file mode 100644 index 0000000..f129244 --- /dev/null +++ b/landsat/colormap_cubehelix @@ -0,0 +1,273 @@ +# Tue Jul 14 2015 14:40:26 GMT+0200 +# --------------------------------------------- +# R/G/B cubehelix colour scheme +# +# see http://www.mrao.cam.ac.uk/~dag/CUBEHELIX/ +#---------------------------------------------- +# see Green (2011), BASI, 39, 289. +# +# start............: 3.0 +# rotations........: -2 +# hue..............: 1.6 +# gamma............: 0.8 +# number of levels.: 254 +#---------------------------------------------- +# Dave Green: dag@mrao.cam.ac.uk +#---------------------------------------------- +# faction and R/G/B values +# +mode = 1 +0.000 0.000 0.000 0.000 +0.004 0.011 0.011 0.018 +0.008 0.019 0.020 0.033 +0.012 0.025 0.027 0.047 +0.016 0.030 0.035 0.060 +0.020 0.034 0.042 0.073 +0.024 0.038 0.050 0.085 +0.028 0.041 0.057 0.097 +0.032 0.043 0.065 0.108 +0.036 0.044 0.073 0.118 +0.040 0.046 0.081 0.128 +0.043 0.046 0.089 0.137 +0.047 0.047 0.097 0.146 +0.051 0.047 0.105 0.154 +0.055 0.046 0.114 0.162 +0.059 0.046 0.122 0.169 +0.063 0.045 0.131 0.175 +0.067 0.044 0.140 0.181 +0.071 0.042 0.148 0.186 +0.075 0.041 0.157 0.190 +0.079 0.040 0.166 0.194 +0.083 0.038 0.175 0.197 +0.087 0.037 0.184 0.199 +0.091 0.036 0.193 0.201 +0.095 0.034 0.202 0.202 +0.099 0.033 0.211 0.202 +0.103 0.033 0.220 0.202 +0.107 0.032 0.229 0.201 +0.111 0.032 0.238 0.200 +0.115 0.032 0.247 0.198 +0.119 0.032 0.255 0.195 +0.123 0.033 0.264 0.192 +0.126 0.034 0.272 0.188 +0.130 0.036 0.280 0.184 +0.134 0.038 0.288 0.179 +0.138 0.040 0.295 0.174 +0.142 0.043 0.303 0.169 +0.146 0.047 0.310 0.163 +0.150 0.051 0.317 0.157 +0.154 0.056 0.323 0.150 +0.158 0.061 0.330 0.144 +0.162 0.067 0.335 0.137 +0.166 0.074 0.341 0.130 +0.170 0.081 0.346 0.123 +0.174 0.089 0.351 0.116 +0.178 0.098 0.356 0.109 +0.182 0.107 0.360 0.102 +0.186 0.117 0.364 0.095 +0.190 0.127 0.367 0.088 +0.194 0.138 0.370 0.082 +0.198 0.150 0.373 0.075 +0.202 0.162 0.375 0.069 +0.206 0.175 0.377 0.064 +0.209 0.188 0.379 0.058 +0.213 0.202 0.380 0.053 +0.217 0.217 0.381 0.049 +0.221 0.232 0.381 0.045 +0.225 0.247 0.381 0.041 +0.229 0.263 0.381 0.038 +0.233 0.279 0.380 0.036 +0.237 0.296 0.379 0.034 +0.241 0.313 0.378 0.033 +0.245 0.330 0.376 0.033 +0.249 0.348 0.374 0.034 +0.253 0.365 0.372 0.035 +0.257 0.383 0.370 0.037 +0.261 0.401 0.367 0.040 +0.265 0.420 0.364 0.043 +0.269 0.438 0.361 0.048 +0.273 0.456 0.358 0.053 +0.277 0.474 0.354 0.059 +0.281 0.492 0.351 0.067 +0.285 0.510 0.347 0.074 +0.289 0.528 0.343 0.083 +0.292 0.545 0.339 0.093 +0.296 0.563 0.335 0.103 +0.300 0.580 0.331 0.115 +0.304 0.596 0.328 0.127 +0.308 0.612 0.324 0.140 +0.312 0.628 0.320 0.154 +0.316 0.643 0.316 0.168 +0.320 0.658 0.313 0.184 +0.324 0.672 0.309 0.200 +0.328 0.686 0.306 0.216 +0.332 0.699 0.303 0.234 +0.336 0.711 0.300 0.252 +0.340 0.723 0.297 0.271 +0.344 0.733 0.295 0.290 +0.348 0.744 0.292 0.310 +0.352 0.753 0.290 0.330 +0.356 0.761 0.289 0.351 +0.360 0.769 0.288 0.372 +0.364 0.776 0.287 0.393 +0.368 0.782 0.286 0.415 +0.372 0.787 0.286 0.437 +0.375 0.791 0.286 0.459 +0.379 0.794 0.287 0.481 +0.383 0.797 0.288 0.504 +0.387 0.798 0.290 0.526 +0.391 0.799 0.292 0.549 +0.395 0.799 0.294 0.571 +0.399 0.798 0.297 0.593 +0.403 0.795 0.300 0.615 +0.407 0.793 0.304 0.637 +0.411 0.789 0.308 0.658 +0.415 0.784 0.313 0.680 +0.419 0.779 0.318 0.700 +0.423 0.773 0.324 0.721 +0.427 0.766 0.330 0.740 +0.431 0.758 0.337 0.760 +0.435 0.750 0.344 0.778 +0.439 0.741 0.352 0.796 +0.443 0.731 0.360 0.814 +0.447 0.721 0.368 0.830 +0.451 0.710 0.377 0.846 +0.455 0.699 0.386 0.861 +0.458 0.687 0.396 0.876 +0.462 0.674 0.406 0.889 +0.466 0.662 0.416 0.902 +0.470 0.649 0.427 0.913 +0.474 0.635 0.438 0.924 +0.478 0.622 0.449 0.934 +0.482 0.608 0.461 0.942 +0.486 0.594 0.473 0.950 +0.490 0.580 0.485 0.957 +0.494 0.566 0.497 0.962 +0.498 0.552 0.510 0.967 +0.502 0.538 0.522 0.971 +0.506 0.524 0.535 0.973 +0.510 0.510 0.548 0.975 +0.514 0.496 0.561 0.975 +0.518 0.483 0.574 0.975 +0.522 0.470 0.587 0.973 +0.526 0.457 0.600 0.971 +0.530 0.445 0.613 0.968 +0.534 0.433 0.626 0.963 +0.538 0.421 0.639 0.958 +0.542 0.410 0.652 0.952 +0.545 0.400 0.664 0.945 +0.549 0.390 0.677 0.937 +0.553 0.381 0.689 0.929 +0.557 0.372 0.701 0.919 +0.561 0.365 0.713 0.909 +0.565 0.357 0.725 0.898 +0.569 0.351 0.736 0.887 +0.573 0.346 0.747 0.875 +0.577 0.341 0.758 0.862 +0.581 0.337 0.768 0.849 +0.585 0.334 0.778 0.836 +0.589 0.332 0.788 0.822 +0.593 0.330 0.797 0.808 +0.597 0.330 0.806 0.793 +0.601 0.330 0.814 0.779 +0.605 0.332 0.822 0.764 +0.609 0.334 0.830 0.749 +0.613 0.337 0.837 0.734 +0.617 0.341 0.844 0.719 +0.621 0.346 0.850 0.703 +0.625 0.352 0.856 0.688 +0.628 0.358 0.861 0.674 +0.632 0.366 0.866 0.659 +0.636 0.374 0.870 0.645 +0.640 0.383 0.874 0.630 +0.644 0.393 0.878 0.617 +0.648 0.403 0.881 0.603 +0.652 0.415 0.883 0.590 +0.656 0.427 0.885 0.578 +0.660 0.439 0.887 0.566 +0.664 0.453 0.888 0.555 +0.668 0.466 0.889 0.544 +0.672 0.481 0.889 0.534 +0.676 0.496 0.889 0.525 +0.680 0.511 0.889 0.516 +0.684 0.527 0.888 0.508 +0.688 0.543 0.887 0.501 +0.692 0.560 0.885 0.494 +0.696 0.577 0.883 0.489 +0.700 0.594 0.881 0.484 +0.704 0.612 0.879 0.480 +0.708 0.629 0.876 0.477 +0.711 0.647 0.873 0.475 +0.715 0.665 0.870 0.474 +0.719 0.682 0.867 0.473 +0.723 0.700 0.864 0.473 +0.727 0.718 0.860 0.475 +0.731 0.736 0.856 0.477 +0.735 0.753 0.853 0.480 +0.739 0.770 0.849 0.484 +0.743 0.788 0.845 0.488 +0.747 0.804 0.841 0.494 +0.751 0.821 0.837 0.500 +0.755 0.837 0.833 0.507 +0.759 0.853 0.830 0.515 +0.763 0.868 0.826 0.523 +0.767 0.883 0.822 0.533 +0.771 0.897 0.819 0.542 +0.775 0.911 0.815 0.553 +0.779 0.924 0.812 0.564 +0.783 0.937 0.809 0.576 +0.787 0.949 0.806 0.588 +0.791 0.961 0.804 0.600 +0.794 0.972 0.801 0.613 +0.798 0.982 0.799 0.627 +0.802 0.992 0.797 0.640 +0.806 1.000 0.796 0.655 +0.810 1.000 0.795 0.669 +0.814 1.000 0.794 0.683 +0.818 1.000 0.793 0.698 +0.822 1.000 0.793 0.713 +0.826 1.000 0.793 0.727 +0.830 1.000 0.793 0.742 +0.834 1.000 0.793 0.757 +0.838 1.000 0.794 0.772 +0.842 1.000 0.796 0.786 +0.846 1.000 0.797 0.801 +0.850 1.000 0.799 0.815 +0.854 1.000 0.802 0.829 +0.858 1.000 0.804 0.843 +0.862 1.000 0.807 0.856 +0.866 1.000 0.810 0.869 +0.870 1.000 0.814 0.882 +0.874 1.000 0.818 0.894 +0.877 1.000 0.822 0.906 +0.881 1.000 0.826 0.917 +0.885 1.000 0.831 0.928 +0.889 1.000 0.836 0.938 +0.893 1.000 0.841 0.948 +0.897 1.000 0.847 0.957 +0.901 1.000 0.852 0.966 +0.905 1.000 0.858 0.973 +0.909 1.000 0.864 0.981 +0.913 1.000 0.870 0.987 +0.917 1.000 0.876 0.994 +0.921 1.000 0.883 0.999 +0.925 1.000 0.889 1.000 +0.929 1.000 0.896 1.000 +0.933 1.000 0.902 1.000 +0.937 1.000 0.909 1.000 +0.941 1.000 0.915 1.000 +0.945 0.998 0.922 1.000 +0.949 0.996 0.928 1.000 +0.953 0.994 0.935 1.000 +0.957 0.992 0.941 1.000 +0.960 0.990 0.947 1.000 +0.964 0.989 0.953 1.000 +0.968 0.988 0.959 1.000 +0.972 0.988 0.965 1.000 +0.976 0.988 0.971 1.000 +0.980 0.989 0.976 1.000 +0.984 0.990 0.982 1.000 +0.988 0.992 0.987 1.000 +0.992 0.994 0.991 1.000 +0.996 0.997 0.996 1.000 +1.000 1.000 1.000 1.000 diff --git a/landsat/image.py b/landsat/image.py index 9437bd5..6e67996 100644 --- a/landsat/image.py +++ b/landsat/image.py @@ -21,6 +21,9 @@ import settings from mixins import VerbosityMixin from utils import get_file, timer, check_create_folder, exit +#for color +import matplotlib.pyplot as pyplot + class FileDoesNotExist(Exception): """ Exception to be used when the file does not exist. """ @@ -205,20 +208,28 @@ class Process(VerbosityMixin): self.output("Writing to file", normal=True, color='green', indent=1) return output_file - def run_ndvi(self, pansharpen=True, mode='grey'): + def run_ndvi(self, mode='grey'): """ Executes the image processing. - :param pansharpen: - Whether the process should also run pansharpenning. Default is True - :type pansharpen: - boolean - :returns: (String) the path to the processed image """ self.output("* Image processing started for NDVI", normal=True) + colorrange=numpy.array(range(0,255)) + colorbar=self._index2rgb(index_matrix=colorrange) + rgbArray = numpy.zeros((1,255,3), 'uint8') + rgbArray[..., 0] = colorbar[0] + rgbArray[..., 1] = colorbar[1] + rgbArray[..., 2] = colorbar[2] + rgbArray=rgbArray.repeat(30,0) + cbfig=pyplot.figure(figsize=(10,2),dpi=3000) + image = pyplot.imshow(rgbArray,extent=[-1,1,0,1],aspect=0.1) + pyplot.xticks(numpy.arange(-1,1.1,0.2)) + pyplot.yticks([]) + pyplot.savefig("colorbar.png") + # Read radiance conversion factors from mtl file try: with open(self.scene_path + '/' + self.scene + '_MTL.txt', 'rU') as mtl: @@ -240,10 +251,6 @@ class Process(VerbosityMixin): with rasterio.drivers(): bands = [] - # Add band 8 for pansharpenning - if pansharpen: - self.bands.append(8) - bands_path = [] for band in self.bands: @@ -290,10 +297,6 @@ class Process(VerbosityMixin): for i in range(0, 3): new_bands.append(numpy.empty(dst_shape, dtype=numpy.float32)) - if pansharpen: - bands[:3] = self._rescale(bands[:3]) - new_bands.append(numpy.empty(dst_shape, dtype=numpy.float32)) - self.output("Projecting", normal=True, arrow=True) for i, band in enumerate(bands): self.output("band %s" % self.bands[i], normal=True, color='green', indent=1) @@ -303,30 +306,21 @@ class Process(VerbosityMixin): # Bands are no longer needed del bands - if pansharpen: - new_bands = self._pansharpenning(new_bands) - del self.bands[3] - self.output("Final Steps", normal=True, arrow=True) - output_file = '%s_NDVI' % (self.scene) - if pansharpen: - output_file += '_pan' mask=(new_bands[1]+new_bands[0])==0 - - new_bands[0]=new_bands[0]*mult_B3+add_B3 new_bands[1]=new_bands[1]*mult_B4+add_B4 ndvi=numpy.true_divide((new_bands[1]-new_bands[0]),(new_bands[1]+new_bands[0])) ndvi[mask]=-1 - + ndvi=((ndvi+1)*255 / 2).astype(numpy.uint8) + + if mode=='grey': - ndvi=((ndvi+1)*255 / 2).astype(numpy.uint8) - output_file += '.TIF' - output_file = join(self.dst_path, output_file) - + output_file += '_grey.TIF' + output_file = join(self.dst_path, output_file) output = rasterio.open(output_file, 'w', driver='GTiff', width=dst_shape[1], height=dst_shape[0], count=1, dtype=numpy.uint8, @@ -336,28 +330,27 @@ class Process(VerbosityMixin): output.write_band(1, ndvi) elif mode=='color': - print('dummie') -# import matplotlib -# matplotlib.use('Agg') -# import matplotlib.pyplot as plt -# plt.imshow(ndvi) -# image = plt.gcf() -# image.set_size_inches(8, 8) -# plt.savefig('myfig',dpi=1000) -# output_file += '.PNG' -# output_file = join(self.dst_path, output_file) -# -# rgb=self._index2rgb(index_matrix=ndvi) -# -# output = rasterio.open(output_file, 'w', driver='GTiff', -# width=dst_shape[1], height=dst_shape[0], -# count=3, dtype=numpy.uint8, -# nodata=0, transform=dst_transform, photometric='RGB', -# crs=self.dst_crs) -# -# for i in range(0, 3): -# output.write_band(i+1, rgb[i]) - + output_file += '_color.TIF' + output_file = join(self.dst_path, output_file) + rgb=self._index2rgb(index_matrix=ndvi) + + output = rasterio.open(output_file, 'w', driver='GTiff', + width=dst_shape[1], height=dst_shape[0], + count=3, dtype=numpy.uint8, + nodata=0, transform=dst_transform, photometric='RGB', + crs=self.dst_crs) + + for i in range(0, 3): + output.write_band(i+1, rgb[i]) + + #colorbar +# a = np.array([[-1,1]]) +# pylab.figure(figsize=(9, 1.5)) +# img = pylab.imshow(a, cmap="Blues") +# pylab.gca().set_visible(False) +# cax = pylab.axes([0.1, 0.2, 0.8, 0.6]) +# pylab.colorbar(cax=cax, orientation='horizontal') +# pylab.savefig("colorbar.pdf") self.output("Writing to file", normal=True, color='green', indent=1) return output_file @@ -373,7 +366,7 @@ class Process(VerbosityMixin): pan = 1/m * bands[-1] del m - del bands[3] + del bands[-1] self.output("computing bands", normal=True, color='green', indent=1) for i, band in enumerate(bands): @@ -398,6 +391,49 @@ class Process(VerbosityMixin): def _read_band(self, band_path): """ Reads a band with rasterio """ return rasterio.open(band_path).read_band(1) + + + def _index2rgb(self, index_matrix): + """ converts a 8bit matrix to rgb values according to the colormap """ + + self._read_cmap() + translate_colormap = numpy.vectorize(self._get_color, otypes=[numpy.uint8]) + rgb_bands = [] + for i in range(3): + rgb_bands.append(translate_colormap(index_matrix, i)) + + return rgb_bands + + def _get_color(self, n,v=-1): + if v==-1: + return self.colormap[n] + else: + return self.colormap[n][v] + + def _read_cmap(self): + try: + i=0 + colormap={0 : (0, 0, 0, 255)} + with open(settings.COLORMAP) as cmap: + lines = cmap.readlines() + for line in lines: + if i == 0 and 'mode = ' in line: + i=1 + maxval = float(line.replace('mode = ', '')) + elif i > 0: + str = line.split(' ') + colormap.update({i : (int(round(float(str[1])*255/maxval)), + int(round(float(str[2])*255/maxval)), + int(round(float(str[3])*255/maxval)), + 255 + )}) + i += 1 + except IOError: + pass + + colormap = {k: v[:4] for k, v in colormap.iteritems()} + self.colormap=colormap + def _rescale(self, bands): """ Rescale bands """ diff --git a/landsat/landsat.py b/landsat/landsat.py index 439570c..13d4840 100755 --- a/landsat/landsat.py +++ b/landsat/landsat.py @@ -383,7 +383,7 @@ def process_image(path, bands=None, verbose=False, pansharpen=False, force_unzip except FileDoesNotExist as e: exit(e.message, 1) if isinstance(ndvi, str): - out=[p.run_ndvi(pansharpen,mode=ndvi)] + out=[p.run_ndvi(mode=ndvi)] else: out=[p.run_rgb(pansharpen)] return out diff --git a/landsat/settings.py b/landsat/settings.py index 3c55113..21a5012 100644 --- a/landsat/settings.py +++ b/landsat/settings.py @@ -27,3 +27,6 @@ BASE_DIR = abspath(dirname(__file__)) LANDSAT_DIR = join(HOME_DIR, 'landsat') DOWNLOAD_DIR = join(LANDSAT_DIR, 'downloads') PROCESSED_IMAGE = join(LANDSAT_DIR, 'processed') + +# Colormap File +COLORMAP = join(abspath(dirname(__file__)), 'colormap_cubehelix') From f687de888a47e449dc38dc57c27893ecb49fe7a4 Mon Sep 17 00:00:00 2001 From: julesair Date: Wed, 22 Jul 2015 15:21:03 +0200 Subject: [PATCH 08/14] finished colorbar creator --- landsat/colormap_cubehelix | 511 +++++++++++++++++++------------------ landsat/image.py | 56 ++-- landsat/landsat.py | 6 +- 3 files changed, 288 insertions(+), 285 deletions(-) diff --git a/landsat/colormap_cubehelix b/landsat/colormap_cubehelix index f129244..db1d8ad 100644 --- a/landsat/colormap_cubehelix +++ b/landsat/colormap_cubehelix @@ -17,257 +17,260 @@ # faction and R/G/B values # mode = 1 -0.000 0.000 0.000 0.000 -0.004 0.011 0.011 0.018 -0.008 0.019 0.020 0.033 -0.012 0.025 0.027 0.047 -0.016 0.030 0.035 0.060 -0.020 0.034 0.042 0.073 -0.024 0.038 0.050 0.085 -0.028 0.041 0.057 0.097 -0.032 0.043 0.065 0.108 -0.036 0.044 0.073 0.118 -0.040 0.046 0.081 0.128 -0.043 0.046 0.089 0.137 -0.047 0.047 0.097 0.146 -0.051 0.047 0.105 0.154 -0.055 0.046 0.114 0.162 -0.059 0.046 0.122 0.169 -0.063 0.045 0.131 0.175 -0.067 0.044 0.140 0.181 -0.071 0.042 0.148 0.186 -0.075 0.041 0.157 0.190 -0.079 0.040 0.166 0.194 -0.083 0.038 0.175 0.197 -0.087 0.037 0.184 0.199 -0.091 0.036 0.193 0.201 -0.095 0.034 0.202 0.202 -0.099 0.033 0.211 0.202 -0.103 0.033 0.220 0.202 -0.107 0.032 0.229 0.201 -0.111 0.032 0.238 0.200 -0.115 0.032 0.247 0.198 -0.119 0.032 0.255 0.195 -0.123 0.033 0.264 0.192 -0.126 0.034 0.272 0.188 -0.130 0.036 0.280 0.184 -0.134 0.038 0.288 0.179 -0.138 0.040 0.295 0.174 -0.142 0.043 0.303 0.169 -0.146 0.047 0.310 0.163 -0.150 0.051 0.317 0.157 -0.154 0.056 0.323 0.150 -0.158 0.061 0.330 0.144 -0.162 0.067 0.335 0.137 -0.166 0.074 0.341 0.130 -0.170 0.081 0.346 0.123 -0.174 0.089 0.351 0.116 -0.178 0.098 0.356 0.109 -0.182 0.107 0.360 0.102 -0.186 0.117 0.364 0.095 -0.190 0.127 0.367 0.088 -0.194 0.138 0.370 0.082 -0.198 0.150 0.373 0.075 -0.202 0.162 0.375 0.069 -0.206 0.175 0.377 0.064 -0.209 0.188 0.379 0.058 -0.213 0.202 0.380 0.053 -0.217 0.217 0.381 0.049 -0.221 0.232 0.381 0.045 -0.225 0.247 0.381 0.041 -0.229 0.263 0.381 0.038 -0.233 0.279 0.380 0.036 -0.237 0.296 0.379 0.034 -0.241 0.313 0.378 0.033 -0.245 0.330 0.376 0.033 -0.249 0.348 0.374 0.034 -0.253 0.365 0.372 0.035 -0.257 0.383 0.370 0.037 -0.261 0.401 0.367 0.040 -0.265 0.420 0.364 0.043 -0.269 0.438 0.361 0.048 -0.273 0.456 0.358 0.053 -0.277 0.474 0.354 0.059 -0.281 0.492 0.351 0.067 -0.285 0.510 0.347 0.074 -0.289 0.528 0.343 0.083 -0.292 0.545 0.339 0.093 -0.296 0.563 0.335 0.103 -0.300 0.580 0.331 0.115 -0.304 0.596 0.328 0.127 -0.308 0.612 0.324 0.140 -0.312 0.628 0.320 0.154 -0.316 0.643 0.316 0.168 -0.320 0.658 0.313 0.184 -0.324 0.672 0.309 0.200 -0.328 0.686 0.306 0.216 -0.332 0.699 0.303 0.234 -0.336 0.711 0.300 0.252 -0.340 0.723 0.297 0.271 -0.344 0.733 0.295 0.290 -0.348 0.744 0.292 0.310 -0.352 0.753 0.290 0.330 -0.356 0.761 0.289 0.351 -0.360 0.769 0.288 0.372 -0.364 0.776 0.287 0.393 -0.368 0.782 0.286 0.415 -0.372 0.787 0.286 0.437 -0.375 0.791 0.286 0.459 -0.379 0.794 0.287 0.481 -0.383 0.797 0.288 0.504 -0.387 0.798 0.290 0.526 -0.391 0.799 0.292 0.549 -0.395 0.799 0.294 0.571 -0.399 0.798 0.297 0.593 -0.403 0.795 0.300 0.615 -0.407 0.793 0.304 0.637 -0.411 0.789 0.308 0.658 -0.415 0.784 0.313 0.680 -0.419 0.779 0.318 0.700 -0.423 0.773 0.324 0.721 -0.427 0.766 0.330 0.740 -0.431 0.758 0.337 0.760 -0.435 0.750 0.344 0.778 -0.439 0.741 0.352 0.796 -0.443 0.731 0.360 0.814 -0.447 0.721 0.368 0.830 -0.451 0.710 0.377 0.846 -0.455 0.699 0.386 0.861 -0.458 0.687 0.396 0.876 -0.462 0.674 0.406 0.889 -0.466 0.662 0.416 0.902 -0.470 0.649 0.427 0.913 -0.474 0.635 0.438 0.924 -0.478 0.622 0.449 0.934 -0.482 0.608 0.461 0.942 -0.486 0.594 0.473 0.950 -0.490 0.580 0.485 0.957 -0.494 0.566 0.497 0.962 -0.498 0.552 0.510 0.967 -0.502 0.538 0.522 0.971 -0.506 0.524 0.535 0.973 -0.510 0.510 0.548 0.975 -0.514 0.496 0.561 0.975 -0.518 0.483 0.574 0.975 -0.522 0.470 0.587 0.973 -0.526 0.457 0.600 0.971 -0.530 0.445 0.613 0.968 -0.534 0.433 0.626 0.963 -0.538 0.421 0.639 0.958 -0.542 0.410 0.652 0.952 -0.545 0.400 0.664 0.945 -0.549 0.390 0.677 0.937 -0.553 0.381 0.689 0.929 -0.557 0.372 0.701 0.919 -0.561 0.365 0.713 0.909 -0.565 0.357 0.725 0.898 -0.569 0.351 0.736 0.887 -0.573 0.346 0.747 0.875 -0.577 0.341 0.758 0.862 -0.581 0.337 0.768 0.849 -0.585 0.334 0.778 0.836 -0.589 0.332 0.788 0.822 -0.593 0.330 0.797 0.808 -0.597 0.330 0.806 0.793 -0.601 0.330 0.814 0.779 -0.605 0.332 0.822 0.764 -0.609 0.334 0.830 0.749 -0.613 0.337 0.837 0.734 -0.617 0.341 0.844 0.719 -0.621 0.346 0.850 0.703 -0.625 0.352 0.856 0.688 -0.628 0.358 0.861 0.674 -0.632 0.366 0.866 0.659 -0.636 0.374 0.870 0.645 -0.640 0.383 0.874 0.630 -0.644 0.393 0.878 0.617 -0.648 0.403 0.881 0.603 -0.652 0.415 0.883 0.590 -0.656 0.427 0.885 0.578 -0.660 0.439 0.887 0.566 -0.664 0.453 0.888 0.555 -0.668 0.466 0.889 0.544 -0.672 0.481 0.889 0.534 -0.676 0.496 0.889 0.525 -0.680 0.511 0.889 0.516 -0.684 0.527 0.888 0.508 -0.688 0.543 0.887 0.501 -0.692 0.560 0.885 0.494 -0.696 0.577 0.883 0.489 -0.700 0.594 0.881 0.484 -0.704 0.612 0.879 0.480 -0.708 0.629 0.876 0.477 -0.711 0.647 0.873 0.475 -0.715 0.665 0.870 0.474 -0.719 0.682 0.867 0.473 -0.723 0.700 0.864 0.473 -0.727 0.718 0.860 0.475 -0.731 0.736 0.856 0.477 -0.735 0.753 0.853 0.480 -0.739 0.770 0.849 0.484 -0.743 0.788 0.845 0.488 -0.747 0.804 0.841 0.494 -0.751 0.821 0.837 0.500 -0.755 0.837 0.833 0.507 -0.759 0.853 0.830 0.515 -0.763 0.868 0.826 0.523 -0.767 0.883 0.822 0.533 -0.771 0.897 0.819 0.542 -0.775 0.911 0.815 0.553 -0.779 0.924 0.812 0.564 -0.783 0.937 0.809 0.576 -0.787 0.949 0.806 0.588 -0.791 0.961 0.804 0.600 -0.794 0.972 0.801 0.613 -0.798 0.982 0.799 0.627 -0.802 0.992 0.797 0.640 -0.806 1.000 0.796 0.655 -0.810 1.000 0.795 0.669 -0.814 1.000 0.794 0.683 -0.818 1.000 0.793 0.698 -0.822 1.000 0.793 0.713 -0.826 1.000 0.793 0.727 -0.830 1.000 0.793 0.742 -0.834 1.000 0.793 0.757 -0.838 1.000 0.794 0.772 -0.842 1.000 0.796 0.786 -0.846 1.000 0.797 0.801 -0.850 1.000 0.799 0.815 -0.854 1.000 0.802 0.829 -0.858 1.000 0.804 0.843 -0.862 1.000 0.807 0.856 -0.866 1.000 0.810 0.869 -0.870 1.000 0.814 0.882 -0.874 1.000 0.818 0.894 -0.877 1.000 0.822 0.906 -0.881 1.000 0.826 0.917 -0.885 1.000 0.831 0.928 -0.889 1.000 0.836 0.938 -0.893 1.000 0.841 0.948 -0.897 1.000 0.847 0.957 -0.901 1.000 0.852 0.966 -0.905 1.000 0.858 0.973 -0.909 1.000 0.864 0.981 -0.913 1.000 0.870 0.987 -0.917 1.000 0.876 0.994 -0.921 1.000 0.883 0.999 -0.925 1.000 0.889 1.000 -0.929 1.000 0.896 1.000 -0.933 1.000 0.902 1.000 -0.937 1.000 0.909 1.000 -0.941 1.000 0.915 1.000 -0.945 0.998 0.922 1.000 -0.949 0.996 0.928 1.000 -0.953 0.994 0.935 1.000 -0.957 0.992 0.941 1.000 -0.960 0.990 0.947 1.000 -0.964 0.989 0.953 1.000 -0.968 0.988 0.959 1.000 -0.972 0.988 0.965 1.000 -0.976 0.988 0.971 1.000 -0.980 0.989 0.976 1.000 -0.984 0.990 0.982 1.000 -0.988 0.992 0.987 1.000 -0.992 0.994 0.991 1.000 -0.996 0.997 0.996 1.000 -1.000 1.000 1.000 1.000 +0 0 0 +0.006299213 0.006299213 0.006299213 +0.012598425 0.012598425 0.012598425 +0.018897638 0.018897638 0.018897638 +0.02519685 0.02519685 0.02519685 +0.031496063 0.031496063 0.031496063 +0.037795275 0.037795275 0.037795275 +0.044094488 0.044094488 0.044094488 +0.050393701 0.050393701 0.050393701 +0.056692913 0.056692913 0.056692913 +0.062992126 0.062992126 0.062992126 +0.069291338 0.069291338 0.069291338 +0.075590551 0.075590551 0.075590551 +0.081889763 0.081889763 0.081889763 +0.088188976 0.088188976 0.088188976 +0.094488189 0.094488189 0.094488189 +0.100787401 0.100787401 0.100787401 +0.107086614 0.107086614 0.107086614 +0.113385826 0.113385826 0.113385826 +0.119685039 0.119685039 0.119685039 +0.125984251 0.125984251 0.125984251 +0.132283464 0.132283464 0.132283464 +0.138582677 0.138582677 0.138582677 +0.144881889 0.144881889 0.144881889 +0.151181102 0.151181102 0.151181102 +0.157480314 0.157480314 0.157480314 +0.163779527 0.163779527 0.163779527 +0.17007874 0.17007874 0.17007874 +0.176377952 0.176377952 0.176377952 +0.182677165 0.182677165 0.182677165 +0.188976377 0.188976377 0.188976377 +0.19527559 0.19527559 0.19527559 +0.201574802 0.201574802 0.201574802 +0.207874015 0.207874015 0.207874015 +0.214173228 0.214173228 0.214173228 +0.22047244 0.22047244 0.22047244 +0.226771653 0.226771653 0.226771653 +0.233070865 0.233070865 0.233070865 +0.239370078 0.239370078 0.239370078 +0.24566929 0.24566929 0.24566929 +0.251968503 0.251968503 0.251968503 +0.25826773 0.25826773 0.25826773 +0.264566928 0.264566928 0.264566928 +0.270866156 0.270866156 0.270866156 +0.277165353 0.277165353 0.277165353 +0.283464581 0.283464581 0.283464581 +0.289763778 0.289763778 0.289763778 +0.296063006 0.296063006 0.296063006 +0.302362204 0.302362204 0.302362204 +0.308661431 0.308661431 0.308661431 +0.314960629 0.314960629 0.314960629 +0.321259856 0.321259856 0.321259856 +0.327559054 0.327559054 0.327559054 +0.333858281 0.333858281 0.333858281 +0.340157479 0.340157479 0.340157479 +0.346456707 0.346456707 0.346456707 +0.352755904 0.352755904 0.352755904 +0.359055132 0.359055132 0.359055132 +0.365354329 0.365354329 0.365354329 +0.371653557 0.371653557 0.371653557 +0.377952754 0.377952754 0.377952754 +0.384251982 0.384251982 0.384251982 +0.39055118 0.39055118 0.39055118 +0.396850407 0.396850407 0.396850407 +0.403149605 0.403149605 0.403149605 +0.409448832 0.409448832 0.409448832 +0.41574803 0.41574803 0.41574803 +0.422047257 0.422047257 0.422047257 +0.428346455 0.428346455 0.428346455 +0.434645683 0.434645683 0.434645683 +0.44094488 0.44094488 0.44094488 +0.447244108 0.447244108 0.447244108 +0.453543305 0.453543305 0.453543305 +0.459842533 0.459842533 0.459842533 +0.466141731 0.466141731 0.466141731 +0.472440958 0.472440958 0.472440958 +0.478740156 0.478740156 0.478740156 +0.485039383 0.485039383 0.485039383 +0.491338581 0.491338581 0.491338581 +0.497637808 0.497637808 0.497637808 +0.503937006 0.503937006 0.503937006 +0.510236204 0.510236204 0.510236204 +0.516535461 0.516535461 0.516535461 +0.522834659 0.522834659 0.522834659 +0.529133856 0.529133856 0.529133856 +0.535433054 0.535433054 0.535433054 +0.541732311 0.541732311 0.541732311 +0.548031509 0.548031509 0.548031509 +0.554330707 0.554330707 0.554330707 +0.560629904 0.560629904 0.560629904 +0.566929162 0.566929162 0.566929162 +0.573228359 0.573228359 0.573228359 +0.579527557 0.579527557 0.579527557 +0.585826755 0.585826755 0.585826755 +0.592126012 0.592126012 0.592126012 +0.59842521 0.59842521 0.59842521 +0.604724407 0.604724407 0.604724407 +0.611023605 0.611023605 0.611023605 +0.617322862 0.617322862 0.617322862 +0.62362206 0.62362206 0.62362206 +0.629921257 0.629921257 0.629921257 +0.636220455 0.636220455 0.636220455 +0.642519712 0.642519712 0.642519712 +0.64881891 0.64881891 0.64881891 +0.655118108 0.655118108 0.655118108 +0.661417305 0.661417305 0.661417305 +0.667716563 0.667716563 0.667716563 +0.67401576 0.67401576 0.67401576 +0.680314958 0.680314958 0.680314958 +0.686614156 0.686614156 0.686614156 +0.692913413 0.692913413 0.692913413 +0.699212611 0.699212611 0.699212611 +0.705511808 0.705511808 0.705511808 +0.711811006 0.711811006 0.711811006 +0.718110263 0.718110263 0.718110263 +0.724409461 0.724409461 0.724409461 +0.730708659 0.730708659 0.730708659 +0.737007856 0.737007856 0.737007856 +0.743307114 0.743307114 0.743307114 +0.749606311 0.749606311 0.749606311 +0.755905509 0.755905509 0.755905509 +0.762204707 0.762204707 0.762204707 +0.768503964 0.768503964 0.768503964 +0.774803162 0.774803162 0.774803162 +0.781102359 0.781102359 0.781102359 +0.787401557 0.787401557 0.787401557 +0.793700814 0.793700814 0.793700814 +0.800000012 0.800000012 0.800000012 +0.768627465 0.368627459 0.168627456 +0.768929541 0.369346976 0.16918768 +0.769231617 0.370066464 0.169747904 +0.769533694 0.370785981 0.170308128 +0.76983577 0.371505469 0.170868352 +0.770137846 0.372224987 0.171428576 +0.770439982 0.372944474 0.1719888 +0.770742059 0.373663992 0.172549024 +0.771044135 0.374383479 0.173109248 +0.771346211 0.375102997 0.173669472 +0.771648288 0.375822484 0.174229696 +0.771950364 0.376542002 0.17478992 +0.77225244 0.37726149 0.175350145 +0.772554517 0.377981007 0.175910369 +0.772856593 0.378700495 0.176470593 +0.773158669 0.379420012 0.177030817 +0.773460805 0.3801395 0.177591041 +0.773762882 0.380859017 0.178151265 +0.774064958 0.381578505 0.178711489 +0.774367034 0.382298023 0.179271713 +0.774669111 0.38301751 0.179831937 +0.774971187 0.383737028 0.180392161 +0.780972838 0.400119334 0.194455713 +0.786974549 0.416501611 0.20851928 +0.792976201 0.432883918 0.222582832 +0.798977852 0.449266195 0.236646384 +0.804979563 0.465648502 0.250709951 +0.810981214 0.482030779 0.264773518 +0.816982865 0.498413086 0.278837055 +0.822984517 0.514795363 0.292900622 +0.828986228 0.53117764 0.306964189 +0.834987879 0.547559977 0.321027726 +0.84098953 0.563942254 0.335091293 +0.846991241 0.580324531 0.34915486 +0.852992892 0.596706867 0.363218397 +0.858994544 0.613089144 0.377281964 +0.864996254 0.629471421 0.391345531 +0.870997906 0.645853698 0.405409068 +0.876999557 0.662236035 0.419472635 +0.883001268 0.678618312 0.433536202 +0.889002919 0.695000589 0.447599739 +0.89500457 0.711382926 0.461663306 +0.901006281 0.727765203 0.475726873 +0.907007933 0.74414748 0.48979041 +0.913009584 0.760529757 0.503853977 +0.919011235 0.776912093 0.517917514 +0.925012946 0.79329437 0.531981111 +0.931014597 0.809676647 0.546044648 +0.937016249 0.826058924 0.560108185 +0.94301796 0.842441261 0.574171782 +0.949019611 0.858823538 0.588235319 +0.913870752 0.858823538 0.566448808 +0.878721833 0.858823538 0.544662356 +0.843572974 0.858823538 0.522875845 +0.808424115 0.858823538 0.501089334 +0.773275256 0.858823538 0.479302853 +0.738126338 0.858823538 0.457516372 +0.702977479 0.858823538 0.435729861 +0.667828619 0.858823538 0.41394338 +0.63267976 0.858823538 0.392156869 +0.597530842 0.858823538 0.370370388 +0.562381983 0.858823538 0.348583907 +0.527233124 0.858823538 0.326797396 +0.492084235 0.858823538 0.305010915 +0.456935376 0.858823538 0.283224404 +0.421786487 0.858823538 0.261437923 +0.386637628 0.858823538 0.239651427 +0.351488739 0.858823538 0.217864931 +0.31633988 0.858823538 0.196078435 +0.281190991 0.858823538 0.174291953 +0.246042117 0.858823538 0.152505457 +0.210893244 0.858823538 0.130718961 +0.17574437 0.858823538 0.108932465 +0.140595496 0.858823538 0.087145977 +0.105446622 0.858823538 0.065359481 +0.070297748 0.858823538 0.043572988 +0.035148874 0.858823538 0.021786494 +0 0.858823538 0 +0 0.846087515 0 +0 0.833351493 0 +0 0.82061547 0 +0 0.807879448 0 +0 0.795143425 0 +0 0.782407403 0 +0 0.769671381 0 +0 0.756935358 0 +0 0.744199336 0 +0 0.731463313 0 +0 0.718727291 0 +0 0.705991268 0 +0 0.693255246 0 +0 0.680519283 0 +0 0.66778326 0 +0 0.655047238 0 +0 0.642311215 0 +0 0.629575193 0 +0 0.61683917 0 +0 0.604103148 0 +0 0.591367126 0 +0 0.578631103 0 +0 0.565895081 0 +0 0.553159058 0 +0 0.540423036 0 +0 0.527687013 0 +0 0.514950991 0 +0 0.514215708 0 +0 0.513480425 0 +0 0.512745082 0 +0 0.512009799 0 +0 0.511274517 0 +0 0.510539234 0 +0 0.509803951 0 +0 0.509068608 0 +0 0.508333325 0 +0 0.507598042 0 +0 0.50686276 0 +0 0.506127477 0 +0 0.505392134 0 +0 0.504656851 0 +0 0.503921568 0 +0 0.503186285 0 +0 0.502451003 0 +0 0.50171566 0 +0 0.500980377 0 +0 0.500245094 0 +0 0.499509811 0 +0 0.498774499 0 +0 0.498039216 0 + diff --git a/landsat/image.py b/landsat/image.py index 6e67996..e67befb 100644 --- a/landsat/image.py +++ b/landsat/image.py @@ -22,7 +22,7 @@ from mixins import VerbosityMixin from utils import get_file, timer, check_create_folder, exit #for color -import matplotlib.pyplot as pyplot +import matplotlib.pylab as pyplot class FileDoesNotExist(Exception): @@ -217,19 +217,6 @@ class Process(VerbosityMixin): self.output("* Image processing started for NDVI", normal=True) - colorrange=numpy.array(range(0,255)) - colorbar=self._index2rgb(index_matrix=colorrange) - rgbArray = numpy.zeros((1,255,3), 'uint8') - rgbArray[..., 0] = colorbar[0] - rgbArray[..., 1] = colorbar[1] - rgbArray[..., 2] = colorbar[2] - rgbArray=rgbArray.repeat(30,0) - cbfig=pyplot.figure(figsize=(10,2),dpi=3000) - image = pyplot.imshow(rgbArray,extent=[-1,1,0,1],aspect=0.1) - pyplot.xticks(numpy.arange(-1,1.1,0.2)) - pyplot.yticks([]) - pyplot.savefig("colorbar.png") - # Read radiance conversion factors from mtl file try: with open(self.scene_path + '/' + self.scene + '_MTL.txt', 'rU') as mtl: @@ -306,8 +293,8 @@ class Process(VerbosityMixin): # Bands are no longer needed del bands - self.output("Final Steps", normal=True, arrow=True) + self.output("Calculating NDVI", normal=True, arrow=True) output_file = '%s_NDVI' % (self.scene) mask=(new_bands[1]+new_bands[0])==0 @@ -318,6 +305,7 @@ class Process(VerbosityMixin): ndvi=((ndvi+1)*255 / 2).astype(numpy.uint8) + self.output("Final Steps", normal=True, arrow=True) if mode=='grey': output_file += '_grey.TIF' output_file = join(self.dst_path, output_file) @@ -343,15 +331,24 @@ class Process(VerbosityMixin): for i in range(0, 3): output.write_band(i+1, rgb[i]) - #colorbar -# a = np.array([[-1,1]]) -# pylab.figure(figsize=(9, 1.5)) -# img = pylab.imshow(a, cmap="Blues") -# pylab.gca().set_visible(False) -# cax = pylab.axes([0.1, 0.2, 0.8, 0.6]) -# pylab.colorbar(cax=cax, orientation='horizontal') -# pylab.savefig("colorbar.pdf") - + #colorbar + output_file2 = '%s_colorbar.png' % (self.scene) + + colorrange=numpy.array(range(0,256)) + colorbar=self._index2rgb(index_matrix=colorrange) + rgbArray = numpy.zeros((1,256,3), 'uint8') + rgbArray[..., 0] = colorbar[0] + rgbArray[..., 1] = colorbar[1] + rgbArray[..., 2] = colorbar[2] + rgbArray=rgbArray.repeat(30,0) + + cbfig=pyplot.figure(figsize=(10,1.5)) + image = pyplot.imshow(rgbArray,extent=[-1,1,0,1],aspect=0.1) + pyplot.xticks(numpy.arange(-1,1.1,0.2)) + pyplot.xlabel('NDVI') + pyplot.yticks([]) + pyplot.savefig(output_file2,dpi=300, bbox_inches='tight', transparent=True) + self.output("Writing to file", normal=True, color='green', indent=1) return output_file @@ -413,7 +410,7 @@ class Process(VerbosityMixin): def _read_cmap(self): try: i=0 - colormap={0 : (0, 0, 0, 255)} + colormap={0 : (0, 0, 0)} with open(settings.COLORMAP) as cmap: lines = cmap.readlines() for line in lines: @@ -421,11 +418,12 @@ class Process(VerbosityMixin): i=1 maxval = float(line.replace('mode = ', '')) elif i > 0: - str = line.split(' ') - colormap.update({i : (int(round(float(str[1])*255/maxval)), + str = line.split() + if str==[]: + break + colormap.update({i : (int(round(float(str[0])*255/maxval)), + int(round(float(str[1])*255/maxval)), int(round(float(str[2])*255/maxval)), - int(round(float(str[3])*255/maxval)), - 255 )}) i += 1 except IOError: diff --git a/landsat/landsat.py b/landsat/landsat.py index 13d4840..3faa109 100755 --- a/landsat/landsat.py +++ b/landsat/landsat.py @@ -179,7 +179,7 @@ def args_options(): parser_download.add_argument('-p', '--process', help='Process the image after download', action='store_true') parser_download.add_argument('--pansharpen', action='store_true', help='Whether to also pansharpen the process ' - 'image. Pansharpening requires larger memory') + 'image. Pansharpening requires larger memory') parser_download.add_argument('--ndvi', type=str, choices=['color','grey'], help='Create an NDVI map. Specify if output should be GTIFF in grayscale (grey) ' 'or a .PNG with a colormap (color)') @@ -309,6 +309,8 @@ def main(args): return [result['message'], 1] elif args.subs == 'download': d = Downloader(download_dir=args.dest) + if isinstance(args.ndvi, str): + args.bands=[4,5] try: bands = convert_to_integer_list(args.bands) if args.pansharpen: @@ -375,7 +377,7 @@ def process_image(path, bands=None, verbose=False, pansharpen=False, force_unzip bands = convert_to_integer_list(bands) if isinstance(ndvi, str): - p = Process(path, bands=[4,5], verbose=verbose, force_unzip=force_unzip) + p = Process(path, bands=bands, verbose=verbose, force_unzip=force_unzip) else: p = Process(path, bands=bands, verbose=verbose, force_unzip=force_unzip) except IOError: From 670f725d3cf06416117a2771f19973dbad5b1db6 Mon Sep 17 00:00:00 2001 From: julesair Date: Wed, 22 Jul 2015 17:06:58 +0200 Subject: [PATCH 09/14] NDVI almost done --- landsat/image.py | 54 +++++++++++++++++++++++++--------------------- landsat/landsat.py | 2 +- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/landsat/image.py b/landsat/image.py index e67befb..55b1851 100644 --- a/landsat/image.py +++ b/landsat/image.py @@ -200,12 +200,13 @@ class Process(VerbosityMixin): for i, band in enumerate(new_bands): # Color Correction - band = self._color_correction(band, self.bands[i], 0, cloud_cover) - - output.write_band(i+1, img_as_ubyte(band)) - - new_bands[i] = None + new_bands[i] = self._color_correction(band, self.bands[i], 0, cloud_cover) + self.output("Writing to file", normal=True, color='green', indent=1) + for i, band in enumerate(new_bands): + output.write_band(i+1, img_as_ubyte(band)) + new_bands[i] = None + return output_file def run_ndvi(self, mode='grey'): @@ -314,42 +315,45 @@ class Process(VerbosityMixin): count=1, dtype=numpy.uint8, nodata=0, transform=dst_transform, crs=self.dst_crs) - + self.output("Writing to file", normal=True, color='green', indent=1) output.write_band(1, ndvi) elif mode=='color': + self.output("Converting to RGB", normal=True, color='green', indent=1) output_file += '_color.TIF' output_file = join(self.dst_path, output_file) rgb=self._index2rgb(index_matrix=ndvi) + del ndvi output = rasterio.open(output_file, 'w', driver='GTiff', width=dst_shape[1], height=dst_shape[0], count=3, dtype=numpy.uint8, nodata=0, transform=dst_transform, photometric='RGB', crs=self.dst_crs) - + + self.output("Writing to file", normal=True, color='green', indent=1) for i in range(0, 3): output.write_band(i+1, rgb[i]) #colorbar - output_file2 = '%s_colorbar.png' % (self.scene) - - colorrange=numpy.array(range(0,256)) - colorbar=self._index2rgb(index_matrix=colorrange) - rgbArray = numpy.zeros((1,256,3), 'uint8') - rgbArray[..., 0] = colorbar[0] - rgbArray[..., 1] = colorbar[1] - rgbArray[..., 2] = colorbar[2] - rgbArray=rgbArray.repeat(30,0) - - cbfig=pyplot.figure(figsize=(10,1.5)) - image = pyplot.imshow(rgbArray,extent=[-1,1,0,1],aspect=0.1) - pyplot.xticks(numpy.arange(-1,1.1,0.2)) - pyplot.xlabel('NDVI') - pyplot.yticks([]) - pyplot.savefig(output_file2,dpi=300, bbox_inches='tight', transparent=True) - - self.output("Writing to file", normal=True, color='green', indent=1) + self.output("Creating colorbar", normal=True, color='green', indent=1) + output_file2 = '%s_colorbar.png' % (self.scene) + output_file2 = join(self.dst_path, output_file2) + colorrange=numpy.array(range(0,256)) + colorbar=self._index2rgb(index_matrix=colorrange) + rgbArray = numpy.zeros((1,256,3), 'uint8') + rgbArray[..., 0] = colorbar[0] + rgbArray[..., 1] = colorbar[1] + rgbArray[..., 2] = colorbar[2] + rgbArray=rgbArray.repeat(30,0) + + cbfig=pyplot.figure(figsize=(10,1.5)) + image = pyplot.imshow(rgbArray,extent=[-1,1,0,1],aspect=0.1) + pyplot.xticks(numpy.arange(-1,1.1,0.2)) + pyplot.xlabel('NDVI') + pyplot.yticks([]) + pyplot.savefig(output_file2,dpi=300, bbox_inches='tight', transparent=True) + return output_file def _pansharpenning(self, bands): diff --git a/landsat/landsat.py b/landsat/landsat.py index 3faa109..e5a35ed 100755 --- a/landsat/landsat.py +++ b/landsat/landsat.py @@ -310,7 +310,7 @@ def main(args): elif args.subs == 'download': d = Downloader(download_dir=args.dest) if isinstance(args.ndvi, str): - args.bands=[4,5] + args.bands='45' try: bands = convert_to_integer_list(args.bands) if args.pansharpen: From d2798ba6d502d707d03947172dab5ae91ba3af9c Mon Sep 17 00:00:00 2001 From: julesair Date: Thu, 23 Jul 2015 10:06:32 +0200 Subject: [PATCH 10/14] completed argparse help for NDVI and option --latest --- landsat/colormapNDVI | 276 ++++++++++++++++++++++++++++++++++++++++++ landsat/downloader.py | 3 +- landsat/landsat.py | 13 +- 3 files changed, 286 insertions(+), 6 deletions(-) create mode 100644 landsat/colormapNDVI diff --git a/landsat/colormapNDVI b/landsat/colormapNDVI new file mode 100644 index 0000000..6a4c816 --- /dev/null +++ b/landsat/colormapNDVI @@ -0,0 +1,276 @@ +# Tue Jul 14 2015 14:40:26 GMT+0200 +# --------------------------------------------- +# R/G/B cubehelix colour scheme +# +# see http://www.mrao.cam.ac.uk/~dag/CUBEHELIX/ +#---------------------------------------------- +# see Green (2011), BASI, 39, 289. +# +# start............: 3.0 +# rotations........: -2 +# hue..............: 1.6 +# gamma............: 0.8 +# number of levels.: 254 +#---------------------------------------------- +# Dave Green: dag@mrao.cam.ac.uk +#---------------------------------------------- +# faction and R/G/B values +# +mode = 1 +0 0 0 +0.006299213 0.006299213 0.006299213 +0.012598425 0.012598425 0.012598425 +0.018897638 0.018897638 0.018897638 +0.02519685 0.02519685 0.02519685 +0.031496063 0.031496063 0.031496063 +0.037795275 0.037795275 0.037795275 +0.044094488 0.044094488 0.044094488 +0.050393701 0.050393701 0.050393701 +0.056692913 0.056692913 0.056692913 +0.062992126 0.062992126 0.062992126 +0.069291338 0.069291338 0.069291338 +0.075590551 0.075590551 0.075590551 +0.081889763 0.081889763 0.081889763 +0.088188976 0.088188976 0.088188976 +0.094488189 0.094488189 0.094488189 +0.100787401 0.100787401 0.100787401 +0.107086614 0.107086614 0.107086614 +0.113385826 0.113385826 0.113385826 +0.119685039 0.119685039 0.119685039 +0.125984251 0.125984251 0.125984251 +0.132283464 0.132283464 0.132283464 +0.138582677 0.138582677 0.138582677 +0.144881889 0.144881889 0.144881889 +0.151181102 0.151181102 0.151181102 +0.157480314 0.157480314 0.157480314 +0.163779527 0.163779527 0.163779527 +0.17007874 0.17007874 0.17007874 +0.176377952 0.176377952 0.176377952 +0.182677165 0.182677165 0.182677165 +0.188976377 0.188976377 0.188976377 +0.19527559 0.19527559 0.19527559 +0.201574802 0.201574802 0.201574802 +0.207874015 0.207874015 0.207874015 +0.214173228 0.214173228 0.214173228 +0.22047244 0.22047244 0.22047244 +0.226771653 0.226771653 0.226771653 +0.233070865 0.233070865 0.233070865 +0.239370078 0.239370078 0.239370078 +0.24566929 0.24566929 0.24566929 +0.251968503 0.251968503 0.251968503 +0.25826773 0.25826773 0.25826773 +0.264566928 0.264566928 0.264566928 +0.270866156 0.270866156 0.270866156 +0.277165353 0.277165353 0.277165353 +0.283464581 0.283464581 0.283464581 +0.289763778 0.289763778 0.289763778 +0.296063006 0.296063006 0.296063006 +0.302362204 0.302362204 0.302362204 +0.308661431 0.308661431 0.308661431 +0.314960629 0.314960629 0.314960629 +0.321259856 0.321259856 0.321259856 +0.327559054 0.327559054 0.327559054 +0.333858281 0.333858281 0.333858281 +0.340157479 0.340157479 0.340157479 +0.346456707 0.346456707 0.346456707 +0.352755904 0.352755904 0.352755904 +0.359055132 0.359055132 0.359055132 +0.365354329 0.365354329 0.365354329 +0.371653557 0.371653557 0.371653557 +0.377952754 0.377952754 0.377952754 +0.384251982 0.384251982 0.384251982 +0.39055118 0.39055118 0.39055118 +0.396850407 0.396850407 0.396850407 +0.403149605 0.403149605 0.403149605 +0.409448832 0.409448832 0.409448832 +0.41574803 0.41574803 0.41574803 +0.422047257 0.422047257 0.422047257 +0.428346455 0.428346455 0.428346455 +0.434645683 0.434645683 0.434645683 +0.44094488 0.44094488 0.44094488 +0.447244108 0.447244108 0.447244108 +0.453543305 0.453543305 0.453543305 +0.459842533 0.459842533 0.459842533 +0.466141731 0.466141731 0.466141731 +0.472440958 0.472440958 0.472440958 +0.478740156 0.478740156 0.478740156 +0.485039383 0.485039383 0.485039383 +0.491338581 0.491338581 0.491338581 +0.497637808 0.497637808 0.497637808 +0.503937006 0.503937006 0.503937006 +0.510236204 0.510236204 0.510236204 +0.516535461 0.516535461 0.516535461 +0.522834659 0.522834659 0.522834659 +0.529133856 0.529133856 0.529133856 +0.535433054 0.535433054 0.535433054 +0.541732311 0.541732311 0.541732311 +0.548031509 0.548031509 0.548031509 +0.554330707 0.554330707 0.554330707 +0.560629904 0.560629904 0.560629904 +0.566929162 0.566929162 0.566929162 +0.573228359 0.573228359 0.573228359 +0.579527557 0.579527557 0.579527557 +0.585826755 0.585826755 0.585826755 +0.592126012 0.592126012 0.592126012 +0.59842521 0.59842521 0.59842521 +0.604724407 0.604724407 0.604724407 +0.611023605 0.611023605 0.611023605 +0.617322862 0.617322862 0.617322862 +0.62362206 0.62362206 0.62362206 +0.629921257 0.629921257 0.629921257 +0.636220455 0.636220455 0.636220455 +0.642519712 0.642519712 0.642519712 +0.64881891 0.64881891 0.64881891 +0.655118108 0.655118108 0.655118108 +0.661417305 0.661417305 0.661417305 +0.667716563 0.667716563 0.667716563 +0.67401576 0.67401576 0.67401576 +0.680314958 0.680314958 0.680314958 +0.686614156 0.686614156 0.686614156 +0.692913413 0.692913413 0.692913413 +0.699212611 0.699212611 0.699212611 +0.705511808 0.705511808 0.705511808 +0.711811006 0.711811006 0.711811006 +0.718110263 0.718110263 0.718110263 +0.724409461 0.724409461 0.724409461 +0.730708659 0.730708659 0.730708659 +0.737007856 0.737007856 0.737007856 +0.743307114 0.743307114 0.743307114 +0.749606311 0.749606311 0.749606311 +0.755905509 0.755905509 0.755905509 +0.762204707 0.762204707 0.762204707 +0.768503964 0.768503964 0.768503964 +0.774803162 0.774803162 0.774803162 +0.781102359 0.781102359 0.781102359 +0.787401557 0.787401557 0.787401557 +0.787401557 0.787401557 0.787401557 +0.768627465 0.368627459 0.168627456 +0.768627465 0.368627459 0.168627456 +0.768929541 0.369346976 0.16918768 +0.769231617 0.370066464 0.169747904 +0.769533694 0.370785981 0.170308128 +0.76983577 0.371505469 0.170868352 +0.770137846 0.372224987 0.171428576 +0.770439982 0.372944474 0.1719888 +0.770742059 0.373663992 0.172549024 +0.771044135 0.374383479 0.173109248 +0.771346211 0.375102997 0.173669472 +0.771648288 0.375822484 0.174229696 +0.771950364 0.376542002 0.17478992 +0.77225244 0.37726149 0.175350145 +0.772554517 0.377981007 0.175910369 +0.772856593 0.378700495 0.176470593 +0.773158669 0.379420012 0.177030817 +0.773460805 0.3801395 0.177591041 +0.773762882 0.380859017 0.178151265 +0.774064958 0.381578505 0.178711489 +0.774367034 0.382298023 0.179271713 +0.774669111 0.38301751 0.179831937 +0.774971187 0.383737028 0.180392161 +0.780972838 0.400119334 0.194455713 +0.786974549 0.416501611 0.20851928 +0.792976201 0.432883918 0.222582832 +0.798977852 0.449266195 0.236646384 +0.804979563 0.465648502 0.250709951 +0.810981214 0.482030779 0.264773518 +0.816982865 0.498413086 0.278837055 +0.822984517 0.514795363 0.292900622 +0.828986228 0.53117764 0.306964189 +0.834987879 0.547559977 0.321027726 +0.84098953 0.563942254 0.335091293 +0.846991241 0.580324531 0.34915486 +0.852992892 0.596706867 0.363218397 +0.858994544 0.613089144 0.377281964 +0.864996254 0.629471421 0.391345531 +0.870997906 0.645853698 0.405409068 +0.876999557 0.662236035 0.419472635 +0.883001268 0.678618312 0.433536202 +0.889002919 0.695000589 0.447599739 +0.89500457 0.711382926 0.461663306 +0.901006281 0.727765203 0.475726873 +0.907007933 0.74414748 0.48979041 +0.913009584 0.760529757 0.503853977 +0.919011235 0.776912093 0.517917514 +0.925012946 0.79329437 0.531981111 +0.931014597 0.809676647 0.546044648 +0.937016249 0.826058924 0.560108185 +0.94301796 0.842441261 0.574171782 +0.949019611 0.858823538 0.588235319 +0.913870752 0.858823538 0.566448808 +0.878721833 0.858823538 0.544662356 +0.843572974 0.858823538 0.522875845 +0.808424115 0.858823538 0.501089334 +0.773275256 0.858823538 0.479302853 +0.738126338 0.858823538 0.457516372 +0.702977479 0.858823538 0.435729861 +0.667828619 0.858823538 0.41394338 +0.63267976 0.858823538 0.392156869 +0.597530842 0.858823538 0.370370388 +0.562381983 0.858823538 0.348583907 +0.527233124 0.858823538 0.326797396 +0.492084235 0.858823538 0.305010915 +0.456935376 0.858823538 0.283224404 +0.421786487 0.858823538 0.261437923 +0.386637628 0.858823538 0.239651427 +0.351488739 0.858823538 0.217864931 +0.31633988 0.858823538 0.196078435 +0.281190991 0.858823538 0.174291953 +0.246042117 0.858823538 0.152505457 +0.210893244 0.858823538 0.130718961 +0.17574437 0.858823538 0.108932465 +0.140595496 0.858823538 0.087145977 +0.105446622 0.858823538 0.065359481 +0.070297748 0.858823538 0.043572988 +0.035148874 0.858823538 0.021786494 +0 0.858823538 0 +0 0.846087515 0 +0 0.833351493 0 +0 0.82061547 0 +0 0.807879448 0 +0 0.795143425 0 +0 0.782407403 0 +0 0.769671381 0 +0 0.756935358 0 +0 0.744199336 0 +0 0.731463313 0 +0 0.718727291 0 +0 0.705991268 0 +0 0.693255246 0 +0 0.680519283 0 +0 0.66778326 0 +0 0.655047238 0 +0 0.642311215 0 +0 0.629575193 0 +0 0.61683917 0 +0 0.604103148 0 +0 0.591367126 0 +0 0.578631103 0 +0 0.565895081 0 +0 0.553159058 0 +0 0.540423036 0 +0 0.527687013 0 +0 0.514950991 0 +0 0.514215708 0 +0 0.513480425 0 +0 0.512745082 0 +0 0.512009799 0 +0 0.511274517 0 +0 0.510539234 0 +0 0.509803951 0 +0 0.509068608 0 +0 0.508333325 0 +0 0.507598042 0 +0 0.50686276 0 +0 0.506127477 0 +0 0.505392134 0 +0 0.504656851 0 +0 0.503921568 0 +0 0.503186285 0 +0 0.502451003 0 +0 0.50171566 0 +0 0.500980377 0 +0 0.500245094 0 +0 0.499509811 0 +0 0.498774499 0 +0 0.498039216 0 + diff --git a/landsat/downloader.py b/landsat/downloader.py index 10bbc6e..552bebe 100644 --- a/landsat/downloader.py +++ b/landsat/downloader.py @@ -278,5 +278,4 @@ if __name__ == '__main__': d = Downloader() - # d.download(['LC81990242015046LGN00', 'LC80030172015001LGN00']) - d.download(['LC80030172015001LGN00'], bands=[5, 4]) + # d.download(['LC81990242015046LGN00', 'LC80030172015001LGN00']) \ No newline at end of file diff --git a/landsat/landsat.py b/landsat/landsat.py index e5a35ed..baad82e 100755 --- a/landsat/landsat.py +++ b/landsat/landsat.py @@ -49,6 +49,8 @@ search, download, and process Landsat imagery. -e END, --end END End Date - Most formats are accepted e.g. Jun 12 2014 OR 06/12/2014 + + --latest N Returns the N latest images within the last 365 days. -c CLOUD, --cloud CLOUD Maximum cloud percentage. Default: 20 perct @@ -75,6 +77,8 @@ search, download, and process Landsat imagery. --pansharpen Whether to also pansharpen the processed image. Pansharpening requires larger memory + + --ndvi [grey,color] Calculates NDVI. Produces either grayscale GTiff or RGB GTiff with seperate colorbar -u --upload Upload to S3 after the image processing completed @@ -103,6 +107,8 @@ search, download, and process Landsat imagery. --pansharpen Whether to also pansharpen the process image. Pansharpening requires larger memory + + --ndvi [grey,color] Calculates NDVI. Produces either grayscale GTiff or RGB GTiff with seperate colorbar. -v, --verbose Show verbose output @@ -155,8 +161,7 @@ def args_options(): help='End Date - Most formats are accepted ' 'e.g. Jun 12 2014 OR 06/12/2014') parser_search.add_argument('--latest',default=-1,type=int, - help='search the latest landsat image. When number is given, the result will be the image' - 'with least cloudcover of the n latest images.') + help='returns the N latest images within the last 365 days') parser_search.add_argument('-c', '--cloud', type=float, default=100.0, help='Maximum cloud percentage ' 'default is 100 perct') @@ -182,7 +187,7 @@ def args_options(): 'image. Pansharpening requires larger memory') parser_download.add_argument('--ndvi', type=str, choices=['color','grey'], help='Create an NDVI map. Specify if output should be GTIFF in grayscale (grey) ' - 'or a .PNG with a colormap (color)') + 'or RGB with a seperate colorbar (color)') parser_download.add_argument('-u', '--upload', action='store_true', help='Upload to S3 after the image processing completed') parser_download.add_argument('--key', help='Amazon S3 Access Key (You can also be set AWS_ACCESS_KEY_ID as ' @@ -201,7 +206,7 @@ def args_options(): 'image. Pansharpening requires larger memory') parser_process.add_argument('--ndvi', type=str, choices=['color','grey'], help='Create an NDVI map. Specify if output should be GTIFF in grayscale (grey) ' - 'or a .PNG with a colormap (color)') + 'or RGB with a seperate colorbar (color)') parser_process.add_argument('-b', '--bands', help='specify band combinations. Default is 432' 'Example: --bands 321') parser_process.add_argument('-v', '--verbose', action='store_true', From f9592d20afcfb6764a9e3978df82bd9fcdc39d9e Mon Sep 17 00:00:00 2001 From: julesair Date: Thu, 23 Jul 2015 10:20:51 +0200 Subject: [PATCH 11/14] added two colormaps -> colormapNDVI, colormap_cubehelix --- landsat/colormapNDVI | 16 +- landsat/colormap_cubehelix | 521 ++++++++++++++++++------------------- 2 files changed, 261 insertions(+), 276 deletions(-) diff --git a/landsat/colormapNDVI b/landsat/colormapNDVI index 6a4c816..89c90ab 100644 --- a/landsat/colormapNDVI +++ b/landsat/colormapNDVI @@ -1,20 +1,6 @@ # Tue Jul 14 2015 14:40:26 GMT+0200 # --------------------------------------------- -# R/G/B cubehelix colour scheme -# -# see http://www.mrao.cam.ac.uk/~dag/CUBEHELIX/ -#---------------------------------------------- -# see Green (2011), BASI, 39, 289. -# -# start............: 3.0 -# rotations........: -2 -# hue..............: 1.6 -# gamma............: 0.8 -# number of levels.: 254 -#---------------------------------------------- -# Dave Green: dag@mrao.cam.ac.uk -#---------------------------------------------- -# faction and R/G/B values +# Intuitive Colormap for NDVI mapping. Not suited for grayscale print or colorblind people though... # mode = 1 0 0 0 diff --git a/landsat/colormap_cubehelix b/landsat/colormap_cubehelix index db1d8ad..15827f3 100644 --- a/landsat/colormap_cubehelix +++ b/landsat/colormap_cubehelix @@ -1,4 +1,4 @@ -# Tue Jul 14 2015 14:40:26 GMT+0200 +# Thu Jul 23 2015 10:14:15 GMT+0200 # --------------------------------------------- # R/G/B cubehelix colour scheme # @@ -6,271 +6,270 @@ #---------------------------------------------- # see Green (2011), BASI, 39, 289. # -# start............: 3.0 -# rotations........: -2 +# start............: 1.0 +# rotations........: -0.8 # hue..............: 1.6 -# gamma............: 0.8 -# number of levels.: 254 +# gamma............: 1.0 +# number of levels.: 256 #---------------------------------------------- # Dave Green: dag@mrao.cam.ac.uk #---------------------------------------------- -# faction and R/G/B values # mode = 1 0 0 0 -0.006299213 0.006299213 0.006299213 -0.012598425 0.012598425 0.012598425 -0.018897638 0.018897638 0.018897638 -0.02519685 0.02519685 0.02519685 -0.031496063 0.031496063 0.031496063 -0.037795275 0.037795275 0.037795275 -0.044094488 0.044094488 0.044094488 -0.050393701 0.050393701 0.050393701 -0.056692913 0.056692913 0.056692913 -0.062992126 0.062992126 0.062992126 -0.069291338 0.069291338 0.069291338 -0.075590551 0.075590551 0.075590551 -0.081889763 0.081889763 0.081889763 -0.088188976 0.088188976 0.088188976 -0.094488189 0.094488189 0.094488189 -0.100787401 0.100787401 0.100787401 -0.107086614 0.107086614 0.107086614 -0.113385826 0.113385826 0.113385826 -0.119685039 0.119685039 0.119685039 -0.125984251 0.125984251 0.125984251 -0.132283464 0.132283464 0.132283464 -0.138582677 0.138582677 0.138582677 -0.144881889 0.144881889 0.144881889 -0.151181102 0.151181102 0.151181102 -0.157480314 0.157480314 0.157480314 -0.163779527 0.163779527 0.163779527 -0.17007874 0.17007874 0.17007874 -0.176377952 0.176377952 0.176377952 -0.182677165 0.182677165 0.182677165 -0.188976377 0.188976377 0.188976377 -0.19527559 0.19527559 0.19527559 -0.201574802 0.201574802 0.201574802 -0.207874015 0.207874015 0.207874015 -0.214173228 0.214173228 0.214173228 -0.22047244 0.22047244 0.22047244 -0.226771653 0.226771653 0.226771653 -0.233070865 0.233070865 0.233070865 -0.239370078 0.239370078 0.239370078 -0.24566929 0.24566929 0.24566929 -0.251968503 0.251968503 0.251968503 -0.25826773 0.25826773 0.25826773 -0.264566928 0.264566928 0.264566928 -0.270866156 0.270866156 0.270866156 -0.277165353 0.277165353 0.277165353 -0.283464581 0.283464581 0.283464581 -0.289763778 0.289763778 0.289763778 -0.296063006 0.296063006 0.296063006 -0.302362204 0.302362204 0.302362204 -0.308661431 0.308661431 0.308661431 -0.314960629 0.314960629 0.314960629 -0.321259856 0.321259856 0.321259856 -0.327559054 0.327559054 0.327559054 -0.333858281 0.333858281 0.333858281 -0.340157479 0.340157479 0.340157479 -0.346456707 0.346456707 0.346456707 -0.352755904 0.352755904 0.352755904 -0.359055132 0.359055132 0.359055132 -0.365354329 0.365354329 0.365354329 -0.371653557 0.371653557 0.371653557 -0.377952754 0.377952754 0.377952754 -0.384251982 0.384251982 0.384251982 -0.39055118 0.39055118 0.39055118 -0.396850407 0.396850407 0.396850407 -0.403149605 0.403149605 0.403149605 -0.409448832 0.409448832 0.409448832 -0.41574803 0.41574803 0.41574803 -0.422047257 0.422047257 0.422047257 -0.428346455 0.428346455 0.428346455 -0.434645683 0.434645683 0.434645683 -0.44094488 0.44094488 0.44094488 -0.447244108 0.447244108 0.447244108 -0.453543305 0.453543305 0.453543305 -0.459842533 0.459842533 0.459842533 -0.466141731 0.466141731 0.466141731 -0.472440958 0.472440958 0.472440958 -0.478740156 0.478740156 0.478740156 -0.485039383 0.485039383 0.485039383 -0.491338581 0.491338581 0.491338581 -0.497637808 0.497637808 0.497637808 -0.503937006 0.503937006 0.503937006 -0.510236204 0.510236204 0.510236204 -0.516535461 0.516535461 0.516535461 -0.522834659 0.522834659 0.522834659 -0.529133856 0.529133856 0.529133856 -0.535433054 0.535433054 0.535433054 -0.541732311 0.541732311 0.541732311 -0.548031509 0.548031509 0.548031509 -0.554330707 0.554330707 0.554330707 -0.560629904 0.560629904 0.560629904 -0.566929162 0.566929162 0.566929162 -0.573228359 0.573228359 0.573228359 -0.579527557 0.579527557 0.579527557 -0.585826755 0.585826755 0.585826755 -0.592126012 0.592126012 0.592126012 -0.59842521 0.59842521 0.59842521 -0.604724407 0.604724407 0.604724407 -0.611023605 0.611023605 0.611023605 -0.617322862 0.617322862 0.617322862 -0.62362206 0.62362206 0.62362206 -0.629921257 0.629921257 0.629921257 -0.636220455 0.636220455 0.636220455 -0.642519712 0.642519712 0.642519712 -0.64881891 0.64881891 0.64881891 -0.655118108 0.655118108 0.655118108 -0.661417305 0.661417305 0.661417305 -0.667716563 0.667716563 0.667716563 -0.67401576 0.67401576 0.67401576 -0.680314958 0.680314958 0.680314958 -0.686614156 0.686614156 0.686614156 -0.692913413 0.692913413 0.692913413 -0.699212611 0.699212611 0.699212611 -0.705511808 0.705511808 0.705511808 -0.711811006 0.711811006 0.711811006 -0.718110263 0.718110263 0.718110263 -0.724409461 0.724409461 0.724409461 -0.730708659 0.730708659 0.730708659 -0.737007856 0.737007856 0.737007856 -0.743307114 0.743307114 0.743307114 -0.749606311 0.749606311 0.749606311 -0.755905509 0.755905509 0.755905509 -0.762204707 0.762204707 0.762204707 -0.768503964 0.768503964 0.768503964 -0.774803162 0.774803162 0.774803162 -0.781102359 0.781102359 0.781102359 -0.787401557 0.787401557 0.787401557 -0.793700814 0.793700814 0.793700814 -0.800000012 0.800000012 0.800000012 -0.768627465 0.368627459 0.168627456 -0.768929541 0.369346976 0.16918768 -0.769231617 0.370066464 0.169747904 -0.769533694 0.370785981 0.170308128 -0.76983577 0.371505469 0.170868352 -0.770137846 0.372224987 0.171428576 -0.770439982 0.372944474 0.1719888 -0.770742059 0.373663992 0.172549024 -0.771044135 0.374383479 0.173109248 -0.771346211 0.375102997 0.173669472 -0.771648288 0.375822484 0.174229696 -0.771950364 0.376542002 0.17478992 -0.77225244 0.37726149 0.175350145 -0.772554517 0.377981007 0.175910369 -0.772856593 0.378700495 0.176470593 -0.773158669 0.379420012 0.177030817 -0.773460805 0.3801395 0.177591041 -0.773762882 0.380859017 0.178151265 -0.774064958 0.381578505 0.178711489 -0.774367034 0.382298023 0.179271713 -0.774669111 0.38301751 0.179831937 -0.774971187 0.383737028 0.180392161 -0.780972838 0.400119334 0.194455713 -0.786974549 0.416501611 0.20851928 -0.792976201 0.432883918 0.222582832 -0.798977852 0.449266195 0.236646384 -0.804979563 0.465648502 0.250709951 -0.810981214 0.482030779 0.264773518 -0.816982865 0.498413086 0.278837055 -0.822984517 0.514795363 0.292900622 -0.828986228 0.53117764 0.306964189 -0.834987879 0.547559977 0.321027726 -0.84098953 0.563942254 0.335091293 -0.846991241 0.580324531 0.34915486 -0.852992892 0.596706867 0.363218397 -0.858994544 0.613089144 0.377281964 -0.864996254 0.629471421 0.391345531 -0.870997906 0.645853698 0.405409068 -0.876999557 0.662236035 0.419472635 -0.883001268 0.678618312 0.433536202 -0.889002919 0.695000589 0.447599739 -0.89500457 0.711382926 0.461663306 -0.901006281 0.727765203 0.475726873 -0.907007933 0.74414748 0.48979041 -0.913009584 0.760529757 0.503853977 -0.919011235 0.776912093 0.517917514 -0.925012946 0.79329437 0.531981111 -0.931014597 0.809676647 0.546044648 -0.937016249 0.826058924 0.560108185 -0.94301796 0.842441261 0.574171782 -0.949019611 0.858823538 0.588235319 -0.913870752 0.858823538 0.566448808 -0.878721833 0.858823538 0.544662356 -0.843572974 0.858823538 0.522875845 -0.808424115 0.858823538 0.501089334 -0.773275256 0.858823538 0.479302853 -0.738126338 0.858823538 0.457516372 -0.702977479 0.858823538 0.435729861 -0.667828619 0.858823538 0.41394338 -0.63267976 0.858823538 0.392156869 -0.597530842 0.858823538 0.370370388 -0.562381983 0.858823538 0.348583907 -0.527233124 0.858823538 0.326797396 -0.492084235 0.858823538 0.305010915 -0.456935376 0.858823538 0.283224404 -0.421786487 0.858823538 0.261437923 -0.386637628 0.858823538 0.239651427 -0.351488739 0.858823538 0.217864931 -0.31633988 0.858823538 0.196078435 -0.281190991 0.858823538 0.174291953 -0.246042117 0.858823538 0.152505457 -0.210893244 0.858823538 0.130718961 -0.17574437 0.858823538 0.108932465 -0.140595496 0.858823538 0.087145977 -0.105446622 0.858823538 0.065359481 -0.070297748 0.858823538 0.043572988 -0.035148874 0.858823538 0.021786494 -0 0.858823538 0 -0 0.846087515 0 -0 0.833351493 0 -0 0.82061547 0 -0 0.807879448 0 -0 0.795143425 0 -0 0.782407403 0 -0 0.769671381 0 -0 0.756935358 0 -0 0.744199336 0 -0 0.731463313 0 -0 0.718727291 0 -0 0.705991268 0 -0 0.693255246 0 -0 0.680519283 0 -0 0.66778326 0 -0 0.655047238 0 -0 0.642311215 0 -0 0.629575193 0 -0 0.61683917 0 -0 0.604103148 0 -0 0.591367126 0 -0 0.578631103 0 -0 0.565895081 0 -0 0.553159058 0 -0 0.540423036 0 -0 0.527687013 0 -0 0.514950991 0 -0 0.514215708 0 -0 0.513480425 0 -0 0.512745082 0 -0 0.512009799 0 -0 0.511274517 0 -0 0.510539234 0 -0 0.509803951 0 -0 0.509068608 0 -0 0.508333325 0 -0 0.507598042 0 -0 0.50686276 0 -0 0.506127477 0 -0 0.505392134 0 -0 0.504656851 0 -0 0.503921568 0 -0 0.503186285 0 -0 0.502451003 0 -0 0.50171566 0 -0 0.500980377 0 -0 0.500245094 0 -0 0.499509811 0 -0 0.498774499 0 -0 0.498039216 0 +0.009 0.002 0.001 +0.018 0.004 0.002 +0.027 0.005 0.004 +0.036 0.007 0.005 +0.046 0.009 0.007 +0.055 0.01 0.009 +0.064 0.012 0.012 +0.073 0.013 0.014 +0.082 0.015 0.017 +0.092 0.016 0.02 +0.101 0.018 0.023 +0.11 0.019 0.027 +0.119 0.02 0.031 +0.128 0.021 0.035 +0.137 0.023 0.039 +0.146 0.024 0.044 +0.155 0.025 0.048 +0.164 0.026 0.053 +0.173 0.027 0.058 +0.182 0.029 0.064 +0.19 0.03 0.069 +0.199 0.031 0.075 +0.208 0.032 0.081 +0.216 0.033 0.087 +0.224 0.035 0.094 +0.233 0.036 0.1 +0.241 0.037 0.107 +0.249 0.038 0.114 +0.257 0.04 0.121 +0.265 0.041 0.129 +0.272 0.042 0.136 +0.28 0.044 0.144 +0.287 0.045 0.152 +0.294 0.046 0.16 +0.301 0.048 0.168 +0.308 0.05 0.177 +0.315 0.051 0.185 +0.322 0.053 0.194 +0.328 0.054 0.203 +0.335 0.056 0.212 +0.341 0.058 0.221 +0.347 0.06 0.23 +0.353 0.062 0.239 +0.359 0.064 0.248 +0.364 0.066 0.258 +0.369 0.068 0.268 +0.375 0.07 0.277 +0.38 0.073 0.287 +0.384 0.075 0.297 +0.389 0.077 0.307 +0.393 0.08 0.317 +0.398 0.082 0.327 +0.402 0.085 0.337 +0.406 0.088 0.347 +0.409 0.091 0.358 +0.413 0.094 0.368 +0.416 0.097 0.378 +0.419 0.1 0.389 +0.422 0.103 0.399 +0.425 0.106 0.409 +0.428 0.11 0.42 +0.43 0.113 0.43 +0.432 0.117 0.44 +0.434 0.121 0.451 +0.436 0.124 0.461 +0.438 0.128 0.471 +0.439 0.132 0.481 +0.441 0.136 0.492 +0.442 0.14 0.502 +0.443 0.145 0.512 +0.444 0.149 0.522 +0.444 0.154 0.532 +0.445 0.158 0.542 +0.445 0.163 0.552 +0.445 0.168 0.562 +0.445 0.172 0.571 +0.445 0.177 0.581 +0.444 0.182 0.59 +0.444 0.188 0.6 +0.443 0.193 0.609 +0.443 0.198 0.618 +0.442 0.203 0.627 +0.441 0.209 0.636 +0.439 0.215 0.645 +0.438 0.22 0.654 +0.437 0.226 0.662 +0.435 0.232 0.671 +0.433 0.238 0.679 +0.432 0.244 0.687 +0.43 0.25 0.695 +0.428 0.256 0.703 +0.426 0.263 0.711 +0.423 0.269 0.718 +0.421 0.275 0.725 +0.419 0.282 0.732 +0.416 0.289 0.739 +0.414 0.295 0.746 +0.411 0.302 0.753 +0.409 0.309 0.759 +0.406 0.316 0.766 +0.403 0.323 0.772 +0.4 0.33 0.777 +0.397 0.337 0.783 +0.394 0.344 0.789 +0.391 0.351 0.794 +0.389 0.358 0.799 +0.386 0.365 0.804 +0.383 0.373 0.809 +0.379 0.38 0.813 +0.376 0.387 0.817 +0.373 0.395 0.822 +0.37 0.402 0.825 +0.367 0.41 0.829 +0.364 0.417 0.833 +0.361 0.425 0.836 +0.358 0.432 0.839 +0.355 0.44 0.842 +0.353 0.448 0.845 +0.35 0.455 0.847 +0.347 0.463 0.849 +0.344 0.47 0.852 +0.341 0.478 0.853 +0.339 0.486 0.855 +0.336 0.494 0.857 +0.334 0.501 0.858 +0.331 0.509 0.859 +0.329 0.517 0.86 +0.327 0.524 0.861 +0.325 0.532 0.861 +0.322 0.539 0.862 +0.32 0.547 0.862 +0.319 0.555 0.862 +0.317 0.562 0.862 +0.315 0.57 0.862 +0.314 0.577 0.861 +0.312 0.585 0.861 +0.311 0.592 0.86 +0.31 0.6 0.859 +0.309 0.607 0.858 +0.308 0.614 0.856 +0.307 0.622 0.855 +0.307 0.629 0.854 +0.306 0.636 0.852 +0.306 0.643 0.85 +0.306 0.65 0.848 +0.306 0.657 0.846 +0.306 0.664 0.844 +0.306 0.671 0.842 +0.307 0.678 0.839 +0.307 0.685 0.837 +0.308 0.691 0.834 +0.309 0.698 0.832 +0.31 0.705 0.829 +0.311 0.711 0.826 +0.313 0.718 0.823 +0.315 0.724 0.82 +0.316 0.73 0.817 +0.318 0.736 0.814 +0.321 0.743 0.811 +0.323 0.749 0.808 +0.326 0.754 0.805 +0.328 0.76 0.802 +0.331 0.766 0.798 +0.335 0.772 0.795 +0.338 0.777 0.792 +0.341 0.783 0.788 +0.345 0.788 0.785 +0.349 0.794 0.782 +0.353 0.799 0.778 +0.357 0.804 0.775 +0.361 0.809 0.772 +0.366 0.814 0.769 +0.371 0.819 0.765 +0.376 0.823 0.762 +0.381 0.828 0.759 +0.386 0.833 0.756 +0.392 0.837 0.753 +0.397 0.841 0.75 +0.403 0.846 0.747 +0.409 0.85 0.744 +0.415 0.854 0.741 +0.421 0.858 0.739 +0.428 0.862 0.736 +0.434 0.865 0.734 +0.441 0.869 0.731 +0.448 0.873 0.729 +0.455 0.876 0.727 +0.462 0.879 0.725 +0.469 0.883 0.723 +0.477 0.886 0.721 +0.484 0.889 0.719 +0.492 0.892 0.718 +0.5 0.895 0.717 +0.508 0.898 0.715 +0.516 0.901 0.714 +0.524 0.903 0.713 +0.532 0.906 0.713 +0.541 0.908 0.712 +0.549 0.911 0.712 +0.558 0.913 0.711 +0.566 0.915 0.711 +0.575 0.918 0.711 +0.584 0.92 0.712 +0.593 0.922 0.712 +0.602 0.924 0.713 +0.611 0.926 0.713 +0.62 0.928 0.714 +0.629 0.929 0.716 +0.638 0.931 0.717 +0.647 0.933 0.719 +0.656 0.934 0.721 +0.665 0.936 0.723 +0.675 0.938 0.725 +0.684 0.939 0.727 +0.693 0.94 0.73 +0.702 0.942 0.733 +0.712 0.943 0.736 +0.721 0.945 0.739 +0.73 0.946 0.743 +0.739 0.947 0.746 +0.748 0.948 0.75 +0.758 0.95 0.755 +0.767 0.951 0.759 +0.776 0.952 0.764 +0.785 0.953 0.768 +0.794 0.954 0.773 +0.802 0.956 0.779 +0.811 0.957 0.784 +0.82 0.958 0.79 +0.829 0.959 0.795 +0.837 0.96 0.801 +0.845 0.962 0.808 +0.854 0.963 0.814 +0.862 0.964 0.821 +0.87 0.965 0.828 +0.878 0.967 0.835 +0.886 0.968 0.842 +0.894 0.969 0.849 +0.901 0.971 0.857 +0.909 0.972 0.865 +0.916 0.973 0.873 +0.923 0.975 0.881 +0.93 0.976 0.889 +0.937 0.978 0.898 +0.944 0.98 0.906 +0.95 0.981 0.915 +0.956 0.983 0.924 +0.963 0.985 0.933 +0.969 0.987 0.942 +0.974 0.989 0.951 +0.98 0.991 0.961 +0.985 0.993 0.971 +0.99 0.995 0.98 +0.995 0.998 0.99 +1.000 1.000 1.000 From 3732a4c48b7c091906023aa54a9985b0ef3ad424 Mon Sep 17 00:00:00 2001 From: julesair Date: Thu, 23 Jul 2015 11:48:23 +0200 Subject: [PATCH 12/14] Fixed bug: missing bands when processing NDVI without download --- landsat/landsat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/landsat/landsat.py b/landsat/landsat.py index baad82e..20bdf49 100755 --- a/landsat/landsat.py +++ b/landsat/landsat.py @@ -382,7 +382,7 @@ def process_image(path, bands=None, verbose=False, pansharpen=False, force_unzip bands = convert_to_integer_list(bands) if isinstance(ndvi, str): - p = Process(path, bands=bands, verbose=verbose, force_unzip=force_unzip) + p = Process(path, bands=[4,5], verbose=verbose, force_unzip=force_unzip) else: p = Process(path, bands=bands, verbose=verbose, force_unzip=force_unzip) except IOError: From 99e498f9da75d1033ef050880138e623b7349a39 Mon Sep 17 00:00:00 2001 From: julesair Date: Thu, 23 Jul 2015 16:39:21 +0200 Subject: [PATCH 13/14] added option for cloudmasking when computing NDVI. Masking is based on the QA Band delivered with Landsat. --- landsat/image.py | 28 ++++++++++++++++++++-------- landsat/landsat.py | 18 +++++++++++++----- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/landsat/image.py b/landsat/image.py index 55b1851..fd21e78 100644 --- a/landsat/image.py +++ b/landsat/image.py @@ -209,7 +209,7 @@ class Process(VerbosityMixin): return output_file - def run_ndvi(self, mode='grey'): + def run_ndvi(self, mode='grey', cmask=False): """ Executes the image processing. :returns: @@ -240,10 +240,14 @@ class Process(VerbosityMixin): bands = [] bands_path = [] - + + if cmask: + self.bands.append('QA') + for band in self.bands: bands_path.append(join(self.scene_path, self._get_full_filename(band))) - + + try: for i, band in enumerate(self.bands): bands.append(self._read_band(bands_path[i])) @@ -293,20 +297,28 @@ class Process(VerbosityMixin): # Bands are no longer needed del bands - + self.output("Calculating NDVI", normal=True, arrow=True) output_file = '%s_NDVI' % (self.scene) - - mask=(new_bands[1]+new_bands[0])==0 + + tilemask = numpy.empty_like(new_bands[-1], dtype=numpy.bool) + tilemask[(new_bands[1]+new_bands[0]==0)] = True new_bands[0]=new_bands[0]*mult_B3+add_B3 new_bands[1]=new_bands[1]*mult_B4+add_B4 ndvi=numpy.true_divide((new_bands[1]-new_bands[0]),(new_bands[1]+new_bands[0])) - ndvi[mask]=-1 ndvi=((ndvi+1)*255 / 2).astype(numpy.uint8) - + ndvi[tilemask]=0 + + if cmask: + # clouds are indicated when Bit 15&16 = 11 or 10 + ndvi[(new_bands[-1].astype(numpy.uint16) & 49152)>=32768]=0 + # cirrus are indicated when Bit 13&14 = 11 or 10 + ndvi[(new_bands[-1].astype(numpy.uint16) & 12288)>=8192]=0 + self.output("Final Steps", normal=True, arrow=True) + if mode=='grey': output_file += '_grey.TIF' output_file = join(self.dst_path, output_file) diff --git a/landsat/landsat.py b/landsat/landsat.py index baad82e..53aa0df 100755 --- a/landsat/landsat.py +++ b/landsat/landsat.py @@ -79,6 +79,8 @@ search, download, and process Landsat imagery. Pansharpening requires larger memory --ndvi [grey,color] Calculates NDVI. Produces either grayscale GTiff or RGB GTiff with seperate colorbar + + --cloudmask Sets pixels affected by cloud cover to 0 resp. black -u --upload Upload to S3 after the image processing completed @@ -188,6 +190,8 @@ def args_options(): parser_download.add_argument('--ndvi', type=str, choices=['color','grey'], help='Create an NDVI map. Specify if output should be GTIFF in grayscale (grey) ' 'or RGB with a seperate colorbar (color)') + parser_download.add_argument('--cloudmask',action='store_true', + help='Sets pixels affected by cloud cover to 0 resp. black') parser_download.add_argument('-u', '--upload', action='store_true', help='Upload to S3 after the image processing completed') parser_download.add_argument('--key', help='Amazon S3 Access Key (You can also be set AWS_ACCESS_KEY_ID as ' @@ -207,6 +211,8 @@ def args_options(): parser_process.add_argument('--ndvi', type=str, choices=['color','grey'], help='Create an NDVI map. Specify if output should be GTIFF in grayscale (grey) ' 'or RGB with a seperate colorbar (color)') + parser_process.add_argument('--cloudmask',action='store_true', + help='Sets pixels affected by cloud cover to 0 resp. black') parser_process.add_argument('-b', '--bands', help='specify band combinations. Default is 432' 'Example: --bands 321') parser_process.add_argument('-v', '--verbose', action='store_true', @@ -247,7 +253,7 @@ def main(args): if args.subs == 'process': verbose = True if args.verbose else False force_unzip = True if args.force_unzip else False - stored = process_image(args.path, args.bands, verbose, args.pansharpen, force_unzip, args.ndvi) + stored = process_image(args.path, args.bands, verbose, args.pansharpen, force_unzip, args.ndvi, args.cloudmask) if args.upload: u = Uploader(args.key, args.secret, args.region) @@ -320,6 +326,8 @@ def main(args): bands = convert_to_integer_list(args.bands) if args.pansharpen: bands.append(8) + if args.cloudmask: + bands.append('QA') downloaded = d.download(args.scenes, bands) @@ -335,7 +343,7 @@ def main(args): if src == 'google': path = path + '.tar.bz' - stored = process_image(path, args.bands, False, args.pansharpen, force_unzip, args.ndvi) + stored = process_image(path, args.bands, False, args.pansharpen, force_unzip, args.ndvi, args.cloudmask) if args.upload: try: @@ -355,7 +363,7 @@ def main(args): return ['The SceneID provided was incorrect', 1] -def process_image(path, bands=None, verbose=False, pansharpen=False, force_unzip=None, ndvi=False): +def process_image(path, bands=None, verbose=False, pansharpen=False, force_unzip=None, ndvi=False, cloudmask=False): """ Handles constructing and image process. :param path: @@ -382,7 +390,7 @@ def process_image(path, bands=None, verbose=False, pansharpen=False, force_unzip bands = convert_to_integer_list(bands) if isinstance(ndvi, str): - p = Process(path, bands=bands, verbose=verbose, force_unzip=force_unzip) + p = Process(path, bands=[4,5], verbose=verbose, force_unzip=force_unzip) else: p = Process(path, bands=bands, verbose=verbose, force_unzip=force_unzip) except IOError: @@ -390,7 +398,7 @@ def process_image(path, bands=None, verbose=False, pansharpen=False, force_unzip except FileDoesNotExist as e: exit(e.message, 1) if isinstance(ndvi, str): - out=[p.run_ndvi(mode=ndvi)] + out=[p.run_ndvi(mode=ndvi,cmask=cloudmask)] else: out=[p.run_rgb(pansharpen)] return out From 35657f4f6e2e655ceaa97fa3966527880e87b384 Mon Sep 17 00:00:00 2001 From: julesair Date: Thu, 23 Jul 2015 16:51:34 +0200 Subject: [PATCH 14/14] added some comments in image.py --- landsat/colormapNDVI | 2 +- landsat/colormap_cubehelix | 2 +- landsat/image.py | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/landsat/colormapNDVI b/landsat/colormapNDVI index 89c90ab..5ada314 100644 --- a/landsat/colormapNDVI +++ b/landsat/colormapNDVI @@ -1,7 +1,7 @@ # Tue Jul 14 2015 14:40:26 GMT+0200 # --------------------------------------------- # Intuitive Colormap for NDVI mapping. Not suited for grayscale print or colorblind people though... -# +# The value "mode" is the maximum value of the color intensity, e.g. 1 or 255 mode = 1 0 0 0 0.006299213 0.006299213 0.006299213 diff --git a/landsat/colormap_cubehelix b/landsat/colormap_cubehelix index 15827f3..1fa4c12 100644 --- a/landsat/colormap_cubehelix +++ b/landsat/colormap_cubehelix @@ -14,7 +14,7 @@ #---------------------------------------------- # Dave Green: dag@mrao.cam.ac.uk #---------------------------------------------- -# +# The value "mode" is the maximum value of the color intensity, e.g. 1 or 255 mode = 1 0 0 0 0.009 0.002 0.001 diff --git a/landsat/image.py b/landsat/image.py index 55b1851..e95f8a7 100644 --- a/landsat/image.py +++ b/landsat/image.py @@ -22,7 +22,7 @@ from mixins import VerbosityMixin from utils import get_file, timer, check_create_folder, exit #for color -import matplotlib.pylab as pyplot +import matplotlib.pyplot as pyplot class FileDoesNotExist(Exception): @@ -210,8 +210,12 @@ class Process(VerbosityMixin): return output_file def run_ndvi(self, mode='grey'): - """ Executes the image processing. + """ Executes the NDVI processing. + :param mode: + Whether to create greyscale GTiff or colorized GTiff + :type mode + string, either 'grey' or 'color' :returns: (String) the path to the processed image """ @@ -298,7 +302,7 @@ class Process(VerbosityMixin): self.output("Calculating NDVI", normal=True, arrow=True) output_file = '%s_NDVI' % (self.scene) - mask=(new_bands[1]+new_bands[0])==0 + mask=(new_bands[1]+new_bands[0])==0 #masking of the area outside the image tile new_bands[0]=new_bands[0]*mult_B3+add_B3 new_bands[1]=new_bands[1]*mult_B4+add_B4 ndvi=numpy.true_divide((new_bands[1]-new_bands[0]),(new_bands[1]+new_bands[0])) @@ -335,7 +339,7 @@ class Process(VerbosityMixin): for i in range(0, 3): output.write_band(i+1, rgb[i]) - #colorbar + #colorbar self.output("Creating colorbar", normal=True, color='green', indent=1) output_file2 = '%s_colorbar.png' % (self.scene) output_file2 = join(self.dst_path, output_file2) @@ -395,7 +399,7 @@ class Process(VerbosityMixin): def _index2rgb(self, index_matrix): - """ converts a 8bit matrix to rgb values according to the colormap """ + """ converts a 8bit matrix to rgb values according to a colormap """ self._read_cmap() translate_colormap = numpy.vectorize(self._get_color, otypes=[numpy.uint8]) @@ -406,12 +410,14 @@ class Process(VerbosityMixin): return rgb_bands def _get_color(self, n,v=-1): + """ returns either RGB for a value [0 ... 255] or only the intensity for R(v=0),G(v=1),B(v=2) """ if v==-1: return self.colormap[n] else: return self.colormap[n][v] def _read_cmap(self): + """ reads the colormap from a text file given in settings.py. See colormap_cubehelix.txt. File must contain 256 RGB values """ try: i=0 colormap={0 : (0, 0, 0)} @@ -423,7 +429,7 @@ class Process(VerbosityMixin): maxval = float(line.replace('mode = ', '')) elif i > 0: str = line.split() - if str==[]: + if str==[]: # when there are empty lines at the end of the file break colormap.update({i : (int(round(float(str[0])*255/maxval)), int(round(float(str[1])*255/maxval)),