#!/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/numpy')
import numpy                                        #import the numpy module
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)          #clean up bad/no data, return a numpy array  
    arrCount  = array.size                          #number of elements 
    arrSum    = numpy.sum(array)                    #sum elements 
    arrAvg    = numpy.average(array)                #average
    arrMedian = numpy.median(array)                 #compute median
    arrMode   = Analytics.mode(array)               #determine mode (returns single mode)           
    arrMin    = numpy.min(array)                    #determine min 
    arrMax    = numpy.max(array)                    #determine max 
    arrRange  = numpy.ptp(array)                    #compute the range
    arrStdDev = numpy.std(array)                    #compute standard deviation

    print("Array...:"      , array)                 #print array
    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
#=================================================================================