########Zpracování dat 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import glob
import os


#Choose output 1 = ON, 0 = OFF
CustomLabel=''              # Custom label in the Title
OutputFormat='pdf'          # Output format, only extension 'pdf' (correct) '.pdf' (wrong). 
                            # Possible options are pdf,png,svg,jpg
UseRelativeValues=1         # Measured values will be relative to 1st measured value
ItoR=0                      # If measured data are Amps convert them to Resistivity using electrodes voltage and measured Amps

R1_Gate=1                   #Plot Readout(1) on Y axis and Gate voltage on X axis
R1_Gate_OnlyLastValue=1     # use only last value on given Gate voltage

R1_Time=1                  #Plot  Readout(1) on Y axis and Time on X axis
RH_Time=0                   #Plot  Humidity(1) on Y axis and Time on X axis
HumidityChamber_RH=0        #Plot Humidity Temperature and Flow on Y axis, Time on X axis
R1_RH=0
R1_RH_Time= 0               #Plot  Readout(1) and Humidity on Y axis and Time on X axis
R1_Gate_Time=1              #Plot Readout(1) and Gate on Y axis and Time on X axis
R1_RH_Gate_Time= 0          #Plot Readout(1), RH, Gate on Y axis and Time on X axis


##################################################################################


# Custom colors similar with LabView colors
myRed='#ff7170ff'
myBlue='#42a5ffff'
myOrange='#fba342ff'
myYellow='#ffe357ff'
myGreen='#6bd559ff'

#Plot properties
plt.style.use('seaborn-deep')

Linewidth=3

for filename in glob.glob('*.txt'): # Load all *.txt file from the same folder this script is saved
    
    Data=pd.read_csv(filename,delimiter='\t',decimal=',') # Read data from file
    Labels=Data.columns.values.tolist() # Read labels
    filename=os.path.splitext(filename)[0]
    # Load data from columns
    Time=Data[Labels[0]]
    Time=Time-min(Time)
    Gate=Data[Labels[1]]
    Electrode=Data[Labels[2]]
    Readout1=Data[Labels[3]]
    Readout2=Data[Labels[4]]
    Humidity=Data[Labels[5]]
    Temperature=Data[Labels[6]]
    Flow=Data[Labels[7]]
    
    if UseRelativeValues is 0:
        Tlabel='Time (s)'
        Rlabel='R ($\Omega)$'
        RHlabel='Relative humidity (%)'
        Glabel='Gate voltage (V)'
        Elabel='Electrode voltage (V)'
        Templabel='Temperature ($^\circ$C)'
    else:
        Tlabel='Time (s)'
        Rlabel='$\Delta$ R (%)'
        RHlabel='Relative humidity (%)'
        Glabel='Gate voltage (V)'
        Elabel='Electrode voltage (V)'
        Templabel='Temperature ($^\circ$C)'
        Readout1=Readout1/Readout1[0]*100-100
        Readout2=Readout2/Readout2[0]*100-100

    
    # Convert current to resistivity
    if ItoR is 1:
        Readout1=Electrode/Readout1


    if R1_Gate is 1:               
        GateReduced=[]
        Readout1Reduced=[]
        for valueG,valueR in zip(Gate,Readout1):
            if valueG !=0:
                Readout1Reduced.append(valueR)
                GateReduced.append(valueG)

        label='Resistivity on Gate (All values)'+CustomLabel
        f=plt.figure(label)
        ax=f.add_subplot(111)
        ax.plot(GateReduced,Readout1Reduced,label='Resistivity ($\Omega$)',color=myGreen,linestyle='',marker='o',markersize=4,)
        plt.title(label,fontsize=14, fontweight=0,loc='center')
        plt.xlabel(Glabel,fontsize=12)
        plt.ylabel(Rlabel,fontsize=12)
        ax.legend(loc='best',fontsize=12,markerscale=3)
        plt.grid()
        plt.show()
         
        f.savefig(filename +'_R1onGate(All).'+OutputFormat, bbox_inches='tight')
         
    if R1_Gate is 1 and R1_Gate_OnlyLastValue is 1:
        Readout1Reduced=[]
        GateReduced=[]
        LastValue=0

        for index,value in enumerate(Gate):
            if value != LastValue:
                Readout1Reduced.append(Readout1[index-1])
                GateReduced.append(Gate[index-1])
            LastValue=value
        
