#!/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/numpy')
import numpy                                        #import the numpy module
import Analytics as Ana                             #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     = Ana.normalize(row);                 #normalize array - clean up bad/no data  
        rowCount  = array.size;                         #number of elements  
        rowSum    = numpy.sum(array);                   #sum elements
        rowAvg    = numpy.average(array);               #average
        rowMedian = numpy.median(array);                #compute median
        rowMode   = Ana.mode(array);                    #compute mode(s)
        rowMin    = numpy.min(array);                   #determine min
        rowMax    = numpy.max(array);                   #determine max
        rowRange  = numpy.ptp(array);                   #compute the range
        rowStdDev = numpy.std(array);                   #compute 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=%-8s"   % 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 the range  
        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

    print("COUNT.:",  end='')            
    for col in range(0,maxCols):                        #loop through all columns                  
        colArray = Ana.slice(dataPoints,"col",col);     #slice the array vertically
        array    = Ana.normalize(colArray);             #clean up the array  
        colCount = array.size;                          #number the elements
        print("%8d" % colCount, end='')                 #print column sum

    print("\nSUM...:", end='')            
    for col in range(0,maxCols):                        #loop through all columns                  
        colArray = Ana.slice(dataPoints,"col",col);     #slice the array vertically
        array    = Ana.normalize(colArray);             #clean up the array  
        colSum   = numpy.sum(array);                    #sum the elements
        print("%8.2f" % colSum, end='')                 #print column sum

    print("\nAVG...:", end='')            
    for col in range(0,maxCols):                        #loop through all columns                  
        colArray = Ana.slice(dataPoints,"col",col);     #slice the array vertically
        array    = Ana.normalize(colArray);             #clean up the array  
        colAvg   = numpy.average(array);                #average the elements
        print("%8.2f" % colAvg, end='')                 #print column average

    print("\nMEDIAN:", end='')            
    for col in range(0,maxCols):                        #loop through all columns                  
        colArray = Ana.slice(dataPoints,"col",col);     #slice the array vertically
        array    = Ana.normalize(colArray);             #clean up the array  
        colMedian= numpy.median(array);                 #compute the median of column
        print("%8.2f" % colMedian, end='')               #printcolumn median

    print("\nMODE..:", end='')            
    for col in range(0,maxCols):                        #loop through all columns                  
        colArray = Ana.slice(dataPoints,"col",col);     #slice the array vertically
        array    = Ana.normalize(colArray);             #clean up the array  
        colMode  = Ana.mode(array);                     #compute the mode(s) for column
        if len(colMode) > 0 :                           #if there is a mode
            print("%8.2f" % colMode[0], end='')         #print it
        else :
            print("%8s" % '--', end='')                 #print dash

    print("\nMIN...:", end='')            
    for col in range(0,maxCols):                        #loop through all columns                  
        colArray = Ana.slice(dataPoints,"col",col);     #slice the array vertically
        array    = Ana.normalize(colArray);             #clean up the array  
        colMin   = numpy.min(array);                    #compute the minimum of column
        print("%8.2f" % colMin, end='')                 #print column minimum

    print("\nMAX...:", end='')            
    for col in range(0,maxCols):                        #loop through all columns                  
        colArray = Ana.slice(dataPoints,"col",col);     #slice the array vertically
        array    = Ana.normalize(colArray);             #clean up the array  
        colMax   = numpy.max(array);                    #compute the maximum of column
        print("%8.2f" % colMax, end='')                 #print column maximum

    print("\nRANGE.:", end='')            
    for col in range(0,maxCols):                        #loop through all columns                  
        colArray = Ana.slice(dataPoints,"col",col);     #slice the array vertically
        array    = Ana.normalize(colArray);             #clean up the array  
        colRange = numpy.ptp(array);                    #compute the range for the column
        print("%8.2f" % colRange, end='')               #print column maximum

    print("\nStdDEV:", end='')            
    for col in range(0,maxCols):                        #loop through all columns                  
        colArray = Ana.slice(dataPoints,"col",col);     #slice the array vertically
        array    = Ana.normalize(colArray);             #clean up the array  
        colStdDev= numpy.std(array);                    #compute the standard deviation of column
        print("%8.2f" % colStdDev, end='')              #print column standard deviation

    print("\n\n------Overall Analytics------") 
    array = Ana.slice(dataPoints,"all")                 #convert 2dim array to 1dim
    array = Ana.normalize(array)                        #clean up the array
    data  = Ana.toString(array)                         #compute and print(all analytics
    print(data)

    print("\n------Column1 Again------")
    array = Ana.slice(dataPoints,"col",0)               #take 1st column
    array = Ana.normalize(array)                        #clean up array
    data  = Ana.toString(array)                         #compute and print(all analytics
    print(data)
    

#=====================================================================================
# 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 = Ana.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
#=================================================================================