#!/usr/bin/env python3
#=====================================================================================
# 2 dimensional numpy array
# All rows must be the same size.  
# If not, will be treated as single dim object, and numpy functions will not work
#=====================================================================================
import numpy 

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


pyArray1 = [[1,2,-3,4,0,5,6.0],[11,12.50,-14,15,25,50,75]]      #rows are equal size
pyArray2 = [[1,2,-3,4,0,5,6.0],[11,12.50,-14,15,25]]            #rows are not equal size
npArray1 = numpy.array(pyArray1)
npArray2 = numpy.array(pyArray2)

print("Python list1: ", pyArray1) 
print("Python list2: ", pyArray2)
print()
print("Numpy array1: \n", npArray1) 
print("Numpy array2: \n", npArray2)
print()
print("Datatype  array1/array2: ", npArray1.dtype,       '/', npArray2.dtype)
print("Dimension array1/array2: ", npArray1.ndim,  '      /', npArray2.ndim, 'not 2dim')
print("Size      array1/array2: ", npArray1.size,   '     /', npArray2.size)
print("Shape     array1/array2: ", npArray1.shape,      ' /', npArray2.shape)

print()
print("Before any change: ")
print(npArray1)

print()
npArray1[0,5] = 55.5
print("Changed element [0,5]: ", npArray1[0,5])
print(npArray1)

print()
rowSlice = npArray1[1,:]                                    #taking row 1 slice
colSlice = npArray1[:,5]                                    #taking column 5 slice
print("Taking a row slice: ", rowSlice)
print("Taking a col slice: ", colSlice)

print()
print("--------ROW 1 ANALYTICS------------")
row = npArray1[0]                                           #take a row slice
print("Data...: ", row)                                        
print("Count..: ", row.size)
print("Sum....: ", numpy.sum(row))
print("Average: ", numpy.average(row))
print("Median.: ", numpy.median(row))
print("Min....: ", numpy.min(row))
print("Max....: ", numpy.max(row))
print("Range..: ", numpy.ptp(row))
print("Std Dev: ", numpy.std(row))

print()
print("--------COL 3 ANALYTICS------------")
col = npArray1[:,2]                                         #take col 2 slice
print("Data...: ", col)                                        
print("Count..: ", col.size)
print("Sum....: ", numpy.sum(col))
print("Average: ", numpy.average(col))
print("Median.: ", numpy.median(col))
print("Min....: ", numpy.min(col))
print("Max....: ", numpy.max(col))
print("Range..: ", numpy.ptp(col))
print("Std Dev: ", numpy.std(col))

print()
print("-------OVERALL ANALYTICS-----------")
print("Count..: ", npArray1.size)                            #take entire array
print("Sum....: ", numpy.sum(npArray1))
print("Average: ", numpy.average(npArray1))
print("Median.: ", numpy.median(npArray1))
print("Min....: ", numpy.min(npArray1))
print("Max....: ", numpy.max(npArray1))
print("Range..: ", numpy.ptp(npArray1))
print("Std Dev: ", numpy.std(npArray1))




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