Exemple #1
0
def real_spec(request):
    band = request.param
    wav, flux = load_aces_spectrum([3900, 4.5, 0.0, 0])
    wav, flux = wav_selector(wav, flux, *band_limits(band))
    flux = flux / snr_constant_band(wav, flux, 100, band)
    atm = Atmosphere.from_band(band).at(wav)
    return wav, flux, atm.transmission
Exemple #2
0
def testing_spectrum(request, use_test_config):
    wav, flux = load_aces_spectrum(request.param)
    return wav, flux
Exemple #3
0
def testing_spectrum(request):
    wav, flux = load_aces_spectrum(request.param)
    return wav, flux
Exemple #4
0
def test_invalid_load_aces_spectrum(params):
    with pytest.raises(ValueError):
        load_aces_spectrum(params, wl_range=[21000, 22000])
Exemple #5
0
def test_load_aces_spectrum(photons, use_test_config):
    wav, flux = load_aces_spectrum([3900, 4.5, 0, 0],
                                   photons=photons,
                                   air=False,
                                   wl_range=[21000, 22000])
    assert len(wav) == len(flux)
Exemple #6
0
def do_analysis(
    star_params,
    vsini: float,
    R: float,
    band: str,
    sampling: float = 3.0,
    conv_kwargs=None,
    snr: float = 100.0,
    ref_band: str = "J",
    rv: float = 0.0,
    air: bool = False,
    model: str = "aces",
    verbose: bool = False,
) -> Tuple[Quantity, ...]:
    """Calculate RV precision and Quality for specific parameter set.

    Parameters
    ----------
    star_param:
        Stellar parameters [temp, logg, feh, alpha] for phoenix model libraries.
    vsini: float
       Stellar equatorial rotation.
    R: float
        Instrumental resolution.
    band: str
        Spectral band.
    sampling: float (default=False)
        Per pixel sampling (after convolutions)
    conv_kwargs: Dict (default=None)
        Arguments specific for the convolutions,
        'epsilon', 'fwhm_lim', 'num_procs', 'normalize', 'verbose'.
    snr: float (default=100)
        SNR normalization level. SNR per pixel and the center of the ref_band.
    ref_band: str (default="J")
        Reference band for SNR normalization.
    rv: float
        Radial velocity in km/s (default = 0.0).
    air: bool
        Get model in air wavelengths (default=False).
    model: str
        Name of synthetic library (aces, btsettl) to use. Default = 'aces'.
    verbose:
        Enable verbose (default=False).

    Returns
    -------
    q: astropy.Quality
     Spectral quality.
    result_1: astropy.Quality
        RV precision under condition 1.
    result_2 : astropy.Quality
        RV precision under condition 2.
    result_3: astropy.Quality
        RV precision under condition 3.

    Notes
    -----
        We apply the radial velocity doppler shift after
            - convolution (rotation and resolution)
            - resampling
            - SNR normalization.

        in this way the RV only effects the precision due to the telluric mask interaction.
        Physically the RV should be applied between the rotational and instrumental convolution
        but we assume this effect is negligible.

    """
    if conv_kwargs is None:
        conv_kwargs = {
            "epsilon": 0.6,
            "fwhm_lim": 5.0,
            "num_procs": num_cpu_minus_1,
            "normalize": True,
            "verbose": verbose,
        }

    if ref_band.upper() == "SELF":
        ref_band = band

    model = check_model(model)

    if model == "aces":
        wav, flux = load_aces_spectrum(star_params, photons=True, air=air)
    elif model == "btsettl":
        wav, flux = load_btsettl_spectrum(star_params, photons=True, air=air)
    else:
        raise Exception("Invalid model name reached.")

    wav_grid, sampled_flux = convolve_and_resample(wav, flux, vsini, R, band,
                                                   sampling, **conv_kwargs)

    # Doppler shift
    try:
        if rv != 0:
            sampled_flux = doppler_shift_flux(wav_grid, sampled_flux, vel=rv)
    except Exception as e:
        print("Doppler shift was unsuccessful")
        raise e

    # Scale normalization for precision
    wav_ref, sampled_ref = convolve_and_resample(wav, flux, vsini, R, ref_band,
                                                 sampling, **conv_kwargs)
    snr_normalize = snr_constant_band(wav_ref,
                                      sampled_ref,
                                      snr=snr,
                                      band=ref_band,
                                      sampling=sampling,
                                      verbose=verbose)
    sampled_flux = sampled_flux / snr_normalize

    if (ref_band == band) and verbose:
        mid_point = band_middle(ref_band)
        index_ref = np.searchsorted(
            wav_grid,
            mid_point)  # searching for the index closer to 1.25 micron
        snr_estimate = np.sqrt(
            np.sum(sampled_flux[index_ref - 1:index_ref + 2]))
        print(
            "\tSanity Check: The S/N at {0:4.02} micron = {1:4.2f}, (should be {2:g})."
            .format(mid_point, snr_estimate, snr))

    # Load Atmosphere for this band.
    atm = Atmosphere.from_band(band=band, bary=True).at(wav_grid)
    assert np.allclose(atm.wl,
                       wav_grid), "The atmosphere does not cover the wav_grid"

    # Spectral Quality/Precision
    q = quality(wav_grid, sampled_flux)

    # Precision given by the first condition:
    result_1 = rv_precision(wav_grid, sampled_flux, mask=None)

    # Precision as given by the second condition
    result_2 = rv_precision(wav_grid, sampled_flux, mask=atm.mask)

    # Precision as given by the third condition: M = T**2
    result_3 = rv_precision(wav_grid, sampled_flux, mask=atm.transmission**2)

    # Turn quality back into a Quantity (to give it a .value method)
    q = q * u.dimensionless_unscaled
    return q, result_1, result_2, result_3
