Package invocations :: Package cu8 :: Module selectEditlog
[hide private]

Source Code for Module invocations.cu8.selectEditlog

 1  #! /usr/bin/env python 
 2  #------------------------------------------------------------------------------ 
 3  #$Id: selectEditlog.py 4139 2007-10-23 15:40:09Z RossCollins $ 
 4  """ 
 5     Edits a specific keyword in all files in a certain filter in a 
 6     recalibration log by a correction term. 
 7   
 8     @author: N.J. Cross 
 9     @org:    WFAU, IfA, University of Edinburgh 
10  """ 
11  #------------------------------------------------------------------------------ 
12  import os 
13  import sys 
14   
15  from   wsatools.DbConnect.DbSession import DbSession 
16  #------------------------------------------------------------------------------ 
17  # Entry point for script. 
18   
19  # Allow module to be imported as well as executed from the command line 
20  if __name__ == '__main__': 
21      logFilePath=sys.argv[1]       # recalibration log name 
22      filterID=int(sys.argv[2])     # filter ID for correction 
23      keywordName=sys.argv[3]       # keyword to be altered 
24      correction=float(sys.argv[4]) # value that keyword must be altered by. 
25      db=DbSession() 
26      catalogues=db.query(selectStr="catName", 
27                          fromStr="Multiframe", 
28                          whereStr="frameType like '%stack' and deprecated<128 " 
29                          +"and filterID=%d" % filterID) 
30      del db 
31      outLogFilePath=os.path.basename(logFilePath.split('.')[0]+'_update.log') 
32      lines=file(logFilePath).readlines() 
33      # first produce dictionary of initial lines 
34      catFileDict={} 
35      for index,line in enumerate(lines): 
36          wrds=line.split() 
37          if wrds[0][:4]=="w200" and wrds[1]=="#1": 
38              catFileDict[wrds[0]]=index 
39      newLines=[] 
40      for catName in catalogues: 
41          prtName=os.path.basename(catName) 
42          try: 
43              index=catFileDict[prtName] 
44          except: 
45              continue 
46          test=False 
47   
48          while not test: 
49              if lines[index][0]=="w" and lines[index].split()[0]!=prtName: 
50                  test=True 
51              elif lines[index][0]!="w" and lines[index].split()[0]!="HISTORY": 
52                  wrds=lines[index].split("=") 
53                  if wrds[0]==keywordName: 
54                      value,comment=wrds[1].split("/") 
55                      value=float(value)+correction 
56                      newline="%s=\t\t%5.2f  / %s" % (keywordName,value,comment) 
57                      newLines.append(newline) 
58                  else: 
59                      newLines.append(lines[index]) 
60              else: 
61                  newLines.append(lines[index]) 
62   
63              index+=1 
64              if index==len(lines): 
65                  test=True 
66      file(outLogFilePath,'w').writelines(newLines) 
67   
68  #------------------------------------------------------------------------------ 
69  # Change log: 
70  # 
71  # 19-Oct-2007,  NJC: Original version 
72  # 23-Oct-2007,  NJC: Added deprecated<128 to avoid duplicate files in the 
73  #                    output log file. 
74