jdaviz
jdaviz copied to clipboard
Flux unit conversion behavior with arrays of different sizes and scalars
This is a spin-off from a discussion in https://github.com/spacetelescope/jdaviz/pull/2889#discussion_r1610076337
The unit conversion class UnitConverterWithSpectral can deal with spectral density conversions, e.g. Fnu to Flam and so on. However, these conversions require knowledge about the wavelength/frequency at which to do the conversion. The unit conversion class knows about the spectral axis of the data that is being converted, so when we convert an array of values where the length of the array is the same as that of the spectral axis, everything is fine. However, there are cases where we might end up converting an array with a different size - for example sometimes to_unit is called with two values, when converting the min/max limits of the axes. In this case, it's not 100% clear what should happen, but there are a couple of options:
- Convert using the first wavelength/frequency in the spectral axis array
- Convert twice - using both the first and last value in the spectral axis array and then determining the smallest and largest result, which will ensure that the limits cover the whole span of resulting values.
I think the second option is probably preferable? Currently https://github.com/spacetelescope/jdaviz/pull/2889 implements the first but I can do a follow-up PR after to switch to the second if we want.
To identify, when to_unit is being called for limits, we have to rely on checking that the length of the values passed is 2. Obviously there is a corner case where this will cause an issue which is if someone loads in a real spectrum with only two wavelengths/frequencies, but I think we can probably agree that's going to be very rare?
Then the question is how we deal with scalars or arrays that don't have the same shape as the spectral axis. These should probably just raise an exception since they aren't valid, but currently exceptions seem to just get swallowed up and aren't apparent to the user. Are we ok with that? In principle I don't think arrays with the wrong length should ever happen (besides length 2 as mentioned above) but it would be good to properly raise an error if they do.