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

Поисковые слова: trifid nebula
#
#MODULE notify_ncc_comcons
#
#***********************************************************************
"""

**PURPOSE** --
Sends NCC a mail message listing the COMCONs for a given SMS to hopefully
avoid having TDRS shortfalls over the time periods for COMCONs.

Based on NOTIFY_NCC_COMCONS.COM and COMCON_SMS_PARSE.FOR

**DEVELOPER** --
Gary Bower

**MODIFICATION HISTORY** --

o Initial implementation 12/21/01
o Include modifications from Python review - G. Bower 1/16/02
o Include more modifications from Python review. - G. Bower 2/11/02
- SMS version number is not required in input.
- Define the function sms_parse_comcon.
- Use calendar_util to parse the calendar display.
o Allow email distribution list APP 07/28/04
"""
#***********************************************************************
import spss_sys_util, string, os, time_util, re
import sms_util, ccl_util, calendar_util

__version__ = "2/11/02"

def run(sms_id_root=None):
"""Parse an SMS, searching for the occurrence of COMCON commands.
Make a list of them, and send e-mail to NCC.

Usage:
do notify_ncc_comcons

sms_id_root must be a valid SMS id. File extension and version number should not
be included in the input.

"""

if not sms_id_root:
# Spew out the usage and quit when no SMS name
# is provided.
print run.__doc__
return spss_sys_util.SUCCESS

# Ensure that input does not have the extension or version number in it.
if (string.find(sms_id_root,'.') != -1):
print "Error: File extension and version number should not be included in "
print "the input."
return not spss_sys_util.SUCCESS

# Find the path to the SMS
smsname = sms_util.sms(sms_name=sms_id_root)
path_sms = os.path.join(smsname.sms_directory_path,
string.lower(smsname.get_name())+".sms_%i" % smsname.sms_version)

# Use the function sms_parse_comcon to search for and parse the COMCONs.
comcon_vals = sms_parse_comcon(path_sms)

# Parse the calendar display because the SMS doesn't contain STATION. Purpose is to
# get the TDRS station and put it into the dictionary comcon_vals.

# Find the path to the calendar.
cclist = ccl_util.ccl(sms_id_root)
cclist.get()
cal = calendar_util.cal_display()
# The lines we are interested in have the strings 'M Com' or 'I Com'.
# disp will be a list of dictionaries. For each observation, disp contains
# begin_time and end_time of SU and "data_string" = the result of the parse.
disp = cal.parse_calendar_display('calendar -display -supp','[IM] Com','true')
for each in disp:
for s in comcon_vals['SMSTIME=']:
# Verify that the SMS time is in between start and end times in calendar display.
stime = time_util.spss_time(s)
if (stime >= each['begin_time']) and (stime <= each['end_time']):
cal_tdrs = each['data_string'][21:22]
# Insert 'value' into 'comcon_vals'.
comcon_vals['STATION'].append('TD'+cal_tdrs)

# Construct the message to NCC. If no COMCONs were found in the input SMS, then
# the message states "no critical COMCONs are required..." If COMCONs were found,
# then state the start time, end time, duration, service, and station for TDRS service
# required.
#
# Open the file containing the mail message, and write header information.
f = open("notify_ncc_comcons.txt",'w')
f.write(" To: NCC Forecast Schedulers\n")
f.write("From: Science Planning and Scheduling Team, STScI\n\n\n")
if len(comcon_vals['SMSTIME=']) > 0:
f.write("The table below lists real-time contacts needed to support\n")
f.write("HST science observations. The start and end time format is\n")
f.write("YYYY.DOY:HH:MM:SS. The format for duration is D:HH:MM:SS\n\n")
f.write("Please try to prevent TDRS shortfalls during these times.\n\n\n")
f.write(" Start Time \t End Time \t Duration \t Service Station\n")
f.write(" ---------- \t -------- \t -------- \t ------- -------\n")
# Pull the lists of values we need out of the dictionary comcon_vals.
smstime = comcon_vals['SMSTIME=']
service=comcon_vals['SERVICE']
start = comcon_vals['START=']
end = comcon_vals['END=']
station = comcon_vals['STATION']
# Loop over the number of values in the START field.
for each in range(0,len(start)):
duration = time_util.delta_time(end[each]) - time_util.delta_time(start[each])
startspss = time_util.spss_time(smstime[each])
endspss = str(startspss + duration)
# Write the times and TDRS data to the mail message file.
line = str(smstime[each][:17])+" "+endspss+"\t"+str(duration)+" "+str(service[each])+\
"\t"+station[each]+"\n"
f.write(line)
else:
f.write(" *** No critical COMCONs are required for the %s SMS. ***\n" % sms_id_root)

