示例#1
0
def wavelength_flux_error_in_range(starting_point: float, ending_point: float,
                                   z: float, spectra_data) -> RangesData:
    wavelength_column = spectra_data[:, column_index.wavelength]

    wavelength_observed_from = (z + 1) * starting_point
    wavelength_observed_to = (z + 1) * ending_point

    wavelength_lower_limit = np.where(
        wavelength_column > wavelength_observed_from)
    wavelength_upper_limit = np.where(
        wavelength_column < wavelength_observed_to)

    wavelength = spectra_data[
        np.min(wavelength_lower_limit[column_index.wavelength]
               ):np.max(wavelength_upper_limit[column_index.wavelength]),
        column_index.wavelength]
    flux = spectra_data[
        np.min(wavelength_lower_limit[column_index.wavelength]
               ):np.max(wavelength_upper_limit[column_index.wavelength]),
        column_index.flux]
    error = spectra_data[
        np.min(wavelength_lower_limit[column_index.wavelength]
               ):np.max(wavelength_upper_limit[column_index.wavelength]),
        column_index.error]

    return RangesData(wavelength, flux, error)
def wavelength_flux_error_in_range(starting_point: float, ending_point: float,
                                   z: float, spectra_data) -> RangesData:
    """Returns a range of a wavelength, flux and error defined by starting and ending points.

    Parameters
    ----------
    starting_point : float
        Uses the range defined by the following variable: WAVELENGTH_RESTFRAME.start,     
    ending_point: float
        Also, uses the range defined by the following variable: WAVELENGTH_RESTFRAME.end
    z: float
        Values from the data base of the redshift, DR16Q (for now..)
    spectra_data: list
        Current spectra data from files, DR16Q (for now...)

    Returns
    -------
    RangesData.

    Examples
    --------
    RangesData(wavelength, flux, error). List of tuples of arrays.
    RangesData creates a list of tuples, within each tuple it stores arrays for the ranges of 
    wavelength, flux, and error values.
    """
    wavelength_column = spectra_data[:, column_index.wavelength]

    wavelength_observed_from = (z + 1) * starting_point
    wavelength_observed_to = (z + 1) * ending_point

    wavelength_lower_limit = np.where(
        wavelength_column > wavelength_observed_from)
    wavelength_upper_limit = np.where(
        wavelength_column < wavelength_observed_to)

    wavelength = spectra_data[
        np.min(wavelength_lower_limit[column_index.wavelength]
               ):np.max(wavelength_upper_limit[column_index.wavelength]),
        column_index.wavelength]
    flux = spectra_data[
        np.min(wavelength_lower_limit[column_index.wavelength]
               ):np.max(wavelength_upper_limit[column_index.wavelength]),
        column_index.flux]
    error = spectra_data[
        np.min(wavelength_lower_limit[column_index.wavelength]
               ):np.max(wavelength_upper_limit[column_index.wavelength]),
        column_index.error]

    return RangesData(wavelength, flux, error)
示例#3
0
def wavelength_flux_error_for_points(starting_point: float,
                                     ending_point: float, z: float,
                                     spectra_data) -> RangesData:
    wavelength_column = spectra_data[:, column_index.wavelength]

    wavelength_observed_start = (z + 1) * starting_point
    wavelength_observed_end = (z + 1) * ending_point

    point_from = np.max(
        np.where(wavelength_column < wavelength_observed_start))
    point_to = np.min(np.where(wavelength_column > wavelength_observed_end))

    wavelength = spectra_data[point_from:point_to, column_index.wavelength]
    flux = spectra_data[point_from:point_to, column_index.flux]
    error = spectra_data[point_from:point_to, column_index.error]

    return RangesData(wavelength, flux, error)
