import numpy as np

def insert_index_into_arr( inNpArr, chanVal, index, NUM_CHANS ) :

    firstNaNcoord = np.where( inNpArr[ chanVal, :] == -1)[0]

    if len(firstNaNcoord) > 0 :
        firstNaNcoord = min( firstNaNcoord )

    nIndices = inNpArr.shape[1]

    # print( "firstNaNcoord: ", firstNaNcoord, " nIndices: ", nIndices )

    if firstNaNcoord == (nIndices-1) :
        newArray = np.full(shape=(NUM_CHANS, 1), fill_value=-1)
        newArray[chanVal] = index
        inNpArr = np.hstack((inNpArr, newArray))

    else :
        inNpArr[chanVal, firstNaNcoord] = index

    return inNpArr



def get_init_channel_array( nRows, dtypeParam=int) :

    return np.full(shape=(nRows, 1), fill_value=-1, dtype=dtypeParam)      



def merge_arrays_horizontally( leftArray, rightArray ) :
    
    # print("leftArray.shape: ", leftArray.shape, "rightArray.shape: ", rightArray.shape)

    nRows = np.max( [leftArray.shape[0],  rightArray.shape[0]] )

    midIndex = leftArray.shape[0] + round(rightArray.shape[1] / 2)

    nCols = leftArray.shape[1] + rightArray.shape[1]

    newArray = np.full( shape=( nRows, nCols ), fill_value=0 )

    # print("newArray.shape: ", newArray.shape)
    newArray[ 0 : (leftArray.shape[0]), 0 : (leftArray.shape[1]) ] = leftArray
    newArray[ 0 : rightArray.shape[0], leftArray.shape[1] : ] = rightArray

    return midIndex, newArray


def insert_1d_input_arr_into_2d_arr(chanNum, orig2Darr, input1Darr) :

	if input1Darr.shape[0] > orig2Darr.shape[1] :
		nChans = orig2Darr.shape[0]
		tempVals = np.full( shape=( nChans, input1Darr.shape[0]), fill_value=-1 )
		tempVals[0:orig2Darr.shape[0], 0:orig2Darr.shape[1]] = np.copy( orig2Darr )
		orig2Darr = np.copy( tempVals )

		# print("vals.shape: ", vals.shape, " inndarray.shape: ", inndarray.shape, " chan: ", chan)
	orig2Darr[ chanNum, 0:input1Darr.shape[0] ] = input1Darr
	return orig2Darr


