def darkcurrent_channel_analyse(directory):
    hfont = {'family':'serif', 'fontsize': 12}

    cal_dict = sort_ibsen_by_int(directory)
    for key, val in sorted(cal_dict.items()):
        plt.plot(val['darkcurrent']['wave'], val['darkcurrent']['mean'], label='%1.f' % key)
    plt.xlabel(r'Wavelength $\lambda$ [nm]', **hfont)
    plt.ylabel(r'Signal [DN]', **hfont)

    fontP = FontProperties()
    fontP.set_family('serif')
    fontP.set_size('small')
    legend = plt.legend(loc=0, ncol=1, prop = fontP,fancybox=True,
                        shadow=False,title='Integration Times [ms]',bbox_to_anchor=(1.0, 1.0))
    #plt.setp(legend.get_title(),fontsize='small')
    plt.tight_layout()
    plt.show()

    plt.plot(cal_dict[5]['darkcurrent']['wave'], cal_dict[5]['reference']['mean'], label='Mean')
    plt.plot(cal_dict[5]['darkcurrent']['wave'], cal_dict[5]['darkcurrent']['mean'], label='Mean')
    plt.plot(cal_dict[5]['darkcurrent']['wave'], cal_dict[5]['darkcurrent']['data'], alpha=0.05)
    plt.plot(cal_dict[5]['darkcurrent']['wave'], cal_dict[5]['reference']['data'], alpha=0.05)
    plt.xlabel(r'Wavelength $\lambda$ [nm]', **hfont)
    plt.ylabel(r'Signal [DN]', **hfont)
    plt.show()

    sorted_keys = sorted(cal_dict.keys()) #[4:-4]
    tmp_channels = range(len(cal_dict[sorted_keys[0]]['darkcurrent']['wave']))
    noise_dict = dict()
    #tmp_channels = np.delete(tmp_channels, [187, 258, 265, 811])794
    gs = gridspec.GridSpec(2, 2)
    ax1 = plt.subplot(gs[0, :])
    ax2 = plt.subplot(gs[1, :])
    IntTimes = np.array(sorted_keys)
    noise = np.array([])

    dark_tmp = cal_dict[110]['darkcurrent']['mean']
    #ax1.plot( dark_tmp, '+')
    print(np.where(dark_tmp > 2361 ))

    for channel in tmp_channels:
        noise_dict[channel] = dict()
        dark = np.array([cal_dict[key]['darkcurrent']['mean'][channel] for key in sorted_keys])
        noise_dict[channel]['dark'] = dark
        coeffs_dark = np.polyfit(sorted_keys, dark, deg=1)

        noise = np.append(noise, coeffs_dark[1])
        ax1.plot(IntTimes, dark, '+')

    #ax1.plot(IntTimes, noise_dict[258]['dark'])
    ax2.plot(tmp_channels, noise)
    ax1.set_xlabel('Integration Time [ms]', **hfont)
    ax2.set_xlabel(r'Wavelength $\lambda$ [nm]', **hfont)
    ax1.set_ylabel('Signal [DN]', **hfont)
    ax2.set_ylabel('Signal [DN]', **hfont)
    plt.tight_layout()
    plt.show()
def test_offset_subtraction():
    """ testing of offset subtraction or darkcurrent subtraction is better"""
    test_directory = '/home/joanna/DLR/Codes/calibration/test_nonlinearity/labor/ibsen_nonlinearity_verschoeben2/'
    offset = 'offset_corrected_calibration/'
    dark_ = 'darkcurrent_corrected_calibration/'
    _file = 'nonlinearity_correction.txt'
    nonlinear_correction_dict = dict()
    nonlinear_correction_dict_dark = dict()
    cal_dict = sort_ibsen_by_int(test_directory)
    nonlinear_correction_dict['DN'], nonlinear_correction_dict['nonlinear'] = read_nonlinear_correction_file(offset + _file)
    nonlinear_correction_dict_dark['DN'], nonlinear_correction_dict_dark['nonlinear'] = read_nonlinear_correction_file(dark_ + _file)
    check_nonlinearity(cal_dict, [nonlinear_correction_dict,nonlinear_correction_dict_dark])
