Ejemplo n.º 1
0
def test_user_supplied_poly_coeff_twodspec():
    # Load the image
    lhs6328_fits = fits.open(
        os.path.join(HERE, "test_data", "v_e_20180810_12_1_0_0.fits.gz")
    )[0]
    spatial_mask = np.arange(50, 200)
    spec_mask = np.arange(50, 1024)

    #
    # Loading two pre-saved spectral traces from a single FITS file.
    #
    lhs6328 = spectral_reduction.TwoDSpec(
        lhs6328_fits,
        spatial_mask=spatial_mask,
        spec_mask=spec_mask,
        cosmicray=True,
        readnoise=2.34,
        log_file_name=None,
    )

    # Trace the spectra
    lhs6328.ap_trace(nspec=2, display=False)

    # Extract the spectra
    lhs6328.ap_extract(apwidth=10, optimal=True, skywidth=10, display=False)

    # Calibrate the 1D spectra
    lhs6328_onedspec = spectral_reduction.OneDSpec(log_file_name=None)
    lhs6328_onedspec.from_twodspec(lhs6328)

    fit_coeff = np.array(
        [
            3.09833375e03,
            5.98842823e00,
            -2.83963934e-03,
            2.84842392e-06,
            -1.03725267e-09,
        ]
    )
    fit_type = "poly"

    # Note that there are two science traces, so two polyfit coefficients
    # have to be supplied by in a list
    lhs6328_onedspec.add_fit_coeff(fit_coeff, fit_type)
    lhs6328_onedspec.apply_wavelength_calibration()

    # Inspect reduced spectrum
    lhs6328_onedspec.inspect_reduced_spectrum(display=False)

    # Save as a FITS file
    lhs6328_onedspec.save_fits(
        output="wavecal+count",
        filename=os.path.join(
            HERE,
            "test_output",
            "user_supplied_wavelength_polyfit_coefficients",
        ),
        stype="science",
        overwrite=True,
    )
Ejemplo n.º 2
0
def test_forced_extraction_tophat():
    lhs6328_twodspec = spectral_reduction.TwoDSpec(
        lhs6328_frame,
        spatial_mask=spatial_mask,
        spec_mask=spec_mask,
        cosmicray=True,
        readnoise=5.7,
        gain=2.45,
        log_level="INFO",
        log_file_folder=os.path.join(HERE, "test_output"),
    )

    lhs6328_twodspec.add_trace(trace, trace_sigma)

    # Force extraction
    lhs6328_twodspec.ap_extract(
        apwidth=15,
        skywidth=10,
        skydeg=1,
        optimal=False,
        forced=True,
        display=False,
        save_fig=True,
        filename=os.path.join(HERE, "test_output", "test_force_extraxtion4"),
        fig_type="iframe+png",
    )

    # Store the forced extracted count
    count_forced = copy.copy(lhs6328_twodspec.spectrum_list[0].count)

    assert (np.nansum(tophat_count) >= np.nansum(count_forced) * 0.999) & (
        np.nansum(tophat_count) <= np.nansum(count_forced) * 1.001)
Ejemplo n.º 3
0
def test_saturated_data():
    # Science frame
    lhs6328_frame = image_reduction.ImageReduction(
        log_level="DEBUG", log_file_folder=os.path.join(HERE, "test_output")
    )
    lhs6328_frame.add_filelist(
        os.path.join(
            HERE, "test_data", "sprat_LHS6328_fake_saturated_data.list"
        )
    )
    lhs6328_frame.load_data()
    lhs6328_frame.reduce()

    lhs6328_frame.save_masks(
        os.path.join(
            HERE, "test_output", "fake_saturated_reduced_image_mask.fits"
        ),
        overwrite=True,
    )

    assert (lhs6328_frame.bad_mask == lhs6328_frame.saturation_mask).all()

    lhs6328_twodspec = spectral_reduction.TwoDSpec(
        lhs6328_frame,
        spatial_mask=spatial_mask,
        spec_mask=spec_mask,
        cosmicray=True,
        readnoise=5.7,
        log_level="DEBUG",
        log_file_folder=os.path.join(HERE, "test_output"),
    )

    lhs6328_twodspec.ap_trace(nspec=1)

    lhs6328_twodspec.ap_extract(model="lowess", lowess_frac=0.8)