示例#4
0
def process_spectra_and_draw_figures(index: int, z, snr, spectrum_file_name):

    print(str(index) + ": " + spectrum_file_name)
    print_to_file(str(index) + ": " + spectrum_file_name, LOG_FILE)

    current_spectra_data = np.loadtxt(SPEC_DIREC + spectrum_file_name)

    wavelength_observed_from = (z + 1) * WAVELENGTH_RESTFRAME.start
    wavelength_observed_to = (z + 1) * WAVELENGTH_RESTFRAME.end

    point_C, point_B, point_A = define_three_anchor_points(
        z, current_spectra_data)

    # THE THREE POINTS THAT THE POWER LAW WILL USE (Points C, B, and A)
    power_law_data_x = (point_C.wavelength, point_B.wavelength,
                        point_A.wavelength)
    power_law_data_y = (point_C.flux, point_B.flux, point_A.flux)

    # DEFINING WAVELENGTH, FLUX, AND ERROR (CHOOSING THEIR RANGE)
    wavelength, flux, error = wavelength_flux_error_in_range(
        WAVELENGTH_RESTFRAME.start, WAVELENGTH_RESTFRAME.end, z,
        current_spectra_data)
    original_ranges = RangesData(wavelength, flux, error)

    print(power_law_data_x)
    print_to_file(power_law_data_x, LOG_FILE)
    print(power_law_data_y)
    print_to_file(power_law_data_y, LOG_FILE)

    # CURVE FIT FOR FIRST POWERLAW
    try:
        pars, covar = curve_fit(powerlaw,
                                power_law_data_x,
                                power_law_data_y,
                                p0=[b, c],
                                maxfev=10000)
    except:
        print("Error - curve_fit failed-1st powerlaw " + spectrum_file_name)
        print_to_file(
            "Error - curve_fit failed-1st powerlaw " + spectrum_file_name,
            LOG_FILE)

    bf, cf = pars[0], pars[1]

    flux_normalized = flux / powerlaw(wavelength, bf, cf)
    error_normalized = error / powerlaw(wavelength, bf, cf)
    normalized_data = DataNormalized(flux_normalized, error_normalized)

    for n in range(1, len(flux_normalized) - 5):
        if abs(flux_normalized[n + 1] - flux_normalized[n]) > 0.5:
            if error_normalized[n + 1] > 0.25:
                error_normalized[n + 1] = error_normalized[n]
                flux_normalized[n + 1] = flux_normalized[n]
                error[n + 1] = error[n]
                flux[n + 1] = flux[n]
        if error_normalized[n] > 0.5:
            error_normalized[n] = error_normalized[n - 1]
            flux_normalized[n] = flux_normalized[n - 1]
            error[n] = error[n - 1]
            flux[n] = flux[n - 1]
        if abs(flux_normalized[n + 1] - flux_normalized[n]) > 5:
            error_normalized[n + 1] = error_normalized[n]
            flux_normalized[n + 1] = flux_normalized[n]
            error[n + 1] = error[n]
            flux[n + 1] = flux[n]

    ############# TESTING TWO REGIONS ##########################
    flagged = False
    test1 = wavelength_flux_error_in_range(WAVELENGTH_RESTFRAME_TEST_1.start,
                                           WAVELENGTH_RESTFRAME_TEST_1.end, z,
                                           current_spectra_data)
    normalized_flux_test_1 = test1.flux / powerlaw(test1.wavelength, bf, cf)
    flagged_by_test1 = abs(np.median(normalized_flux_test_1) - 1) >= 0.05
    if flagged_by_test1:
        print("flagged_by_test1: ", flagged_by_test1)
        print_to_file("flagged_by_test1: " + str(flagged_by_test1), LOG_FILE)

    test2 = wavelength_flux_error_in_range(WAVELENGTH_RESTFRAME_TEST_2.start,
                                           WAVELENGTH_RESTFRAME_TEST_2.end, z,
                                           current_spectra_data)
    normalized_flux_test_2 = test2.flux / powerlaw(test2.wavelength, bf, cf)
    flagged_by_test2 = abs(np.median(normalized_flux_test_2) - 1) >= 0.05
    if flagged_by_test2:
        print("flagged_by_test2: ", flagged_by_test2)
        print_to_file("flagged_by_test2: " + str(flagged_by_test2), LOG_FILE)

    if flagged_by_test1 and flagged_by_test2:
        flagged = True
        error_message = "Flagging figure #" + str(
            index) + ", file name: " + spectrum_file_name
        print(error_message)
        print_to_file(error_message, LOG_FILE)

    residuals_test1 = test1.flux - powerlaw(test1.wavelength, bf, cf)
    residuals_test2 = test2.flux - powerlaw(test2.wavelength, bf, cf)
    residuals_test1_and_2 = np.concatenate([residuals_test1, residuals_test2])
    wavelength_tests_1_and_2 = np.concatenate(
        [test1.wavelength, test2.wavelength])
    chi_sq = sum((residuals_test1_and_2**2) /
                 powerlaw(wavelength_tests_1_and_2, bf, cf))

    fields = [index - STARTS_FROM + 1, index, chi_sq]
    append_row_to_csv(GOODNESS_OF_FIT_FILE, fields)
    if chi_sq > 8 and flagged_by_test1 and flagged_by_test2:
        append_row_to_csv(BAD_NORMALIZATION_FLAGGED_FILE, fields)
    else:
        append_row_to_csv(GOOD_NORMALIZATION_FLAGGED_FILE, fields)

    ##########################################################

    ############# SNR Calculations ##########################
    wavelengths_for_snr_lower = np.where(wavelength /
                                         (z + 1.) < WAVELENGTH_FOR_SNR.start)
    wavelengths_for_snr_upper = np.where(wavelength /
                                         (z + 1.) > WAVELENGTH_FOR_SNR.end)
    snr_mean_in_ehvo = round(
        np.mean(1. / error_normalized[np.max(wavelengths_for_snr_lower[0]):np.
                                      min(wavelengths_for_snr_upper)]), 5)
    ##########################################################

    flagged_snr_mean_in_ehvo = False
    if snr_mean_in_ehvo < 10.:
        flagged_snr_mean_in_ehvo = True

    figure_data = FigureData(spectrum_file_name, wavelength_observed_from,
                             wavelength_observed_to, z, snr, snr_mean_in_ehvo)
    original_figure_data = FigureDataOriginal(figure_data, bf, cf,
                                              power_law_data_x,
                                              power_law_data_y)

    draw_original_figure(index, original_ranges, original_figure_data, test1,
                         test2)
    draw_normalized_figure(index, original_ranges, figure_data,
                           normalized_data, test1, test2,
                           normalized_flux_test_1, normalized_flux_test_2)

    norm_w_f_e = (wavelength, flux_normalized, error_normalized)
    norm_w_f_e = (np.transpose(norm_w_f_e))
    np.savetxt(SPEC_DIREC + spectrum_file_name[0:20] + NORM_FILE_EXTENSION,
               norm_w_f_e)
    return bf, cf, flagged, flagged_snr_mean_in_ehvo, snr_mean_in_ehvo
示例#5
0
        non_sm_flux = flux
        non_sm_error = error
        flux = sm_flux
        error = sm_error

    ## SMOOTHING NORMALIZED FIGURES
    if sm == 'yes':
        sm_flux_norm = smooth(flux_normalized, BOXCAR_SIZE)
        sm_error_norm = smooth(error_normalized,
                               BOXCAR_SIZE) / np.sqrt(BOXCAR_SIZE)
        non_sm_flux_norm = flux_normalized
        non_sm_error_norm = error_normalized
        flux_normalized = sm_flux_norm
        error_normalized = sm_error_norm

    original_ranges = RangesData(wavelength, flux, error)

    #############################################################################################
    #################################### TESTING TWO REGIONS ####################################

    flagged = False  # BAD FIT
    #flagged_anchor_point_bad_fit = False # POWERLAW NOT CLOSE ENOUGH TO ANCHOR POINT
    #flagged_anchor_point_bad_fit = True

    # UNFLAGGING
    flagged_fit_too_high_green = False
    flagged_fit_too_high_pink = False
    unflagged = False

    # POSSIBLE ABSORPTION
    flagged_completely_below_green = False