import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import os

# df1 = pd.read_csv('../data/lowini_test/simparticle4_lowini_shift2/velocity.csv')
# df2 = pd.read_csv('../data/lowini_test/simparticle6_lowini_shift2/velocity.csv')
# df3 = pd.read_csv('../data/lowini_test/simparticle10_lowini_shift2/velocity.csv')



def segment(data):
    pos_x_1 = data[:, 0]
    seg_indices = []
    seg_indices.append(0)
    for k in range(pos_x_1.shape[0] - 1):
        if pos_x_1[k + 1] < pos_x_1[k]:
            seg_indices.append(k + 1) # starting of the next segment
    result = []
    
    for k in range(len(seg_indices) - 1):
        left = seg_indices[k]
        right = seg_indices[k + 1]
        result.append(data[left: right])
    result.append(data[right: ])
    return result

if __name__ == "__main__":
    root = '../data'
    exp = '2/shift4/'
    offsetY = ['dY2', 'dY4', 'dY6', 'dY8', 'dY10', 'dY12']

    df1 = pd.read_csv(os.path.join(root, exp, offsetY[0], 'velocity.csv'))
    df2 = pd.read_csv(os.path.join(root, exp, offsetY[1], 'velocity.csv'))
    # df4 = pd.read_csv(os.path.join(root, exp, offsetY[3], 'velocity.csv'))
    df3 = pd.read_csv(os.path.join(root, exp, offsetY[4], 'velocity.csv'))

    data1 = df1.to_numpy()
    data2 = df2.to_numpy()
    data3 = df3.to_numpy()

    segments1 = segment(data1)
    segments2 = segment(data2)
    segments3 = segment(data3)

    print(segments1)
    
    font = {'family' : 'Arial',
            'size'   : 20}

    plt.rc('font', **font)


    fig1, ax = plt.subplots(1, 1, figsize = (6, 6))
    for seg in segments1[0:-1]:
        ax.plot(seg[0:, 0], seg[0:, 1], color='b', label = '')

    for seg in segments2[0:-1]:
        ax.plot(seg[0:, 0], seg[0:, 1], color='r', label = '')
    
    for seg in segments3[0:-1]:
        ax.plot(seg[0:, 0], seg[0:, 1], color='g', label = '')

    ax.plot(segments1[-1][0:, 0], segments1[-1][0:, 1], color='b', label = 'Y_ini = 2')
    ax.plot(segments2[-1][0:, 0], segments2[-1][0:, 1], color='r', label = 'Y_ini = 4')
    ax.plot(segments3[-1][0:, 0], segments3[-1][0:, 1], color='g', label = 'Y_ini = 12')
    ax.legend()


    fig1.savefig("viz_shift4.png")
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import os

# df1 = pd.read_csv('../data/lowini_test/simparticle4_lowini_shift2/velocity.csv')
# df2 = pd.read_csv('../data/lowini_test/simparticle6_lowini_shift2/velocity.csv')
# df3 = pd.read_csv('../data/lowini_test/simparticle10_lowini_shift2/velocity.csv')



def segment(data):
    pos_x_1 = data[:, 0]
    seg_indices = []
    seg_indices.append(0)
    for k in range(pos_x_1.shape[0] - 1):
        if pos_x_1[k + 1] < pos_x_1[k]:
            seg_indices.append(k + 1) # starting of the next segment
    result = []
    
    for k in range(len(seg_indices) - 1):
        left = seg_indices[k]
        right = seg_indices[k + 1]
        result.append(data[left: right])
    result.append(data[right: ])
    return result

if __name__ == "__main__":
    root = '../data'
    exp = '2/shift4/'
    offsetY = ['dY2', 'dY4', 'dY6', 'dY8', 'dY10', 'dY12']

    df1 = pd.read_csv(os.path.join(root, exp, offsetY[0], 'velocity.csv'))
    df2 = pd.read_csv(os.path.join(root, exp, offsetY[1], 'velocity.csv'))
    # df4 = pd.read_csv(os.path.join(root, exp, offsetY[3], 'velocity.csv'))
    df3 = pd.read_csv(os.path.join(root, exp, offsetY[4], 'velocity.csv'))

    data1 = df1.to_numpy()
    data2 = df2.to_numpy()
    data3 = df3.to_numpy()

    segments1 = segment(data1)
    segments2 = segment(data2)
    segments3 = segment(data3)

    print(segments1)
    
    font = {'family' : 'Arial',
            'size'   : 20}

    plt.rc('font', **font)


    fig1, ax = plt.subplots(1, 1, figsize = (6, 6))
    for seg in segments1[0:-1]:
        ax.plot(seg[0:, 0], seg[0:, 1], color='b', label = '')

    for seg in segments2[0:-1]:
        ax.plot(seg[0:, 0], seg[0:, 1], color='r', label = '')
    
    for seg in segments3[0:-1]:
        ax.plot(seg[0:, 0], seg[0:, 1], color='g', label = '')

    ax.plot(segments1[-1][0:, 0], segments1[-1][0:, 1], color='b', label = 'Y_ini = 2')
    ax.plot(segments2[-1][0:, 0], segments2[-1][0:, 1], color='r', label = 'Y_ini = 4')
    ax.plot(segments3[-1][0:, 0], segments3[-1][0:, 1], color='g', label = 'Y_ini = 12')
    ax.legend()


    fig1.savefig("viz_shift4.png")
# df1 = pd.read_csv('../data/lowini_test/simparticle4_lowini_shift2/velocity.csv') # df2 = pd.read_csv('../data/lowini_test/simparticle6_lowini_shift2/velocity.csv') # df3 = pd.read_csv('../data/lowini_test/simparticle10_lowini_shift2/velocity.csv') # starting of the next segment # df4 = pd.read_csv(os.path.join(root, exp, offsetY[3], 'velocity.csv'))