Ejemplo n.º 4
0
def test_add_arc_path_with_hdu_number():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_arc(
        os.path.join(HERE, "test_data", "v_a_20180810_13_1_0_1.fits.gz[0]")
    )
    assert (twodspec.arc == arc_fits.data).all()
    assert twodspec.arc_header == arc_fits.header
Ejemplo n.º 5
0
def test_add_data_numpy_array():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_data(
        img_with_fits.image_fits.data, img_with_fits.image_fits.header
    )
    assert (twodspec.img == img_with_fits.image_fits.data).all()
    assert twodspec.header == img_with_fits.image_fits.header
Ejemplo n.º 6
0
def test_forced_extraction_horne86_lowess():
    lhs6328_twodspec = spectral_reduction.TwoDSpec(
        lhs6328_frame,
        spatial_mask=spatial_mask,
        spec_mask=spec_mask,
        cosmicray=True,
        readnoise=5.7,
        gain=2.45,
        log_level="INFO",
        log_file_folder=os.path.join(HERE, "test_output"),
    )

    lhs6328_twodspec.add_trace(trace, trace_sigma)

    # Force extraction
    lhs6328_twodspec.ap_extract(
        apwidth=15,
        skywidth=10,
        skydeg=1,
        optimal=True,
        model="lowess",
        forced=True,
        variances=horne86_var,
        display=False,
    )

    # Store the forced extracted count
    count_forced = copy.copy(lhs6328_twodspec.spectrum_list[0].count)

    assert (np.nansum(horne86_count) >= np.nansum(count_forced) * 0.999) & (
        np.nansum(horne86_count) <= np.nansum(count_forced) * 1.001)
Ejemplo n.º 7
0
def test_spectral_extraction():

    # masking
    spec_mask = np.arange(10, 900)
    spatial_mask = np.arange(15, 85)

    # initialise the two spectral_reduction.TwoDSpec()
    dummy_twodspec = spectral_reduction.TwoDSpec(
        dummy_data,
        spatial_mask=spatial_mask,
        spec_mask=spec_mask,
        log_file_name=None,
        log_level="DEBUG",
        saxis=1,
        flip=False,
        cosmicray_sigma=5.0,
        readnoise=0.1,
        gain=1.0,
        seeing=1.0,
        exptime=1.0,
    )

    # Trace the spectrum, note that the first 15 rows were trimmed from the
    # spatial_mask
    dummy_twodspec.ap_trace(
        rescale=True,
        ap_faint=0,
        save_fig=True,
        fig_type="iframe+png",
        filename=os.path.join(HERE, "test_output", "test_full_run_aptrace"),
        return_jsonstring=True,
    )
    trace = np.round(np.mean(dummy_twodspec.spectrum_list[0].trace))
    assert trace == 35, ("Trace is at row " + str(trace) +
                         ", but it is expected to be at row 35.")

    # Optimal extracting spectrum by summing over the aperture along the trace
    dummy_twodspec.ap_extract(
        apwidth=5,
        optimal=False,
        filename=os.path.join(HERE, "test_output", "test_full_run_apextract"),
        save_fig=True,
        fig_type="iframe+png",
        return_jsonstring=True,
    )

    count = np.mean(dummy_twodspec.spectrum_list[0].count)
    assert np.round(count).astype("int") == 54, ("Extracted count is " +
                                                 str(count) +
                                                 " but it should be 54.")

    dummy_twodspec.inspect_extracted_spectrum(
        display=False,
        filename=os.path.join(HERE, "test_output",
                              "test_full_run_extracted_spectrum"),
        save_fig=True,
        fig_type="iframe+png",
        return_jsonstring=True,
    )
Ejemplo n.º 8
0
def test_add_data_path():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_data(
        os.path.join(HERE, "test_data", "v_e_20180810_12_1_0_0.fits.gz"))
    data_array = fits.open(
        os.path.join(HERE, "test_data", "v_e_20180810_12_1_0_0.fits.gz"))[0]
    assert (twodspec.img == data_array.data).all()
    assert twodspec.header == data_array.header
Ejemplo n.º 9
0
def test_gauss_vs_lowess_ap_extract():
    twodspec_gauss = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec_gauss.add_data(img)
    twodspec_gauss.ap_trace()
    twodspec_lowess = copy.copy(twodspec_gauss)
    twodspec_gauss.ap_extract(model="gauss")
    twodspec_lowess.ap_extract(model="lowess")
    count_g = np.nansum(twodspec_gauss.spectrum_list[0].count)
    count_l = np.nansum(twodspec_lowess.spectrum_list[0].count)
    assert (count_l > count_g * 0.95) & (count_l < count_g * 1.05)
