#===============================================================================
# Convert_All_IV_Files_SF_00_1_Hz.py
# Python script for iterating through IV text files generated by "Tapping_IV_browser - log - 4Gfit copy - Modified for Saving IV Files.vi" LabVIEW Program.
# Iteratively calls "IV_to_IT_File_Conversion_SF_00_1_Hz.py" to convert each IV file in the "IV Curves" folder from Voltage to Time, and save new files in the "IT Curves" folder.
#     "IV_to_IT_File_Conversion_SF_00_1_Hz.py" Opens IV text file with two columns (voltage, current) and convert voltage to time (V --> Seconds) using voltage sweep frequency (Hz) and range (V).
#     In other words, converts 4 quadrants of voltage sweep cycle [0 --> +, + --> 0, 0 --> -, - --> 0] into one continuous linear time progression.
#     Also removes bias (0.3 V in the default case) before converting voltage to time.
# Author: Jon Marrs
# Date: 2017-07-28
#===============================================================================

import os
import subprocess
import time

#===============================================================================
# BEGIN Configuration
#===============================================================================

# Subscript Name (Name of the script called by this script)
subscript_name = "IV_to_IT_File_Conversion_SF_00_1_Hz.py"

# IV File Directory Name (Name of the directory where the IV files are relative to the script(s) location)
iv_file_directory_name = "IV Curves"

#===============================================================================
# END Configuration
#===============================================================================

default_directory = os.getcwd()

print(default_directory)

iv_file_directory = os.getcwd() + "\\" + iv_file_directory_name

os.chdir(iv_file_directory)

current_directory = os.getcwd()

print(current_directory)

parent_directory = os.listdir(os.curdir)

print(parent_directory)

print()

# Process each IV file in the directory and call the processing subscript for each IV file
for file in os.listdir():
    file_name, file_ext = os.path.splitext(file)
    
    file_amp, file_bias, file_sol, file_mc, file_rr, file_num, file_iv = file_name.split('_')
    
    file_iv_cleansed = file_iv.replace("IV","")

    print(current_directory)

    print(file)
    
    new_file_name = file_name.replace("IV","IT")
    
    # Call Python script and pass file_name as argument
    subprocess.Popen("python ../" + subscript_name + " " + str(file_name) + " " + str(new_file_name), shell=True)
    
    time.sleep(1)

