#!/usr/bin/env python3
#=====================================================================================
# Get numeric data from sys.arg
# if not supplied, use hardcoded array
#=====================================================================================
import sys                                          #sys module to accept arguments
sys.path.insert(0,'/home/s/sultans/web/python/demo/analytics')
import Analytics                                    #import the Analytics module

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

in_array = ["Sam Sultan", 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 2.2]   #hardcoded array

if (len(sys.argv) > 1):                             #if argument are provided
    in_array = sys.argv[1:len(sys.argv)]            #eliminate the first arg (script name)

    
#=====================================================================================
# display: Display the 2 dimensional data array
#=====================================================================================
def display(array):

    print("\nINPUT DATA...")
     
    for value in array:                               #loop thru the array
        print(value, "\t", end='')                    #print each value 
    print()

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

    print("\nCOMPUTATION...")

    array     = Analytics.normalize(array)          #normalize array - clean up bad/no data  
    print(array)                                    #print(clean array
    arrCount  = Analytics.count(array)              #count elements  
    arrSum    = sum(array)                          #sum elements (Built-in function)
    arrAvg    = Analytics.avg(array)                #average
    arrMedian = Analytics.median(array)             #compute median
    arrMode   = Analytics.mode(array)               #compute mode(s)
    arrMin    = min(array)                          #determine min (Built-in function)
    arrMax    = max(array)                          #determine max (Built-in function)
    arrRange  = Analytics.range(array)              #compute the range
    arrStdDev = Analytics.stdDev(array)             #compute standard deviation

    print("Count...:"      , arrCount)              #print array count
    print("Sum.....: %.2f" % arrSum)                #print array sum   
    print("Avg.....: %.2f" % arrAvg)                #print array average   
    print("Median..: %.2f" % arrMedian)             #print array median   
    print("Mode....: %s"   % arrMode)               #print array mode(s)   
    print("Min.....: %.2f" % arrMin)                #print array value   
    print("Max.....: %.2f" % arrMax)                #print array value  
    print("Range...: %.2f" % arrRange)              #print array range  
    print("stdDev..: %.2f" % arrStdDev)             #print array standard deviation 


#=====================================================================================
# main code
#=====================================================================================

display(in_array)                                   #display formatted
compute(in_array)                                   #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
#=================================================================================