ChiantiPy icon indicating copy to clipboard operation
ChiantiPy copied to clipboard

argCheck method prevents creating ion objects by name only

Open andrewfullard opened this issue 1 year ago • 4 comments

We would like to use the latest ChiantiPy for the package Carsus https://github.com/tardis-sn/carsus. However, in the process of upgrading, I found that it is no longer possible to create an ion object without a defined temperature. Is this intended? The ion object states the temperature is optional, and it is None by default.

Output is:

self = <carsus.io.tests.test_chianti.TestChiantiIonReader object at 0x7f49994531d0>
request = <SubRequest 'ch_ion_reader' for <Function test_chianti_bound_levels[ne_2]>>
    @pytest.fixture(scope="class", params=["ne_2", "n_5"])
    def ch_ion_reader(self, request):
>       return ChiantiIonReader(request.param)
carsus/io/tests/test_chianti.py:9: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
carsus/io/chianti_/chianti_.py:112: in __init__
    self.ion = ch.ion(ion_name)
../miniforge3/envs/carsus/lib/python3.12/site-packages/ChiantiPy/core/Ion.py:160: in __init__
    self.argCheck(temperature, eDensity, pDensity, em)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self = <ChiantiPy.core.Ion.ion object at 0x7f4998f2b440>, temperature = None
eDensity = None, pDensity = 'default', em = None, verbose = 0
    def argCheck(self, temperature=None, eDensity=None, pDensity='default', em = None,  verbose=0):
        ''' to check the compatibility of the three arguments
        and put them into numpy arrays of atleast_1d
        and create attributes to the object
        '''
        if temperature is not None:
            self.Temperature = np.atleast_1d(temperature)
            if isinstance(self.Temperature[0], str):
                raise ValueError(' temperature can not be a string')
            if np.any(self.Temperature <= 0.):
                raise ValueError(' all temperatures must be positive')
            self.Ntemp = self.Temperature.size
        else:
>           raise ValueError('temperature not defined')
E           ValueError: temperature not defined
../miniforge3/envs/carsus/lib/python3.12/site-packages/ChiantiPy/base/_IonTrails.py:33: ValueError

andrewfullard avatar Sep 09 '24 18:09 andrewfullard

I will look into it. I used to work that way

kdere avatar Sep 09 '24 19:09 kdere

There is a workaround- give the ion class a temperature of 1 as an init value.

andrewfullard avatar Sep 10 '24 01:09 andrewfullard

If that works for you, I will not make any changes just yet

kdere avatar Sep 10 '24 13:09 kdere

It's not ideal, but it seems to pass our tests

andrewfullard avatar Sep 10 '24 15:09 andrewfullard

I have updated the ion class so that if you invoke:

fe14 = ch.ion('fe_14', setup=False)

then no error occurs but the various attributes are available.

I have not yet done a new release but will do one in the near future

kdere avatar Dec 05 '24 17:12 kdere

Thank you! That's greatly appreciated.

andrewfullard avatar Dec 05 '24 18:12 andrewfullard