Exemple #7
0
def do_analysis(
    star_params,
    vsini: float,
    R: float,
    band: str,
    sampling: float = 3.0,
    conv_kwargs=None,
    snr: float = 100.0,
    ref_band: str = "J",
    rv: float = 0.0,
    air: bool = False,
    model="phoenix",
):
    """Precision and Quality for specific parameter set.

    Parameters
    ----------
    air: bool
        Get model in air wavelengths.
    model: str
        Name of synthetic library to use. (phoenix, btsettl).
    rv: float
        Radial velocity.

    Notes:
        We apply the radial velocity doppler shift after
           - convolution (rotation and resolution)
           - resampling
           - SNR normalization.
        in this way the RV only effects the precision due to the telluric mask interaction.
        The RV should maybe come between the rotational and instrumental convolution
        but we assume this effect is negligible.
    """
    if conv_kwargs is None:
        conv_kwargs = {
            "epsilon": 0.6,
            "fwhm_lim": 5.0,
            "num_procs": num_procs_minus_1,
            "normalize": True,
        }

    if ref_band.upper() == "SELF":
        ref_band = band

    if model == "phoenix":
        # Full photon count spectrum
        wav, flux = load_aces_spectrum(star_params, photons=True, air=air)
    elif model == "btsettl":
        wav, flux = load_btsettl_spectrum(star_params, photons=True, air=air)
    else:
        raise ValueError(
            "Model name error in '{}'. Valid choices are 'phoenix and 'btsettl'"
            .format(model))

    wav_grid, sampled_flux = convolve_and_resample(wav, flux, vsini, R, band,
                                                   sampling, **conv_kwargs)

    # Doppler shift
    try:
        if rv != 0:
            sampled_flux = doppler_shift_flux(wav_grid, sampled_flux, vel=rv)
    except Exception as e:
        print("Doppler shift was unsuccessful")
        raise e

    # Spectral Quality
    q = quality(wav_grid, sampled_flux)

    # Scale normalization for precision
    wav_ref, sampled_ref = convolve_and_resample(wav, flux, vsini, R, ref_band,
                                                 sampling, **conv_kwargs)
    snr_normalize = snr_constant_band(wav_ref,
                                      sampled_ref,
                                      snr=snr,
                                      band=ref_band,
                                      sampling=sampling)
    sampled_flux = sampled_flux / snr_normalize

    if ref_band == band:
        mid_point = band_middle(ref_band)
        index_ref = np.searchsorted(
            wav_grid,
            mid_point)  # searching for the index closer to 1.25 micron
        snr_estimate = np.sqrt(
            np.sum(sampled_flux[index_ref - 1:index_ref + 2]))
        print(
            "\tSanity Check: The S/N at {0:4.02} micron = {1:4.2f}, (should be {2:g})."
            .format(mid_point, snr_estimate, snr))

    # Load Atmosphere for this band.
    atm = Atmosphere.from_band(band=band, bary=True).at(wav_grid)
    assert np.allclose(atm.wl,
                       wav_grid), "The atmosphere does not cover the wav_grid"

    # Spectral Quality/Precision
    q = quality(wav_grid, sampled_flux)

    # Precision given by the first condition:
    prec1 = rv_precision(wav_grid, sampled_flux, mask=None)

    # Precision as given by the second condition
    prec2 = rv_precision(wav_grid, sampled_flux, mask=atm.mask)

    # Precision as given by the third condition: M = T**2
    prec3 = rv_precision(wav_grid, sampled_flux, mask=atm.transmission**2)

    # Turn quality back into a Quantity (to give it a .value method)
    q = q * u.dimensionless_unscaled
    return [q, prec1, prec2, prec3]
Exemple #8
0
def test_invalid_load_aces_spectrum(params):
    # Invalid CIFIST parameters
    from Starfish.constants import GridError

    with pytest.raises(GridError):
        load_aces_spectrum(params, wl_range=[21000, 22000])