# Add the name of the SMS supervisor according to time_util to the bottom of the
# notification. Assume that the SMS week is the first five characters of the SMS name.
# If the supervisor cannot be found in sms-honcho.dat, then prompt the user for it.
# Use the variable sms_yydoy from above.
week_id = time_util.weekid(sms_id_root)
honcho = week_id.get_honcho()
if (not honcho):
print "Warning: Supervisor name was not found for SMS %s" % week_id
print "Please enter supervisor name, phone #, and e-mail address."
honcho=raw_input(">")
f.write("\nIf there are any concerns, please contact:\n")
f.write(honcho+"\n")
f.write("or send e-mail to planinst@stsci.edu.")
f.close()

# Mail the notification and delete it from the local directory.
print "Mailing notification to NCC..."
subject = 'HST real time contact listing for ' + str(week_id) + ' SMS'
pe_dat = spss_sys_util.resolver('PE_DAT')
distr = os.path.join(pe_dat,'ncc.dis')
spss_sys_util.mail(distr,subject,'notify_ncc_comcons.txt',1)
# Also send the message to operations account
email_dis =spss_sys_util.resolver('PE_DAT','operations_email.dis')
spss_sys_util.mail(email_dis,subject,'notify_ncc_comcons.txt',1)
os.remove('notify_ncc_comcons.txt')

return spss_sys_util.SUCCESS

def sms_parse_comcon(path_sms=None):
# Parse the SMS, searching for SU's that contain ':COMCON'.
# Dictionary 'comcon_vals' contains the keywords and values we are searching for.
# Loop over lines in SMS. If line contains the string ':COMCON', then append the
# next few lines to 'su_comcon'. End after the string ';SMSTIME=' is encountered.
comcon_vals = {'SMSTIME=':[],'START=':[],'END=':[],'SERVICE':[],'STATION':[]}
f = open(path_sms,'r')
eof=0
while (not eof):
line = f.readline()
if (len(line) == 0):
eof = 1
else:
if (string.find(line,':COMCON') != -1):
su_comcon = line
not_end=1
while (not_end):
next_line = f.readline()
su_comcon = su_comcon + next_line
if (string.find(next_line,';SMSTIME=') != -1):
not_end=0
# Parse su_comcon and fill values in comcon_vals.
for key in comcon_vals.keys():
su_split = re.split(str(key),string.strip(su_comcon))
if (len(su_split) > 1):
if (key == 'START=' or key == 'END='):
# In this case, key and field is '(start or end)=(...,...,...)'.
# Split this string on the ')'; Get the last relative time entry.
field = string.split(su_split[1],')')
time_list = string.split(field[0],',')
rel_time = time_list[len(time_list)-1]
# Convert from a string to a delta_time object.
rel_time = time_util.delta_time(rel_time)
else:
su_split = string.split(su_split[1],',')
rel_time = su_split[0]
# If the relative time string has any parentheses or whitespace, remove them
rel_time = re.sub('[() ]','',rel_time)
# Insert the relative time string into 'comcon_vals'.
comcon_vals[key].append(rel_time)
f.close()

return comcon_vals