Skip to content

Commit

Permalink
Add a _has_dataset method to Ion (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtbarnes committed Mar 9, 2024
1 parent 0d26448 commit c015fe1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
19 changes: 12 additions & 7 deletions fiasco/ions.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ def _instance_kwargs(self):
kwargs['abundance'] = self.abundance
return kwargs

def _has_dataset(self, dset_name):
# There are some cases where we need to check for the existence of a dataset
# within a function as opposed to checking for the existence of that dataset
# before entering the function using the decorator approach.
try:
needs_dataset(dset_name)(lambda _: None)(self)
except MissingDatasetException:
return False
else:
return True

def next_ion(self):
"""
Return an `~fiasco.Ion` instance with the next highest ionization stage.
Expand Down Expand Up @@ -1409,13 +1420,7 @@ def free_bound(self,
wavelength = np.atleast_1d(wavelength)
prefactor = (2/np.sqrt(2*np.pi)/(const.h*(const.c**3) * (const.m_e * const.k_B)**(3/2)))
recombining = self.next_ion()
try:
# NOTE: This checks whether the fblvl data is available for the
# recombining ion
needs_dataset('fblvl')(lambda _: None)(recombining)
omega_0 = recombining._fblvl['multiplicity'][0]
except MissingDatasetException:
omega_0 = 1.0
omega_0 = recombining._fblvl['multiplicity'][0] if recombining._has_dataset('fblvl') else 1.0
E_photon = const.h * const.c / wavelength
energy_temperature_factor = np.outer(self.temperature**(-3/2), E_photon**5)
# Fill in observed energies with theoretical energies
Expand Down
9 changes: 9 additions & 0 deletions fiasco/tests/test_ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,12 @@ def test_new_instance_abundance_preserved_string(ion):
new_ion = ion._new_instance()
assert u.allclose(new_ion.abundance, 2.818382931264455e-05)
assert new_ion._dset_names['abundance'] == 'sun_photospheric_2007_grevesse'


def test_has_dataset(ion, c6):
# Fe 5 has energy level data
assert ion._has_dataset('elvlc')
# Fe 5 has no proton data
assert not ion._has_dataset('psplups')
# C VI has no dielectronic data
assert not c6._has_dataset('dielectronic_elvlc')

0 comments on commit c015fe1

Please sign in to comment.