# Path: cmt/util/miscUtilities.py
# def safe_get_info(ee_object, max_num_attempts=5):
#     '''Keep trying to call getInfo() on an Earth Engine object until it succeeds.'''
#     
#     # Do not try again if we get one of these exceptions
#     fail_messages = ['Too many pixels']
#     
#     num_attempts = 0
#     while True:
#         try:
#             return ee_object.getInfo()
#         except Exception as e:
#             # Rethrow the exception if it is not timeout related
#             for message in fail_messages:
#                 if message in str(e):
#                     raise e
#                     
#             print('Earth Engine Error: %s. Waiting 10s and then retrying.' % (e))
#             time.sleep(10)
#             num_attempts += 1
#         if max_num_attempts and (num_attempts >= max_num_attempts):
#             raise Exception('safe_get_info failed to succeed after ' +str(num_attempts)+ ' attempts!')


import ee
from cmt.util.miscUtilities import safe_get_info
    text = image.get('METADATA_FILE_NAME').getInfo()
    if 'LE5' in text:
        return 'landsat5'
    if 'LE7' in text:
        return 'landsat7'
    if 'LC8' in text:
        return 'landsat8'
    print(text)
    raise Exception('Unrecognized Landsat image!')

def rename_landsat_bands(collection, collectionName):
    '''Selects and renames the landsat bands we are interested in.  
       Works with any Landsat satellite.'''

    # The list of bands we are interested in
    # - temp = temperature.  Landsat8 splits this into two bands, we use the first of these.
    # - The panchromatic band might be nice but it is only on Landsat7/8
    LANDSAT_BANDS_OF_INTEREST = ee.List(['blue', 'green', 'red', 'nir', 'swir1', 'temp', 'swir2'])

    # The indices where these bands are found in the Landsat satellites
    LANDSAT_BAND_INDICES = {'L8': ee.List([1, 2, 3, 4, 5, 9, 6]),
                            'L7': ee.List([0, 1, 2, 3, 4, 5, 7]),
                            'L5': ee.List([0, 1, 2, 3, 4, 5, 6])}

    landsat_index = 'L8';
    if '5' in collectionName:
        landsat_index = 'L5'
    if '7' in collectionName:
        landsat_index = 'L7'


# Path: cmt/util/miscUtilities.py
# def safe_get_info(ee_object, max_num_attempts=5):
#     '''Keep trying to call getInfo() on an Earth Engine object until it succeeds.'''
#     
#     # Do not try again if we get one of these exceptions
#     fail_messages = ['Too many pixels']
#     
#     num_attempts = 0
#     while True:
#         try:
#             return ee_object.getInfo()
#         except Exception as e:
#             # Rethrow the exception if it is not timeout related
#             for message in fail_messages:
#                 if message in str(e):
#                     raise e
#                     
#             print('Earth Engine Error: %s. Waiting 10s and then retrying.' % (e))
#             time.sleep(10)
#             num_attempts += 1
#         if max_num_attempts and (num_attempts >= max_num_attempts):
#             raise Exception('safe_get_info failed to succeed after ' +str(num_attempts)+ ' attempts!')


# Path: cmt/util/landsat_functions.py
import ee
from cmt.util.miscUtilities import safe_get_info

    text = image.get('METADATA_FILE_NAME').getInfo()
    if 'LE5' in text:
        return 'landsat5'
    if 'LE7' in text:
        return 'landsat7'
    if 'LC8' in text:
        return 'landsat8'
    print(text)
    raise Exception('Unrecognized Landsat image!')

def rename_landsat_bands(collection, collectionName):
    '''Selects and renames the landsat bands we are interested in.  
       Works with any Landsat satellite.'''

    # The list of bands we are interested in
    # - temp = temperature.  Landsat8 splits this into two bands, we use the first of these.
    # - The panchromatic band might be nice but it is only on Landsat7/8
    LANDSAT_BANDS_OF_INTEREST = ee.List(['blue', 'green', 'red', 'nir', 'swir1', 'temp', 'swir2'])

    # The indices where these bands are found in the Landsat satellites
    LANDSAT_BAND_INDICES = {'L8': ee.List([1, 2, 3, 4, 5, 9, 6]),
                            'L7': ee.List([0, 1, 2, 3, 4, 5, 7]),
                            'L5': ee.List([0, 1, 2, 3, 4, 5, 6])}

    landsat_index = 'L8';
    if '5' in collectionName:
        landsat_index = 'L5'
    if '7' in collectionName:
        landsat_index = 'L7'


    return collection.select(LANDSAT_BAND_INDICES[landsat_index], LANDSAT_BANDS_OF_INTEREST)