astropy / astroquery
1 1
from collections import OrderedDict
2

3

4 1
dtype_dict = {'f': 'f', 's': 's', 'd': 'i', 'e': 'f', 'F': 'f', 'A': 's',
5
              'I': 'i'}
6 1
fmt_dict = {'f': float, 's': str, 'd': int, 'e': float, 'A': str, 'I': int,
7
            'F': float}
8

9

10 1
def parse_readme(filename, group_global=None, group_local=None):
11 1
    with open(filename, 'r') as f:
12 1
        lines = f.readlines()
13

14 1
    formats = OrderedDict()
15

16 1
    if group_local is not None and group_local is not None:
17 0
        qfl, qfg = quanta_formatter(group_local=group_local,
18
                                    group_global=group_global)
19 0
        use_qf = True
20
    else:
21 1
        use_qf = False
22

23 1
    for ii, line in enumerate(lines):
24 1
        if line[0] == '-':
25 1
            row_name = lines[ii-1].strip()
26 1
        if 'C-style format specifier:' in line:
27 1
            fmt = line[len('C-style format specifier:'):].strip()
28 1
            if '.' in fmt:
29 1
                length = int(fmt[1:fmt.find('.')])
30
            else:
31 1
                length = int(fmt[1:-1])
32 1
            dtype = dtype_dict[fmt[-1]] if fmt[-1] != 's' else 'S'+fmt[1:-1]
33

34 1
            if 'quanta' in row_name and use_qf:
35 0
                g = ('global' in row_name)
36 0
                ul = 'u' if 'upper' in row_name else 'l'
37
                # gl = 'g' if g else 'l'
38 0
                qf = qfg if g else qfl
39

40 0
                assert sum([x['length'] for x in qf.values()]) == length
41

42 0
                for rn in qf:
43 0
                    formats[rn+"_"+ul] = qf[rn]
44
            else:
45 1
                formats[row_name] = {'format_str': fmt,
46
                                     'length': length,
47
                                     'dtype': dtype,
48
                                     'formatter': fmt_dict[fmt[-1]]}
49

50 1
    assert sum([x['length'] for x in formats.values()]) == 160
51 1
    return formats
52

53

54 1
def quanta_formatter(group_global='class1', group_local='group1'):
55
    """
56
    Format based on the global/local formatters from the HITRAN04 paper
57
    """
58

59 0
    local_dict = {'group1':  # asymmetric
60
                  OrderedDict([('J', 'I3'),
61
                               ('Ka', 'I3'),
62
                               ('Kc', 'I3'),
63
                               ('F', 'A5'),
64
                               ('Sym', 'A1'),
65
                               ]
66
                              ),
67
                  'hc3n':  # special
68
                  OrderedDict([('J', 'I3'),
69
                               ('Ka', 'I3'),
70
                               ('Kc', 'I3'),
71
                               ('F', 'A5'),
72
                               ('Sym', 'A1'),
73
                               ]
74
                              ),
75
                  }
76

77 0
    global_dict = {'class9':  # non-linear tetratomic
78
                   OrderedDict([('v1', 'I5'),  # preceded by 3x space
79
                                ('v2', 'I2'),
80
                                ('v3', 'I2'),
81
                                ('v4', 'I2'),
82
                                ('v5', 'I2'),
83
                                ('v6', 'I2'),
84
                                ]
85
                               ),
86
                   'class10':
87
                   OrderedDict([('v1', 'I5'),  # preceded by 3x space
88
                                ('v2', 'I2'),
89
                                ('v3', 'I2'),
90
                                ('v4', 'I2'),
91
                                ('n', 'A2'),
92
                                ('C', 'A2'),
93
                                ]
94
                               ),
95
                   'hc3n':
96
                   OrderedDict([('v1', 'I3'),  # preceded by 2x space
97
                                ('v2', 'I1'),
98
                                ('v3', 'I1'),
99
                                ('v4', 'I1'),
100
                                ('v5', 'I1'),
101
                                ('v6', 'I1'),
102
                                ('v7', 'I1'),
103
                                ('l5', 'I2'),
104
                                ('l6', 'I2'),
105
                                ('l7', 'I2'),
106
                                ]
107
                               ),
108
                   }
109

110 0
    loc = OrderedDict()
111 0
    for key, value in local_dict[group_local].items():
112 0
        loc[key] = {'format_str': value, 'length': int(value[1:]),
113
                    'dtype': 'S'+value[1:] if dtype_dict[value[0]] == 's' else
114
                    dtype_dict[value[0]],
115
                    'formatter': fmt_dict[value[0]]}
116

117 0
    glob = OrderedDict()
118 0
    for key, value in global_dict[group_global].items():
119 0
        glob[key] = {'format_str': value, 'length': int(value[1:]),
120
                     'dtype': 'S'+value[1:] if dtype_dict[value[0]] == 's' else
121
                     dtype_dict[value[0]],
122
                     'formatter': fmt_dict[value[0]]}
123

124 0
    return loc, glob

Read our documentation on viewing source code .

Loading