# -*- coding: utf-8 -*-
"""
Created on Tue Aug 13 14:09:58 2024

@author: chuffard
"""

import os
import pandas as pd
import glob

folder_path = 'C:/USERS/chuffard/Desktop/to trash/currents/'


all_files= [folder_path + f for f in os.listdir(folder_path)]


csv_files = [f for f in all_files if f.endswith('.csv')]

df_list = []

for csv in csv_files:
    file_path = os.path.join(folder_path, csv)
    try:
        # Try reading the file using default UTF-8 encoding
        df = pd.read_csv(file_path)
        df_list.append(df)
    except UnicodeDecodeError:
        try:
            # If UTF-8 fails, try reading the file using UTF-16 encoding with tab separator
            df = pd.read_csv(file_path, sep='\t', encoding='utf-16')
            df_list.append(df)
        except Exception as e:
            print(f"Could not read file {csv} because of error: {e}")
    except Exception as e:
        print(f"Could not read file {csv} because of error: {e}")


# Concatenate all data into one DataFrame
big_df = pd.concat(df_list, ignore_index=True)

# Save the final result to a new CSV file
big_df.to_csv(os.path.join(folder_path, 'combined_file.csv'), index=False)


#remove rows with empty data
big_df = big_df.dropna()

#decimate
decimateddf = big_df[::10]

decimateddf.to_csv('C:/USERS/chuffard/Desktop/to trash/finalRovercurrentmeter.csv') 

