def test_roman(): # out of limits with pytest.raises(OutOfRangeError): s = roman.toRoman(6000) # not integer with pytest.raises(NotIntegerError): s = roman.toRoman(1.5) # no input with pytest.raises(InvalidRomanNumeralError): s = roman.fromRoman(None) # bad pattern with pytest.raises(InvalidRomanNumeralError): s = roman.fromRoman('YT')
def name_ion(ion): """ Convert string into ion tuple Parameters ---------- ion : str Name of the ion, e.g. 'SiII' or 'Si II' Returns ------- ion_tup : tuple Z, ion -- e.g. (14,2) """ if isinstance(ion,basestring): pass else: raise ValueError('ionization.name_ion: Not ready for this input yet.') ion = ion.strip('*') # e.g. CII* if ion[1] in ['I','V', 'X', ' ']: iion = 1 else: iion = 2 # Element elm = ion[0:iion] if elm == 'D': # Deuterium Z = 1 else: Z = ELEMENTS[ion[0:iion]].number # Ion ion_state = roman.fromRoman(ion[iion:].strip()) return Z, ion_state
def name_to_ion(ion): """ Convert string into ion tuple Parameters ---------- ion : str Name of the ion, e.g. 'SiII' or 'Si II' Returns ------- ion_tup : tuple Z, ion -- e.g. (14,2) """ if isinstance(ion,basestring): pass else: raise ValueError('ionization.name_ion: Not ready for this input yet.') ion = ion.strip('*') # e.g. CII* if ion[1] in ['I','V', 'X', ' ']: iion = 1 else: iion = 2 # Element elm = ion[0:iion] if elm == 'D': # Deuterium Z = 1 else: Z = ELEMENTS[ion[0:iion]].number # Ion ion_state = roman.fromRoman(ion[iion:].strip()) return Z, ion_state
def parse_morton03(orig=False, tab_fil=None, HIcombine=True): """Parse tables from Morton 2003, ApJS, 149, 205 Parameters ---------- orig : bool, optional Use original code to parse the ASCII file tab_fil : str, optional Filename to use. Default = /data/lines/morton03_table2.dat HIcombine : bool, optional Combine doublet for HI [True] Returns ------- data : Table Atomic data """ # Look for FITS fitsf = lt_path + '/data/lines/morton03_table2.fits.gz' morton03_tab2 = glob.glob(fitsf) if (len(morton03_tab2) > 0) & (not orig): print('linetools.lists.parse: Reading linelist --- \n {:s}'.format( morton03_tab2[0])) data = Table(Table.read(morton03_tab2[0]), masked=True) else: ## Read Table 2 if tab_fil is None: morton03_tab2 = lt_path + '/data/lines/morton03_table2.dat' else: morton03_tab2 = tab_fil print( 'linetools.lists.parse: Reading linelist --- \n {:s}'.format( morton03_tab2)) f = open(morton03_tab2, 'r', encoding="ISO-8859-1") lines = f.readlines() f.close() ## Find Elements and Ions elmi = [] elmZ = [] elmc = [] ioni = [] isoi = [] ionv = [] for kk,line in enumerate(lines): #print('kk = {:d}'.format(kk)) try: # Deals with bad Byte in Morton00 tmp = ('Z = ' in line) & ('A =' in line) except UnicodeDecodeError: tmp = False if tmp: # Grab Z ipos = line.find('Z = ') elmZ.append(int(line[ipos+4:ipos+7])) ipos2 = line.find('= ') elmc.append(line[ipos2+2:ipos].strip()) #xdb.set_trace() # Line index elmi.append(kk) # Kludge!! if 'HOLMIUM' in line: elmc[-1] = 'Ho' # ISOTOPE and ION try: # Deals with bad Byte in Morton00 tmp2 = ( (('I ' in line[0:13]) | ('V ' in line[0:13])) & (line[0:3] not in ['IOD','VAN']) & (line[0:2] != 'I ') ) except UnicodeDecodeError: tmp2 = False if tmp2: # Grab ion ipos = line[0:10].find(' ') if ipos > 4: ipos3 = line[0:10].find('I') iionv = line[ipos3:ipos] else: iionv = line[ipos:6].strip() if (len(iionv) == 0) | (iionv == '5s') | (iionv == 'B I'): pdb.set_trace() ionv.append(iionv) if iionv == 'Z =': pdb.set_trace() # Line index ioni.append(kk) # Deal with Isotope if line[0] in ['0','1','2','3','4','5','6','7','8','9']: # Skip ArI ! if 'Ar I' in line: pass else: isoi.append(kk) # Deuterium if line[0] == 'D': Dline = kk #pdb.set_trace() ## Initialize table ldict, tbl = line_data(nrows=len(lines)) ## Parse lines with UV rest wavelength count = 0 for kk,line in enumerate(lines): try: tmp = line[23] == '.' except IndexError: pass else: if tmp: # UV wavelength? # Parse # Ion/Isotope if kk > np.max(ioni): gdi = len(ioni)-1 else: gdi = np.where( (kk > np.array(ioni)) & (kk < np.roll(np.array(ioni),-1)))[0] if len(gdi) != 1: pdb.set_trace() raise ValueError('Uh oh ion') else: gdi = gdi[0] if ioni[gdi] in isoi: # Isotope continue # Ion tbl[count]['ion'] = roman.fromRoman(ionv[gdi]) # Wavelength tbl[count]['wrest'] = float(line[19:28]) #* u.AA # Z gdZ = np.where( (kk > np.array(elmi)) & (kk < np.roll(np.array(elmi),-1)))[0] if len(gdZ) != 1: if kk > np.max(elmi): gdZ = len(elmi)-1 else: #xdb.set_trace() raise ValueError('Uh oh elm') else: gdZ = gdZ[0] tbl[count]['Z'] = elmZ[gdZ] # Name try: tbl[count]['name'] = elmc[gdZ]+ionv[gdi]+' {:d}'.format( int(tbl['wrest'][count])) except UnicodeEncodeError: pdb.set_trace() # Isotope (Atomic number) if ioni[gdi] == Dline: tbl[count]['Am'] = 2 tbl[count]['name'] = 'D'+ionv[gdi]+' {:d}'.format( int(tbl[count]['wrest'])) # f try: tbl[count]['f'] = float(line[79:89]) except ValueError: continue # Skip ones without f-value # Ej, Ek tbl[count]['Ej'] = float(line[29:38]) #/ u.cm tbl[count]['Ek'] = float(line[40:50]) #/ u.cm # A try: tbl[count]['A'] = float(line[59:68]) #/ u.s except ValueError: pass # gamma try: tbl[count]['gamma'] = float(line[69:79]) #/ u.s except ValueError: pass # gl, gu tbl[count]['gj'] = int(line[52:54]) tbl[count]['gk'] = int(line[56:58]) # Only use combined HI lines if HIcombine: if ((tbl[count]['Z'] == 1) & (tbl[count]['ion']==1) & (tbl[count]['gk'] != 6)): #print('Skipping HI line {:g}'.format(tbl[count]['wrest'])) continue # Ex #all_dict[count]['Ex'] = 0. # Zero out units (for Table) # Increment count += 1 # Trim data = tbl[0:count] # Last data['group'] = 1 data['Ref'] = 'Morton2003' data['mol'] = '' # Return #pdb.set_trace() return data
def parse_morton03(orig=False, tab_fil=None, HIcombine=True): """Parse tables from Morton 2003, ApJS, 149, 205 Parameters ---------- orig : bool, optional Use original code to parse the ASCII file tab_fil : str, optional Filename to use. Default = /data/lines/morton03_table2.dat HIcombine : bool, optional Combine doublet for HI [True] Returns ------- data : Table Atomic data """ # Look for FITS fitsf = lt_path + '/data/lines/morton03_table2.fits.gz' morton03_tab2 = glob.glob(fitsf) if (len(morton03_tab2) > 0) & (not orig): print('linetools.lists.parse: Reading linelist --- \n {:s}'.format( morton03_tab2[0])) data = Table(Table.read(morton03_tab2[0]), masked=True) else: ## Read Table 2 if tab_fil is None: morton03_tab2 = lt_path + '/data/lines/morton03_table2.dat' else: morton03_tab2 = tab_fil print('linetools.lists.parse: Reading linelist --- \n {:s}'.format( morton03_tab2)) f = open(morton03_tab2, 'r', encoding="ISO-8859-1") lines = f.readlines() f.close() ## Find Elements and Ions elmi = [] elmZ = [] elmc = [] ioni = [] isoi = [] ionv = [] for kk, line in enumerate(lines): #print('kk = {:d}'.format(kk)) try: # Deals with bad Byte in Morton00 tmp = ('Z = ' in line) & ('A =' in line) except UnicodeDecodeError: tmp = False if tmp: # Grab Z ipos = line.find('Z = ') elmZ.append(int(line[ipos + 4:ipos + 7])) ipos2 = line.find('= ') elmc.append(line[ipos2 + 2:ipos].strip()) #xdb.set_trace() # Line index elmi.append(kk) # Kludge!! if 'HOLMIUM' in line: elmc[-1] = 'Ho' # ISOTOPE and ION try: # Deals with bad Byte in Morton00 tmp2 = ((('I ' in line[0:13]) | ('V ' in line[0:13])) & (line[0:3] not in ['IOD', 'VAN']) & (line[0:2] != 'I ')) except UnicodeDecodeError: tmp2 = False if tmp2: # Grab ion ipos = line[0:10].find(' ') if ipos > 4: ipos3 = line[0:10].find('I') iionv = line[ipos3:ipos] else: iionv = line[ipos:6].strip() if (len(iionv) == 0) | (iionv == '5s') | (iionv == 'B I'): pdb.set_trace() ionv.append(iionv) if iionv == 'Z =': pdb.set_trace() # Line index ioni.append(kk) # Deal with Isotope if line[0] in [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ]: # Skip ArI ! if 'Ar I' in line: pass else: isoi.append(kk) # Deuterium if line[0] == 'D': Dline = kk #pdb.set_trace() ## Initialize table ldict, tbl = line_data(nrows=len(lines)) ## Parse lines with UV rest wavelength count = 0 for kk, line in enumerate(lines): try: tmp = line[23] == '.' except IndexError: pass else: if tmp: # UV wavelength? # Parse # Ion/Isotope if kk > np.max(ioni): gdi = len(ioni) - 1 else: gdi = np.where((kk > np.array(ioni)) & (kk < np.roll(np.array(ioni), -1)))[0] if len(gdi) != 1: pdb.set_trace() raise ValueError('Uh oh ion') else: gdi = gdi[0] if ioni[gdi] in isoi: # Isotope continue # Ion tbl[count]['ion'] = roman.fromRoman(ionv[gdi]) # Wavelength tbl[count]['wrest'] = float(line[19:28]) #* u.AA # Z gdZ = np.where((kk > np.array(elmi)) & (kk < np.roll(np.array(elmi), -1)))[0] if len(gdZ) != 1: if kk > np.max(elmi): gdZ = len(elmi) - 1 else: #xdb.set_trace() raise ValueError('Uh oh elm') else: gdZ = gdZ[0] tbl[count]['Z'] = elmZ[gdZ] # Name try: tbl[count][ 'name'] = elmc[gdZ] + ionv[gdi] + ' {:d}'.format( int(tbl['wrest'][count])) except UnicodeEncodeError: pdb.set_trace() # Isotope (Atomic number) if ioni[gdi] == Dline: tbl[count]['Am'] = 2 tbl[count]['name'] = 'D' + ionv[gdi] + ' {:d}'.format( int(tbl[count]['wrest'])) # f try: tbl[count]['f'] = float(line[79:89]) except ValueError: continue # Skip ones without f-value # Ej, Ek tbl[count]['Ej'] = float(line[29:38]) #/ u.cm tbl[count]['Ek'] = float(line[40:50]) #/ u.cm # A try: tbl[count]['A'] = float(line[59:68]) #/ u.s except ValueError: pass # gamma try: tbl[count]['gamma'] = float(line[69:79]) #/ u.s except ValueError: pass # gl, gu tbl[count]['gj'] = int(line[52:54]) tbl[count]['gk'] = int(line[56:58]) # Only use combined HI lines if HIcombine: if ((tbl[count]['Z'] == 1) & (tbl[count]['ion'] == 1) & (tbl[count]['gk'] != 6)): #print('Skipping HI line {:g}'.format(tbl[count]['wrest'])) continue # Ex #all_dict[count]['Ex'] = 0. # Zero out units (for Table) # Increment count += 1 # Trim data = tbl[0:count] # Last data['group'] = 1 data['Ref'] = 'Morton2003' data['mol'] = '' # Return #pdb.set_trace() return data