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

Поисковые слова: http astrokuban.info astrokuban
#
#MODULE tdrs_optimizer
#
#***********************************************************************
"""

**PURPOSE** --
Run the PASS TDO (TDRS Optimization) tool.

**DEVELOPER** --
Don Chance

**MODIFICATION HISTORY** --
Initial implementation 12/20/06
setup mscl pickle for next mscl run. dc 1/2/07
fix bug in parsing arguments. dc 3/20/07
sort pickle list. dc 4/10/07
add GUI. dc 7/27/09
updated for PASS build 33.84. drc 8/24/11
comment out message at end. drc 11/1/11
modified for PASS 33.87. drc 3/20/12
check number of tapedumps in output merge SMS. drc 4/3/12
"""
#***********************************************************************
import spss_sys_util
import configure_util
import spst_getopt
import mscl_util
import re
import sms_util
import os
import glob
import sys
import string
HAS_GUI = False
# must have at least Python version 2.5 to use GUI
if sys.hexversion >= 33882352:
try:
import tdo_namelist_editor
HAS_GUI = True
except:
pass

__version__ = '4/3/12'

def run(ms_name=None, *args):
"""Run TDO

Usage:
do tdrs_optimizer [-namelist= [-software_version=new|ops|old]
[-gui]

"""

if not ms_name:
print run.__doc__
return spss_sys_util.SUCCESS

allowed_options = ['namelist=', 'software_version=', 'gui']

options, parms = spst_getopt.spst_getopt(args, allowed_options)

if '-software_version' in options.keys():
sw_version = string.lower(options['-software_version'])
else:
sw_version = 'ops'

if '-namelist' in options.keys():
namelist_path = options['-namelist']
else:
namelist_path = None

if '-gui' in options.keys() and HAS_GUI:
use_gui = True
elif '-gui' in options.keys() and not HAS_GUI:
print "GUI option specified, but no GUI available!"
use_gui = False
else:
use_gui = False


path = spss_sys_util.resolver('PASS_OUTPUT_PICKLES')
lib = configure_util.pickle_library_list(path)
pickle_list = lib.get_pickle_list().filter('mscl', ms_name.lower())
pickle_list.sort()

mscl=configure_util.get_pickled_object(pickle_list.pick_a_pickle())

# Check to see if there is an SSR file in the MS output directory.
# Warn if this file is found.
ssr_files = mscl.my_ms.get_output_file('ssr')
if ssr_files:
print "WARNING: TDO is normally only be run on a PASS products that"
print " have NOT produced an SSR file."
print " SSR file:", ssr_files[0]
yn = raw_input("Do you want to continue anyway (y/n)? ").lower()
if len(yn) == 0 or yn[0] != 'y':
print "Quitting"
return not spss_sys_util.SUCCESS

tdo=mscl_util.tdo(mscl.my_ms)
print "Using TDO input directory:", tdo.input_directory
#tdo.set_top_level(1)
tdo.set_software_version(sw_version)
if namelist_path:
tdo.set_namelist_template(namelist_path)
if use_gui:
new_namelist_path = os.path.join(tdo.input_directory, 'tdo_input_'
+ tdo.run_time.get_localtime().strftime("%Y%j%H%M")
+ '.nml')
tdo_namelist_editor.gui(tdo, new_namelist_path, mscl.my_ms.sms.get_begin_time())
print "TDO input namelist (output of the GUI):", new_namelist_path
tdo.set_namelist_template(new_namelist_path)
status, output = tdo.run()
print output
print "TDO return status:", status
if not status:
print "Constructing new MSCL pickle..."
merge_sms_list = mscl.my_ms.get_merge_sms_list()
# Search the namelist for the merge SMS letter
match = re.findall("MERG_SMS_LETTER\s*=\s*'(.)'",
open(tdo.get_namelist()).read())
if not match:
print "Error: Could not find the merge SMS letter in the TDO namelist (%s)" % tdo.get_namelist()
print "Quitting..."
return not spss_sys_util.SUCCESS
elif len(match) > 1:
print "Error: More than one merge SMS letter found in TDO namelist (%s)" % tdo.get_namelist()
print "Quitting"
return not spss_sys_util.SUCCESS
else:
merge_sms_letter = match[0]

merge_sms_name = merge_sms_letter + mscl.my_ms.get_ms_name()[1:]
d_sms = 'D' + mscl.my_ms.get_ms_name()[1:]

# Remove the D sms and replace it with the Q sms
if (d_sms in [str(x) for x in merge_sms_list] and
merge_sms_name not in [str(x) for x in merge_sms_list]):
new_merge_sms_list = []
for sms in merge_sms_list:
if str(sms) == d_sms:
new_merge_sms_list.append(sms_util.sms_file(merge_sms_name))
else:
new_merge_sms_list.append(sms)
# if there is no D sms, just add the Q sms.
elif d_sms not in [str(x) for x in merge_sms_list]:
new_merge_sms_list = merge_sms_list + [sms_util.sms_file(merge_sms_name)]
# if the Q sms is already in the list, do nothing.
else:
new_merge_sms_list = merge_sms_list

mscl.my_ms.set_merge_sms_list(new_merge_sms_list)

# Now set the sch config file to the newly created sch pickle.
slib = configure_util.pickle_library_list(tdo.get_output_directory())
spickle_list = slib.get_pickle_list().filter('sch')

if len(spickle_list) > 1:
sched = configure_util.get_pickled_object(spickle_list.pick_a_pickle())
elif len(spickle_list) == 1:
sched = configure_util.get_pickled_object(spickle_list[0])
else:
print "Error: No sch pickle could be found in", tdo.get_output_directory()
print "Quitting"
return not spss_sys_util.SUCCESS
mscl.set_config('sch', sched)
mscl.set_comment('TDO run ' + os.path.basename(mscl.my_ms.get_tdo_base_directory()) + ' for ' + mscl.get_comment())

# Save the modified MSCL pickle into the TDO output directory
new_mscl_pickle_path = mscl.configure_pickle(tdo.get_output_directory())

print "New MSCL pickle using TDO created TDRS schedule and %s merge SMS:" % merge_sms_name
print new_mscl_pickle_path
#print "Start your next MSCL run with:"
#print ' do pass_gui %s' % new_mscl_pickle_path
#print "to use this pickle."

check_merge_sms(tdo, mscl)

return spss_sys_util.SUCCESS


def check_merge_sms(tdo, mscl):
"""Check that there is one tapedump per day in the merge SMS.
"""
sms_span = mscl.my_ms.get_span()
dms_sms = glob.glob(os.path.join(tdo.get_output_directory(), '*.sms'))
if dms_sms:
ntapedumps = open(dms_sms[0]).read().count('TAPEDUMP')
ndays_sms = (float(sms_span.end - sms_span.start))/86400.
if ntapedumps < int(ndays_sms):
print "WARNING -- One TAPEDUMP per day is expected in the merge SMS, but less than one was found."
print "Number of TAPEDUMPS: ", ntapedumps
print "Length of SMS (days):", ndays_sms
else:
print "ERROR -- No merge SMS found!"


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