import sys                                             
import csv
import asyncio
import logging
import numpy as np 
import matplotlib.pyplot as plt

import os

from epics import caput, caget, cainfo, PV
import epics

import argparse
from datetime import datetime
from tqdm import tqdm

aerotechPath = os.path.realpath(os.path.dirname(sys.argv[0]))
os.chdir(aerotechPath)
sys.path.append('../')

import aerotech

# import matplotlib.animation as animation
# from scipy.interpolate import interp1d

logger = logging.getLogger(__name__)

'''

------------------------------------------------------------------------------------------------------------------------------------
>> General Structure (Python-side)
    -> initiate connection with Aerotech Control Software through 'host', 'port', 'scanner' via specific class of EnsembleComm (refer to comm.py)
    -> Get data flag IGLOBAL(94), "Listening" flag, indicating where Aerotech data allocation is
        -> Once IGLOBAL(94) flags Aerotech request and data allocation as complete, Pyton function 'acquisition' will execute
    -> Get data parameters IGLOBAL(97), IGLOBAL(98), and IGLOBAL(99) for downsample factor, request input, and time duration of executed wire scan
    -> Get data flag IGLOBAL(98), "Request" flag, indicating data collection type (Position, Current... Feedback, Command...)
        -> Upon request flag received into Python-side, collect data via DGLOBALS based on downsampled data length
        -> Store .png and .csv files into a folder
    -> Reset data flags IGLOBAL(94) and IGLOBAL(98) upon completion of requested data retrieval
    -> End of Python-side retrieval execution

------------------------------------------------------------------------------------------------------------------------------------
>> for both Aerotech data allocation and Python data retrieval structures...
    -> Refer to 'request == 1  ... 1x1 Position Command' for general structure of single-plotting requests
    -> Refer to 'request == 30 ... 2x1 Feedbacks : Position, Current' for general structure of multi-plotting requests

------------------------------------------------------------------------------------------------------------------------------------
>> Future Implementations
    -> Implement asynchronous data-pulling (refer to comm.py EnsembleScopeWS class, 'dglobal_pull_new()' and 'epics_PV' async functions)
        -> Pull multiple data values at the same time and cut down clunkiness of code 
    -> Implement velocity feedback and command data values
        -> Direct implementation in Aerotech Control Software

'''

# >>> Define Scope Retrieval Function

