import numpy as np
import matplotlib.pyplot as plt

sensor_numb = []
a = int(input("Vib1 ="))
b = int(input("Vib2 ="))
c = int(input("Vib3 ="))

sensor_numb.append(a)
sensor_numb.append(b)
sensor_numb.append(c) 

input_array = sensor_numb


# chris = array[a-Vib1 /b-Vib2][0-time 1-values 2-sensor_name] 



# sensors_to_read=['Vib1','Vib2']
def read_data_file():
    data = []
    file_path = "C:/talash/FFT/Project/Test_data.txt"
    with open(file_path, 'r') as file:
        # Optionally, read and skip header line if present
        header = file.readline()  # comment out if no header
        for line in file:
            # Remove any leading/trailing whitespace and newline characters
            line = line.strip()
            # Split the line by tab delimiter
            parts = line.split('\t')
            # Convert each part to float (or int) as needed
            # Here, assuming all are numbers
            row = [float(value) for value in parts]
            data.append(row)
            
    return data

array=read_data_file()

my_array=np.array(array)



def sensor_data(sensors):
    for i in range(0,3):
        if sensors[i] == 1:
           # index = sensors.index(i)
           # print(index)
           
           a = my_array[:,i+1]
           print(a)
           sampling_rate = 2000    #sample_rate_frequency (Hz)
           end_time = 1000
           Num_samples = 1819468
           t = np.linspace(0, end_time, Num_samples, endpoint=False)      # Time vector  
           # t = t[:len(sendata)]
           # plt.figure(figsize=(12,6))     # show the results

           plt.subplot(2,1,1)
           plt.plot(t,a)
           plt.title("main signal")
           plt.xlable=("time(second)")
           plt.ylable=("amplitude")
           
           plt.grid()
           plt.tight_layout()
           plt.show()
 
                  #    Fast Fourier
           fft_result = np.fft.fft(a)
           frequencies = np.fft.fftfreq(len(a), d=(t[1]-t[0]))
           
            # fast_fourie = fast_fourier(sendata)

           plt.subplot(2,1,2)
           # segments = [([xi, xi], [0, yi]) for xi, yi in zip(x, y)]
           plt.stem(frequencies[:len(frequencies)//2], np.abs(fft_result)[:len(fft_result)//2])
           plt.title("Magnitude spectrum")
           plt.xlabel("Frequency(Hz)")
           plt.ylabel("Amplitude")


           plt.tight_layout()
           plt.show()  
           
           # vertical =my_array[:,i+1].reshape(-1, 1)
           # print(vertical)
        else:
           continue            
    return (a)

            
sendata = sensor_data(input_array)
# print(sendata)

# sampling_rate = 2000    #sample_rate_frequency (Hz)
# end_time = 909.733500
# Num_samples = 1819468
# t = np.linspace(0, end_time, Num_samples, endpoint=False)      # Time vector  
# # t = t[:len(sendata)]

# # plt.figure(figsize=(12,6))     # show the results

# plt.subplot(2,1,1)
# plt.plot(t,sendata)
# plt.title("main signal")
# plt.xlable=("time(second)")
# plt.ylable=("amplitude")


# plt.grid()
# plt.tight_layout()
# plt.show()




         #    Fast Fourier
# fft_result = np.fft.fft(sendata)
# frequencies = np.fft.fftfreq(len(sendata), d=(t[1]-t[0]))



# # fast_fourie = fast_fourier(sendata)

# plt.subplot(2,1,2)
# # segments = [([xi, xi], [0, yi]) for xi, yi in zip(x, y)]
# plt.stem(frequencies[:len(frequencies)//2], np.abs(fft_result)[:len(fft_result)//2])
# plt.title("Magnitude spectrum")
# plt.xlabel("Frequency(Hz)")
# plt.ylabel("Amplitude")


# plt.tight_layout()
# plt.show()










# column_data = my_array[:,2]
# print(column_data)




# print(array[1][2])
# result=[]

# for x in my_array:
    # result.append(x.split('')[1])
    
    
# print(result)
# print([x.split(' ')[1] for x in open(content).readlines()])




# column_index = 1  # Change this to the desired column index
# column_data = [row[column_index] for row in array]
# a=len(column_data)
# print(a)
# print("Extracted Column Data:", column_data)

 
# a = array[0-Vib1 /1-Vib2][0-time 1-values 2-sensor_name] 

# print(my_array[0][1])
# # print(a)  
# output_array=function(input_array)

# print(output_array[0][1][0])

    
# Milad1 = 'Test_data.txt'
# data_array, sensor = read_data_file(Milad1)
# sensor_numb = int(input("Please enter the sensor number: "))
# a = data_array[0][sensor_numb]
# print(a)

# def check_sensor(meghdar):
    # if meghdar == 1:
        # return "Vib1"
    # elif meghdar == 2:
        # return "Vib2"
    # else:
        # return "Vib3"
        


# result = check_sensor(sensor_numb)
 
 
# print(result)
 
# a = data_array[0][2]


# print(a)
# print(select_sens)






    
# # print(data_array)
# # var_number: 0: time var_number, 1:value var_number, 

# def get_data_by_title(data_array, sensor_number, var_number):
   
    # for sensor_number in data_array:
        # if item['title'] == 'sensor_number':
            # return item
        # # elif item['Vib2'] == 'Vib2'
            # # return item
    # return None
    
# title_name = 'sensor_number'
# result = get_data_by_title(data_array, title_name, var_number)
# if result:
    # print(result)
# else:
    # print(f"Title '{title_name}' not found.")   
    
    
    
    
    