#!/usr/bin/python3

import os,sys,math
import numpy as np
import matplotlib.pyplot as plt

#################
## Subroutines ##
#################

def binload_float_ndarray(fp):
    arr = np.zeros((0),dtype=np.float32,order='F')

    qb = fp.read(4)
    Nd = np.frombuffer(qb,dtype=np.int32,count=1)[0]
    shp = np.zeros((Nd),dtype=np.int32)

    piprod = 1
    for I in range(0,Nd):
        qb = fp.read(4)
        shp[I] = np.frombuffer(qb,dtype=np.int32,count=1)[0]
        piprod = piprod*shp[I]
    
    qb = fp.read(4*piprod)
    arr = np.frombuffer(qb,dtype=np.float32,count=piprod)

    arr = arr.reshape(shp)
    
    return arr;

def binload_int_ndarray(fp):
    arr = np.zeros((0),dtype=np.float32,order='F')

    qb = fp.read(4)
    Nd = np.frombuffer(qb,dtype=np.int32,count=1)[0]
    shp = np.zeros((Nd),dtype=np.int32)

    piprod = 1
    for I in range(0,Nd):
        qb = fp.read(4)
        shp[I] = np.frombuffer(qb,dtype=np.int32,count=1)[0]
        piprod = piprod*shp[I]
    
    qb = fp.read(4*piprod)
    arr = np.frombuffer(qb,dtype=np.int32,count=piprod)

    arr = arr.reshape(shp)
    
    return arr;


#################
## Main Script ##
#################

def periodcheck(arr):

    arr = np.asarray(arr).copy().flatten()
    N = arr.shape[0]
    q1 = arr[0]
    q2 = arr[1]
    q3 = arr[2]
    ind = -1
    for I in range(3,N-3):
        if(arr[I]==q1):
            if(arr[I+1]==q2):
                if(arr[I+2]==q3):
                    ind = I
                    break
    if(ind>=0):
        print("array has detected a period of {} out of {}".format(ind,N))
    
    return ind

def test_1():

    fname = "./test_scripts/randf_array.bin"
    try:
        fp = open(fname,"rb")
    except:
        print("Could not open {} for reading".format(fname))
        return
    arr = binload_float_ndarray(fp)
    periodcheck(arr)
    fp.close()

    fname = "./test_scripts/randf_array2.bin"
    try:
        fp = open(fname,"rb")
    except:
        print("Could not open {} for reading".format(fname))
        return
    arr2 = binload_float_ndarray(fp)
    periodcheck(arr2)
    fp.close()

    fname = "./test_scripts/randint_array.bin"
    try:
        fp = open(fname,"rb")
    except:
        print("Could not open {} for reading".format(fname))
        return
    arr3 = binload_int_ndarray(fp)
    periodcheck(arr3)
    print("array 3 max {} min {}".format(np.max(arr3),np.min(arr3)))
    fp.close()

    fname = "./test_scripts/randint_array2.bin"
    try:
        fp = open(fname,"rb")
    except:
        print("Could not open {} for reading".format(fname))
        return
    arr4 = binload_int_ndarray(fp)
    periodcheck(arr4)
    print("array 4 max {} min {}".format(np.max(arr4),np.min(arr4)))
    fp.close()

    plt.subplot(2,2,1)
    plt.imshow(arr)
    plt.subplot(2,2,2)
    plt.imshow(arr2)
    plt.show()

    plt.subplot(2,2,1)
    plt.imshow(arr3)
    plt.colorbar()
    plt.subplot(2,2,2)
    plt.imshow(arr4)
    plt.colorbar()
    plt.show()
    

    return

if(__name__=="__main__"):
    test_1()

    exit(0)

