# $Header$
import numpy as np

""" INPUT from embedding C++ code
dataHistoryFinal
OUTPUT to embedding C++ code
dataInterp
timeHistoryGain
"""

# remove half of each axis time history
# report to mean value
# apply sinusoidal wave to firt 50 elements
# apply sinusoidal wave to last 50 elements
# concatenate the two axis in a single vector
# set timeHistoryGain


if ("dataInterpX2" in globals()) and ("dataInterpY2" in globals()):
    thYSize = dataInterpY2.size
    dataInterpY2 = dataInterpY2[0:thYSize/2]
    thXSize = dataInterpX2.size
    dataInterpX2 = dataInterpX2[0:thXSize/2]

    dataInterpX2 = dataInterpX2 - np.mean(dataInterpX2)
    dataInterpY2 = dataInterpY2 - np.mean(dataInterpY2)

    if ("waveArrayFirst50" not in globals()):
        waveArrayFirst50 = (1.-np.cos(np.linspace(0, np.pi, num=50, dtype=np.float32)))/2.

        waveArrayLast50 = (1.-np.cos(np.linspace(0, np.pi, num=50, dtype=np.float32)))/2.
        if ("waveArrayLast50" not in globals()):

    dataInterpX2[0:50] = np.multiply(dataInterpX2[0:50], waveArrayFirst50, dtype=np.float32)
    dataInterpX2[-50:] = np.multiply(dataInterpX2[-50:], waveArrayLast50, dtype=np.float32)

    dataInterpY2[0:50] = np.multiply(dataInterpY2[0:50], waveArrayFirst50, dtype=np.float32)
    dataInterpY2[-50:] = np.multiply(dataInterpY2[-50:], waveArrayLast50, dtype=np.float32)

    dataInterpStack = np.stack((dataInterpX2, dataInterpY2))
    # the following returns an array dataInterpX2.size*2 with a sequence of (x,y)
    # coordinates. In case this turns out to be wrong, switch to use
    # np.concatenate(dataInterpX2, dataInterpY2)
    dataInterp = np.ravel(dataInterpStack, order='F')
else:
    thSize = dataHistoryFinal.shape[0] / 2
    dataHistoryFinal = np.resize(dataHistoryFinal, (thSize, dataHistoryFinal.shape[1]))

    dataHistoryFinal = dataHistoryFinal - np.meam(dataHistoryFinal, axis=0)

    if ("waveArrayFirst50" not in globals()):
        waveArrayFirst50 = np.reshape(waveArrayFirst50, (50,1), order='F')
        waveArrayFirst50 = np.hstack((waveArrayFirst50, waveArrayFirst50))

    if ("waveArrayLast50" not in globals()):
        waveArrayLast50 = np.reshape(waveArrayLast50, (50,1), order='F')
        waveArrayLast50 = np.hstack((waveArrayLast50, waveArrayLast50))

    dataHistoryFinal[0:][0:50] = np.multiply(dataHistoryFinal[0:][0:50], waveArrayFirst50, dtype=np.float32)
    dataHistoryFinal[0:][-50:] = np.multiply(dataHistoryFinal[0:][-50:], waveArrayLast50, dtype=np.float32)

    # OUTPUT to C++ code
    # the following returns an array dataHistoryFinal.shape[0]*2 with a sequence of (x,y)
    # coordinates.
    dataInterp = np.ravel(dataHistoryFinal)


# values from MicroGate producer of BCU
# 3.73 is the coefficient mas/pixel_camera_TT
# 21 is the coefficient mas/pixel_camera_CRED

masOverPxOrig = 3.73
masOverPxCRED2 = 21

masOverPxRatio = masOverPxOrig/masOverPxCRED2
# OUTPUT to C++ code
timeHistoryGain = 1.2 * np.asarray([masOverPxRatio] *0.72, dtype=np.float32)
