Newer
Older
rtsm_error_types_to_error_types = dict(HN='HIGH_NOISE',
SN='SUMMATOR_NOISE',
CR='CABLE_REFLECTION',
LN='LOW_NOISE',
J='JITTER',
OSC='OSCILLATION',
FLAT='FLAT',
DOWN='DOWN',
SHORT='SHORT')
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
def split_raw_rtsm_test(content):
"""
Split the raw rtsm test in row and filters out comments and empty lines
:param content: content of rtsm output file
"""
results = filter(lambda x: not x.startswith('#'),
filter(lambda x: len(x) > 0,
map(lambda x: x.strip(),
content.split('\n')
)
))
return results
def parse_data(date_time):
"""
Parses the utc timestamp to a date object aware of the time zone
:param date_time: utc timestamp
:return:
"""
return pytz.utc.localize(datetime.utcfromtimestamp(float(date_time)))
def preparse_rtsm_test_file(content):
"""
Parse the content of the RTSM output into a dict representation
:param content:
:return:
"""
results = []
result = dict()
observation = dict()
for line in content:
key, value = line.split('=')
if 'INFO' in key and 'OBS' not in key:
if 'rcu' in result:
results.append(result)
result = dict()
rcu, mode, observation_id, error_type, start_frequency, stop_frequency, time = value.split(',')
result.update(rcu=int(rcu),
mode=int(mode),
error_type=rtsm_error_types_to_error_types[error_type],
start_frequency=float(start_frequency),
stop_frequency=float(stop_frequency),
time=parse_data(time))
elif 'INFO' in key:
observation_id, start_time, stop_time, samples = value.split(',')
observation.update(observation_id=int(observation_id),
start_datetime=parse_data(start_time),
end_datetime=parse_data(stop_time),
samples=int(samples))
elif 'BAD' in key:
values = [float(item) for item in value.lstrip('[').rstrip(']').strip().split(' ')]
result.update(bad_spectrum=values)
elif 'MEAN' in key:
values = [float(item) for item in value.lstrip('[').rstrip(']').strip().split(' ')]
result.update(average_spectrum=values)
results.append(result)
observation.update(errors=results)
return observation
def parse_rtsm_test(content):
"""
Expects a string content with the rtsm test output
and output a list of Django Models
:param content: string content with the station test output
:return: a list of Django models
"""
return preparse_rtsm_test_file(split_raw_rtsm_test(content))