Ejemplo n.º 10
0
def test_user_supplied_wavelength_twodspec(mock_show):
    # Load the image
    lhs6328_fits = fits.open(
        os.path.join(HERE, "test_data", "v_e_20180810_12_1_0_0.fits.gz")
    )[0]
    spatial_mask = np.arange(50, 200)
    spec_mask = np.arange(50, 1024)

    #
    # Loading two pre-saved spectral traces from a single FITS file.
    #
    lhs6328 = spectral_reduction.TwoDSpec(
        lhs6328_fits,
        spatial_mask=spatial_mask,
        spec_mask=spec_mask,
        cosmicray=True,
        readnoise=2.34,
        log_file_name=None,
    )

    # Trace the spectra
    lhs6328.ap_trace(nspec=2, display=True)

    # Extract the spectra
    lhs6328.ap_extract(apwidth=10, optimal=True, skywidth=10, display=True)

    # Calibrate the 1D spectra
    lhs6328_onedspec = spectral_reduction.OneDSpec(log_file_name=None)
    lhs6328_onedspec.from_twodspec(lhs6328)

    wavelength = np.genfromtxt(
        os.path.join(
            HERE, "test_data", "test_full_run_standard_wavelength.csv"
        )
    )
    # Manually supply wavelengths
    lhs6328_onedspec.add_wavelength([wavelength, wavelength])

    # Inspect reduced spectrum
    lhs6328_onedspec.inspect_reduced_spectrum(display=False)

    # Save as a FITS file
    lhs6328_onedspec.save_fits(
        output="count",
        filename=os.path.join(HERE, "test_output", "user_supplied_wavelength"),
        stype="science",
        overwrite=True,
    )
Ejemplo n.º 11
0
def test_lowess_ap_extract(mock_show):
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_data(img)
    twodspec.ap_trace()
    twodspec.ap_extract(model="lowess")
    twodspec.inspect_extracted_spectrum(
        display=True,
        save_fig=True,
        return_jsonstring=True,
        filename="test/test_output/extracted_spectrum",
    )
    twodspec.inspect_residual(
        display=False,
        save_fig=True,
        return_jsonstring=True,
        filename=os.path.join(HERE, "test_output", "residual_image"),
    )
Ejemplo n.º 12
0
def test_user_supplied_trace():

    spatial_mask = np.arange(50, 200)
    spec_mask = np.arange(50, 1024)

    # Loading a single pre-saved spectral trace.
    lhs6328_extracted = fits.open(
        os.path.join(HERE, "test_data", "test_full_run_science_0.fits"))
    lhs6328_trace = lhs6328_extracted[1].data
    lhs6328_trace_sigma = lhs6328_extracted[2].data

    lhs6328_twodspec = spectral_reduction.TwoDSpec(
        os.path.join(HERE, "test_data", "v_e_20180810_12_1_0_0.fits.gz"),
        spatial_mask=spatial_mask,
        spec_mask=spec_mask,
        readnoise=2.34,
        log_file_name=None,
    )

    lhs6328_twodspec.add_trace(trace=lhs6328_trace,
                               trace_sigma=lhs6328_trace_sigma)

    lhs6328_twodspec.ap_extract(apwidth=15,
                                optimal=True,
                                skywidth=10,
                                skydeg=1,
                                display=False)

    lhs6328_twodspec.save_fits(
        output="count",
        filename=os.path.join(HERE, "test_output",
                              "user_supplied_trace_for_extraction"),
        overwrite=True,
    )

    lhs6328_twodspec.remove_trace()
