forked from astropy/astroquery
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
124 lines (106 loc) · 4.61 KB
/
utils.py
File metadata and controls
124 lines (106 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from collections import OrderedDict
dtype_dict = {'f': 'f', 's': 's', 'd': 'i', 'e': 'f', 'F': 'f', 'A': 's',
'I': 'i'}
fmt_dict = {'f': float, 's': str, 'd': int, 'e': float, 'A': str, 'I': int,
'F': float}
def parse_readme(filename, group_global=None, group_local=None):
with open(filename, 'r') as f:
lines = f.readlines()
formats = OrderedDict()
if group_local is not None and group_local is not None:
qfl, qfg = quanta_formatter(group_local=group_local,
group_global=group_global)
use_qf = True
else:
use_qf = False
for ii, line in enumerate(lines):
if line[0] == '-':
row_name = lines[ii-1].strip()
if 'C-style format specifier:' in line:
fmt = line[len('C-style format specifier:'):].strip()
if '.' in fmt:
length = int(fmt[1:fmt.find('.')])
else:
length = int(fmt[1:-1])
dtype = dtype_dict[fmt[-1]] if fmt[-1] != 's' else 'S'+fmt[1:-1]
if 'quanta' in row_name and use_qf:
g = ('global' in row_name)
ul = 'u' if 'upper' in row_name else 'l'
# gl = 'g' if g else 'l'
qf = qfg if g else qfl
assert sum([x['length'] for x in qf.values()]) == length
for rn in qf:
formats[rn+"_"+ul] = qf[rn]
else:
formats[row_name] = {'format_str': fmt,
'length': length,
'dtype': dtype,
'formatter': fmt_dict[fmt[-1]]}
assert sum([x['length'] for x in formats.values()]) == 160
return formats
def quanta_formatter(group_global='class1', group_local='group1'):
"""
Format based on the global/local formatters from the HITRAN04 paper
"""
local_dict = {'group1': # asymmetric
OrderedDict([('J', 'I3'),
('Ka', 'I3'),
('Kc', 'I3'),
('F', 'A5'),
('Sym', 'A1'),
]
),
'hc3n': # special
OrderedDict([('J', 'I3'),
('Ka', 'I3'),
('Kc', 'I3'),
('F', 'A5'),
('Sym', 'A1'),
]
),
}
global_dict = {'class9': # non-linear tetratomic
OrderedDict([('v1', 'I5'), # preceded by 3x space
('v2', 'I2'),
('v3', 'I2'),
('v4', 'I2'),
('v5', 'I2'),
('v6', 'I2'),
]
),
'class10':
OrderedDict([('v1', 'I5'), # preceded by 3x space
('v2', 'I2'),
('v3', 'I2'),
('v4', 'I2'),
('n', 'A2'),
('C', 'A2'),
]
),
'hc3n':
OrderedDict([('v1', 'I3'), # preceded by 2x space
('v2', 'I1'),
('v3', 'I1'),
('v4', 'I1'),
('v5', 'I1'),
('v6', 'I1'),
('v7', 'I1'),
('l5', 'I2'),
('l6', 'I2'),
('l7', 'I2'),
]
),
}
loc = OrderedDict()
for key, value in local_dict[group_local].items():
loc[key] = {'format_str': value, 'length': int(value[1:]),
'dtype': 'S'+value[1:] if dtype_dict[value[0]] == 's' else
dtype_dict[value[0]],
'formatter': fmt_dict[value[0]]}
glob = OrderedDict()
for key, value in global_dict[group_global].items():
glob[key] = {'format_str': value, 'length': int(value[1:]),
'dtype': 'S'+value[1:] if dtype_dict[value[0]] == 's' else
dtype_dict[value[0]],
'formatter': fmt_dict[value[0]]}
return loc, glob