# Path: cmt/mapclient_qt.py
# 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 os
import ee
import math
import numpy
import scipy
import scipy.special
import scipy.optimize
import histogram
import matplotlib
import matplotlib.pyplot as plt
from cmt.mapclient_qt import addToMap
#    '''Create a plot of a histogram'''
#    plt.bar(binCenters, histogram)
#
#    plt.show()


#def __show_histogram(histogram, params=None):
#    '''Create a plot of a histogram'''
#    #values = histogram['histogram']
#    #start  = histogram['bucketMin']
#    #width  = histogram['bucketWidth']
#    ind    = numpy.arange(start=start, stop=start + width * len(values), step=width)[:-1]
#    plt.bar(ind, height=values[:-1], width=width, color='b')
#    #if params != None:
#    #    m = domains.MINIMUM_VALUES[instrument]
#    #    if instrument == domains.UAVSAR:
#    #        m = math.log10(m)
#    #    mid = int((params[0] - start) / width)
#    #    cumulative = sum(values[:mid]) + values[mid] / 2
#    #    scale = cumulative / __cdf(params, m, params[0])
#    #    plt.bar(ind, map(lambda x : scale * (__cdf(params, m, x + width / 2) - __cdf(params, m, x - width 
#

def applyCutlerLinearLogScale(grayImage, roi):
    '''Translates the input SAR image into a hybrid linear-log scale as described in
        "Robust automated thresholding of SAR imagery for open-water detection"
        by Patrick J Cutler and Frederick W Koehler'''
    
    TOP_SECTION_PERCENTILE = 99
    TOP_SECTION_START      = 221

# Path: cmt/mapclient_qt.py
# 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/martinis.py
import os
import ee
import math
import numpy
import scipy
import scipy.special
import scipy.optimize
import histogram
import matplotlib
import matplotlib.pyplot as plt
from cmt.mapclient_qt import addToMap

#    '''Create a plot of a histogram'''
#    plt.bar(binCenters, histogram)
#
#    plt.show()


#def __show_histogram(histogram, params=None):
#    '''Create a plot of a histogram'''
#    #values = histogram['histogram']
#    #start  = histogram['bucketMin']
#    #width  = histogram['bucketWidth']
#    ind    = numpy.arange(start=start, stop=start + width * len(values), step=width)[:-1]
#    plt.bar(ind, height=values[:-1], width=width, color='b')
#    #if params != None:
#    #    m = domains.MINIMUM_VALUES[instrument]
#    #    if instrument == domains.UAVSAR:
#    #        m = math.log10(m)
#    #    mid = int((params[0] - start) / width)
#    #    cumulative = sum(values[:mid]) + values[mid] / 2
#    #    scale = cumulative / __cdf(params, m, params[0])
#    #    plt.bar(ind, map(lambda x : scale * (__cdf(params, m, x + width / 2) - __cdf(params, m, x - width 
#

def applyCutlerLinearLogScale(grayImage, roi):
    '''Translates the input SAR image into a hybrid linear-log scale as described in
        "Robust automated thresholding of SAR imagery for open-water detection"
        by Patrick J Cutler and Frederick W Koehler'''
    
    TOP_SECTION_PERCENTILE = 99
    TOP_SECTION_START      = 221

    topRange = 256 - TOP_SECTION_START