Ejemplo n.º 13
0
def test_set_all_properties():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_data(img)
    twodspec.set_properties(
        saxis=1,
        variance=10.0,
        spatial_mask=(1, ),
        spec_mask=(1, ),
        flip=True,
        cosmicray=True,
        sigclip=3.0,
        readnoise=10.0,
        gain=2.6,
        seeing=1.7,
        exptime=300.0,
        airmass=1.5,
        verbose=True,
    )
    assert twodspec.saxis == 1
    assert twodspec.waxis == 0
    assert (twodspec.variance == 10.0).all()
    assert twodspec.spatial_mask == (1, )
    assert twodspec.spec_mask == (1, )
    assert twodspec.flip
    assert twodspec.cosmicray
    assert twodspec.readnoise == 10.0
    assert twodspec.gain == 2.6
    assert twodspec.seeing == 1.7
    assert twodspec.exptime == 300.0
    assert twodspec.airmass == 1.5
    assert twodspec.verbose
    # Assert the cosmic ray cleaning is a different copy of the data
    assert twodspec.img is not img.image_fits.data

    # Changing the saxis, and everything else should still be the same
    twodspec.set_properties(
        saxis=0,
        gain=None,
        seeing=None,
        exptime=None,
        airmass=None,
        variance=None,
    )
    assert twodspec.saxis == 0
    assert twodspec.waxis == 1
    assert twodspec.spatial_mask == (1, )
    assert twodspec.spec_mask == (1, )
    assert twodspec.flip
    assert twodspec.cosmicray
    assert twodspec.readnoise == 10.0
    # The gain is now read from the header, so it's 2.45
    assert twodspec.gain == 2.45
    # The seeing is now read from the header, so it's 0.712134
    assert twodspec.seeing == 0.712134
    # The *total* exposure time is now read from the header, so it's 240.
    assert twodspec.exptime == 240.0
    # The airmass is now read from the header, so it's 1.250338
    assert twodspec.airmass == 1.250338
    assert twodspec.verbose

    # Resetting all values to the header values
    twodspec.set_properties(saxis=0, cosmicray=False, variance=None)
    # The readnoise is now set
    twodspec.set_readnoise(20.0)
    # The gain is now set
    twodspec.set_gain(np.pi)
    # The seeing is now set
    twodspec.set_seeing(123.4)
    # The seeing is now set
    twodspec.set_exptime(0.1234)
    # The airmass is now set
    twodspec.set_airmass(1.2345)
    # Asset all the changes
    assert twodspec.readnoise == 20.0
    assert twodspec.gain == np.pi
    assert twodspec.seeing == 123.4
    assert twodspec.exptime == 0.1234
    assert twodspec.airmass == 1.2345

    # Now without resetting, setting all the values based on the given
    # FITS header keyword.
    twodspec.set_readnoise("CRDNOISE")
    # The gain is now read from the header, so it's 2.45
    twodspec.set_gain("GAIN")
    # The seeing is now read from the header, so it's 0.712134
    twodspec.set_seeing("ESTSEE")
    # The exptime is now read from the header, so it's 120.
    twodspec.set_exptime("EXPTIME")
    # The airmass is now read from the header, so it's 1.250338
    twodspec.set_airmass("AIRMASS")
    # Asset all the changes
    assert twodspec.readnoise == 0.0
    assert twodspec.gain == 2.45
    assert twodspec.seeing == 0.712134
    assert twodspec.exptime == 120.0
    assert twodspec.airmass == 1.250338

    twodspec.add_data(img_with_fits)
    # Now set all of them to zeros
    twodspec.set_readnoise(0)
    twodspec.set_gain(0)
    twodspec.set_seeing(0)
    twodspec.set_exptime(0)
    twodspec.set_airmass(0)

    # Now add the header keywords without updating
    twodspec.set_readnoise_keyword("CRDNOISE", append=True, update=False)
    twodspec.set_gain_keyword("GAIN", append=True, update=False)
    twodspec.set_seeing_keyword("ESTSEE", append=True, update=False)
    twodspec.set_exptime_keyword("EXPTIME", append=True, update=False)
    twodspec.set_airmass_keyword("AIRMASS", append=True, update=False)

    # Asset nothing is changed
    assert twodspec.readnoise == 0
    assert twodspec.gain == 0
    assert twodspec.seeing == 0
    assert twodspec.exptime == 0
    assert twodspec.airmass == 0

    # Now update
    twodspec.set_readnoise_keyword("CRDNOISE")
    twodspec.set_gain_keyword("GAIN")
    twodspec.set_seeing_keyword("ESTSEE")
    twodspec.set_exptime_keyword("EXPTIME")
    twodspec.set_airmass_keyword("AIRMASS")

    # Asset all the changes
    assert twodspec.readnoise == 0.0
    assert twodspec.gain == 2.45
    assert twodspec.seeing == 0.712134
    assert twodspec.exptime == 120.0
    assert twodspec.airmass == 1.250338

    # Again, supplying with list instead
    twodspec.set_readnoise_keyword(["CRDNOISE"])
    twodspec.set_gain_keyword(["GAIN"])
    twodspec.set_seeing_keyword(["ESTSEE"])
    twodspec.set_exptime_keyword(["EXPTIME"])
    twodspec.set_airmass_keyword(["AIRMASS"])

    # Asset
    assert twodspec.readnoise == 0.0
    assert twodspec.gain == 2.45
    assert twodspec.seeing == 0.712134
    assert twodspec.exptime == 120.0
    assert twodspec.airmass == 1.250338

    # Again, supplying with numpy.ndarray instead
    twodspec.set_readnoise_keyword(np.array(["CRDNOISE"]))
    twodspec.set_gain_keyword(np.array(["GAIN"]))
    twodspec.set_seeing_keyword(np.array(["ESTSEE"]))
    twodspec.set_exptime_keyword(np.array(["EXPTIME"]))
    twodspec.set_airmass_keyword(np.array(["AIRMASS"]))

    # Asset
    assert twodspec.readnoise == 0.0
    assert twodspec.gain == 2.45
    assert twodspec.seeing == 0.712134
    assert twodspec.exptime == 120.0
    assert twodspec.airmass == 1.250338

    # Again, supplying None
    twodspec.set_readnoise_keyword(None)
    twodspec.set_gain_keyword(None)
    twodspec.set_seeing_keyword(None)
    twodspec.set_exptime_keyword(None)
    twodspec.set_airmass_keyword(None)

    # Asset
    assert twodspec.readnoise == 0
    assert twodspec.gain == 1
    assert twodspec.seeing == 1
    assert twodspec.exptime == 1
    assert twodspec.airmass == 1
