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

Поисковые слова: vallis
#
#MODULE create_dummy_tdrs
#
#***********************************************************************
"""

**PURPOSE** --
Fake TDRS Schedule generator

**DEVELOPER** --
Don Chance

**MODIFICATION HISTORY** --
Initial implementation 4/22/03
fix several bugs dc 10/9/03
fix more bugs dc 12/1/03
fix backwards logic dc 10/19/04
fix bug so that correct tdrs.dat file is copied. dc 1/11/05
add missing default. dc 1/17/06
changes for PASS 32.90 (TDO). dc 12/18/06
fix output directory problem. dc 5/8/09
code cleanup; use namelist_util.read_namelist dc 9/3/15
"""
#***********************************************************************
import spss_sys_util
import os
import pass_util
import sched_util
import spst_getopt
import sys
import configure_util
import re
from namelist_util import read_namelist

__version__ = '15.09.03'


def run(ms_output_directory=None, *args):
"""If needed, create a TDRS.LIS file with the PASSS program DST.

Then, create a TDRS schedule with SCHEDMAN.

Usage:
do create_dummy_tdrs [-out=]
[-software_version=ops|old|new]
[-config_file=]
[-debug]

The '-out=' defaults to .
The '-software_version' defaults to 'ops' , others are 'old','new'
The '-config_file= is the path to the config file
"""

if not ms_output_directory:
# Spew out the usage and quit when no ms_output_directory is given
print run.__doc__
return spss_sys_util.SUCCESS

if not os.path.isdir(ms_output_directory):
raise ValueError("ms output directory %s does not exist." % ms_output_directory)

if not spss_sys_util.on_sun():
raise OSError('\n\nPlease run on UNIX!')

allowed_options = ['out=', 'software_version=', 'config_file=', 'debug']

try:
options, parms = spst_getopt.spst_getopt(args, allowed_options)
except Exception, e:
e = '%s\n%s' % (e, run.__doc__)
raise IOError(e)

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

if '-out' in options.keys():
out_dir = options['-out']
if not os.path.isdir(out_dir):
raise ValueError("-out directory %s does not exist." % out_dir)
else:
out_dir = ms_output_directory

if '-config_file' in options.keys():
config_file = options['-config_file']
else:
config_file = spss_sys_util.glob(os.path.join(ms_output_directory,
"*_config.csh"))
if config_file:
config_file = config_file[0]
else:
raise ValueError("Config file not found in %s" % ms_output_directory)

if '-debug' in options.keys():
debug = True
else:
debug = False

ssr_file = (spss_sys_util.glob(os.path.join(ms_output_directory,
"*.ssr")) or
spss_sys_util.glob(os.path.join(ms_output_directory,
"*.sdr")))
if debug:
print "ssr_file", ssr_file
rsv_path = spss_sys_util.glob(os.path.join(ms_output_directory,
"*.rsv"))
if debug:
print "rsv_path", rsv_path

tdrs_dat = os.path.join(out_dir,
'tdrs.dat')
gstdn_dat = os.path.join(out_dir,
'gstdn.dat')

# Copy the tdrs dat file to the ms output directory
for line in open(config_file).readlines():
split_line = line.split()
if "CFG_TDRSSCHD" in split_line:
old_tdrs_dat = split_line[2]
if debug:
print "old_tdrs_dat", old_tdrs_dat
if "CFG_GSTDNCON" in split_line:
if debug:
print "copy", split_line[2], "to", gstdn_dat
spss_sys_util.filecopy(split_line[2], gstdn_dat)
if ssr_file:
update_option = 'UPD'
if rsv_path:
lines = open(rsv_path[0]).readlines()

for line in lines:
if re.search(r'.*INITIAL\s+REQUEST\s.*', line):
update_option = 'CREA'

nml_path = create_dst_namelist(out_dir,
update_option,
sw_version)
if debug:
print "config_file", config_file
print "nml_path", nml_path
print "tdrs_dat", tdrs_dat
print "old_tdrs_dat", old_tdrs_dat
print "ssr_file[0]", ssr_file[0]
print "out_dir", out_dir
print "running dst"
run_dst(config_file,
nml_path,
tdrs_dat,
old_tdrs_dat,
ssr_file[0],
out_dir,
sw_version)
else:
if debug:
print "copying", old_tdrs_dat, tdrs_dat
spss_sys_util.filecopy(old_tdrs_dat, tdrs_dat)

if debug:
print "tdrs_dat", tdrs_dat
print "gstdn_dat", gstdn_dat
print "running schedman"
run_schedman(out_dir,
tdrs_dat,
gstdn_dat,
sw_version)


def run_dst(config_file, namelist_path, dat_file_path, old_dat_file_path, ssr_path, out_dir, sw_version='ops'):
"""Run the DST program.
"""
script = pass_util.pass_script("dst", sw_version)

# Source the config file
script.source_file(config_file)

script.add_environment_definition("USCH_OUT_RPT",
os.path.join(out_dir, "tdrs.sum"))
script.add_environment_definition("USCH_OUT_SCHED", dat_file_path)
script.add_environment_definition("USCH_IN_SCHED", old_dat_file_path)
script.add_environment_definition("USCH_IN_SSR", ssr_path)
script.add_environment_definition("USCH_IN_NML", namelist_path)
print "Running dst..."
status, out = script.run(out_dir, "run_dst")
print "STATUS =", status
print out

return


def create_dst_namelist(out_dir, run_opt='UPD', sw_version='ops'):
"""Create the namelist for the DST program.

Returns the path to the namelist.
"""
generic_namelist_path = pass_util.get_pass_namelist_path('dst.nml', sw_version)
namelist = read_namelist(generic_namelist_path)
namelist['DSNDSTNML']['TDR_PROC_FUN'] = ["'%s'" % run_opt]
namelist_path = os.path.join(out_dir, "dst.nml")
pass_util.write_namelist(namelist, namelist_path)
return namelist_path


def run_schedman(out_dir, dat_file_path, gstdn_file_path, sw_version='ops'):
"""Run the PASS executable DSDT.
"""
sched = sched_util.tdrs_schedule('summary', out_dir)
sched.set_output_directory(out_dir)
sched.set_old_tdrs_file_path(dat_file_path)
print "Running schedman (dsdt)..."
sched.run(sw_version=sw_version)
if os.path.exists(gstdn_file_path) and os.path.exists(dat_file_path):
gstdn = configure_util.configured_file('CFG_GSTDNCON', gstdn_file_path)
sched.span = sched_util.get_times_from_tdrs(dat_file_path)
#print "sched.span", sched.span
if sched.span:
new_product = configure_util.configured_product('sch', span=sched.span)
new_product.add_configured_file(sched)
new_product.add_configured_file(gstdn)
comment = "Schedule data spans:%s updated for TDRS schedule file" % new_product.get_span()
new_product.set_comment(comment)
new_product.configure_pickle(pickle_directory=out_dir)
# echo the log file
logfile = sched.get_log_path()
if os.path.isfile(logfile):
print open(logfile).read()
else:
print "WARNING: schedman log file could not be found:", logfile

if __name__ == '__main__':
run(*tuple(sys.argv[1:]))