def fake_obs(simnum, snr=200, suffix=None, plot=False, mode="iam"):
    snr = 200

    params1 = [5300, 4.5, 0.0]
    params2 = [2500, 4.5, 0.0]
    gamma = 5
    rv = -3
    normalization_limits = [2100, 2180]

    mod1_spec = load_starfish_spectrum(params1,
                                       limits=normalization_limits,
                                       hdr=True,
                                       normalize=False,
                                       area_scale=True,
                                       flux_rescale=True)

    mod2_spec = load_starfish_spectrum(params2,
                                       limits=normalization_limits,
                                       hdr=True,
                                       normalize=False,
                                       area_scale=True,
                                       flux_rescale=True)

    if broadcast:
        broadcast_result = inherent_alpha_model(mod1_spec.xaxis,
                                                mod1_spec.flux,
                                                mod2_spec.flux,
                                                rvs=rv,
                                                gammas=gamma)

        broadcast_values = broadcast_result(obs_spec.xaxis)
        result_spectrum = Spectrum(flux=broadcast_values,
                                   xaxis=mod1_spec.xaxis)
    else:
        # Manually redo the join
        mod2_spec.doppler_shift(rv)
        mod_combine = mod1_spec.copy()
        mod_combine += mod2_spec
        mod_combine.doppler_shift(gamma)
        mod_combine.normalize(method="exponential")
        mod_combine.interpolate(obs_spec.xaxis)
        result_spectrum = mod_combine
        # result_spectrum = Spectrum(flux=broadcast_values, xaxis=obs_spec.xaxis)

    result_spectrum.add_noise(snr)

    # Save as
    # Detector limits
    dect_limits = [(2112, 2123), (2127, 2137), (2141, 2151), (2155, 2165)]

    for ii, dect in enumerate(dect_limits):
        spec = result_spectrum.copy()
        spec.wav_select(*dect)
        spec.resample(1024)

        name = "HDsim-{0}_{1}_snr_{2}".format(sim_num, ii, snr)
def save_fake_observation(spectrum: Spectrum,
                          star: str,
                          sim_num: int,
                          params1: str,
                          params2: Optional[str] = None,
                          gamma: Optional[int] = None,
                          rv: Optional[int] = None,
                          noise: None = None,
                          replace: bool = False,
                          noplots: bool = False) -> None:
    # Detector limits
    detector_limits = [(2112, 2123), (2127, 2137), (2141, 2151), (2155, 2165)]
    npix = 1024

    header = fits.Header.fromkeys({})
    for ii, detector in enumerate(detector_limits):
        spec = spectrum.copy()
        spec.wav_select(*detector)
        spec.interpolate1d_to(np.linspace(spec.xaxis[0], spec.xaxis[-1], npix))

        if not noplots:
            plt.plot(spec.xaxis, spec.flux)
            plt.title("Fake spectrum {0} {1} detector {2}".format(
                star, sim_num, ii + 1))
            plt.show()
        name = obs_name_template().format(star, sim_num, ii + 1)
        # name = "{0}-{1}-mixavg-tellcorr_{2}.fits".format(star, sim_num, ii + 1)
        name = os.path.join(simulators.paths["spectra"], name)
        # spec.save...
        hdrkeys = [
            "OBJECT", "Id_sim", "num", "chip", "snr", "c_gamma", "cor_rv",
            "host", "compan"
        ]
        hdrvals = [
            star, "Fake simulation data", sim_num, ii + 1, noise, gamma, rv,
            params1, params2
        ]
        if os.path.exists(name) and not replace:
            print(name, "Already exists")
        else:
            if os.path.exists(name):
                print("Replacing {0}".format(name))
                os.remove(name)
            export_fits(name, spec.xaxis, spec.flux, header, hdrkeys, hdrvals)
            print("Saved fits to {0}".format(name))
next_spec.plot(linestyle=":", label="NEXTGEN")
#plt.plot(w_dusty_fits, f_dusty_fits/max(f_dusty_fits), label="Dusty fits")

plt.title("HD30501 host - 5200K")
plt.xlabel("Wavelength(nm)")
plt.ylabel("Flux")
plt.xlim([1012, 1013])
plt.legend()
plt.show()

# In[ ]:

# Adjust resolution to R=100000
R = 100000
from convolve_spectrum import convolve_spectrum
old_spec = aces_spec.copy()

next_spec = convolve_spectrum(
    next_spec,
    chip_limits=[next_spec.xaxis[0], next_spec.xaxis[-1]],
    R=R,
    plot=False)
dusty_spec = convolve_spectrum(
    dusty_spec,
    chip_limits=[dusty_spec.xaxis[0], dusty_spec.xaxis[-1]],
    R=R,
    plot=False)
settl_spec = convolve_spectrum(
    settl_spec,
    chip_limits=[settl_spec.xaxis[0], settl_spec.xaxis[-1]],
    R=R,