Ejemplo n.º 14
0
def test_add_bad_pixel_mask_expect_fail():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_bad_mask(np.polyval)
Ejemplo n.º 15
0
def test_add_bad_pixel_mask_hdu_list():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_bad_mask(fits.HDUList(fits.ImageHDU(bad_mask.astype("int"))))
Ejemplo n.º 16
0
def test_add_bad_pixel_mask_numpy_array():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_bad_mask(bad_mask)
Ejemplo n.º 17
0
def test_add_arc_path_wrong_hdu_number_expect_fail():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_arc(
        os.path.join(HERE, "test_data", "v_a_20180810_13_1_0_1.fits.gz[10]"))
Ejemplo n.º 18
0
def test_add_arc_hdulist():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_arc(arc_in_CCDData)
    assert (twodspec.arc == arc_fits.data).all()
    assert twodspec.arc_header == arc_fits.header
Ejemplo n.º 19
0
def test_add_arc_primaryhdu():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_arc(arc_fits)
    assert (twodspec.arc == arc_fits.data).all()
    assert twodspec.arc_header == arc_fits.header
Ejemplo n.º 20
0
spec_mask = np.arange(50, 1024)

# Science frame
lhs6328_frame = image_reduction.ImageReduction(log_level="DEBUG",
                                               log_file_folder=os.path.join(
                                                   HERE, "test_output"))
lhs6328_frame.add_filelist(
    os.path.join(HERE, "test_data", "sprat_LHS6328.list"))
lhs6328_frame.load_data()
lhs6328_frame.reduce()

lhs6328_twodspec = spectral_reduction.TwoDSpec(
    lhs6328_frame,
    spatial_mask=spatial_mask,
    spec_mask=spec_mask,
    cosmicray=True,
    sigclip=1.0,
    readnoise=5.7,
    log_level="DEBUG",
    log_file_folder=os.path.join(HERE, "test_output"),
)

lhs6328_twodspec.ap_trace(nspec=2, display=False)


