Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/OEM_parser.py
Дата изменения: Fri Apr 8 12:46:10 2016
Дата индексирования: Mon Apr 11 00:46:38 2016
Кодировка:

Поисковые слова: http astrokuban.info astrokuban
#!/usr/bin/env python
#
# Module OEM_parser
#
#***********************************************************************
"""

**PURPOSE** --
Parse OEM (Orbit Ephemeris Message) files.

**DEVELOPER** --
Don Chance

**MODIFICATION HISTORY** --
Initial implemetation 9/8/11

"""
#***********************************************************************

import jwst_time_util


class OEM_header(object):
def __init__(self):
self.header_dict = {'CCSDS_OEM_VERS': '2.0',
'CREATION_DATE': jwst_time_util.jwst_time(),
'ORIGINATOR': 'FDF',
'COMMENT': []}

def set(self, key, value):
"""Set header key-value pairs. Only legal header keywords are allowed as keys.
"""
if key not in self.header_dict.keys():
raise ValueError('illegal OEM keyword: ' + key)
if key == 'ORIGINATOR' or key == 'CCSDS_OEM_VERS':
self.header_dict[key] = value
elif key == 'CREATION_DATE':
self.header_dict[key] = jwst_time_util.jwst_time(value)
else:
# COMMMENT lines. This is a list because there can be multiple comment lines
self.header_dict[key].append(value)

def output(self):
outstring = 'CCSDS_OEM_VERS = ' + self.header_dict['CCSDS_OEM_VERS'] + '\n'
for c in self.header_dict['COMMENT']:
outstring += 'COMMENT ' + c
if c[-1] != '\n':
outstring += '\n'
outstring += 'CREATION_DATE = ' + str(self.header_dict['CREATION_DATE']) + '\n'
outstring += 'ORIGINATOR = ' + self.header_dict['ORIGINATOR'] + '\n'

return outstring

def parser(self, lines):
"""Given a file pointer, extract the OEM header.
"""
got_originator = got_ccsds_oem_vers = got_creation_date = False
while not (got_originator and got_ccsds_oem_vers and got_creation_date):
try:
line = lines.pop(0).strip()
except IndexError,e:
raise SyntaxError("end of file reached before a complete header was read.")
if not line:
#Ignore blank lines
continue

if line[:7] == "COMMENT":
self.header_dict["COMMENT"].append(line[8:])
continue
else:
key, value = line.split('=', 1)
key = key.strip()
value = value.strip()

if key not in self.header_dict.keys():
raise ValueError("unknown OEM keyword: " + key)

if key == 'CREATION_DATE':
self.header_dict['CREATION_DATE'] = jwst_time_util.jwst_time(value)
got_creation_date = True
elif key == 'CCSDS_OEM_VERS':
self.header_dict['CCSDS_OEM_VERS'] = value
got_ccsds_oem_vers = True
else:
# only thing left is ORIGINATOR
self.header_dict[key] = value
got_originator = True


def __repr__(self):
return self.output()


class OEM_metadata(object):
def __init__(self):
self.metadata = {'COMMENT': [],
'OBJECT_NAME': 'JWST',
'OBJECT_ID': '2018-001A',
'CENTER_NAME': 'SUN',
'REF_FRAME': 'EME2000',
'TIME_SYSTEM': 'UTC',
'START_TIME': jwst_time_util.jwst_time('2018.001'),
'STOP_TIME': jwst_time_util.jwst_time('2019.001')}

def set(self, key, value):
if key == 'START_TIME' or key == 'STOP_TIME':
self.metadata = jwst_time_util.jwst_time(value)
elif key == 'COMMENT':
self.metadata[key].append(value)
else:
self.metadata[key] = value

def output(self):
outstring = "META_START\n"
for line in self.metadata['COMMENT']:
outstring += "COMMENT " + line + "\n"
for key in self.metadata.keys():
if key == "COMMENT":
continue
outstring += key + " = " + str(self.metadata[key]) + "\n"
outstring += "META_STOP\n"
return outstring

def parser(self, lines):
line = ''
while line != "META_STOP":
try:
line = lines.pop(0).strip()
except IndexError,e:
raise SyntaxError("META_STOP record not found.")
if line == "META_START" or line == "META_STOP" or line == "":
continue
if line[:7] == "COMMENT":
self.metadata["COMMENT"].append(line[8:])
continue
else:
key, value = line.split('=',1)
key = key.strip()
value = value.strip()

if key == "START_TIME" or key == "STOP_TIME":
self.metadata[key] = jwst_time_util.jwst_time(value)
else:
self.metadata[key] = value

def __repr__(self):
return self.output()

class OEM_data(object):
def __init__(self):
self.data = []

def parser(self, lines):
while 1:
try:
line = lines.pop(0).strip()
except IndexError, e:
return 'EOF'
if line == 'META_START':
return 'META_START'
elif line:
l = line.split()
self.data.append((jwst_time_util.jwst_time(l[0]),
float(l[1]),
float(l[2]),
float(l[3]),
float(l[4]),
float(l[5]),
float(l[6])))
def output(self):
outstring = ""
for line in self.data:
outstring += (str(line[0]) + " " + str(line[1])
+ " " + str(line[2])
+ " " + str(line[3])
+ " " + str(line[4])
+ " " + str(line[5])
+ " " + str(line[6])
+ "\n")
return outstring

def __repr__(self):
return self.output()

class OEM(object):
def __init__(self, filename):
lines = open(filename).readlines()

self.header = OEM_header()
self.header.parser(lines)

self.metadata = []
self.data = []

rv = "META_START"
while rv != 'EOF':
md = OEM_metadata()
md.parser(lines)
data = OEM_data()
rv = data.parser(lines)

self.metadata.append(md)
self.data.append(data)

def output(self):
outstring = str(self.header)
for m,d in zip(self.metadata, self.data):
outstring += str(m)
outstring += str(d)
return outstring

def __repr(self):
return self.output()


def test(*args):
if args:
o = OEM(args[0])




if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
run(*sys.argv[1:])
else:
run()