Esempio n. 1
0
def exit(message, code=0):
    v = VerbosityMixin()
    if code == 0:
        v.output(message, normal=True, arrow=True)
        v.output('Done!', normal=True, arrow=True)
    else:
        v.output(message, normal=True, error=True)
    sys.exit(code)
Esempio n. 2
0
def exit(message, code=0):
    """ output a message to stdout and terminates the process.

    :param message:
        Message to be outputed.
    :type message:
        String
    :param code:
        The termination code. Default is 0
    :type code:
        int

    :returns:
        void
    """

    v = VerbosityMixin()
    if code == 0:
        v.output(message, normal=True, arrow=True)
        v.output('Done!', normal=True, arrow=True)
    else:
        v.output(message, normal=True, error=True)
    sys.exit(code)
Esempio n. 3
0
def main(args):
    """
    Main function - launches the program.

    :param args:
        The Parser arguments
    :type args:
        Parser object

    :returns:
        List

    :example:
        >>> ["The latitude and longitude values must be valid numbers", 1]
    """

    v = VerbosityMixin()

    if args:

        if 'clip' in args:
            bounds = convert_to_float_list(args.clip)
        else:
            bounds = None

        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, args.ndvi, force_unzip,
                                   args.ndvigrey, bounds)

            if args.upload:
                u = Uploader(args.key, args.secret, args.region)
                u.run(args.bucket, get_file(stored), stored)

            return ["The output is stored at %s" % stored]

        elif args.subs == 'search':

            try:
                if args.start:
                    args.start = reformat_date(parse(args.start))
                if args.end:
                    args.end = reformat_date(parse(args.end))
                if args.latest > 0:
                    args.limit = 25
                    end = datetime.now()
                    start = end - relativedelta(days=+365)
                    args.end = end.strftime("%Y-%m-%d")
                    args.start = start.strftime("%Y-%m-%d")
            except (TypeError, ValueError):
                return ["Your date format is incorrect. Please try again!", 1]

            s = Search()

            try:
                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
                ]

            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':
                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!']

            elif result['status'] == 'error':
                return [result['message'], 1]
        elif args.subs == 'download':
            d = Downloader(download_dir=args.dest)
            try:
                bands = convert_to_integer_list(args.bands)
                if args.pansharpen:
                    bands.append(8)

                if args.ndvi:
                    bands = [4, 5]

                downloaded = d.download(args.scenes, bands)

                if args.process:
                    force_unzip = True if args.force_unzip else False
                    for scene, src in downloaded.iteritems():
                        if args.dest:
                            path = join(args.dest, scene)
                        else:
                            path = join(settings.DOWNLOAD_DIR, scene)

                        # Keep using Google if the image is before 2015
                        if src == 'google':
                            path = path + '.tar.bz'

                        stored = process_image(path,
                                               args.bands,
                                               False,
                                               args.pansharpen,
                                               args.ndvi,
                                               force_unzip,
                                               bounds=bounds)

                        if args.upload:
                            try:
                                u = Uploader(args.key, args.secret,
                                             args.region)
                            except NoAuthHandlerFound:
                                return ["Could not authenticate with AWS", 1]
                            except URLError:
                                return [
                                    "Connection timeout. Probably the region parameter is incorrect",
                                    1
                                ]
                            u.run(args.bucket, get_file(stored), stored)

                    return ['The output is stored at %s' % stored, 0]
                else:
                    return ['Download Completed', 0]
            except IncorrectSceneId:
                return ['The SceneID provided was incorrect', 1]
