#!/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 binsave_float_ndarray(fp,arr):




    return


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

def test_1():

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

    fp.close()

    plt.subplot(2,2,1)
    plt.imshow(arr1)
    plt.subplot(2,2,2)
    plt.imshow(arr2)
    plt.subplot(2,2,3)
    plt.hist(arr1.flatten(),bins=100)
    plt.subplot(2,2,4)
    plt.hist(arr2.flatten(),bins=100)
    plt.show()

    print("{} {}".format(np.mean(arr2),np.std(arr2)))
    

    return

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

    exit(0)

