Source code for irfpy.vexop.itlparser
''' Parse itl formatted string.
'''
import re
[docs]def get_orb(line):
''' Return the orbit number of the command line.
Only VPER command can be recognized.
'''
# print line
pTime = re.compile(r"VPER.*COUNT.*=(.*)\).*ASPERA") # To obtain time
sTime = pTime.search(line)
# print sTime
if sTime is None:
return 0
[o]=sTime.groups()
# print o
return int(o)
[docs]def get_vpertime(line):
''' Return the time in seconds corresponding to the pericenter time.
'''
pTime = re.compile(r"VPER.*\(.*\)(.*):(.*):(.*).*ASPERA") # To obtain time
sTime = pTime.search(line)
if sTime is None:
return 0
[h,m,s] = sTime.groups()
h=h.strip()
if h.startswith("-"):
negatime=True
else:
negatime=False
h=int(h)
m=int(m)
s=int(s)
if negatime:
t=-((-h)*3600+m*60+s)
else:
t=h*3600+m*60+s
return t
[docs]def str_vpertime(vpertime):
'''
>>> print(str_vpertime(13800))
+03:50:00
>>> print(str_vpertime(-1000))
-00:16:40
'''
negatime=False
if vpertime < 0:
negatime=True;
vpertime=-vpertime
h=vpertime//3600
vpertime=vpertime%3600
m=vpertime//60
s=vpertime%60
str="+"
if negatime:
str="-"
str=str+"%02d"%h+":"+"%02d"%m+":"+"%02d"%s
# print str
return str
[docs]def itl_split_line(file):
''' ITL file convert to "1-line 1-command format".
:param file: A file-like object to be parsed.
'''
linebuf = ""
lines = []
for line in file:
line=line.split("#")[0].strip(); # Remove \n and spaces
if line != "":
if line.endswith("\\"): # If continueing line
line=line.split("\\")[0] # Remove \
linebuf=linebuf+line # Append to linebuf
else:
linebuf=linebuf+line # Append to linebuf
# print linebuf #
lines=lines+[linebuf]
linebuf=""
return lines
[docs]def get_command(line):
r''' Return the command in text, e.g. 'AASF009A' or 'AASF056A'.
>>> cmdline = 'VPER (COUNT = 1492) 03:23:05 ASPERA HK AASF093A ( VAS52200 = 40 [RAW] POWER_PROFILE = 00:00:00 23.4 [Watts] DATA_RATE_PROFILE = 00:00:00 6000 [bits/sec])'
>>> print(get_command(cmdline))
AASF093A
# A wrong example.
>>> cmdline = 'VPER (COUNT = 1492) 03:23:05 ASPERA HK AASF03A ( VAS52200 = 40 [RAW] )'
>>> try:
... print(get_command(cmdline))
... print('!!!! Should not reach here')
... except ValueError:
... print('Exception correctly caught')
Exception correctly caught
'''
cmd = re.compile('AASF\d\d\d[A-Z]')
cmdsearch = cmd.search(line)
if cmdsearch is None:
raise ValueError('No command for the given command-line')
return cmdsearch.group()
[docs]def get_param(line, param):
r''' Return the parameter as string. Parameter here is for example like "VASD6503" or "VAS01060".
>>> cmdline = 'VPER (COUNT = 1492) 03:23:05 ASPERA HK AASF093A ( VAS52200 = 40 [RAW] VASXX060 = OFF [DUMMY] POWER_PROFILE = 00:00:00 23.4 [Watts] DATA_RATE_PROFILE = 00:00:00 6000 [bits/sec])'
>>> vas52200 = get_param(cmdline, 'VAS52200')
>>> print(int(vas52200))
40
>>> vasxx060 = get_param(cmdline, 'VASXX060')
>>> print(vasxx060.strip())
OFF
'''
p = re.compile("%s\s*=\s*\S+" % param)
s = p.search(line)
if s is None:
raise ValueError('No parameter %s in the given command-line\n\t%s' % (param, line))
param = s.group().split('=')[1]
return param
import unittest,doctest
[docs]def doctests():
return unittest.TestSuite((
doctest.DocTestSuite(),
))
if __name__=='__main__':
unittest.main(defaultTest='doctests')