Esempio n. 4
0
def main(args):
    """
    Main function - launches the program
    """

    v = VerbosityMixin()

    if args:
        if args.subs == 'process':
            verbose = True if args.verbose else False
            stored = process_image(args.path, args.bands, verbose,
                                   args.pansharpen)

            if args.upload:
                u = Uploader(args.key, args.secret, args.region)
                u.run(args.bucket, get_file(stored), stored)

            return ["The output is stored at %s" % stored]

        elif args.subs == 'search':

            try:
                if args.start:
                    args.start = reformat_date(parse(args.start))
                if args.end:
                    args.end = reformat_date(parse(args.end))
            except (TypeError, ValueError):
                return ["You date format is incorrect. Please try again!", 1]

            s = Search()

            try:
                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
                ]

            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':
                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!']
            elif result['status'] == 'error':
                return [result['message'], 1]
        elif args.subs == 'download':
            d = Downloader(download_dir=args.dest)
            try:
                if d.download(args.scenes,
                              convert_to_integer_list(args.bands)):
                    if args.process:
                        if args.dest:
                            path = join(args.dest, args.scenes[0])
                        else:
                            path = join(settings.DOWNLOAD_DIR, args.scenes[0])

                        # Keep using Google if the image is before 2015
                        if (int(args.scenes[0][12]) < 5 or not args.bands):
                            path = path + '.tar.bz'

                        stored = process_image(path, args.bands, False,
                                               args.pansharpen)

                        if args.upload:
                            try:
                                u = Uploader(args.key, args.secret,
                                             args.region)
                            except NoAuthHandlerFound:
                                return ["Could not authenticate with AWS", 1]
                            except URLError:
                                return [
                                    "Connection timeout. Probably the region parameter is incorrect",
                                    1
                                ]
                            u.run(args.bucket, get_file(stored), stored)

                        return ["The output is stored at %s" % stored]
                    else:
                        return ['Download Completed', 0]
            except IncorrectSceneId:
                return ['The SceneID provided was incorrect', 1]
Esempio n. 5
0
def main(args):
    """
    Main function - launches the program.

    :param args:
        The Parser arguments
    :type args:
        Parser object

    :returns:
        List

    :example:
        >>> ["The latitude and longitude values must be valid numbers", 1]
    """

    v = VerbosityMixin()

    if args:

        if 'clip' in args:
            bounds = convert_to_float_list(args.clip)
        else:
            bounds = None

        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, args.ndvi, force_unzip,
                                   args.ndvigrey, bounds)

            if args.upload:
                u = Uploader(args.key, args.secret, args.region)
                u.run(args.bucket, get_file(stored), stored)

            return ["The output is stored at %s" % stored]

        elif args.subs == 'search':

            try:
                if args.start:
                    args.start = reformat_date(parse(args.start))
                if args.end:
                    args.end = reformat_date(parse(args.end))
                if args.latest > 0:
                    args.limit = 25
                    end = datetime.now()
                    start = end - relativedelta(days=+365)
                    args.end = end.strftime("%Y-%m-%d")
                    args.start = start.strftime("%Y-%m-%d")
            except (TypeError, ValueError):
                return ["Your date format is incorrect. Please try again!", 1]

            s = Search()

            try:
                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]

            address = args.address
            if address and (lat and lon):
                return ["Cannot specify both address and latitude-longitude"]

            result = s.search(paths_rows=args.pathrow,
                              lat=lat,
                              lon=lon,
                              address=address,
                              limit=args.limit,
                              start_date=args.start,
                              end_date=args.end,
                              cloud_max=args.cloud)

            if result['status'] == 'SUCCESS':
                if args.json:
                    return json.dumps(result)

                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!']

            elif result['status'] == 'error':
                return [result['message'], 1]
        elif args.subs == 'download':
            d = Downloader(download_dir=args.dest)
            try:
                bands = convert_to_integer_list(args.bands)

                if args.process:
                    if args.pansharpen:
                        bands.append(8)

                    if args.ndvi or args.ndvigrey:
                        bands = [4, 5]

                    if not args.bands:
                        bands = [4, 3, 2]

                downloaded = d.download(args.scenes, bands)

                if args.process:
                    if not args.bands:
                        args.bands = '432'
                    force_unzip = True if args.force_unzip else False
                    for scene, src in downloaded.iteritems():
                        if args.dest:
                            path = join(args.dest, scene)
                        else:
                            path = join(settings.DOWNLOAD_DIR, scene)

                        # Keep using Google if the image is before 2015
                        if src == 'google':
                            path = path + '.tar.bz'

                        stored = process_image(path, args.bands, False, args.pansharpen, args.ndvi, force_unzip,
                                               args.ndvigrey, bounds=bounds)

                        if args.upload:
                            try:
                                u = Uploader(args.key, args.secret, args.region)
                            except NoAuthHandlerFound:
                                return ["Could not authenticate with AWS", 1]
                            except URLError:
                                return ["Connection timeout. Probably the region parameter is incorrect", 1]
                            u.run(args.bucket, get_file(stored), stored)

                    return ['The output is stored at %s' % stored, 0]
                else:
                    return ['Download Completed', 0]
            except IncorrectSceneId:
                return ['The SceneID provided was incorrect', 1]
