I have a grid of model spectra, which have a constant, very high spectral resolution, and I need to down-sample them to a lower resolution, while preserving the total number of counts.
In essence, if the first 5 bins have (nominal center-of-bin) wavelengths [7.8, 7.81, 7.82, 7.83, 7.84]
, and the values [1.01, 1.02, 1.015, 1.014, 1.02]
, and my desired bins are some (non-integer) factor (say, 2.5) times as wide, I want my new spectrum to have nominal wavelengths [7.81, 7.83]
and values [1.01+1.02+0.5*1.015, 0.5*1.015+1.014+1.02]
(in general, though, the bins are not lined up as well, so you may get fractions of bins on either side).
I'll call my grid spec_ssp
, and it has a shape
of (93, 16, 39848)
. Wavelength varies along axis 2, and the first two axes are other parameters. I also have the nominal (central) wavelengths for each wavelength bin (technically, they're the log of the wavelength, but that shouldn't matter), called logL_ssp
, and the desired new spacing of the logL grid, dlogL_new
. I can figure out the nominal logL spacing of my templates dlogL_ssp
by calculating np.median(logL_ssp[1:] - logL_ssp[:-1])
, and it's about 20% the desired logL spacing. We'll call that fraction f
.
I originally tried to use scipy.ndimage.zoom
, using the aforementioned factor f
, but discovered that it gives me an array that's downsampled by a factor of exactly 4. I need an exact resampling, so this won't work.
Next, I tried linearly interpolating along axis 2 using np.interp1d
, after setting up new bin limits, with the aim of integrating the spectra in my grid using np.integrate.quad
between successive bin limits, effectively getting an estimate of the total light in each of my new bins, more or less rigorously. However, quad
doesn't play nicely with interp1d
's interpolators (quad
doesn't like non-float inputs). And, since I have ~1500 model spectra, the whole thing takes ages to run while iterating over all three axes (yes, I'm only making a new interpolator once per model spectrum).
Any ideas how to tackle this?