def residual_quartic(param):
    '''Function which computes the residual (as sum of squares) comparing the
    ratio of expt to theoretical intensity ratio to the sensitivity  profile
    modelled as  a line, ( 1+ c1*x + c2*x**2 + c3*x**3 + c4*x**4 )

    param : T, c1, c2, c3, c4

    '''
    TK = param[0]

    computed_D2 = compute_series.spectra_D2(TK, OJ_D2, QJ_D2, SJ_D2)
    computed_HD = compute_series.spectra_HD(TK, OJ_HD, QJ_HD, SJ_HD)
    computed_H2 = compute_series.spectra_H2_c(TK, OJ_H2, QJ_H2)

    # ------ D2 ------
    trueR_D2 = gen_intensity_mat(computed_D2, 2)
    expt_D2 = gen_intensity_mat(dataD2, 0)
    I_D2 = np.divide(expt_D2, trueR_D2)
    I_D2 = clean_mat(I_D2)
    # ----------------

    # ------ HD ------
    trueR_HD = gen_intensity_mat(computed_HD, 2)
    expt_HD = gen_intensity_mat(dataHD, 0)
    I_HD = np.divide(expt_HD, trueR_HD)
    I_HD = clean_mat(I_HD)
    # ----------------

    # ------ H2 ------
    trueR_H2 = gen_intensity_mat(computed_H2, 2)
    expt_H2 = gen_intensity_mat(dataH2, 0)
    I_H2 = np.divide(expt_H2, trueR_H2)
    I_H2 = clean_mat(I_H2)
    # ----------------

    #I_H2 = clean_and_scale_elements(I_H2, index_H2, 2)
    #I_HD = clean_and_scale_elements(I_HD, index_HD, 2)
    #I_D2 = clean_and_scale_elements(I_D2, index_D2, 2)

    # generate the RHS : sensitivity factor
    sD2 = gen_s_quartic(computed_D2, param)
    sHD = gen_s_quartic(computed_HD, param)
    sH2 = gen_s_quartic(computed_H2, param)

    # residual matrix
    eD2 = (np.multiply(wMat_D2, I_D2)) - sD2
    eHD = (np.multiply(wMat_HD, I_HD)) - sHD
    eH2 = (np.multiply(wMat_H2, I_H2)) - sH2

    eD2 = clean_mat(eD2)
    eHD = clean_mat(eHD)
    eH2 = clean_mat(eH2)

    # E = np.sum(np.square(eD2)) + np.sum(np.square(eHD))\
    #    + np.sum(np.square(eH2))

    E=np.sum(np.abs(eD2)) + np.sum(np.abs(eHD)) +\
        np.sum(np.abs(eH2))

    return (E)
param_quartic[0] = 298
param_quartic[1] = -0.9340
param_quartic[2] = -0.2140
param_quartic[3] = -0.00100
param_quartic[4] = -0.000001

# ------------------------------------------------
# ------------------------------------------------

wMat_D2 = 1
wMat_HD = 1
wMat_H2 = 1

computed_D2 = compute_series.spectra_D2(299, OJ_D2, QJ_D2, SJ_D2)
computed_HD = compute_series.spectra_HD(299, OJ_HD, QJ_HD, SJ_HD)
computed_H2 = compute_series.spectra_H2_c(299, OJ_H2, QJ_H2)

trueR_D2 = gen_intensity_mat(computed_D2, 2)
expt_D2 = gen_intensity_mat(dataD2, 0)

trueR_HD = gen_intensity_mat(computed_HD, 2)
expt_HD = gen_intensity_mat(dataHD, 0)

trueR_H2 = gen_intensity_mat(computed_H2, 2)
expt_H2 = gen_intensity_mat(dataH2, 0)

I_D2 = np.divide(expt_D2, trueR_D2)
I_HD = np.divide(expt_HD, trueR_HD)
I_H2 = np.divide(expt_H2, trueR_H2)

I_D2 = clean_mat(I_D2)