|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" Represent atomic transitions. """ |
| 5 | + |
| 6 | +from __future__ import absolute_import, print_function |
| 7 | + |
| 8 | +__all__ = ["AtomicTransition"] |
| 9 | +__author__ = "Andy Casey <[email protected]>" |
| 10 | + |
| 11 | +import json |
| 12 | +import yaml |
| 13 | + |
| 14 | +from numpy import log, nan |
| 15 | + |
| 16 | + |
| 17 | +class AtomicTransition(object): |
| 18 | + |
| 19 | + _DEFAULTS = {} |
| 20 | + |
| 21 | + def __init__(self, wavelength, species, e_low, log_gf, mask=None, **kwargs): |
| 22 | + """ |
| 23 | + Initialise the class. |
| 24 | + """ |
| 25 | + |
| 26 | + self.wavelength = float(wavelength) |
| 27 | + self.species = float(species) |
| 28 | + self.e_low = float(e_low) |
| 29 | + self.log_gf = float(log_gf) |
| 30 | + self.mask = mask |
| 31 | + |
| 32 | + self._raw = self._DEFAULTS.copy() |
| 33 | + self._raw.update({ |
| 34 | + "wavelength": self.wavelength, |
| 35 | + "species": self.species, |
| 36 | + "e_low": self.e_low, |
| 37 | + "log_gf": self.log_gf, |
| 38 | + "mask": self.mask |
| 39 | + }) |
| 40 | + self._raw.update(kwargs) |
| 41 | + |
| 42 | + def __str__(self): |
| 43 | + return unicode(self).encode("utf-8").strip() |
| 44 | + |
| 45 | + def __unicode__(self): |
| 46 | + return u"<{element} {ion} at {wavelength:.2f} Å>".format( |
| 47 | + element=self.element, ion="I"*self.ion, wavelength=self.wavelength) |
| 48 | + |
| 49 | + def __repr__(self): |
| 50 | + return "<oracle.transitions.{klass} {element} {ion} at {wavelength:.1f}"\ |
| 51 | + " Angstroms at {location}>".format(klass=self.__class__.__name__, |
| 52 | + element=self.element, ion="I" * self.ion, |
| 53 | + wavelength=self.wavelength, location=hex(id(self))) |
| 54 | + |
| 55 | + def to_json(self, **kwargs): |
| 56 | + return json.dumps(self._raw, **kwargs) |
| 57 | + |
| 58 | + def to_yaml(self, **kwargs): |
| 59 | + return yaml.dump(self._raw, **kwargs) |
| 60 | + |
| 61 | + @property |
| 62 | + def element(self): |
| 63 | + periodictable = """H He |
| 64 | + Li Be B C N O F Ne |
| 65 | + Na Mg Al Si P S Cl Ar |
| 66 | + K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr |
| 67 | + Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe |
| 68 | + Cs Ba Lu Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn |
| 69 | + Fr Ra Lr Rf Db Sg Bh Hs Mt Ds Rg Cn UUt""" |
| 70 | + |
| 71 | + lanthanoids = "La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb" |
| 72 | + actinoids = "Ac Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No" |
| 73 | + |
| 74 | + periodictable = periodictable.replace("Ba ", "Ba " + lanthanoids) \ |
| 75 | + .replace("Ra ", "Ra " + actinoids).split() |
| 76 | + return periodictable[self.atomic_number - 1] |
| 77 | + |
| 78 | + @property |
| 79 | + def atomic_number(self): |
| 80 | + return int(self.species) |
| 81 | + |
| 82 | + @property |
| 83 | + def ion(self): |
| 84 | + return int((self.species % 1) * 10) + 1 |
| 85 | + |
| 86 | + @property |
| 87 | + def equivalent_width(self): |
| 88 | + return self._raw.get("equivalent_width", nan) |
| 89 | + |
| 90 | + @equivalent_width.setter |
| 91 | + def equivalent_width(self, value): |
| 92 | + self._raw["equivalent_width"] = value |
| 93 | + |
| 94 | + @property |
| 95 | + def log_eps(self): |
| 96 | + return self._raw.get("log_eps", nan) |
| 97 | + |
| 98 | + @log_eps.setter |
| 99 | + def log_eps(self, value): |
| 100 | + self._raw["log_eps"] = value |
| 101 | + |
| 102 | + @property |
| 103 | + def reduced_equivalent_width(self): |
| 104 | + """ |
| 105 | + Return the reduced equivalent width of the line, if the equivalent width |
| 106 | + has been measured. |
| 107 | +
|
| 108 | + The reduced equivalent width is defined by: |
| 109 | +
|
| 110 | + >> REW = log(equivalent width/wavelength) |
| 111 | +
|
| 112 | + :raise AttributeError: |
| 113 | + If no equivalent width has been measured for this line. |
| 114 | + """ |
| 115 | + return log(self.equivalent_width/self.wavelength) |
| 116 | + |
| 117 | + # Some default line behaviour that we don't want passed to _raw unless |
| 118 | + # explicitly defined. |
| 119 | + @property |
| 120 | + def continuum_degree(self): |
| 121 | + return self._raw.get("continuum_degree", 0) # Assumes normalised spectra |
| 122 | + |
| 123 | + @continuum_degree.setter |
| 124 | + def continuum_degree(self, value): |
| 125 | + self._raw["continuum_degree"] = value |
| 126 | + |
| 127 | + @property |
| 128 | + def fitting_region(self): |
| 129 | + return self._raw.get("fitting_region", |
| 130 | + [self.wavelength - 1.5, self.wavelength + 1.5]) |
| 131 | + |
| 132 | + @fitting_region.setter |
| 133 | + def fitting_region(self, value): |
| 134 | + self._raw["fitting_region"] = value |
| 135 | + |
| 136 | + @property |
| 137 | + def v_rad_tolerance(self): |
| 138 | + return self._raw.get("v_rad_tolerance", 1.0) # km/s |
| 139 | + |
| 140 | + @v_rad_tolerance.setter |
| 141 | + def v_rad_tolerance(self, value): |
| 142 | + self._raw["v_rad_tolerance"] = value |
0 commit comments