import numpy as np
import pandas as pd
import h5py
import glob
import os
from datetime import datetime

'''
Set the path to the folder containing the raw-Frame csv files. The script will only read
the raw-Frame files and ignore any other files present.
Set the start time in the START variable below using the ISO-8601 format (i.e. yyyy-MM-ddThh:mm:ss.zzz)
Set the time between files in the DELTA variable below
'''
path = 'D:\LIBS_datarepo\Wien\spotsize variation\Exported\one file per frame'

START = '2022-07-22T14:46:55.101'
DELTA = 1.0

st_time = datetime.strptime(START, '%Y-%m-%dT%H:%M:%S.%f').timestamp()
end_time = '1658428202000'
key = '12345678'

wl = None

main_df = pd.DataFrame()
timer = 0.

os.chdir(path)
print(f'\nFinding data in {os.getcwd()}')
print('...')
for filename in sorted(glob.glob(path + '\*raw-Frame-*.csv')):
    df = pd.read_csv(filename,sep='\t')

    if not 'wavelengths' in main_df.columns:
        main_df['wavelengths'] = df['Wavelength']

    df.drop('Wavelength', axis=1, inplace=True)
    df.columns = [str(timer)]

    main_df = pd.concat([main_df, df], axis=1)

    timer += DELTA

filename = ""

with h5py.File('export.h5', 'w') as f:
    md_grp = f.create_group('Metadata')

    md_grp['ExperimentStart'] = np.bytes_(st_time)
    md_grp['ExperimentEnd'] = np.bytes_(end_time)
    md_grp['Key'] = np.bytes_(key)

    data_grp = f.create_group('Avantes')
    for column in main_df.columns:
        data_grp[column] = main_df[column]

    filename = f.filename

print(f"Export to {filename} completed\n")
