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
sensor_names=['Vib1','Vib3']


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

class SensorDataReader:
        def __init__(self):
            pass
        
        def read_data(self, path, filename, sensor_names=None):
            
            import os

            full_path = os.path.join(path, filename)  
            data_array = []  
            file_path = "C:/talash/FFT/Project/Test_data.txt"  
            with open(full_path, 'r') as file:
                lines = file.readlines()
        
            header = lines[0].strip().split('\t')
            sensor_headers = header[1:]

            sensor_indices = {}

            for idx, name in enumerate(sensor_headers):
                sensor_indices[name] = idx + 1 

            if not sensor_names:
                sensors_to_process = sensor_headers
            else:

                sensors_to_process = [s for s in sensor_names if s in sensor_headers]
                if not sensors_to_process:
                    print("No matching sensors found in the data.")
                    return []

            for sensor in sensors_to_process:
                # Each sensor: [times_list, values_list, [sensor_name]]
                data_array.append([[], [], [sensor]])


            sensor_index_map = {sensor: idx for idx, sensor in enumerate(sensors_to_process)}

            # Process data lines
            for line in lines[1:]:
                parts = line.strip().split('\t')
                time_value = parts[0]
                for sensor in sensors_to_process:
                    col_idx = sensor_indices[sensor]
                    value = parts[col_idx] if col_idx < len(parts) else ''
                    # Append time to the sensor's time list
                    data_array[sensor_index_map[sensor]][0].append(time_value)
                    # Append value to the sensor's value list
                    data_array[sensor_index_map[sensor]][1].append(value)

                    return data_array                      
  



reader = SensorDataReader()
data = reader.read_data('C:/talash/FFT/Project', 'Test_data.txt', ['SensorA', 'SensorB'])
print(data)  
            

# # 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)
