import numpy as np
from os import path, listdir
from sys import exit, stderr, float_info
from pathlib import Path

path_in = path.abspath("in")
path_out = path.abspath("out")


def convert_to_deltas():
    files_in = listdir(path_in)
    for f in files_in:
        input_array = []
        output_array = []
        try:
            current_file = open("in/%s" % f, "r")
            print("Processing %s...." % current_file.name, file=stderr)
            for line in current_file.readlines():
                input_array.append(float(line))

            if len(input_array) < 2:
                raise Exception("Array of len < 2")

            _prev = input_array[0]
            _next = None
            for i in range(1, len(input_array)):
                _next = input_array[i]
                output_array.append(_next - _prev)
                _prev = _next
            current_file.close()

            out_name = f.split('.')[0]
            f_out = open("out/%s_deltas.txt" % out_name, "w")
            for j in output_array:
                f_out.write("{:.10f}\n".format(j))
            f_out.close()
            # print(output_array)
            print("Done %s!" % current_file.name, file=stderr)
        except IOError:
            exit(-1)
    print("All done!", file=stderr)

convert_to_deltas()