Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/find_backup_contacts.py
Дата изменения: Fri Feb 28 14:46:09 2014
Дата индексирования: Sat Mar 1 22:18:08 2014
Кодировка:

Поисковые слова: п п п п п п п п п п п п п п п п п п п п п п п п п
#
#MODULE find_backup_contacts
#
#***********************************************************************
"""
**PURPOSE** --
Locate possible times for TDRS SSAR service on the backup transmitter.

**DEVELOPER** --
Gary Bower

**MODIFICATION HISTORY** --

o Initial implementation 11/12/02
o Minor modifications from code review. gab 12/09/02
o Use tdrs_util to get tdrs events. gab 3/03/03
"""
#***********************************************************************
import os, re, spss_sys_util, spst_getopt, string, time_util, tdrs_util

__version__ = "3/03/03"

def run(ms_run_name=None,backup_transmitter=None,*args):
"""Locate possible times for I29 returns on the backup transmitter.

Usage:
do find_backup_contacts ms_run_name backup_transmitter [-tdrs_file=tdrs_file] [-output=file]

ms_run_name = Mission Scheduler run name (e.g., sa322m01_f).

backup_transmitter = 1 or 2

tdrs_file is the name of the tdrs.lis file. It must exist in the
directory $PASSOPS/ms/. If no file is specified, then tdrs_file
defaults to 'tdrs.lis'.

The output is written to the requested file or to backup_transmitter.rpt (default) in the
default directory.

"""

if not ms_run_name or not backup_transmitter:
# Spew out the usage and quit when not enough parameters are given.
print run.__doc__
return spss_sys_util.SUCCESS

# Verify that backup_transmitter has a value of 1 or 2.
if (backup_transmitter not in ['1','2']):
print "Error: backup_transmitter = %s is not valid." % backup_transmitter
print "It must be set to 1 or 2."
return not spss_sys_util.SUCCESS
backup_transmitter = int(backup_transmitter)

# Get the allowed options to set the tdrs lis file and the output filename.
allowed_options = ['tdrs_file=','TDRS_FILE=','output=','OUTPUT=']
options, parms = spst_getopt.spst_getopt(args,allowed_options)
if options.has_key('-tdrs_file'):
tdrs_file = string.lower(options['-tdrs_file'])
elif options.has_key('-TDRS_FILE'):
tdrs_file = string.lower(options['-TDRS_FILE'])
else:
tdrs_file = 'tdrs.lis'
if options.has_key('-output'):
output = string.lower(options['-output'])
elif options.has_key('-OUTPUT'):
output = string.lower(options['-OUTPUT'])
else:
output = 'backup_transmitter.rpt'

# Construct the path to the MS directory, test for existence, and get files.
passops = spss_sys_util.resolver('PASSOPS')
ms_dir = os.path.join(passops,'ms',ms_run_name)
if (not os.path.exists(ms_dir)):
print "Error: the MS directory %s does not exist." % ms_dir
return not spss_sys_util.SUCCESS
if not tdrs_file:
tdrs_file = 'tdrs.lis'
tdrs_path = os.path.join(ms_dir,tdrs_file)
if (not os.path.exists(tdrs_path)):
print "Error: the TDRS file %s does not exist." % tdrs_path
return not spss_sys_util.SUCCESS

# Get rms file to determine SMS start and end times.
filelist = spss_sys_util.glob(os.path.join(ms_dir,'*.rms'))
if len(filelist) == 1:
rmsfile = filelist[0]
elif (not filelist):
print "Error: There is no .rms file in %s." % ms_dir
return not spss_sys_util.SUCCESS
else:
print "There are multiple .rms files in %s.\n" % ms_dir
for file in filelist:
(head, tail) = os.path.split(file)
print tail
print " "
answer = raw_input("Which .rms file do you want? ")
rmsfile = os.path.join(ms_dir,answer)
if not os.path.exists(rmsfile):
print "Error: %s does not exist." % rmsfile
return not spss_sys_util.SUCCESS