# assert the resampled image has the total photon count within 0.1% of the
# input
def test_rectify():
    twodspec = copy.copy(lhs6328_twodspec)
    twodspec.get_rectification(
        bin_size=6,
        n_bin=[2, 4],
Ejemplo n.º 21
0
def test_lowess_ap_extract_upper_detector_edge():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_data(missing_upper_half_spectrum_image_fits)
    twodspec.ap_trace()
    twodspec.ap_extract(model="lowess")
Ejemplo n.º 22
0
def test_add_random_data_expect_fail():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_data(np.polyfit)
Ejemplo n.º 23
0
def test_add_no_data():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_data(None)
Ejemplo n.º 24
0
def test_add_data_hdulist():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_data(img_in_hdulist)
    assert (twodspec.img == img_with_fits.image_fits.data).all()
    assert twodspec.header == img_with_fits.image_fits.header
Ejemplo n.º 25
0
spatial_mask = np.arange(30, 200)
spec_mask = np.arange(50, 1024)

# Science frame
lhs6328_frame = image_reduction.ImageReduction(log_level="INFO",
                                               log_file_name=None)
lhs6328_frame.add_filelist(
    os.path.join(HERE, "test_data", "sprat_LHS6328.list"))
lhs6328_frame.load_data()
lhs6328_frame.reduce()

lhs6328_twodspec = spectral_reduction.TwoDSpec(
    lhs6328_frame,
    spatial_mask=spatial_mask,
    spec_mask=spec_mask,
    cosmicray=True,
    readnoise=5.7,
    log_level="DEBUG",
    log_file_name=None,
)

lhs6328_twodspec.ap_trace(nspec=1, display=False)


def test_tophat_extraction():
    lhs6328_twodspec.ap_extract(optimal=False, display=False)
    lhs6328_twodspec.ap_extract(optimal=False, display=False, spec_id=0)
    lhs6328_twodspec.ap_extract(optimal=False, display=False, spec_id=[0])
    lhs6328_twodspec.ap_extract(
        optimal=False,
        apwidth=[4, 5],
Ejemplo n.º 26
0
def test_add_arc_image_reduction():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_data(img)
    assert (twodspec.arc == arc_fits.data).all()
    assert twodspec.arc_header == arc_fits.header
Ejemplo n.º 27
0
def test_gausian_spectral_extraction_10000s_exptime_2x_gain():
    # masking
    spec_mask = np.arange(10, 900)
    spatial_mask = np.arange(15, 85)

    # initialise the two spectral_reduction.TwoDSpec()
    dummy_twodspec = spectral_reduction.TwoDSpec(
        dummy_gaussian_data,
        spatial_mask=spatial_mask,
        spec_mask=spec_mask,
        log_file_name=None,
        log_level="CRITICAL",
        saxis=1,
        flip=False,
        cosmicray_sigma=5.0,
        readnoise=0.1,
        gain=2.0,
        seeing=1.0,
        exptime=10000.0,
    )

    # Trace the spectrum, note that the first 15 rows were trimmed from the
    # spatial_mask
    dummy_twodspec.ap_trace(
        rescale=True,
        fit_deg=0,
    )
    trace = np.round(np.mean(dummy_twodspec.spectrum_list[0].trace))
    assert np.isclose(trace, 35), ("Trace is at row " + str(trace) +
                                   ", but it is expected to be at row 35.")

    # Direct extraction by summing over the aperture along the trace
    dummy_twodspec.ap_extract(
        apwidth=5,
        optimal=False,
    )
    count = np.mean(dummy_twodspec.spectrum_list[0].count)
    count_err = np.mean(dummy_twodspec.spectrum_list[0].count_err)
    snr_tophat = count / count_err
    assert np.isclose(count, 10000.0, rtol=0.01,
                      atol=count_err), ("Extracted count is " + str(count) +
                                        " but it should be ~10000.")

    # Optimal extraction (Horne86 gauss)
    dummy_twodspec.ap_extract(apwidth=5, optimal=True, model="gauss")
    count = np.mean(dummy_twodspec.spectrum_list[0].count)
    count_err = np.mean(dummy_twodspec.spectrum_list[0].count_err)
    snr_horne = count / count_err
    assert np.isclose(count, 10000.0, rtol=0.01,
                      atol=count_err), ("Extracted count is " + str(count) +
                                        " but it should be ~10000.")

    # Optimal extraction (Horne86 lowess)
    dummy_twodspec.ap_extract(
        apwidth=5,
        optimal=True,
        model="lowess",
        lowess_frac=0.05,
    )
    count = np.mean(dummy_twodspec.spectrum_list[0].count)
    count_err = np.mean(dummy_twodspec.spectrum_list[0].count_err)
    snr_horne = count / count_err
    assert np.isclose(count, 10000.0, rtol=0.01,
                      atol=count_err), ("Extracted count is " + str(count) +
                                        " but it should be ~10000.")

    # Optimal extraction (Marsh89)
    dummy_twodspec.ap_extract(apwidth=5, optimal=True, model="marsh89")
    count = np.mean(dummy_twodspec.spectrum_list[0].count)
    count_err = np.mean(dummy_twodspec.spectrum_list[0].count_err)
    snr_marsh = count / count_err
    assert np.isclose(count, 10000.0, rtol=0.01,
                      atol=count_err), ("Extracted count is " + str(count) +
                                        " but it should be ~10000.")
Ejemplo n.º 28
0
def test_add_arc_numpy_array_and_header_in_list():
    twodspec = spectral_reduction.TwoDSpec(log_file_name=None)
    twodspec.add_arc(arc_fits.data, [arc_fits.header])
    assert (twodspec.arc == arc_fits.data).all()
    assert twodspec.arc_header == arc_fits.header
Ejemplo n.º 29
0
def extract_floyds(light_fits,
                   flat_fits,
                   arc_fits,
                   coeff_red=None,
                   coeff_blue=None):
    light_data = light_fits.data
    light_header = fits.Header(light_fits.header)

    red = spectral_reduction.TwoDSpec(light_data,
                                      header=light_header,
                                      spatial_mask=red_spatial_mask,
                                      spec_mask=red_spec_mask,
                                      cosmicray=True,
                                      sigclip=2.,
                                      readnoise=3.5,
                                      gain=2.3,
                                      log_level='INFO',
                                      log_file_name=None)

    blue = spectral_reduction.TwoDSpec(light_data,
                                       header=light_header,
                                       spatial_mask=blue_spatial_mask,
                                       spec_mask=blue_spec_mask,
                                       cosmicray=True,
                                       sigclip=2.,
                                       readnoise=3.5,
                                       gain=2.3,
                                       log_level='INFO',
                                       log_file_name=None)

    # Add the arcs before rectifying the image, which will apply the
    # rectification to the arc frames too
    blue.add_arc(arc_fits.data, fits.Header(arc_fits.header))
    blue.apply_mask_to_arc()
    red.add_arc(arc_fits.data, fits.Header(arc_fits.header))
    red.apply_mask_to_arc()

    # Get the trace to rectify the image
    red.ap_trace(nspec=1,
                 ap_faint=20,
                 percentile=10,
                 trace_width=50,
                 shift_tol=35,
                 fit_deg=5,
                 display=False)
    red.get_rectification(upsample_factor=5, coeff=coeff_red, display=True)
    red.apply_rectification()
    # Need to store the traces for fringe correction before overwriting them
    # with the new traces
    trace_red = copy.deepcopy(red.spectrum_list[0].trace)
    trace_sigma_red = copy.deepcopy(red.spectrum_list[0].trace_sigma)

    # Get the trace again for the rectified image and then extract
    red.ap_trace(nspec=1,
                 ap_faint=20,
                 percentile=10,
                 trace_width=20,
                 fit_deg=3,
                 display=False)
    red.ap_extract(apwidth=10, spec_id=0, display=False)

    # Do the same with the blue
    blue.ap_trace(nspec=1,
                  ap_faint=10,
                  percentile=10,
                  trace_width=20,
                  shift_tol=50,
                  fit_deg=5,
                  display=False)
    blue.get_rectification(upsample_factor=5, coeff=coeff_blue, display=True)
    blue.apply_rectification()

    blue.ap_trace(nspec=1,
                  percentile=10,
                  trace_width=20,
                  fit_deg=3,
                  display=False)
    blue.ap_extract(apwidth=10, display=False)

    red.extract_arc_spec(spec_width=15, display=False)
    blue.extract_arc_spec(spec_width=15, display=False)

    # Get the blue and red traces for fringe removal
    trace_red_rectified = red.spectrum_list[0].trace
    trace_sigma_red_rectified = red.spectrum_list[0].trace_sigma

    # Extract the red flat
    flat_data = flat_fits.data
    flat_header = fits.Header(flat_fits.header)

    flat = spectral_reduction.TwoDSpec(flat_data,
                                       header=flat_header,
                                       spatial_mask=red_spatial_mask,
                                       spec_mask=red_spec_mask,
                                       cosmicray=True,
                                       readnoise=3.5,
                                       gain=2.3,
                                       log_level='INFO',
                                       log_file_name=None)

    # Force extraction from the flat for fringe correction
    flat.add_trace(trace_red, trace_sigma_red)
    flat.get_rectification(coeff=red.rec_coeff)
    flat.apply_rectification()
    flat.add_trace(trace_red_rectified, trace_sigma_red_rectified)
    flat.ap_extract(apwidth=10, skywidth=0, display=False)

    return red, blue, flat
Ejemplo n.º 30
0
def test_logger():
    twodspec_debug = spectral_reduction.TwoDSpec(
        log_level="DEBUG",
        logger_name="twodspec_debug",
        log_file_name="twodspec_debug.log",
        log_file_folder=os.path.join(HERE, "test_output"),
    )
    twodspec_info = spectral_reduction.TwoDSpec(
        log_level="INFO",
        logger_name="twodspec_info",
        log_file_name="twodspec_info.log",
        log_file_folder=os.path.join(HERE, "test_output"),
    )
    twodspec_warning = spectral_reduction.TwoDSpec(
        log_level="WARNING",
        logger_name="twodspec_warning",
        log_file_name="twodspec_warning.log",
        log_file_folder=os.path.join(HERE, "test_output"),
    )
    twodspec_error = spectral_reduction.TwoDSpec(
        log_level="ERROR",
        logger_name="twodspec_error",
        log_file_name="twodspec_error.log",
        log_file_folder=os.path.join(HERE, "test_output"),
    )
    twodspec_critical = spectral_reduction.TwoDSpec(
        log_level="CRITICAL",
        logger_name="twodspec_critical",
        log_file_name="twodspec_critical.log",
        log_file_folder=os.path.join(HERE, "test_output"),
    )

    twodspec_debug.logger.debug("debug: debug mode")
    twodspec_debug.logger.info("debug: info mode")
    twodspec_debug.logger.warning("debug: warning mode")
    twodspec_debug.logger.error("debug: error mode")
    twodspec_debug.logger.critical("debug: critical mode")

    twodspec_info.logger.debug("info: debug mode")
    twodspec_info.logger.info("info: info mode")
    twodspec_info.logger.warning("info: warning mode")
    twodspec_info.logger.error("info: error mode")
    twodspec_info.logger.critical("info: critical mode")

    twodspec_warning.logger.debug("warning: debug mode")
    twodspec_warning.logger.info("warning: info mode")
    twodspec_warning.logger.warning("warning: warning mode")
    twodspec_warning.logger.error("warning: error mode")
    twodspec_warning.logger.critical("warning: critical mode")

    twodspec_error.logger.debug("error: debug mode")
    twodspec_error.logger.info("error: info mode")
    twodspec_error.logger.warning("error: warning mode")
    twodspec_error.logger.error("error: error mode")
    twodspec_error.logger.critical("error: critical mode")

    twodspec_critical.logger.debug("critical: debug mode")
    twodspec_critical.logger.info("critical: info mode")
    twodspec_critical.logger.warning("critical: warning mode")
    twodspec_critical.logger.error("critical: error mode")
    twodspec_critical.logger.critical("critical: critical mode")

    debug_debug_length = file_len(
        os.path.join(HERE, "test_output", "twodspec_debug.log"))
    debug_info_length = file_len(
        os.path.join(HERE, "test_output", "twodspec_info.log"))
    debug_warning_length = file_len(
        os.path.join(HERE, "test_output", "twodspec_warning.log"))
    debug_error_length = file_len(
        os.path.join(HERE, "test_output", "twodspec_error.log"))
    debug_critical_length = file_len(
        os.path.join(HERE, "test_output", "twodspec_critical.log"))

    assert (debug_debug_length == 5
            ), "Expecting 5 lines in the log file, " + "{} is logged.".format(
                debug_debug_length)
    assert (debug_info_length == 4
            ), "Expecting 4 lines in the log file, " + "{} is logged.".format(
                debug_info_length)
    assert (debug_warning_length == 3
            ), "Expecting 3 lines in the log file, " + "{} is logged.".format(
                debug_warning_length)
    assert (debug_error_length == 2
            ), "Expecting 2 lines in the log file, " + "{} is logged.".format(
                debug_error_length)
    assert (debug_critical_length == 1
            ), "Expecting 1 lines in the log file, " + "{} is logged.".format(
                debug_critical_length)