Ejemplo n.º 1
0
Archivo: io.py Proyecto: bgriffen/Barak
def read_HITRAN(thelot=False):
    """ Returns a list of molecular absorption features.

    This uses the HITRAN 2004 list with wavelengths < 25000 Ang
    (Journal of Quantitative Spectroscopy & Radiative Transfer 96,
    2005, 139-204). By default only lines with intensity > 5e-26 are
    returned. Set thelot=True if you really want the whole catalogue
    (not recommended).

    The returned wavelengths are in Angstroms.

    The strongest absorption features in the optical range are
    typically due to O2.
    """
    from barak.utilities import get_data_path
    DATAPATH = get_data_path()
    filename = DATAPATH + '/linelists/HITRAN2004_wa_lt_25000.fits.gz'
    import astropy.table
    lines = astropy.table.Table.read(filename)
    if not thelot:
        lines = lines[lines['intensity'] > 5e-26]
    lines.sort('wav')
    return lines
Ejemplo n.º 2
0
def process_options(args):
    opt = adict()
    filename = os.path.abspath(__file__).rsplit('/', 1)[0] + '/default.cfg'
    opt = parse_config(filename)
    if os.path.lexists('./plot.cfg'):
        opt = parse_config('./plot.cfg', opt)

    opt.atom = readatom(molecules=True)

    if opt.Rfwhm is not None:
        if isinstance(opt.Rfwhm, basestring):
            if opt.Rfwhm == 'convolve_with_COS_FOS':
                if convolve_with_COS_FOS is None:
                    raise ValueError('convolve_with_COS_FOS() not available')
                print('Using tailored FWHM for COS/FOS data')
                opt.Rfwhm = 'convolve_with_COS_FOS'
            elif opt.Rfwhm.endswith('fits'):
                print('Reading Resolution FWHM from', opt.Rfwhm)
                res = readtabfits(opt.Rfwhm)
                opt.Rfwhm = res.res / 2.354
            else:
                print('Reading Resolution FWHM from', opt.Rfwhm)
                fh = open(opt.Rfwhm)
                opt.Rfwhm = 1 / 2.354 * np.array([float(r) for r in fh])
                fh.close()
        else:
            opt.Rfwhm = float(opt.Rfwhm)

    if opt.features is not None:
        print('Reading feature list from', opt.features)
        opt.features = readtabfits(opt.features)

    if opt.f26 is not None:
        name = opt.f26
        print('Reading ions and fitting regions from', name)
        opt.f26 = readf26(name)
        opt.f26.filename = name

    if opt.transitions is not None:
        print('Reading transitions from', opt.transitions)
        fh = open(opt.transitions)
        trans = list(fh)
        fh.close()
        temp = []
        for tr in trans:
            tr = tr.strip()
            if tr and not tr.startswith('#'):
                junk = tr.split()
                tr = junk[0] + ' ' + junk[1]
                t = findtrans(tr, atomdat=opt.atom)
                temp.append(dict(name=t[0], wa=t[1][0], tr=t[1]))
        opt.linelist = temp
    else:
        opt.linelist = readtxt(get_data_path() + 'linelists/qsoabs_lines',
                               names='wa,name,select')

    if opt.f26 is None and opt.taulines is not None:
        print('Reading ions from', opt.taulines)
        fh = open(opt.taulines)
        lines = []
        for row in fh:
            if row.lstrip().startswith('#'):
                continue
            items = row.split()
            lines.append([items[0]] + list(map(float, items[1:])))
        fh.close()
        opt.lines = lines

    if opt.show_regions is None:
        opt.show_regions = True

    if hasattr(opt, 'aodname'):
        opt.aod = Table.read(opt.aodname)

    return opt