def test_offset_subtraction():
    """ testing of offset subtraction or darkcurrent subtraction is better"""
    test_directory = "/home/jana_jo/DLR/Codes/calibration/test_nonlinearity/labor/ibsen_nonlinearity_verschoeben2/"
    offset = "offset_corrected_calibration/"
    dark_ = "darkcurrent_corrected_calibration/"
    _file = "nonlinearity_correction.txt"
    nonlinear_correction_dict = dict()
    nonlinear_correction_dict_dark = dict()
    cal_dict = sort_ibsen_by_int(test_directory)
    nonlinear_correction_dict["DN"], nonlinear_correction_dict["nonlinear"] = read_nonlinear_correction_file(
        offset + _file
    )
    nonlinear_correction_dict_dark["DN"], nonlinear_correction_dict_dark["nonlinear"] = read_nonlinear_correction_file(
        dark_ + _file
    )
    check_nonlinearity(cal_dict, [nonlinear_correction_dict, nonlinear_correction_dict_dark])
def darkcurrent_channel_analyse(directory):
    hfont = {"family": "serif", "fontsize": 12}

    cal_dict = sort_ibsen_by_int(directory)
    for key, val in sorted(cal_dict.items()):
        plt.plot(val["darkcurrent"]["wave"], val["darkcurrent"]["mean"], label="%1.f" % key)
    plt.xlabel(r"Wavelength $\lambda$ [nm]", **hfont)
    plt.ylabel(r"DN [a.u.]", **hfont)

    fontP = FontProperties()
    fontP.set_family("serif")
    fontP.set_size("small")
    legend = plt.legend(
        loc=0,
        ncol=1,
        prop=fontP,
        fancybox=True,
        shadow=False,
        title="Integration Times [ms]",
        bbox_to_anchor=(1.0, 1.0),
    )
    plt.setp(legend.get_title(), fontsize="small")
    plt.tight_layout()
    plt.show()

    plt.plot(cal_dict[5]["darkcurrent"]["wave"], cal_dict[5]["reference"]["mean"], label="Mean")
    plt.plot(cal_dict[5]["darkcurrent"]["wave"], cal_dict[5]["darkcurrent"]["mean"], label="Mean")
    plt.plot(cal_dict[5]["darkcurrent"]["wave"], cal_dict[5]["darkcurrent"]["data"], alpha=0.05)
    plt.plot(cal_dict[5]["darkcurrent"]["wave"], cal_dict[5]["reference"]["data"], alpha=0.05)
    plt.xlabel(r"Wavelength $\lambda$ [nm]", **hfont)
    plt.ylabel(r"DN [a.u.]", **hfont)
    plt.show()

    sorted_keys = sorted(cal_dict.keys())  # [4:-4]
    tmp_channels = range(len(cal_dict[sorted_keys[0]]["darkcurrent"]["wave"]))
    noise_dict = dict()
    # tmp_channels = np.delete(tmp_channels, [187, 258, 265, 811])794
    gs = gridspec.GridSpec(2, 2)
    ax1 = plt.subplot(gs[0, :])
    ax2 = plt.subplot(gs[1, :])
    IntTimes = np.array(sorted_keys)
    noise = np.array([])

    dark_tmp = cal_dict[110]["darkcurrent"]["mean"]
    # ax1.plot( dark_tmp, '+')
    print(np.where(dark_tmp > 2361))

    for channel in tmp_channels:
        noise_dict[channel] = dict()
        dark = np.array([cal_dict[key]["darkcurrent"]["mean"][channel] for key in sorted_keys])
        noise_dict[channel]["dark"] = dark
        coeffs_dark = np.polyfit(sorted_keys, dark, deg=1)

        noise = np.append(noise, coeffs_dark[1])
        ax1.plot(IntTimes, dark, "+")

    # ax1.plot(IntTimes, noise_dict[258]['dark'])
    ax2.plot(tmp_channels, noise)
    ax1.set_xlabel("Integration Time [ms]", **hfont)
    ax2.set_xlabel("Wavelength [nm]", **hfont)
    ax1.set_ylabel("DN [a.u.]", **hfont)
    ax2.set_ylabel("DN [a.u.]", **hfont)
    plt.tight_layout()
    plt.show()