# Open the rms file and get the starting and ending time of the SMS.
f = open(rmsfile,'r')
found = 0
while (not found):
line = f.readline()
if (not line):
print "Error: Start and End times were not found from %s." % rmsfile
return not spss_sys_util.SUCCESS
else:
if (string.find(line,'START') != -1) and (string.find(line,'END') != -1):
# We want to read the number of seconds as an integer, even if it's given
# as a floating point.
timelist = re.findall(r'\d{4,4}\.\d{3,3}\:\d\d\:\d\d\:\d\d',line)
if (len(timelist) == 2):
tstart = timelist[0]
tend = timelist[1]
found = 1
f.close()

# Convert start and end times from character strings to SPSS objects.
tstart_spss = time_util.spss_time().strptime(tstart,'%Y.%j:%H:%M:%S')
tend_spss = time_util.spss_time().strptime(tend,'%Y.%j:%H:%M:%S')

# Get a list of TDRS events, start and end times from tdrs_path.
tdrslis = tdrs_util.tdrslis_file(tdrs_path)
header = tdrslis.get_header_info()
tdrs_start_spss = header['start']
tdrs_end_spss = header['end']
new_events = tdrslis.get_tdrs_events()
# Sort the events by start time.
events_sort = tdrs_util.event_list_by_start(new_events)
# For each event, pull out the parameters from each service that we need.
# This includes event_id (string), start and end times (spss_time obj),
# service (string), and transmitter number (integer). Put these into
# the list tdrs_events, whose structure is [[[...],[...]],...].
tdrs_events = []
for event in events_sort:
event_id = event.get_event_id()
next_event = []
for svc in event.get_services():
start = svc.get_start()
end = svc.get_end()
service = svc.get_ssc()
trans = svc.get_antenna_num()
next_event.append([event_id,start,end,service,trans])
tdrs_events.append(next_event)

# Check to ensure that SMS time range is within tdrs time range. The SMS end time (tend) is
# not checked against the TDRS end time (tdrs_end) because the latter is usually several
# minutes shorter than tend in the nominal case.
if ((tstart_spss < tdrs_start_spss) or (tstart_spss > tdrs_end_spss)) \
or ((tend_spss < tdrs_start_spss)):
print "Error: TDRS and SMS time ranges are inconsistent."
print "TDRS window %s %s." % (tdrs_start_spss, tdrs_end_spss)
print "SMS window %s %s." % (tstart,tend)
return not spss_sys_util.SUCCESS

# Loop through the list tdrs_events. For each event, analyze it to determine if an
# SSAR could be added on the backup transmitter. This is accomplished by searching for
# acceptable events on the backup transmitter that has an SSAF H04 but no SSAR's (I29
# or I24). The list of acceptable events includes MAR B13; others can be added later.
acc_events = ['B13']
g = open(output,'w')
print "Possible start and stop times for adding SSA returns using transmitter %i" % backup_transmitter
print "found in the tdrs schedule %s." % tdrs_path
print "The output is written to the file %s" % output
g.write("Possible start and stop times for adding SSA returns using transmitter %i\n" % backup_transmitter)
g.write("found in the tdrs schedule %s.\n" % tdrs_path)
g.write("-"*70+"\n")
g.write("Start Time"+" "*10+"Stop Time"+" "*9+"Duration\n")
for event in tdrs_events:
for each in event:
if (each[3] in acc_events) and (each[4] == backup_transmitter):
if ('I29' != each[3]) and ('I24' != each[3]):
# Find the start and stop times of the SSA forward H04
for x in event:
if ('H04' == x[3]):
start = x[1]
stop = x[2]
# Calculate the time duration.
duration = stop - start
# If event is in SMS time window, then output it.
if (start >= tstart_spss) and (stop <= tend_spss):
g.write("%s %s %s\n" % (start,stop,duration))
g.close()
return spss_sys_util.SUCCESS