Ejemplo n.º 3
0
def process_options(args):
    opt = adict()
    filename = os.path.abspath(__file__).rsplit('/', 1)[0] + '/default.cfg'
    opt = parse_config(filename)
    if os.path.lexists('./plot.cfg'):
        opt = parse_config('./plot.cfg', opt)

    opt.atom = readatom(molecules=True)

    if opt.Rfwhm is not None:
        if isinstance(opt.Rfwhm, basestring):
            if opt.Rfwhm == 'convolve_with_COS_FOS':
                if convolve_with_COS_FOS is None:
                    raise ValueError('convolve_with_COS_FOS() not available')
                print('Using tailored FWHM for COS/FOS data')
                opt.Rfwhm = 'convolve_with_COS_FOS'
            elif opt.Rfwhm.endswith('fits'):
                print('Reading Resolution FWHM from', opt.Rfwhm)
                res = readtabfits(opt.Rfwhm)
                opt.Rfwhm = res.res / 2.354
            else:
                print('Reading Resolution FWHM from', opt.Rfwhm)
                fh = open(opt.Rfwhm)
                opt.Rfwhm = 1 / 2.354 * np.array([float(r) for r in fh])
                fh.close()
        else:
            opt.Rfwhm = float(opt.Rfwhm)

    if opt.features is not None:
        print('Reading feature list from', opt.features)
        opt.features = readtabfits(opt.features)

    if opt.f26 is not None:
        name = opt.f26
        print('Reading ions and fitting regions from', name)
        opt.f26 = readf26(name)
        opt.f26.filename = name

    if opt.transitions is not None:
        print('Reading transitions from', opt.transitions)
        fh = open(opt.transitions)
        trans = list(fh)
        fh.close()
        temp = []
        for tr in trans:
            tr = tr.strip()
            if tr and not tr.startswith('#'):
                junk = tr.split()
                tr = junk[0] + ' ' + junk[1]
                t = findtrans(tr, atomdat=opt.atom)
                temp.append(dict(name=t[0], wa=t[1][0], tr=t[1]))
        opt.linelist = temp
    else:
        opt.linelist = readtxt(get_data_path() + 'linelists/qsoabs_lines',
                        names='wa,name,select')

    if opt.f26 is None and opt.taulines is not None:
        print('Reading ions from', opt.taulines)
        fh = open(opt.taulines)
        lines = []
        for row in fh:
            if row.lstrip().startswith('#'):
                continue
            items = row.split()
            lines.append([items[0]] + list(map(float, items[1:])))
        fh.close()
        opt.lines = lines

    if opt.show_regions is None:
        opt.show_regions = True

    if hasattr(opt, 'aodname'):
        opt.aod = Table.read(opt.aodname)

    return opt
Ejemplo n.º 4
0
Archivo: io.py Proyecto: bgriffen/Barak
def readatom(filename=None, debug=False,
             flat=False, molecules=False, isotopes=False):
    """ Reads atomic transition data from a vpfit-style atom.dat file.

    Parameters
    ----------
    filename : str, optional
      The name of the atom.dat-style file. If not given, then the
      version bundled with `barak` is used.
    flat : bool (False)
      If True, return a flattened array, with the data not grouped by
      transition.
    molecules : bool (False)
      If True, also return data for H2 and CO molecules.
    isotopes : bool (False)
      If True, also return data for isotopes.

    Returns
    -------
    atom [, atom_flat] : dict [, dict]
      A dictionary of transition data, in general grouped by
      electronic transition (MgI, MgII and so on). If `flat` = True,
      also return a flattened version of the same data.
    """

    # first 2 chars - element.
    #        Check that only alphabetic characters
    #        are used (if not, discard line).
    # next 4 chars - ionization state (I, II, II*, etc)
    # remove first 6 characters, then:
    # first string - wavelength
    # second string - osc strength
    # third string - lifetime? (intrinsic width constant)
    # ignore anything else on the line

    if filename is None:
        filename = get_data_path() + '/linelists/atom.dat'

    if filename.endswith('.gz'):
        import gzip
        fh = gzip.open(filename, 'rb')
    else:
        fh = open(filename, 'rb')

    atom = dict()
    atomflat = []
    specials = set(['??', '__', '>>', '<<', '<>'])
    for line in fh:
        line = line.decode('utf-8')
        if debug:  print(line)
        if not line[0].isupper() and line[:2] not in specials:
            continue
        ion = line[:6].replace(' ','')
        if not molecules:
            if ion[:2] in set(['HD','CO','H2']):
                continue
        if not isotopes:
            if ion[-1] in 'abc' or ion[:3] == 'C3I':
                continue
        wav,osc,gam = [float(item) for item in line[6:].split()[:3]]
        if ion in atom:
            atom[ion].append((wav,osc,gam))
        else:
            atom[ion] = [(wav,osc,gam)]
        atomflat.append( (ion,wav,osc,gam) )

    fh.close()
    # turn each ion into a record array

    for ion in atom:
        atom[ion] = np.rec.fromrecords(atom[ion], names=str('wa,osc,gam'))

    atomflat = np.rec.fromrecords(atomflat,names=str('name,wa,osc,gam'))

    if flat:
        return atom, atomflat
    else:
        return atom