# Path: cmt/mapclient_qt.py
# def centerMap(lng, lat, zoom):  # pylint: disable=g-bad-name
#     """Center the default map instance at the given lat, lon and zoom values."""
#     addEmptyGui()
#     map_instance.CenterMap(lng, lat, zoom)
# 
# def addToMap(self, eeobject, vis_params=None, name="", show=True):
#     '''Ads an EE object to the map'''
#         
#     # Flatten any lists to comma separated strings - needed for eeobject.getMapId() call below!
#     if vis_params:
#         vis_params = dict(vis_params)
#         for key in vis_params.keys():
#             item = vis_params.get(key)
#             if (isinstance(item, collections.Iterable) and (not isinstance(item, basestring))):
#                  vis_params[key] = ','.join([str(x) for x in item])
# 
#     def execute_thread(waiting_threads):
#         # get thread before starting
#         with self.thread_lock:
#             pass
#         result = eeobject.getMapId(vis_params)
#         for t in waiting_threads:
#             t.join()
#         with self.thread_lock:
#             self.executing_threads.pop(0)
#         return result
# 
#     with self.thread_lock:
#         self.executing_threads.append(cmt.util.miscUtilities.waitForEeResult(functools.partial(execute_thread, list(self.executing_threads)),
#                     lambda a : self.addOverlay(MakeTileManager(a), eeobject, name, show, vis_params)))


import ee
from cmt.mapclient_qt import centerMap, addToMap
from histogram import RadarHistogram
    for i in range(1, len(sensor.band_names)):
        loose_thresholded = loose_thresholded.And(sensor.image.select([sensor.band_names[i]]).lte(thresholds[i]))
    addToMap(loose_thresholded, {'min': 0, 'max': 1}, 'Loose', False)
    for i in range(REGION_GROWTH_RANGE):
        thresholded = thresholded.convolve(neighborhood_kernel).And(loose_thresholded)
    return thresholded

def threshold(domain, historical_domain=None):
    '''An implementation of the paper:
           Matgen, Hostache, Schumann, et. al. "Towards an automated SAR-based flood monitoring system:
           Lessons learned from two case studies." Physics and Chemistry of the Earth, 2011.
           
        TODO: ????
    '''    
    
    sensor = domain.get_radar() # Get the first radar sensor available
    hist   = RadarHistogram(domain, sensor)
    
    thresholds = hist.get_thresholds() # Get thresholds for each band
    
    # For each band, get pixels less than a threshold
    results = []
    for c in range(len(thresholds)):
        ch = sensor.band_names[c]
        results.append(sensor.image.select([ch], [ch]).lte(thresholds[c]))

    #?????
    result_image = results[0]
    for c in range(1, len(results)):
        result_image = result_image.addBands(results[c], [sensor.band_names[c]])

# Path: cmt/mapclient_qt.py
# def centerMap(lng, lat, zoom):  # pylint: disable=g-bad-name
#     """Center the default map instance at the given lat, lon and zoom values."""
#     addEmptyGui()
#     map_instance.CenterMap(lng, lat, zoom)
# 
# def addToMap(self, eeobject, vis_params=None, name="", show=True):
#     '''Ads an EE object to the map'''
#         
#     # Flatten any lists to comma separated strings - needed for eeobject.getMapId() call below!
#     if vis_params:
#         vis_params = dict(vis_params)
#         for key in vis_params.keys():
#             item = vis_params.get(key)
#             if (isinstance(item, collections.Iterable) and (not isinstance(item, basestring))):
#                  vis_params[key] = ','.join([str(x) for x in item])
# 
#     def execute_thread(waiting_threads):
#         # get thread before starting
#         with self.thread_lock:
#             pass
#         result = eeobject.getMapId(vis_params)
#         for t in waiting_threads:
#             t.join()
#         with self.thread_lock:
#             self.executing_threads.pop(0)
#         return result
# 
#     with self.thread_lock:
#         self.executing_threads.append(cmt.util.miscUtilities.waitForEeResult(functools.partial(execute_thread, list(self.executing_threads)),
#                     lambda a : self.addOverlay(MakeTileManager(a), eeobject, name, show, vis_params)))


# Path: cmt/radar/matgen.py
import ee
from cmt.mapclient_qt import centerMap, addToMap
from histogram import RadarHistogram

    for i in range(1, len(sensor.band_names)):
        loose_thresholded = loose_thresholded.And(sensor.image.select([sensor.band_names[i]]).lte(thresholds[i]))
    addToMap(loose_thresholded, {'min': 0, 'max': 1}, 'Loose', False)
    for i in range(REGION_GROWTH_RANGE):
        thresholded = thresholded.convolve(neighborhood_kernel).And(loose_thresholded)
    return thresholded

def threshold(domain, historical_domain=None):
    '''An implementation of the paper:
           Matgen, Hostache, Schumann, et. al. "Towards an automated SAR-based flood monitoring system:
           Lessons learned from two case studies." Physics and Chemistry of the Earth, 2011.
           
        TODO: ????
    '''    
    
    sensor = domain.get_radar() # Get the first radar sensor available
    hist   = RadarHistogram(domain, sensor)
    
    thresholds = hist.get_thresholds() # Get thresholds for each band
    
    # For each band, get pixels less than a threshold
    results = []
    for c in range(len(thresholds)):
        ch = sensor.band_names[c]
        results.append(sensor.image.select([ch], [ch]).lte(thresholds[c]))

    #?????
    result_image = results[0]
    for c in range(1, len(results)):
        result_image = result_image.addBands(results[c], [sensor.band_names[c]])

    addToMap(result_image, {'min': 0, 'max': 1}, 'Color Image', False)