Esempio n. 6
0
def main(args):
    """
    Main function - launches the program.

    :param args:
        The Parser arguments
    :type args:
        Parser object

    :returns:
        List

    :example:
        >>> ["The latitude and longitude values must be valid numbers", 1]
    """

    v = VerbosityMixin()

    if args:

        if 'clip' in args:
            bounds = convert_to_float_list(args.clip)
        else:
            bounds = None

        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, args.ndvi, force_unzip,
                                   args.ndvigrey, bounds)

            if args.upload:
                u = Uploader(args.key, args.secret, args.region)
                u.run(args.bucket, get_file(stored), stored)

            return ["The output is stored at %s" % stored]

        elif args.subs == 'search':

            try:
                if args.start:
                    args.start = reformat_date(parse(args.start))
                if args.end:
                    args.end = reformat_date(parse(args.end))
                if args.latest > 0:
                    args.limit = 25
                    end = datetime.now()
                    start = end - relativedelta(days=+365)
                    args.end = end.strftime("%Y-%m-%d")
                    args.start = start.strftime("%Y-%m-%d")
            except (TypeError, ValueError):
                return ["Your date format is incorrect. Please try again!", 1]

            s = Search()

            try:
                if args.lat is not None:
                    lat = float(args.lat)
                else:
                    lat = None

                if args.lon is not None:
                    lon = float(args.lon)
                else:
                    lon = None
            except ValueError:
                return ["The latitude and longitude values must be valid numbers", 1]

            address = args.address
            if address and (lat and lon):
                return ["Cannot specify both address and latitude-longitude"]

            result = s.search(paths_rows=args.pathrow,
                              lat=lat,
                              lon=lon,
                              address=address,
                              limit=args.limit,
                              start_date=args.start,
                              end_date=args.end,
                              cloud_max=args.cloud,
                              geojson=args.geojson)

            if 'status' in result:

                if result['status'] == 'SUCCESS':
                    if args.json:
                        return json.dumps(result)

                    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!']

                elif result['status'] == 'error':
                    return [result['message'], 1]

            if args.geojson:
                return json.dumps(result)

        elif args.subs == 'download':
            d = Downloader(download_dir=args.dest, usgs_user=args.username, usgs_pass=args.password)
            try:
                bands = convert_to_integer_list(args.bands)

                if args.process:
                    if args.pansharpen:
                        bands.append(8)

                    if args.ndvi or args.ndvigrey:
                        bands = [4, 5]

                    if not args.bands:
                        bands = [4, 3, 2]

                files = d.download(args.scenes, bands)

                if args.process:
                    if not args.bands:
                        args.bands = '432'
                    force_unzip = True if args.force_unzip else False
                    for f in files:
                        stored = process_image(f, args.bands, False, args.pansharpen, args.ndvi, force_unzip,
                                               args.ndvigrey, bounds=bounds)

                        if args.upload:
                            try:
                                u = Uploader(args.key, args.secret, args.region)
                            except NoAuthHandlerFound:
                                return ["Could not authenticate with AWS", 1]
                            except URLError:
                                return ["Connection timeout. Probably the region parameter is incorrect", 1]
                            u.run(args.bucket, get_file(stored), stored)

                    return ['The output is stored at %s' % stored, 0]
                else:
                    return ['Download Completed', 0]
            except IncorrectSceneId:
                return ['The SceneID provided was incorrect', 1]
            except (RemoteFileDoesntExist, USGSInventoryAccessMissing) as e:
                return [e.message, 1]
Esempio n. 7
0
def main(args):
    """
    Main function - launches the program.

    :param args:
        The Parser arguments
    :type args:
        Parser object

    :returns:
        List

    :example:
        >>> ["The latitude and longitude values must be valid numbers", 1]
    """

    v = VerbosityMixin()

    if 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, args.ndvi, force_unzip, args.ndvi1)

            if args.upload:
                u = Uploader(args.key, args.secret, args.region)
                u.run(args.bucket, get_file(stored), stored)

            return ["The output is stored at %s" % stored]

        elif args.subs == 'search':

            try:
                if args.start:
                    args.start = reformat_date(parse(args.start))
                if args.end:
                    args.end = reformat_date(parse(args.end))
            except (TypeError, ValueError):
                return ["You date format is incorrect. Please try again!", 1]

            s = Search()

            try:
                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]

            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':
                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!']
            elif result['status'] == 'error':
                return [result['message'], 1]
        elif args.subs == 'download':
            d = Downloader(download_dir=args.dest)
            try:
                bands = convert_to_integer_list(args.bands)
                if args.pansharpen:
                    bands.append(8)
                if args.ndvi:
                    bands = [4, 5]

                downloaded = d.download(args.scenes, bands)

                if args.process:
                    force_unzip = True if args.force_unzip else False
                    for scene, src in downloaded.iteritems():
                        if args.dest:
                            path = join(args.dest, scene)
                        else:
                            path = join(settings.DOWNLOAD_DIR, scene)

                        # Keep using Google if the image is before 2015
                        if src == 'google':
                            path = path + '.tar.bz'

                        stored = process_image(path, args.bands, False, args.pansharpen, args.ndvi, force_unzip)

                        if args.upload:
                            try:
                                u = Uploader(args.key, args.secret, args.region)
                            except NoAuthHandlerFound:
                                return ["Could not authenticate with AWS", 1]
                            except URLError:
                                return ["Connection timeout. Probably the region parameter is incorrect", 1]
                            u.run(args.bucket, get_file(stored), stored)

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

                    return ['Image Processing Completed', 0]
                else:
                    return ['Download Completed', 0]
            except IncorrectSceneId:
                return ['The SceneID provided was incorrect', 1]
