-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobs80_clip.py
162 lines (139 loc) · 4.59 KB
/
obs80_clip.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""snip raw text observation records corresponding to selected fields,
operates on all plain text observation formats
Command line usage:
args: [datf] [{ field | - },...]
datf -- raw observation text records (stdin)
field -- keyword in named obs80 tuple objects selecting values to be output
"-" means pass through the input data record in this position; see -a
opts:
#-a -- all fields (in place of giving all the fields as command arguments)
ISB 8/2017
"""
import sys, os
# package parent directory must be in sys.path
from collections import namedtuple
from components.obs80.obs80 import *
from components.obs80 import obs80
def parseOpt(line):
n2 = obs80.note2(line)
if n2 not in 'PeCTMcEOHNn AXx':
raise ValueError('note2 = "{}"'.format(n2))
return line, Optical(
num=line[:5].strip(),
desig=line[5:12].strip(),
disc=line[12].strip(),
note1=line[13].strip(),
note2=n2.strip(),
jdutc=line[15:32].strip(),
ra=line[32:44].strip(),
dec=line[44:56].strip(),
mag=line[65:70].strip(),
band=line[70].strip(),
cod=line[77:80]
)
obs80.parseOpt = parseOpt
au_km = obs80.au_km
floatPx = obs80.floatPx
def parseSat(line1, line2):
""" returns Spacebased for successful parse, raises exception otherwise"""
ax = floatPx(line2[34:45].strip())
ay = floatPx(line2[46:57].strip())
az = floatPx(line2[58:69].strip())
if line2[32] == '1':
ax /= au_km
ay /= au_km
az /= au_km
origs = (line1+line2).replace('\n',r'\n')
return origs, SpaceBased(
# line1
num=line1[:5].strip(),
desig=line1[5:12].strip(),
disc=line1[12].strip(),
note1=line1[13].strip(),
jdutc=line1[15:32].strip(),
ra=line1[32:44].strip(),
dec=line1[44:56].strip(),
mag=line1[65:70].strip(),
band=line1[70].strip(),
cod=line1[77:80].strip(),
# line2
x=str(ax),
y=str(ay),
z=str(az)
)
obs80.parseSat = parseSat
def parseRoving(line1, line2):
""" returns Roving for successful parse, raises exception otherwise"""
origs = (line1+line2).replace('\n',r'\n')
return origs, Roving(
# line1
num=line1[:5].strip(),
desig=line1[5:12].strip(),
disc=line1[12].strip(),
note1=line1[13].strip(),
jdutc=line1[15:32].strip(),
ra=line1[32:44].strip(),
dec=line1[44:56].strip(),
mag=line1[65:70].strip(),
band=line1[70].strip(),
# line2
lon=line2[34:44].strip(),
lat=str(floatPx(line2[45:55])),
alt=line2[56:61].strip()
)
obs80.parseRov = parseRoving
def parseRadar(line1, line2):
""" returns Radar for successful parse, raises exception otherwise"""
origs = (line1+line2).replace('\n',r'\n')
return origs, Radar(
# line1
num=line1[:5].strip(),
desig=line1[5:12].strip(),
note1=line1[13].strip(),
jdutc=line1[15:32].strip(),
delay=line1[32:43] + '.' + line1[43:47] if line1[42] != ' ' else None,
doppler=str(floatPx(
line1[47:58]+'.'+line1[58:62])) if line1[57] != ' ' else None,
freq=line1[62:67] + '.' + line1[67:68] + line2[62:68],
txcod=line1[68:71],
rxcod=line1[77:80],
# line2
ret=line2[32],
udelay=line2[33:43]+'.'+line2[43:47] if line2[42] != ' ' else None,
udoppler=line2[47:58]+'.'+line2[58:62] if line2[57] != ' ' else None
)
obs80.parseRad = parseRadar
def split_obs(strm):
for s in strm:
eol = s.find(r'\n')
if eol > 0:
s = s.rstrip().replace(r'\n','\n').replace('\\\\','\\')
yield s[:eol+1]
if eol+1 < len(s):
yield s[eol+1:]
else:
yield s
def run(src, flds):
"""print CSV formatted input for Postgres COPY FROM"""
def qq(s):
s = s.replace('"','""').replace("'","''")
return s if s.find(',') < 0 else '"%s"' % s
for t in parse80(split_obs(src)):
if not isinstance(t[0],Exception):
s, obs = t
print(','.join([getattr(obs,k,"") if k != '-' else qq(
s.rstrip()) for k in flds]))
else:
print('parse80: Error, {}'.format(t), file=sys.stderr)
def main():
src = len(sys.argv) > 1 and sys.argv[1]
if not (src and os.path.exists(src)):
src = sys.stdin
else:
src = open(src)
sys.argv.pop(0)
flds = sys.argv[1].split(',') if len(sys.argv) > 1 else [
'cod','jdutc','ra','dec']
run(src, flds)
if __name__ == '__main__':
main()