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

Поисковые слова: arp 220
#!/usr/bin/env python
#
# *****************************************************************************
#
# ctm_search.py - Python module to perform parsing of the CL CTMLINE.RPT
# report file. There are two methods for using this module:
#
# 1) From Python - Invocation of the ctmSearch function in this
# module generates a set of 7 data files that
# are used as the input to the Load Check
# Utility.
#
# 2) From Command line, DCL procedure, or shell script -
# Invocation of ctm_search.py as a Python
# script generates a set of 7 data files that
# are used as the input to the Load Check
# Utility.
#
# *****************************************************************************
#
# DEVELOPMENT HISTORY
#
# AUTHOR PR NO. DATE DESCRIPTION OF CHANGE
# ---------- --------- -------- --------------------------------------------
# J. Lecourt PR44502 06/02 Original Implementation
# + J. Taylor PR44503
#
# J. Lecourt PR46322 10/02 Add HGA_INI to MSCH text block search,
# move text block search first, and then
# add MSCH text block "| " to list of
# lines removed from cleanLines
#
# INVOCATION METHOD:
# From Python -
# ctmSearch input_file_spec
#
# From Command line, DCL procedure, or shell script -
# VMS: python ctm_search.py input_file_spec
# UNIX: ctm_search.py input_file_spec
#
# INPUT PARAMETERS:
# input_file_spec - file specification for the CTMLINE.RPT file to be parsed
# (required); For CL runs with TMLSEGMENT not equal 0
# this file specification should include a wild card (*),
# when calling from UNIX command line with a wild card
# the input file specification must be enclosed in quotes
# output_dir - directory specification for the output directory
# (optional - default is the current working directory)
#
# LOGICALS (VMS) OR ENVIRONMENT VARIABLES (UNIX):
# None
#
# REFERENCES:
# None
#
# NOTES:
# 1) The function ctmSearch returns the following values:
# 0 - Success
# 1 - Error
#
# 2) The script exit status is as follows:
# 0 - Success
# 1 - Error
#
# -----------------------------------------------------------------------------
#
# Import required modules
#
import sys
import string
import os
import glob
#
# -----------------------------------------------------------------------------
# Local function definitions
# -----------------------------------------------------------------------------
#
# *****************************************************************************
# testline - search a line of text for elements in a list of search strings
# and returns true if the line contains any element from the list
# *****************************************************************************
#
def testline(line,searchStrings):
for element in searchStrings:
if string.find(line,element) >=0:
return 1
return 0
#
# *****************************************************************************
# testLines - search a list of lines of text for lines containing any element a
# from a list of search strings and return a list of the matching
# lines surrounded by the given number of lines of text before and
# after the matching line
# *****************************************************************************
#
def testLines(lines,searchStrings,linesBefore=0,linesAfter=0):
outLines=[]
index=0
#
for line in lines:
if testline(line,searchStrings):
if index-abs(linesBefore) < 0:
startIndex=0
else:
startIndex=index-abs(linesBefore)
#
outLines.extend(lines[startIndex:index+abs(linesAfter)+1])
#
index=index+1
#
return outLines
#
# *****************************************************************************
# searchText - search a list of lines of text for lines containing any element
# from a list of search strings and output the matching lines
# sorrounded by the given number of lines of text before and
# after the matching lines to the given file
# *****************************************************************************
#
def searchText(lines,searchStrings,outFile,linesBefore=0,linesAfter=0):
ofile = open(outFile,"w")
outLines=testLines(lines,searchStrings,linesBefore,linesAfter)
ofile.writelines(outLines)
ofile.close
#
# *****************************************************************************
# ctmSearch - search the given report file placing output in the given output
# directory.
# *****************************************************************************
#
def ctmSearch(inputFile, outDir="."):
#
# ----------------------------------------------------------------------------
# See if output directory is valid
# ----------------------------------------------------------------------------
#
if not os.path.isdir(outDir):
print "ctmSearch: Invalid Output Directory: "+outDir
return 1
#
# ----------------------------------------------------------------------------
# Terminate output directory with '/' if it is not a full VMS file
# specification
# ----------------------------------------------------------------------------
#
if string.find(outDir,"]") < 0:
outDir = outDir + "/"
#
# ----------------------------------------------------------------------------
# Process multiple input files in case a * or ? was used in the input file
# specification
# ----------------------------------------------------------------------------
#
cleanLines = []
#
# - - - - - - - - - - - - - - - - - - - - - - - - - -
# Development; VMS DCL or UNIX
#
# if sys.platform == 'OpenVMS':
# import pyvms
# file_list = pyvms.crtl_from_vms(inputFile,None,1)
# else:
# file_list = glob.glob(inputFile)
# - - - - - - - - - - - - - - - - - - - - - - - - - -
# Operations; require UNIX style input regardless of platform
#
file_list = glob.glob(inputFile)
if not file_list:
raise IOError('\n\nInput file: %s\n not found!' %inputFile)
# - - - - - - - - - - - - - - - - - - - - - - - - - -
#
for inFile in file_list:
#
# ----------------------------------------------------------------------------
# See if input file is valid.
# ----------------------------------------------------------------------------
#
if not os.path.isfile(inFile):
print "ctmSearch: Invalid Input file: "+inFile
return 1
#
# ----------------------------------------------------------------------------
# Open the input file, read in all the lines into a list, close the input file
# ----------------------------------------------------------------------------
#
file = open(inFile,"r")
cleanLines.extend(file.readlines())
file.close
#
# ----------------------------------------------------------------------------
# HGA stop track or HGA ini, from MSCH text block, one HGA per file
# These items are extracted first so that all MSCH text
# block lines can be deleted from 'cleanLines' prior to
# performing the searches for the CL commanding items.
# ----------------------------------------------------------------------------
#
hgaStpLines = testLines(cleanLines,["HGA_STP","HGA_INI"])
searchText(hgaStpLines,["HGA = 1"],outDir+"ctm_hg1end.dat")
searchText(hgaStpLines,["HGA = 2"],outDir+"ctm_hg2end.dat")
#
# ----------------------------------------------------------------------------
# Remove all blank lines, the four page header lines, and the MSCH text
# block lines from the list of 'cleanLines'
# ----------------------------------------------------------------------------
#
delIndex = []
index = 0
#
for line in cleanLines:
if (string.strip(line)=="" or
line=="1\n" or
testline(line,["MISSION TIMELINE REPORT FOR THE PERIOD:",
"ORBITAL SCP CRIT",
"EVENT COMMAND CMD DESCRIPTION",
"------- -------- ---- -----------",
"| "])):
delIndex.append(index)
#
index=index+1
#
delIndex.reverse()
#
for index in delIndex:
del cleanLines[index]
#
# ----------------------------------------------------------------------------
# ZOE data
# ----------------------------------------------------------------------------
#
searchText(cleanLines,["XZOE","EZOE"],outDir+"ctm_zoe.dat")
#
# ----------------------------------------------------------------------------
# Xmitter and Lga Commands
# ----------------------------------------------------------------------------
#
searchText(cleanLines,["HMA1N","HMA1F","HMA2N","HMA2F","HSSA1N","HSSA1F",
"HSSA2N","HSSA2F","HR1AL2P","HR1FL2P"],
outDir+"ctm_xl.dat")
#
# ----------------------------------------------------------------------------
# 486 SSR record commands, science and engineering
# ----------------------------------------------------------------------------
#
searchText(cleanLines,["D486SRMG"],outDir+"ctm_ssr.dat",0,3)
#
# ----------------------------------------------------------------------------
# HGA spline commands, first pass with GEA1 and GEA2 together
# ----------------------------------------------------------------------------
#
ctmHgabegLines = testLines(cleanLines,["HHGATEPH"],0,3)
#
# ----------------------------------------------------------------------------
# Split HGA splines into individual files for GEA1 and GEA2
# ----------------------------------------------------------------------------
#
searchText(ctmHgabegLines,["GEA1"],outDir+"ctm_hgea1.dat",1,2)
searchText(ctmHgabegLines,["GEA2"],outDir+"ctm_hgea2.dat",1,2)
#
return 0
#
# *****************************************************************************
# ctm_search - Invocation of ctm_search.py as a Python script generates a set
# of 7 data files that are used as the input to the Load Check
# Utility.
# *****************************************************************************
#
if __name__ == '__main__':
#
# *****************
# * START PROGRAM *
# *****************
#
# -----------------------------------------------------------------------------
# Check the number of arguments
# -----------------------------------------------------------------------------
#
if len(sys.argv) > 3:
print "ctm_search.py: Too many arguments (%i)"%(len(sys.argv)-1)
print "VMS Usage: python ctm_search.py input_file [output_directory]"
print "UNIX Usage: ctm_search.py input_file [output_directory]"
sys.exit(1)
#
# -----------------------------------------------------------------------------
# See if input file was input on the command line.
# -----------------------------------------------------------------------------
#
try:
inputFile = sys.argv[1]
except IndexError:
inputFile = ""
#
# -----------------------------------------------------------------------------
# See if output directory was input on the command line.
# -----------------------------------------------------------------------------
#
try:
outDir = sys.argv[2]
except IndexError:
outDir = "."
#
# -----------------------------------------------------------------------------
# Call the search routine
# -----------------------------------------------------------------------------
#
stat = ctmSearch(inputFile,outDir)
#
if stat:
print "VMS Usage: python ctm_search.py input_file [output_directory]"
print "UNIX Usage: ctm_search.py input_file [output_directory]"

sys.exit(stat)