Esempio n. 8
0
def main(args):
    """
    Main function - launches the program
    """

    v = VerbosityMixin()

    if args:
        if args.subs == 'process':
            verbose = True if args.verbose else False
            try:
                bands = convert_to_integer_list(args.bands)
                p = Process(args.path, bands=bands, verbose=verbose)
            except IOError:
                exit("Zip file corrupted", 1)
            except FileDoesNotExist as e:
                exit(e.message, 1)

            stored = p.run(args.pansharpen)

            exit("The output is stored at %s" % stored)

        elif args.subs == 'search':

            try:
                if args.start:
                    args.start = reformat_date(parse(args.start))
                if args.end:
                    args.end = reformat_date(parse(args.end))
            except (TypeError, ValueError):
                exit("You date format is incorrect. Please try again!", 1)

            s = Search()

            try:
                lat = float(args.lat) if args.lat else None
                lon = float(args.lon) if args.lon else None
            except ValueError:
                exit("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 result['status'] == 'SUCCESS':
                v.output('%s items were found' % result['total'],
                         normal=True,
                         arrow=True)
                if result['total'] > 100:
                    exit('Over 100 results. Please narrow your search', 1)
                else:
                    v.output(json.dumps(result, sort_keys=True, indent=4),
                             normal=True,
                             color='green')
                    exit('Search completed!')
            elif result['status'] == 'error':
                exit(result['message'], 1)
        elif args.subs == 'download':
            d = Downloader(download_dir=args.dest)
            try:
                if d.download(args.scenes,
                              convert_to_integer_list(args.bands)):
                    exit('Download Completed', 0)
            except IncorrectSceneId:
                exit('The SceneID provided was incorrect', 1)
Esempio n. 9
0
def main(args):
    """
    Main function - launches the program
    """

    v = VerbosityMixin()

    if args:
        if args.subs == 'process':
            verbose = True if args.verbose else False
            try:
                bands = convert_to_integer_list(args.bands)
                p = Process(args.path, bands=bands, verbose=verbose)
            except IOError:
                exit("Zip file corrupted", 1)
            except FileDoesNotExist as e:
                exit(e.message, 1)

            stored = p.run(args.pansharpen)

            exit("The output is stored at %s" % stored)

        elif args.subs == 'search':

            try:
                if args.start:
                    args.start = reformat_date(parse(args.start))
                if args.end:
                    args.end = reformat_date(parse(args.end))
            except (TypeError, ValueError):
                exit("You date format is incorrect. Please try again!", 1)

            s = Search()

            try:
                lat = float(args.lat) if args.lat else None
                lon = float(args.lon) if args.lon else None
            except ValueError:
                exit("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 result['status'] == 'SUCCESS':
                v.output('%s items were found' % result['total'], normal=True, arrow=True)
                if result['total'] > 100:
                    exit('Over 100 results. Please narrow your search', 1)
                else:
                    v.output(json.dumps(result, sort_keys=True, indent=4), normal=True, color='green')
                    exit('Search completed!')
            elif result['status'] == 'error':
                exit(result['message'], 1)
        elif args.subs == 'download':
            d = Downloader(download_dir=args.dest)
            try:
                if d.download(args.scenes, convert_to_integer_list(args.bands)):
                    exit('Download Completed', 0)
            except IncorrectSceneId:
                exit('The SceneID provided was incorrect', 1)