def get_distances(data,chaining_distance):
    percentage_gap_dict = OrderedDict()
    for days_to_increment in [7,28,90]:
        dates_data = lsl.get_day_seperate_data(data,days_to_increment)
        if days_to_increment not in percentage_gap_dict:
            percentage_gap_dict[days_to_increment] = OrderedDict()
        for day_with_gap in dates_data:
            print day_with_gap
            timestamps = dates_data[day_with_gap].keys()
            timestamps.sort()
            current_point = ''
            dx = total_distance = 0.0
            chaining = 0.0
            chaining_distances = []
            gaps = []
            dx_array = []
            for ts in timestamps:
                start = [dates_data[day_with_gap][ts]['start_lat'],dates_data[day_with_gap][ts]['start_lng']]
                end   = [dates_data[day_with_gap][ts]['end_lat'],dates_data[day_with_gap][ts]['end_lng']]
                next_point = start
                if current_point:
                    dx = lsl.get_distance(current_point,next_point)
                    dx_array.append(dx)
                    if dx <= chaining_distance:
                        chaining+=dates_data[day_with_gap][ts]["distance_km"]/1000.0
                        
                    else:
                        chaining_distances.append(chaining)
                        gaps.append(dx)
                        chaining = 0.0
                current_point = end
            
                if len(chaining_distances)*len(gaps)!=0:
                    N = len(timestamps)
                    if day_with_gap not in percentage_gap_dict[days_to_increment]:
                        percentage_gap_dict[days_to_increment][day_with_gap] = {}
                    #percentage_gap_dict[days_to_increment][day_with_gap]['percentage'] = lsl.get_percentage_chain(chaining_distances,gaps)
                    percentage_gap_dict[days_to_increment][day_with_gap]['percentage'] = lsl.get_percentage_chain_counts(gaps,N)
                    percentage_gap_dict[days_to_increment][day_with_gap]['gaps'] = dx_array
                    if days_to_increment == 7:
                        percentage_gap_dict[days_to_increment][day_with_gap]['number_of_trips'] = len(timestamps) 

    return percentage_gap_dict
def get_percentages(dates_data,timestamps,dates):
    chaining_distance = 852.0
    percentage_gap_dict = {}
    for d_gap in [7,28,90]:
        timestamp = timestamps[d_gap]
        N = len(timestamp)
        current_point = ''
        dx = total_distance = 0.0
        chaining = 0.0
        chaining_distances = []
        gaps = []
        dx_array = []
        for ts in timestamp:
            ts = str(ts)
            start = [dates_data[ts]['start_lat'],dates_data[ts]['start_lng']]
            end   = [dates_data[ts]['end_lat'],dates_data[ts]['end_lng']]
            next_point = start
            if current_point:
                dx = lsl.get_distance(current_point,next_point)
                dx_array.append(dx)
                total_distance+=dates_data[ts]["distance_km"]
                if dx <= chaining_distance:
                    chaining+=dates_data[ts]["distance_km"]
                    
                else:
                    chaining_distances.append(chaining)
                    gaps.append(dx/1000.0)
                    chaining = 0.0
            current_point = end
        if len(chaining_distances)*len(gaps)!=0:
            if d_gap not in percentage_gap_dict:
                percentage_gap_dict[d_gap] = {}
            percentage_gap_dict[d_gap]['chaining_distance_percentage_{0}'.format(d_gap)] = lsl.get_percentage_chain(chaining_distances,gaps)
            #percentage_gap_dict[d_gap]['chaining_count_percentage_{0}'.format(d_gap)] = lsl.get_percentage_chain_counts(gaps,N)
            percentage_gap_dict[d_gap]['number_isolated_chains_{0}'.format(d_gap)] = len(chaining_distances)
            percentage_gap_dict[d_gap]['total_trips_{0}'.format(d_gap)] = N
            percentage_gap_dict[d_gap]['covered_distance_{0}'.format(d_gap)] = total_distance
            percentage_gap_dict[d_gap]['number_chained_trips_{0}'.format(d_gap)] = 'N/A'
    return percentage_gap_dict