OttoStruve / muler

A Python package for working with pipeline-produced spectra from IGRINS, HPF, and Keck NIRSPEC
https://muler.readthedocs.io
MIT License
14 stars 9 forks source link

Band math for EchelleSpectrumList objects does not work with single values #120

Closed kfkaplan closed 1 year ago

kfkaplan commented 2 years ago

When you try to add, subtract, multiply, or divide an EchelleSpectrumList object by a single value, it doesn't work.

For example from the EchelleSpectrumList class in echelle.py:

    def __add__(self, other):
        """Bandmath addition"""
        spec_out = copy.deepcopy(self)
        for i in range(len(spec_out)):
            spec_out[i] = spec_out[i] + other[i]
            # if "x_values" not in spec_out[i].meta:
            #    spec_out[i].meta["x_values"] = self[i].meta["x_values"]
        return spec_out

expects other to be another EchelleSpectrumList object and not just a single value such as 1.0.

A simple solution is to check the length of other and act accordingly. Something like:

    def __add__(self, other):
        """Bandmath addition"""
        spec_out = copy.deepcopy(self)
        other_is_single = np.size(other) == 1
        for i in range(len(spec_out)):
            if other_is_single:
                spec_out[i] = spec_out[i] + other
            else:
                spec_out[i] = spec_out[i] + other[i]
            # if "x_values" not in spec_out[i].meta:
            #    spec_out[i].meta["x_values"] = self[i].meta["x_values"]
        return spec_out

I will submit a pull request once I finish coding all this up.

kfkaplan commented 2 years ago

Updated band math code for EchelleSpectrumList objects to fix the above issue. Also cleaned up how "x_values" is passed to increase compatibility. Added tests for the band math for the IGRINS and HPF test data. I have folded all of this into the fix_igrins_uncertainity branch and the pull request https://github.com/OttoStruve/muler/pull/89

kfkaplan commented 1 year ago

Fixed with pull request #89