zkbt / chromatic

Tools for visualizing spectrosopic light curves, with flux as a function of wavelength and time.
MIT License
14 stars 4 forks source link

Calculate `'effective_wavelength'` when binning in wavelength. #248

Closed zkbt closed 2 months ago

zkbt commented 2 months ago

To address #245 , this calculates an inverse-variance weighted average wavelength value for each wavelength bin, whenever binning a Rainbow in wavelength. It can be retrieved either through the wavelike dictionary or as an attribute, like this:

binned = rainbow.bin(dw=1*u.micron)
binned.wavelike['effective_wavelength'], binned.effective_wavelength

This effective wavelength is a better represented average to use when plotting comparisons to models in wavelength space, as shown in the text example below.

def test_binning_effective_wavelength():
    N = 101
    blank = SimulatedRainbow(wavelength=np.linspace(5, 10, N)*u.micron)
    with_slope = blank*(1 + np.linspace(-1, 1, N)*1e-1)
    noisy = with_slope.inject_noise(signal_to_noise=np.logspace(2, 5, N))
    binned = noisy.bin(dw=1*u.micron)

    i = 0
    ekw = dict(marker='o', linewidth=0, elinewidth=1)
    #plt.plot(s.wavelength, s.model[:,i])
    plt.errorbar(noisy.wavelength, noisy.flux[:,i], noisy.uncertainty[:,i], color='gray', alpha=0.25, **ekw)
    plt.errorbar(binned.wavelength, binned.flux[:,i], binned.uncertainty[:,i], color='red', label='unweighted', **ekw)
    plt.errorbar(binned.effective_wavelength, binned.flux[:,i], binned.uncertainty[:,i], color='black', label='weighted', **ekw)
    plt.plot(noisy.wavelength, noisy.model[:,0]);
    plt.legend()

image

zkbt commented 2 months ago

@Pat-Wachiraphan , sorry, I realized it was slightly more complicated to put this upstream in bintogrid because it needed to have access to the wavelength-integrated uncertainty, so I just put it in rainbow.bin_to_wavelength. If we need it there (which I don't think we need for binning the stellar spectrum, it's really just to figure out how to put the depths at the right wavelength), please open another Issue and we'll work on it there!

zkbt commented 2 months ago

@Pat-Wachiraphan , I merged this into develop!