astropy / astroquery
1
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2 1
"""
3
Utilities for working with Splatalogue query results
4
"""
5 1
import numpy as np
6 1
import astropy
7

8
# Remap column headings to something IPAC-compatible
9 1
column_headings_map = {'Log<sub>10</sub> (A<sub>ij</sub>)': 'log10_Aij',
10
                       'Resolved QNs': 'QNs',
11
                       'CDMS/JPL Intensity': 'CDMSJPL_Intensity',
12
                       'S<sub>ij</sub>': 'Sij',
13
                       'Freq-GHz': 'FreqGHz',
14
                       'Meas Freq-GHz': 'MeasFreqGHz',
15
                       'Lovas/AST Intensity': 'LovasAST_Intensity',
16
                       'E_L (cm^-1)': 'EL_percm',
17
                       'E_L (K)': 'EL_K',
18
                       'E_U (cm^-1)': 'EU_percm',
19
                       'E_U (K)': 'EU_K',
20
                       'Chemical Name': 'ChemicalName',
21
                       'Freq Err': 'FreqErr',
22
                       'Meas Freq Err': 'MeasFreqErr',
23
                       'Freq-GHz(rest frame,redshifted)': 'FreqGHz',
24
                       'Freq Err(rest frame,redshifted)': 'eFreqGHz',
25
                       'Meas Freq-GHz(rest frame,redshifted)': 'MeasFreqGHz',
26
                       'Meas Freq Err(rest frame,redshifted)': 'eMeasFreqGHz',
27
                       }
28

29

30 1
def clean_column_headings(table, renaming_dict=column_headings_map):
31
    """
32
    Rename column headings to shorter version that are easier for display
33
    on-screen / at the terminal
34
    """
35

36 1
    for key in renaming_dict:
37 1
        if key in table.colnames:
38 1
            table.rename_column(key, renaming_dict[key])
39

40 1
    return table
41

42

43 1
def merge_frequencies(table, prefer='measured',
44
                      theor_kwd='Freq-GHz(rest frame,redshifted)',
45
                      meas_kwd='Meas Freq-GHz(rest frame,redshifted)'):
46
    """
47
    Replace "Freq-GHz" and "Meas Freq-GHz" with a single "Freq" column.
48

49
    Parameters
50
    ----------
51
    table : table
52
        The Splatalogue table
53
    prefer: 'measured' or 'theoretical'
54
        Which of the two columns to prefer if there is a conflict
55
    """
56

57 1
    if prefer == 'measured':
58 1
        Freq = np.copy(table[theor_kwd]).astype('float')
59 1
        measmask = np.logical_not(table[meas_kwd].mask)
60 1
        Freq[measmask] = table[meas_kwd][measmask].astype('float')
61 0
    elif prefer == 'theoretical':
62 0
        Freq = np.copy(table[meas_kwd]).astype('float')
63 0
        theomask = np.logical_not(table[theor_kwd].mask)
64 0
        Freq[theomask] = table[theor_kwd][theomask].astype('float')
65
    else:
66 0
        raise ValueError('prefer must be one of "measured" or "theoretical"')
67

68 1
    index = table.index_column(theor_kwd)
69 1
    table.remove_columns([theor_kwd, meas_kwd])
70 1
    newcol = astropy.table.Column(name='Freq', data=Freq)
71 1
    table.add_column(newcol, index=index)
72

73 1
    return table
74

75

76 1
def minimize_table(table, columns=['Species', 'Chemical Name',
77
                                   'Resolved QNs',
78
                                   'Freq-GHz(rest frame,redshifted)',
79
                                   'Meas Freq-GHz(rest frame,redshifted)',
80
                                   'Log<sub>10</sub> (A<sub>ij</sub>)',
81
                                   'E_U (K)'],
82
                   merge=True,
83
                   clean=True):
84
    """
85
    Strip a table's headers and rename them to their minimalist form
86

87
    Parameters
88
    ----------
89
    table : table
90
        The Splatalogue table
91
    columns : list
92
        A list of column names to keep before merging and cleaning
93
    merge : bool
94
        Run merge_frequencies to get a single reported frequency for each line?
95
    clean_column_headings : bool
96
        Run clean_column_headings to shorted the headers?
97
    """
98

99 1
    table = table[columns]
100

101 1
    if merge:
102 1
        table = merge_frequencies(table)
103 1
    if clean:
104 1
        table = clean_column_headings(table)
105

106 1
    return table

Read our documentation on viewing source code .

Loading