#        for each in GateReduced:

        label='Resistivity on Gate (Last value)'+CustomLabel
        f=plt.figure(label)
        ax=f.add_subplot(111)
        ax.plot(GateReduced,Readout1Reduced,label='Resistivity ($\Omega$)',color=myGreen,linestyle='',marker='o',markersize=4,)
        plt.title(label,fontsize=14, fontweight=0,loc='center')
        plt.xlabel(Glabel,fontsize=12)
        plt.ylabel(Rlabel,fontsize=12)
        ax.legend(loc='best',fontsize=12,markerscale=3)
        plt.grid()
        plt.show()
        f.savefig(filename +'_R1onGate(LastValue).'+OutputFormat, bbox_inches='tight')
    
    if R1_Time is 1:
         label='Resistivity on Time'+CustomLabel
         f=plt.figure(label)
         ax=f.add_subplot(111)
         ax.plot(Time,Readout1,label='Resistivity ($\Omega$)',color=myGreen)
         plt.title(label,fontsize=14, fontweight=0,loc='center')
         plt.xlabel(Tlabel,fontsize=12)
         plt.ylabel(Rlabel,fontsize=12)
         ax.legend(bbox_to_anchor=(1.4, 1),fontsize=12)
         plt.grid()
         plt.show()
         f.savefig(filename +'_R1onTime.'+OutputFormat, bbox_inches='tight')
    if RH_Time is 1:
        label= ('Humidity on Time')+CustomLabel
        f=plt.figure(label)
        ax=f.add_subplot(111)
        ax.plot(Time,Humidity,label='Humidity (%)',color=myGreen)
        plt.title(label,fontsize=14, fontweight=0,loc='center')
        plt.xlabel(Tlabel,fontsize=12)
        plt.ylabel(RHlabel,fontsize=12)
        ax.legend(bbox_to_anchor=(1.4, 1),fontsize=12)
        plt.grid()
        plt.show()
        f.savefig(filename +'_RHonTime.'+OutputFormat, bbox_inches='tight')     
        
    if HumidityChamber_RH is 1:
        label='Humidity chamber'+CustomLabel
        fig = plt.figure()
        host = fig.add_subplot(111)        
        par1 = host.twinx()
        par2 = host.twinx()
        
        host.set_xlabel(Tlabel, fontsize=12)
        host.set_ylabel(RHlabel, fontsize=12)
        par1.set_ylabel(Templabel, fontsize=12)
        par2.set_ylabel('Flow', fontsize=12)
                
        p1, = host.plot(Time, Humidity, color=myBlue,label=RHlabel)
        p2, = par1.plot(Time, Temperature, color=myRed, label=Templabel)
        p3, = par2.plot(Time, Flow, color=myGreen, label="Flow")
        
        lns = [p1, p2, p3]
        host.legend(handles=lns,bbox_to_anchor=(1.75, 1), fontsize=12)
        
        
        par2.spines['right'].set_position(('outward', 60))      
                       
        
       
        par2.yaxis.set_ticks_position('right')
        plt.grid()
        plt.title(label, fontsize=14)
        plt.show()                
        f.savefig(filename +'_HumidityChamber.'+OutputFormat, bbox_inches='tight')                
    if R1_RH is 1:
        label='Resistivity on Humidity'+CustomLabel
        f=plt.figure(label)
        ax=f.add_subplot(111)
        ax.plot(Humidity,Readout1,label='Resistivity ($\Omega$)',color=myBlue)
        plt.title(label,fontsize=14, fontweight=0,loc='center')
        plt.xlabel(RHlabel,fontsize=12)
        plt.ylabel(Rlabel,fontsize=12)
        ax.legend(bbox_to_anchor=(1.4, 1),fontsize=12)
        plt.grid()
        plt.show()
        f.savefig(filename +'_R1onRH.'+OutputFormat, bbox_inches='tight')       
        
        
    if R1_RH_Time is 1:
        label = 'Resistivity and Humidity '+CustomLabel
        f=plt.figure()
        host=f.add_subplot(111)
        ax1=host.twinx()
        
        host.set_xlabel(Tlabel,fontsize=12)
        host.set_ylabel(Rlabel,fontsize=12)
        ax1.set_ylabel(RHlabel,fontsize=12)
        
        p1,=host.plot(Time,Readout1,label=Rlabel,color=myGreen)
        p2,=ax1.plot(Time,Humidity,label=RHlabel,color=myBlue)
        
        legends=[p1,p2]
        host.legend(handles=legends,bbox_to_anchor=(1.6,1), fontsize=12)
        plt.grid()
        plt.title(label, fontsize=14)
        plt.show()
        f.savefig(filename +'_R1_RH_onTime.'+OutputFormat, bbox_inches='tight')        
        
    if R1_Gate_Time is 1:
        label = 'Resistivity and Gate '+CustomLabel
        f=plt.figure()
        host=f.add_subplot(111)
        ax1=host.twinx()
        
        host.set_xlabel(Tlabel,fontsize=12)
        host.set_ylabel(Rlabel,fontsize=12)
        ax1.set_ylabel(Glabel,fontsize=12)
        
        p1,=host.plot(Time,Readout1,label=Rlabel,color=myGreen)
        p2,=ax1.plot(Time,Gate,label=Glabel,color=myRed)
        
        legends=[p1,p2]
        host.legend(handles=legends,bbox_to_anchor=(1.6,1), fontsize=12)
        plt.grid()
        plt.title(label, fontsize=14)
        plt.show()
        f.savefig(filename +'_R1_Gate_OnTime.'+OutputFormat, bbox_inches='tight')
        
    if R1_RH_Gate_Time is 1:
        label = 'Resistivity,Gate and Humidity '+CustomLabel
        f=plt.figure()
        host=f.add_subplot(111)
        ax1=host.twinx()
        ax2=host.twinx()
        
        host.set_xlabel(Tlabel,fontsize=12)
        host.set_ylabel(Rlabel,fontsize=12)
        ax1.set_ylabel(Glabel,fontsize=12)
        ax2.set_ylabel(RHlabel,fontsize=12)
        
        p1,=host.plot(Time,Readout1,label=Rlabel,color=myGreen)
        p2,=ax1.plot(Time,Gate,label=Glabel,color=myRed)
        p3,=ax2.plot(Time,Humidity,label=RHlabel,color=myBlue)
        ax2.spines['right'].set_position(('outward',40))      
        ax2.yaxis.set_ticks_position('right')
        legends=[p1,p2,p3]
        host.legend(handles=legends,bbox_to_anchor=(1.7,1), fontsize=12)
        plt.grid()
        plt.title(label, fontsize=14)
        plt.show()
        f.savefig(filename +'_R1_RH_RH_OnTime.'+OutputFormat, bbox_inches='tight')

#f.savefig('Output.pdf', bbox_inches='tight')