#!/usr/bin/env python3
#=====================================================================================
# Get data from a server file - one record at a time 
# Display to an html form
#=====================================================================================
import sys                                          #sys module to accept arguments
sys.path.insert(0,'/home/s/sultans/web/python/demo/oo/zAnalytics')
from Analytics import Analytics                     #import the Analytics module

print("Content-Type: text/html \n")                 #http header with newline char (required for web) 

#input_file = "dataset1.csv";
input_file = "/home/s/sultans/web/java/demo/8inpout/analytics/dataset1.csv";

#=====================================================================================
# readFile: Read the input file, and store into a 2 dimensional array
#=====================================================================================
def readFile(filename):

    print("SOURCE INPUT...")

    input = open(filename,'r')                          #Open file for reading
    
    for rec in input:                                   #loop thru the data rows
        rec   = rec.rstrip()                            #get rid of the rightmost trailing newline
        print(rec)
        array = rec.rsplit(',')                         #split the record on ,
        dataPoints.append(array)

    input.close()                                       #close the file
    
#=====================================================================================
# display: Display the 2 dimensional data array
#=====================================================================================
def display(dataPoints):

    print("\nCOLUMN ORIENTED...")
     
    for array in dataPoints:                            #loop thru the data rows
        for col in array:                               #loop thru each row
            print("{:>8s}".format(col),end='')          #print(each column - right justified 8 char
        print()                                         #print(newline

#=====================================================================================
# compute: Compute and print
#=====================================================================================
def compute(dataPoints):

    print("\nCOMPUTATION...")

    for row in dataPoints:                              #loop thru the data rows
        print("\t",end='')
        for col in row:                                 #loop thru each row
            print("{:>8s}".format(col),end='')          #print(each column - right justified 8 char

    #Perform row oriented analytics ------------------ 

        array  = Analytics.normalize(row);              #normalize array - clean up bad/no data  

        obj = Analytics(array)                          #Create an Analytics object

        rowCount  = obj.getCount()                      #get count of elements  
        rowSum    = obj.getSum()                        #get sum of elements
        rowAvg    = obj.getAvg()                        #get the average
        rowMedian = obj.getMedian()                     #get the median
        rowMode   = obj.getMode()                       #get the mode(s)
        rowMin    = obj.getMin()                        #get the minimum
        rowMax    = obj.getMax()                        #get the maximum
        rowRange  = obj.getRange()                      #get the range
        rowStdDev = obj.getStdDev()                     #get the standard deviation

        print(" | Count="      , rowCount,  end='')     #print row count
        print(("\tSum=%.2f"    % rowSum),   end='')     #print row sum   
        print(("\tAvg=%.2f"    % rowAvg),   end='')     #print row average   
        print(("\tMedian=%.2f" % rowMedian),end='')     #print row median   
        print(("\tMode=%s"     % rowMode),  end='')     #print row mode(s)   
        print(("\tMin=%.2f"    % rowMin),   end='')     #print min value   
        print(("\tMax=%.2f"    % rowMax),   end='')     #print max value  
        print(("\tRange=%.2f"  % rowRange), end='')     #print range value  
        print(("\tstdDev=%.2f" % rowStdDev))            #print standard deviation 

    print("\t-------------------------------------------------------------------------------------------")

    #Perform column oriented analytics ------------------ 

    maxCols = 0;                                #determine maximum number of columns
    for row in dataPoints:                      #loop through all rows                 
       if (len(row) > maxCols):                 #if number of columns for the row > max
            maxCols = len(row);                 #take it

    colObj = []                                           #array of objects to hold analytics for each column
    for col in range(0,maxCols):
        colArray = Analytics.slice(dataPoints,"col",col); #slice the array vertically
        array    = Analytics.normalize(colArray);         #clean up the array  
        obj      = Analytics(array)                       #Create an Analytics object for each column
        colObj.append(obj)                                #append to array

    print("COUNT.:",end='')            
    for col in range(0,maxCols):                          #loop through all columns                  
        colCount = colObj[col].getCount()                 #get the count for each column
        print("%8d" % colCount,end='')                    #print column count

    print("\nSUM...:",end='')            
    for col in range(0,maxCols):                          #loop through all columns                  
        colSum   = colObj[col].getSum()                   #get the sum for each column
        print("%8.2f" % colSum,end='')                    #print column sum

    print("\nAVG...:",end='')            
    for col in range(0,maxCols):                          #loop through all columns                  
        colAvg   = colObj[col].getAvg()                   #get the average for each column
        print("%8.2f" % colAvg,end='')                    #print column average

    print("\nMEDIAN:",end='')            
    for col in range(0,maxCols):                          #loop through all columns                  
        colMedian= colObj[col].getMedian()                #get the median for each column
        print("%8.2f" % colMedian,end='')                 #print column median

    print("\nMODE..:",end='')            
    for col in range(0,maxCols):                          #loop through all columns                  
        colMode  = colObj[col].getMode()                  #get the mode(s) for each column
        if len(colMode) > 0 :                             #if there is a mode
            print("%8.2f" % colMode[0],end='')            #print only one
        else :
            print("%8s" % '--',end='')                    #print dash

    print("\nMIN...:",end='')            
    for col in range(0,maxCols):                          #loop through all columns                  
        colMin   = colObj[col].getMin()                   #get the minimum for each column
        print("%8.2f" % colMin,end='')                    #print(column minimum

    print("\nMAX...:",end='')            
    for col in range(0,maxCols):                          #loop through all columns                  
        colMax   = colObj[col].getMax()                   #get the maximum for each column
        print("%8.2f" % colMax,end='')                    #print column maximum

    print("\nRANGE.:",end='')            
    for col in range(0,maxCols):                          #loop through all columns                  
        colRange = colObj[col].getRange()                 #get the range for each column
        print("%8.2f" % colRange,end='')                  #print column range

    print("\nStdDEV:",end='')            
    for col in range(0,maxCols):                          #loop through all columns                  
        colStdDev= colObj[col].getStdDev()                #get the standard deviation for each column
        print("%8.2f" % colStdDev,end='')                 #print column standard deviation

    print("\n\n------Overall Analytics------") 
    array = Analytics.slice(dataPoints,"all",0)           #convert 2dim array to 1dim
    array = Analytics.normalize(array)                    #clean up the array
    obj   = Analytics(array)                              #Create an Analytics object
    print(obj)

    print("\n-------Column1 Again-------")
    print(colObj[0])                                      #reprint(analytics for column 1											
    

#============================================================================================
# main code
#============================================================================================
dataPoints = []                                     #2 dim array to hold all the data points

if (len(sys.argv) > 1):                             #if argument is provided (remember argv[0] is this pgm name) 
    input_file = sys.argv[1];                       #use it as an input file

readFile(input_file)                                #read CSV file into 2dim array
#display(dataPoints)                                #display formatted
compute(dataPoints)                                 #compute analytics

print("\nTRANSPOSED...")
xArray = Analytics.transpose(dataPoints)            #transpose the 2dim array
display(xArray)                                     #display formatted
compute(xArray)                                     #compute analytics





#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/s/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================