async def acquisition(host, port, scanner, wsname, acquire = bool):         
    comm = aerotech.EnsembleScopeWS(host, port) # -> Initiate class functions and connections  
    
    iglobal_ls = comm.iglobal(94)
    listen = await iglobal_ls.get()             # -> Aerotech data retrieval flag, 'listen', where listen = 1 when Aerotech data allocation is complete for a wire scan request

    if listen == 1:                             # -> if listen == 1 , immediately execute Python-side retrieval
        acquire = True                                               
    else:                                       # -> if listen != 1 , hold Python-side retrieval until listen == 1
        while listen != 1:                      
            iglobal_ls = comm.iglobal(94)
            listen = await iglobal_ls.get()                                 
            await asyncio.sleep(1.0)                                       

            if listen == 1:                     # -> once listen == 1, retrieve request and execute Python-side retrieval for DGLOBALs
                acquire = True
                # logger.critical(f'Executing Python-side data retrieval...')
                await asyncio.sleep(0.2)
            elif listen == 2:
                # logger.critical(f'Executing Aerotech-side data allocation...')
                await asyncio.sleep(0.2)
            elif listen == -1:
                await asyncio.sleep(0.2)
                # logger.critical(f'Collecting Digital Scope data...')
            else:
                await asyncio.sleep(1.0)
            #     logger.critical(f'Listening for Request... IGLOBAL(ireg_listen) == {listen}')
                
    if acquire == True:                                             # >> START: start Python-side retrieval 
                                                                    # >> Get data parameter IGLOBAL variables from Aerotech-side before retrieving data
        iglobal_dp = comm.iglobal(99) 
        data_points = await iglobal_dp.get()    # -> retrieve Aerotech 'numpoints' via IGLOBAL(ireg_scope_data_length)
        
        iglobal_ds = comm.iglobal(97)
        downsample = await iglobal_ds.get()     # -> retrieve Aerotech 'downsample' via IGLOBAL(ireg_downsample)
        await asyncio.sleep(0.2)

        iglobal_rq = comm.iglobal(98)           # -> retrieve Aerotech 'request' fia IGLOBAL(ireg_scope_data_request)
        request = await iglobal_rq.get() 

        try:                                                        # >> EXECUTION: start Python-side scope data retrieval based on request IGLOBAL(ireg_scope_data_request)   
                                                                    # >> Define data retrieval file location paramters          
            now = datetime.now()
            timestring = now.strftime("%Y%m%d_%H%M%S")
            scope_data_dir = os.path.join(os.getenv("PHYSICS_DATA"),'genMotion/wirescanners/scope_data/', wsname)
            print(scope_data_dir)
            if not os.path.exists(scope_data_dir):
                os.makedirs(scope_data_dir)
                print("No Folder Existed! Created a new folder.")
                                                                    # >> Initialize time and downsampled data length based on IGLOBAL data parameters
            time = np.arange(0, data_points, downsample)       
            downlen = int(data_points/downsample) 
                                                                    # >> Initialize data value arrays and progress statuses
            data_values1 = np.zeros(downlen); data_values2 = np.zeros(downlen); data_values3 = np.zeros(downlen); data_values4 = np.zeros(downlen)
            progress1 = len(data_values1) ; progress2 = len(data_values2) ; progress3 = len(data_values3) ; progress4 = len(data_values4)  
                                                                    # >> Empty PyDM GUI display via EPICS PVs
            #epics_time = PV(f'{scanner}:SCOPEDATA:TIME') ; epics_time.put(time)
            #epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD') ; epics_poscmd.put(data_values1)
            #epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK') ; epics_posfbk.put(data_values2)
            #epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD') ; epics_curcmd.put(data_values3)
            #epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK') ; epics_curfbk.put(data_values4)  
            #progress_pydm = 0 ; epics_progress = PV(f'{scanner}:SCOPEPROGRESS') ; epics_progress.put(progress_pydm)
            
            epics_useXWire = PV(f'{scanner}:USEXWIRE'); useXWire = epics_useXWire.get()
            epics_useYWire = PV(f'{scanner}:USEYWIRE'); useYWire = epics_useYWire.get()
            epics_useUWire = PV(f'{scanner}:USEUWIRE'); useUWire = epics_useUWire.get()
            epics_ScopeRequest = PV(f'{scanner}:SCOPEREQUEST'); scopeRequest = epics_ScopeRequest.get()
            epics_scanStat = PV(f'{scanner}:SCANSTAT'); scanStat = epics_scanStat.get() 

            wire_selected = ""
            if useXWire: wire_selected += "x"
            if useYWire: wire_selected += "y"
            if useUWire: wire_selected += "z"
            
            # indicator for filename if scan was successful or not
            scanResult = "failure" if scanStat == -1 else "success"

            # empty array to reset old graph for every new scan            
            empty = np.zeros(downlen)

                                                # > Identify a directory path for data storage for .csv tables and .png plots

            png_filename = os.path.join(scope_data_dir, f"{wsname}_{wire_selected}_rq{scopeRequest}_{timestring}_{scanResult}.png")
            csv_filename = os.path.join(scope_data_dir, f"{wsname}_{wire_selected}_rq{scopeRequest}_{timestring}_{scanResult}.csv")

            if request == 1:                    # > 1x1 Position Command  

                pos_cmd = []                    # > Initialize data array by request 

                                                # > Data pulling for-loop up to downsampled length, stepping by 1
                for i in range(0, downlen, 1):
                                                # > Connect to a dglobal value as 'dglobal_val'
                    dglobal_val = comm.dglobal(100+i)
                                                # > Pull dglobal value as 'pos_cmd_val' , convert to [mm]
                    pos_cmd_val = await dglobal_val.get()
                    pos_cmd_val = pos_cmd_val/1000
                                                # > Append pos_cmd_val values into an array 'pos_cmd' for plotting
                    pos_cmd.append(pos_cmd_val)
                                                # > Continually update progress based on length of 'pos_cmd' array as an EPICS PV integer
                    progress = len(pos_cmd)
                    progress_pydm = ((progress*100) // (downlen))
                    epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                    epics_progress.put(progress_pydm+1)
                                                # > Push appended data lists into EPICS PV -based plots in PyDM GUI
                epics_time = PV(f'{scanner}:SCOPEDATA:TIME')
                epics_time.put(time)
                epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD')
                epics_poscmd.put(pos_cmd)
                #epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD')
                epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK') ; epics_posfbk.put(empty)
                epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD') ; epics_curcmd.put(empty)
                epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK') ; epics_curfbk.put(empty)
                print("test - trying to reset other graphs")
                                                # > Identify a directory path for data storage for .csv tables and .png plots
                #png_filename = os.path.join(scope_data_dir, f"{timestring}_scope_retrieval_rq01_PosCmd.png")
                # ${WIRENAME}_${wire seleted}_${scoperequest}_${Year/Month/Day}_{Hour/Minutes/Seconds}.cvs 
   
                # save .png of pos_cmd plot
                plt.figure()
                plt.plot(time , pos_cmd, color = 'cyan', linewidth = 1.25)
                plt.title('Position Command over Time')
                plt.ylabel('Position Command [um]')
                plt.xlabel('Time [ms]')
                plt.grid(True)
                plt.tight_layout(pad=2.5)
                plt.savefig(png_filename)
                                                # > Ensure data arrays are lists to be tabulated
                time_list = list(time) ; pos_cmd_list = list(pos_cmd)
                time_list.insert(0, 'Time [ms]') ; pos_cmd_list.insert(0, 'Position Command [um]')
                titled_data = [time_list, pos_cmd_list]
                transposed = np.array(titled_data).T.tolist()

                                                # > Create a .csv table in directory path
                with open(csv_filename, 'w', encoding='UTF8', newline='') as file:
                    writer = csv.writer(file)
                    writer.writerows(transposed)
                
            elif request == 2:                  # > 1x1 Position Feedback
                pos_fbk = []
                
                for i in range(0, downlen, 1):
                    dglobal_val = comm.dglobal(100+i)
                    pos_fbk_val = await dglobal_val.get()
                    pos_fbk_val = pos_fbk_val/1000
                    pos_fbk.append(pos_fbk_val)
                    progress = len(pos_fbk)
                    progress_pydm = ((progress*100) // (downlen))
                    epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                    epics_progress.put(progress_pydm+1)

                epics_time = PV(f'{scanner}:SCOPEDATA:TIME')
                epics_time.put(time)
                epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK')
                epics_posfbk.put(pos_fbk)
                epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD') ; epics_poscmd.put(empty)
                #epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK') ; epics_posfbk.put(empty)
                epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD') ; epics_curcmd.put(empty)
                epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK') ; epics_curfbk.put(empty)
                
                # save .png of pos_fbk plot
                plt.figure()
                plt.plot(time , pos_fbk, color = 'black', linewidth = 1.25)
                plt.title('Position Feedback over Time')
                plt.ylabel('Position Feedback [um]')
                plt.xlabel('Time [ms]')
                plt.grid(True)
                plt.tight_layout(pad=2.5)
                plt.savefig(png_filename)
  
                time_list = list(time) ; pos_fbk_list = list(pos_fbk)
                time_list.insert(0, 'Time [ms]') ; pos_fbk_list.insert(0, 'Position Feedback [um]')

                titled_data = [time_list, pos_fbk_list]
                transposed = np.array(titled_data).T.tolist()

                with open(csv_filename, 'w', encoding='UTF8', newline='') as file:
                    writer = csv.writer(file)
                    writer.writerows(transposed)

            elif request == 14:                 # > 1x1 Current Command
                cur_cmd = []
                
                for i in range(0, downlen, 1):
                    dglobal_val = comm.dglobal(100+i)
                    cur_cmd_val = await dglobal_val.get()
                    cur_cmd.append(cur_cmd_val)
                    progress = len(cur_cmd)
                    progress_pydm = ((progress*100) // (downlen))
                    epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                    epics_progress.put(progress_pydm+1)

                epics_time = PV(f'{scanner}:SCOPEDATA:TIME')
                epics_time.put(time)
                epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD')
                epics_curcmd.put(cur_cmd)
                epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD') ; epics_poscmd.put(empty)
                epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK') ; epics_posfbk.put(empty)
                #epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD') ; epics_curcmd.put(empty)
                epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK') ; epics_curfbk.put(empty)

                # save .png of cur_cmd plot
                plt.figure()
                plt.plot(time , cur_cmd, color = 'magenta', linewidth = 1.25)
                plt.title('Current Command over Time')
                plt.ylabel('Current Command [mA]')
                plt.xlabel('Time [ms]')
                plt.grid(True)
                plt.tight_layout(pad=2.5)
                plt.savefig(png_filename)
      
                time_list = list(time) ; cur_cmd_list = list(cur_cmd)
                time_list.insert(0, 'Time [ms]') ; cur_cmd_list.insert(0, 'Current Command [mA]')

                titled_data = [time_list, cur_cmd_list]
                transposed = np.array(titled_data).T.tolist()

                with open(csv_filename, 'w', encoding='UTF8', newline='') as file:
                    writer = csv.writer(file)
                    writer.writerows(transposed)

            elif request == 15:                 # > 1x1 Current Feedback
                cur_fbk = []
                
                for i in range(0, downlen, 1):
                    dglobal_val = comm.dglobal(100+i)
                    cur_fbk_val = await dglobal_val.get()
                    cur_fbk.append(cur_fbk_val)
                    progress = len(cur_fbk)
                    progress_pydm = ((progress*100) // (downlen))
                    epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                    epics_progress.put(progress_pydm+1)

                epics_time = PV(f'{scanner}:SCOPEDATA:TIME')
                epics_time.put(time)
                epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK')
                epics_curfbk.put(cur_fbk)
                epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD') ; epics_poscmd.put(empty)
                epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK') ; epics_posfbk.put(empty)
                epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD') ; epics_curcmd.put(empty)
                #epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK') ; epics_curfbk.put(empty)

                time_list = list(time) ; cur_fbk_list = list(cur_fbk)
                time_list.insert(0, 'Time [ms]') ; cur_fbk_list.insert(0, 'Current Feedback [mA]')
                titled_data = [time_list, cur_fbk_list]
                transposed = np.array(titled_data).T.tolist()

                #png_filename = os.path.join(scope_data_dir, f"{timestring}_scope_retrieval_rq15_CurFbk.png")
                #csv_filename = os.path.join(scope_data_dir, f"{wsname}_{wire_selected}_rq{scopeRequest}_{timestring}.csv")

                # save .png of cur_fbk plot
                plt.figure()
                plt.plot(time , cur_fbk, color = 'red', linewidth = 1.25)
                plt.title('Current Feedback over Time')
                plt.ylabel('Current Feedback [mA]')
                plt.xlabel('Time [ms]')
                plt.grid(True)
                plt.tight_layout(pad=2.5)
                plt.savefig(png_filename)

                with open(csv_filename, 'w', encoding = 'UTF8', newline = '') as file:
                    writer = csv.writer(file)
                    writer.writerows(transposed)

            elif request == 30:                 # > 2x1 Feedbacks : Position, Current
                                                # > Initialize data arrays for respective data-pulling values with progress 
                
                # -> Position Feedback, Current Feedback
                data_values1 = []; data_values2 = []
                progress1 = len(data_values1) ; progress2 = len(data_values2) 

                                                # > Data pulling for-loop up to downsampled length, stepping by 1
                for i in range(0, downlen, 1):  # > Position Feedback
                                                # > Pull and progress dglobal values up to a downsampled length, initially from 100
                    dglobal_val1 = comm.dglobal(100+i)
                    data1 = await dglobal_val1.get()
                    data1 = data1/1000
                    data_values1.append(data1)
                    progress1 = len(data_values1)                    
                    if len(data_values1) != len(time):
                        progress_total = progress1 + progress2
                        progress_pydm = ((progress_total*100) // (2*downlen))
                        epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        epics_progress.put(progress_pydm+1)
          
                for i in range(0, downlen, 1):  # > Current Feedback
                                                # > Pull and progress dglobal values up to a downsampled length, initially from 100 + downsampled length
                    dglobal_val2 = comm.dglobal(100+downlen+i)
                    data2 = await dglobal_val2.get()
                    data_values2.append(data2)
                    progress2 = len(data_values2)
                    if len(data_values2) != len(time):
                        progress_total = progress1 + progress2 
                        progress_pydm = ((progress_total*100) // (2*downlen))
                        epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        epics_progress.put(progress_pydm+1)

                                                # > Push appended data lists into EPICS PV -based plots in PyDM GUI
                epics_time = PV(f'{scanner}:SCOPEDATA:TIME')
                epics_time.put(time)
                epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK')
                epics_posfbk.put(data_values1)    
                epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK')
                epics_curfbk.put(data_values2)
                epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD') ; epics_poscmd.put(empty)
                #epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK') ; epics_posfbk.put(empty)
                epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD') ; epics_curcmd.put(empty)
                #epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK') ; epics_curfbk.put(empty)

                # save .png of Position & Current feedback plots
                #data_graphs = {'Position Command over Time': (time, pos_cmd), 'Position Feedback over Time': (time, pos_fbk), 'Current Command over Time': (time, cur_cmd), 'Current Feedback over Time': (time, cur_fbk)}
                fig, axs = plt.subplots(1, 2)
                fig.suptitle('Scope retrieval data - Position & Current feedback')

                axs[0].plot(time , data_values1, color = 'black', linewidth = 1.25)
                axs[0].set_title('Position Feedback over Time')
                axs[0].set_ylabel('Position Feedback [mm]')
                axs[0].set_xlabel('Time [ms]')
                axs[1].plot(time , data_values2, color = 'red', linewidth = 1.25)
                axs[1].set_title('Current Feedback over Time')
                axs[1].set_ylabel('Current Feedback [mA]')
                axs[1].set_xlabel('Time [ms]')

                plt.grid(True)
                #plt.tight_layout(pad=2.5)
                plt.tight_layout()
                plt.savefig(png_filename)

                                                # > Ensure data arrays are lists to be tabulated
                time_list = list(time) ; pos_fbk = list(data_values1) ; cur_fbk = list(data_values2)
                time_list.insert(0, 'Time [ms]') ;  pos_fbk.insert(0, 'Position Feedback [mm]'); cur_fbk.insert(0, 'Current Feedback [mA]')
                titled_data = [time_list, pos_fbk, cur_fbk]
                transposed = np.array(titled_data).T.tolist()

                                                # > Create a .csv table in directory path
                with open(csv_filename, 'w', encoding='UTF8', newline='') as file:
                    writer = csv.writer(file)
                    writer.writerows(transposed)

            elif request == 31:                 # > 2x1 Commands: Position, Current
                # -> Position Command, Current Command
                data_values1 = [] ;  data_values2 = []
                progress1 = len(data_values1) ; progress2 = len(data_values2) 

                for i in range(0, downlen, 1): # > Position Command
                    dglobal_val1 = comm.dglobal(100+i)
                    data1 = await dglobal_val1.get()
                    data1 = data1/1000
                    data_values1.append(data1)
                    progress1 = len(data_values1)
                    if len(data_values1) != len(time):
                        progress_total = progress1 + progress2
                        progress_pydm = ((progress_total*100) // (2*downlen))
                        epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        epics_progress.put(progress_pydm+1)

                for i in range(0, downlen, 1): # > Current Command
                    dglobal_val2 = comm.dglobal(100+downlen+i)
                    data2 = await dglobal_val2.get()
                    data_values2.append(data2)
                    progress2 = len(data_values2)
                    if len(data_values2) != len(time):
                        progress_total = progress1 + progress2 
                        progress_pydm = ((progress_total*100) // (2*downlen))
                        epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        epics_progress.put(progress_pydm+1)
                        
                epics_time = PV(f'{scanner}:SCOPEDATA:TIME')
                epics_time.put(time)
                epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD')
                epics_poscmd.put(data_values1)
                epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD')
                epics_curcmd.put(data_values2)
                #epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD') ; epics_poscmd.put(empty)
                epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK') ; epics_posfbk.put(empty)
                #epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD') ; epics_curcmd.put(empty)
                epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK') ; epics_curfbk.put(empty)

                # save .png of pos_cmd & cur_cmd plots
                #data_graphs = {'Position Command over Time': (time, pos_cmd), 'Position Feedback over Time': (time, pos_fbk), 'Current Command over Time': (time, cur_cmd), 'Current Feedback over Time': (time, cur_fbk)}
                fig, axs = plt.subplots(1, 2)
                fig.suptitle('Scope retrieval data - Position & Current Command')

                axs[0].plot(time , data_values1, color = 'cyan', linewidth = 1.25)
                axs[0].set_title('Position Command over Time')
                axs[0].set_ylabel('Position Command [mm]')
                axs[0].set_xlabel('Time [ms]')
                axs[1].plot(time , data_values2, color = 'magenta', linewidth = 1.25)
                axs[1].set_title('Current Command over Time')
                axs[1].set_ylabel('Current Command [mA]')
                axs[1].set_xlabel('Time [ms]')

                plt.grid(True)
                #plt.tight_layout(pad=2.5)
                plt.tight_layout()
                plt.savefig(png_filename)

                time_list = list(time) ; pos_cmd = list(data_values1) ; cur_cmd = list(data_values2)
                time_list.insert(0, 'Time [ms]') ;  pos_cmd.insert(0, 'Position Command [mm]'); cur_cmd.insert(0, 'Current Command [mA]')

                # titled_data = [time, pos_cmd, cur_cmd] # should use time_list instead
                titled_data = [time_list, pos_cmd, cur_cmd] 
                transposed = np.array(titled_data).T.tolist()

                with open(csv_filename, 'w', encoding='UTF8', newline='') as file:
                    writer = csv.writer(file)
                    writer.writerows(transposed)
                    
            elif request == 32:                 # > 2x1 Feedback-Command: Position
                # -> Position Feedback, Position Command
                data_values1 = []; data_values2 = []
                progress1 = len(data_values1) ; progress2 = len(data_values2) 

                for i in range(0, downlen, 1): # Position Feedback
                    dglobal_val1 = comm.dglobal(100+i)
                    data1 = await dglobal_val1.get()
                    data1 = data1/1000
                    data_values1.append(data1)
                    progress1 = len(data_values1)
                    if len(data_values1) != len(time):
                        progress_total = progress1 + progress2
                        progress_pydm = ((progress_total*100) // (2*downlen))
                        epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        epics_progress.put(progress_pydm+1)

                for i in range(0, downlen, 1): # > Position Command
                    dglobal_val2 = comm.dglobal(100+downlen+i)
                    data2 = await dglobal_val2.get()
                    data2 = data2/1000
                    data_values2.append(data2)
                    progress2 = len(data_values2)
                    if len(data_values2) != len(time):
                        progress_total = progress1 + progress2 
                        progress_pydm = ((progress_total*100) // (2*downlen))
                        epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        epics_progress.put(progress_pydm+1)

                #png_filename = os.path.join(scope_data_dir, f"{timestring}_scope_retrieval_rq32_PosFbkCmd.png")
                #csv_filename = os.path.join(scope_data_dir, f"{wsname}_{wire_selected}_rq{scopeRequest}_{timestring}.csv")

                # save .png of Position Feedback & Position Command plots
                #data_graphs = {'Position Command over Time': (time, pos_cmd), 'Position Feedback over Time': (time, pos_fbk), 'Current Command over Time': (time, cur_cmd), 'Current Feedback over Time': (time, cur_fbk)}
                fig, axs = plt.subplots(1, 2)
                fig.suptitle('Scope retrieval data - Position Feedback & Position Command')

                axs[0].plot(time , data_values1, color = 'black', linewidth = 1.25)
                axs[0].set_title('Position Feedback over Time')
                axs[0].set_ylabel('Position Feedback [mm]')
                axs[0].set_xlabel('Time [ms]')
                axs[1].plot(time , data_values2, color = 'cyan', linewidth = 1.25)
                axs[1].set_title('Position Command over Time')
                axs[1].set_ylabel('Position Command [mm]')
                axs[1].set_xlabel('Time [ms]')

                plt.grid(True)
                #plt.tight_layout(pad=2.5)
                plt.tight_layout()
                plt.savefig(png_filename)

                time_list = list(time) ; pos_fbk = list(data_values1) ; pos_cmd = list(data_values2)
                time_list.insert(0, 'Time [ms]') ;  pos_fbk.insert(0, 'Position Feedback [mm]'); pos_cmd.insert(0, 'Position Command [mm]')

                titled_data = [time_list, pos_cmd, pos_fbk]
                transposed = np.array(titled_data).T.tolist()

                with open(csv_filename, 'w', encoding='UTF8', newline='') as file:
                    writer = csv.writer(file)
                    writer.writerows(transposed)
                
                epics_time = PV(f'{scanner}:SCOPEDATA:TIME')
                epics_time.put(time)
                epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD')
                epics_poscmd.put(data_values2)
                epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK')
                epics_posfbk.put(data_values1)
                #epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD') ; epics_poscmd.put(empty)
                #epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK') ; epics_posfbk.put(empty)
                epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD') ; epics_curcmd.put(empty)
                epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK') ; epics_curfbk.put(empty)
                
            elif request == 33:                 # > 2x1 Feedback-Command: Current
                # -> Current Feedback, Current Command
                data_values1 = []; data_values2 = []
                progress1 = len(data_values1) ; progress2 = len(data_values2) 

                for i in range(0, downlen, 1): # > Current Feedback
                    dglobal_val1 = comm.dglobal(100+i)
                    data1 = await dglobal_val1.get()
                    data_values1.append(data1)
                    progress1 = len(data_values1) 
                    if len(data_values1) != len(time):
                        progress_total = progress1 + progress2 
                        progress_pydm = ((progress_total*100) // (2*downlen))
                        epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        epics_progress.put(progress_pydm+1)
                    
                for i in range(0, downlen, 1): # > Current Command
                    dglobal_val2 = comm.dglobal(100+downlen+i)
                    data2 = await dglobal_val2.get()
                    data_values2.append(data2)
                    progress2 = len(data_values2) 
                    if len(data_values2) != len(time):
                        progress_total = progress1 + progress2 
                        progress_pydm = ((progress_total*100) // (2*downlen))
                        epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        epics_progress.put(progress_pydm+1)
                
                #png_filename = os.path.join(scope_data_dir, f"{timestring}_scope_retrieval_rq33_CurFbkCmd.png")
                #csv_filename = os.path.join(scope_data_dir, f"{wsname}_{wire_selected}_rq{scopeRequest}_{timestring}.csv")

                # save .png of Current Feedback & Current Command plots
                #data_graphs = {'Position Command over Time': (time, pos_cmd), 'Position Feedback over Time': (time, pos_fbk), 'Current Command over Time': (time, cur_cmd), 'Current Feedback over Time': (time, cur_fbk)}
                fig, axs = plt.subplots(1, 2)
                fig.suptitle('Scope retrieval data - Current Feedback & Current Command')

                axs[0].plot(time , data_values1, color = 'red', linewidth = 1.25)
                axs[0].set_title('Current Feedback over Time')
                axs[0].set_ylabel('Current Feedback [mA]')
                axs[0].set_xlabel('Time [ms]')
                axs[1].plot(time , data_values2, color = 'magenta', linewidth = 1.25)
                axs[1].set_title('Current Command over Time')
                axs[1].set_ylabel('Current Command [mA]')
                axs[1].set_xlabel('Time [ms]')

                plt.grid(True)
                #plt.tight_layout(pad=2.5)
                plt.tight_layout()
                plt.savefig(png_filename)

                time_list = list(time) ; cur_fbk = list(data_values1) ; cur_cmd = list(data_values2)
                time_list.insert(0, 'Time [ms]') ;  cur_fbk.insert(0, 'Current Feedback [mA]'); cur_cmd.insert(0, 'Current Command [mA]')

                titled_data = [time_list, cur_cmd, cur_fbk]
                transposed = np.array(titled_data).T.tolist()

                with open(csv_filename, 'w', encoding='UTF8', newline='') as file:
                    writer = csv.writer(file)
                    writer.writerows(transposed)

                epics_time = PV(f'{scanner}:SCOPEDATA:TIME')
                epics_time.put(time)
                epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK')
                epics_curfbk.put(data_values1)
                epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD')
                epics_curcmd.put(data_values2)
                epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSCMD') ; epics_poscmd.put(empty)
                epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSFDBK') ; epics_posfbk.put(empty)
                #epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD') ; epics_curcmd.put(empty)
                #epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK') ; epics_curfbk.put(empty)
               
            elif request == 34:                 # > 4x1 All Direct Wire Scan Data Values
                # -> Position Command, Position Feedback, Current Command, Current Feedback
                data_values1 = []; data_values2 = [] ; data_values3 = []; data_values4 = []
                progress1 = len(data_values1) ; progress2 = len(data_values2) ; progress3 = len(data_values3) ; progress4 = len(data_values4) 
                epics_time = PV(f'{scanner}:SCOPEDATA:TIME')
                epics_time.put(time)

                # pos_cmd, progress_poscmd, time = await comm.dglobal_pull_new(ds_num = 0)
                # pos_fbk, progress_posfbk, time = await comm.dglobal_pull_new(ds_num = 1)
                # cur_cmd, progress_curcmd, time = await comm.dglobal_pull_new(ds_num = 2)
                # cur_fbk, progress_curfbk, time = await comm.dglobal_pull_new(ds_num = 3)

                for i in range(0, downlen, 1): # > Position Command
                    dglobal_val1 = comm.dglobal(100 + i)
                    data1 = await dglobal_val1.get()
                    data1 = data1/1000
                    data_values1.append(data1)
                    progress1 = len(data_values1)                    
                    #if len(data_values1) != len(time):
                        #progress_total = progress1 + progress2 + progress3 + progress4
                        #progress_pydm = ((progress_total*100) // (4*downlen))
                        #epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        #epics_progress.put(progress_pydm+1)
          
                #for i in range(0, downlen, 1): # > Position Feedback
                    dglobal_val2 = comm.dglobal(100 + downlen + i)
                    data2 = await dglobal_val2.get()
                    data2 = data2/1000
                    data_values2.append(data2)
                    progress2 = len(data_values2)
                    #if len(data_values2) != len(time):
                        #progress_total = progress1 + progress2 + progress3 + progress4
                        #progress_pydm = ((progress_total*100) // (4*downlen))
                        #epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        #epics_progress.put(progress_pydm+1)

                #for i in range(0, downlen, 1): # > Current Command
                    dglobal_val3 = comm.dglobal(100+ + downlen + downlen + i)
                    data3 = await dglobal_val3.get()
                    data_values3.append(data3)
                    progress3 = len(data_values3)
                    #if len(data_values3) != len(time):
                        #progress_total = progress1 + progress2 + progress3 + progress4
                        #progress_pydm = ((progress_total*100) // (4*downlen))
                        #epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        #epics_progress.put(progress_pydm+1)

                #for i in range(0, downlen, 1): # > Current Feedback
                    dglobal_val4 = comm.dglobal(100 + downlen + downlen + downlen + i)
                    data4 = await dglobal_val4.get()
                    data_values4.append(data4)
                    progress4 = len(data_values4)
                    if len(data_values4) != len(time) or len(data_values3) != len(time) or len(data_values2) != len(time) or len(data_values1) != len(time):
                        progress_total = progress1 + progress2 + progress3 + progress4
                        progress_pydm = ((progress_total*100) // (4*downlen))
                        epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                        epics_progress.put(progress_pydm+1)

                pos_cmd = data_values1 ; pos_fbk = data_values2 ; cur_cmd = data_values3 ; cur_fbk = data_values4

                #uncommented this section to allow sending graph data for 4-scope-mode
                epics_posfbk = PV(f'{scanner}:SCOPEDATA:POSCMD')
                epics_posfbk.put(data_values1)    
                epics_poscmd = PV(f'{scanner}:SCOPEDATA:POSFDBK')
                epics_poscmd.put(data_values2)
                epics_curcmd = PV(f'{scanner}:SCOPEDATA:ICMD')
                epics_curcmd.put(data_values3)
                epics_curfbk = PV(f'{scanner}:SCOPEDATA:IFDBK')
                epics_curfbk.put(data_values4)

                epics_posfdbk_max = PV(f'{scanner}:SCOPEDATA:MAXPOSFDBK')
                posfdbk_max = epics.caget(f'{scanner}:SCOPEDATA:POSFDBK').max()
                posfdbk_max = np.max(data_values2)
                epics_posfdbk_max.put(posfdbk_max)

                epics_curfdbk_max = PV(f'{scanner}:SCOPEDATA:MAXIFDBK')
                curfdbk_max = epics.caget(f'{scanner}:SCOPEDATA:IFDBK').max()
                curfdbk_max = np.max(data_values4)
                epics_curfdbk_max.put(curfdbk_max)

                epics_progress = PV(f'{scanner}:SCOPEPROGRESS')
                epics_progress.put(progress_pydm+1)
                
                #png_filename = os.path.join(scope_data_dir, f"{timestring}_scope_retrieval_rq34__ALL.png")
                #csv_filename = os.path.join(scope_data_dir, f"{wsname}_{wire_selected}_rq{scopeRequest}_{timestring}.csv")
                
                # save .png of pos_cmd plot
                #data_graphs = {'Position Command over Time': (time, pos_cmd), 'Position Feedback over Time': (time, pos_fbk), 'Current Command over Time': (time, cur_cmd), 'Current Feedback over Time': (time, cur_fbk)}
                fig, axs = plt.subplots(2, 2)
                fig.suptitle('Scope retrieval data - 4 scope mode')

                axs[0, 0].plot(time , pos_cmd, color = 'cyan', linewidth = 1.25)
                axs[0, 0].set_title('Position Command over Time', fontsize=10)
                axs[0, 0].set_ylabel('Position Command [mm]')
                axs[0, 0].set_xlabel('Time [ms]')
                axs[0, 1].plot(time , pos_fbk, color = 'black', linewidth = 1.25)
                axs[0, 1].set_title('Position Feedback over Time', fontsize=10)
                axs[0, 1].set_ylabel('Position Feedback [mm]')
                axs[0, 1].set_xlabel('Time [ms]')
                axs[1, 0].plot(time , cur_cmd, color = 'magenta', linewidth = 1.25)
                axs[1, 0].set_title('Current Command over Time', fontsize=10)
                axs[1, 0].set_ylabel('Current Command [mA]')
                axs[1, 0].set_xlabel('Time [ms]')
                axs[1, 1].plot(time , cur_fbk, color = 'red', linewidth = 1.25)
                axs[1, 1].set_title('Current Feedback over Time', fontsize=10)
                axs[1, 1].set_ylabel('Current Feedback [um]')
                axs[1, 1].set_xlabel('Time [mA]')
                #axs[0, 0].text(0, time[-1], f"max position feedback: {posfdbk_max}", fontsize=10)
                
                plt.grid(True)
                #plt.tight_layout(pad=2.5)
                plt.tight_layout()

                # add max position and current text in plot .png file & format them relative to the png's coordinates 
                plt.text(0.01, 0.92, f"max_posfbk: {posfdbk_max:.3f}", fontsize=9, transform=fig.transFigure)
                plt.text(0.01, 0.96, f"max_curfbk: {curfdbk_max:.3f}", fontsize=9, transform=fig.transFigure)
                plt.savefig(png_filename)

                time_list = list(time) ; pos_cmd = list(pos_cmd) ; pos_fbk = list(pos_fbk); cur_cmd = list(cur_cmd); cur_fbk = list(cur_fbk)
                time_list.insert(0, 'Time [ms]') 
                pos_cmd.insert(0, 'Position Command [mm]'); pos_fbk.insert(0, 'Position Feedback [mm]')
                cur_cmd.insert(0, 'Current Command [mA]'); cur_fbk.insert(0, 'Current Feedback [mA]')

                titled_data = [time_list, pos_cmd, pos_fbk, cur_cmd, cur_fbk]
                transposed = np.array(titled_data).T.tolist()

                with open(csv_filename, 'w', encoding='UTF8', newline='') as file:
                    writer = csv.writer(file)
                    writer.writerows(transposed)
                    
            else:                               # > No request command found, resetting listening flag
                # logger.critical('Request not recognized')
                iglobal_ls = comm.iglobal(94)
                listen = await iglobal_ls.set(value = 0)
                await comm.close()

            try:                                                # >> RESET: Get and set IGLOBALs(94, 98) to allow new request allocation and data retrieval on Aerotech
                                                                # >> Must be done after a request is executed and Python-side retrieval is complete
                iglobal_ls = comm.iglobal(94)
                listen = await iglobal_ls.get()
                iglobal_rq = comm.iglobal(98)
                request = await iglobal_rq.get()

                # logger.critical(f'Data Retrieval completed, resetting Listening and Request flags...')
                await asyncio.sleep(0.5)

                if listen != 1 or request != 0 : 
                    listen = await iglobal_ls.set(value = 0)
                    # logger.critical('listening set to 0')
                    request = await iglobal_rq.set(value = 0)
                    # logger.critical('request set to 0')
                    # await asyncio.sleep(0.5)

# >>> allow Keyboard Interruption, recognize Connection Error instances
            except KeyboardInterrupt or ConnectionError or ConnectionRefusedError :
                listen = await iglobal_ls.set(value = 0)
                await comm.close()

        except KeyboardInterrupt or ConnectionError or ConnectionRefusedError :            
            listen = await iglobal_ls.set(value = 0)
            await comm.close()
            
        try:                                                    # >> END OF DATA ACQUISITION EXECUTION
                                                                # >> Colse connection and reset listening flag to zero no matter of interruption or error
            # logger.critical('Data Acquisition Complete')      # -> Data acquisition finished cleanly
            await comm.close()
           
        except KeyboardInterrupt or ConnectionError or ConnectionRefusedError :       
            # logger.critical('Data Acquisition Incomplete')    # -> Data acquisition did not finish cleanly  
            listen = await iglobal_ls.set(value = 0)
            await comm.close()
    
# >>> Execute scope_retrieval.py

if __name__ == '__main__':                                          

    try:
        comm_port = 8000
        scope_port = 8001
        port = scope_port

        parser = argparse.ArgumentParser(description= "Define host argument")
        parser.add_argument('host', metavar='host', type = str, help = 'enter host')
        parser.add_argument('scanner', metavar='scanner', type = str, help = 'enter scanner')
        parser.add_argument('wsname', metavar='wsname', type = str, help = 'enter wsname') #didn't add this line before - caused the "unrecognized argument: WSDEV1" error
        args = parser.parse_args()
        host = args.host
        scanner = args.scanner
        wsname = args.wsname
        # host = 'moc-b34-mc03.slac.stanford.edu'
        # scanner = 'WIRE:B34:100'

    except IndexError or ConnectionRefusedError:
        host = 'moc-b34-mc03.slac.stanford.edu'
        # logger.critical('scope_retrieval.py: except IndexError/ConnectionError condition met')

    aerotech.logger.setLevel(logging.DEBUG)

    logging.basicConfig(format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    
    # -> Directly implement this into LinuxOS command window
    '''
    python scope_run_retrieval.py 'moc-b34-mc03.slac.stanford.edu' 'WIRE:B34:100'
    '''
    asyncio.run(acquisition(host = host, port = port, scanner = scanner , wsname = wsname,  acquire = True))





