Package helpers :: Module UpdateStacks
[hide private]

Source Code for Module helpers.UpdateStacks

  1  #! /usr/bin/env python 
  2  #------------------------------------------------------------------------------ 
  3  #$Id: UpdateStacks.py 6790 2010-04-07 15:10:17Z RossCollins $ 
  4  """ 
  5     Updates stacks from catalogues with CASUs perl script. 
  6     The timestamp of the updated stack will be set to the timestamp of 
  7     the (input) catalogue file. 
  8   
  9     usage: ./UpdateStacks.py [-d/--dir dirlist] [-o/--outdir log_outdir] date versionnr 
 10   
 11     @author: E. Sutorius 
 12     @org:    WFAU, IfA, University of Edinburgh 
 13  """ 
 14  #------------------------------------------------------------------------------ 
 15  import os 
 16  import sys 
 17  import time 
 18  import getopt 
 19  import dircache as dc 
 20   
 21  import wsatools.FitsUtils       as fits 
 22  import wsatools.SystemConstants as sysc 
 23  import wsatools.Utilities       as utils 
 24  #------------------------------------------------------------------------------ 
 25   
26 -def getFileInfo(dateDir):
27 cmd = ''.join(["ls --indicator-style=none -o --time-style=full-iso ", 28 os.path.join(dateDir,"*" + sysc.stackSuffix() + "*" \ 29 + sysc.mefType() + "*")]) 30 fileDict = {} 31 for entry in os.popen(cmd): 32 _modes, _uid, _uname, _size, ymd, hms, _tz, filename = \ 33 entry.replace('\n', '').split() 34 fileDict[filename] = (ymd, hms) 35 return fileDict
36 37 #------------------------------------------------------------------------------ 38
39 -def fixTime(fileDict):
40 for filename in fileDict: 41 ymdhms = 'T'.join([fileDict[filename][0], 42 fileDict[filename][1]\ 43 [:fileDict[filename][1].find('.')]]) 44 timetpl = time.strptime(ymdhms,'%Y-%m-%dT%H:%M:%S') 45 modtime = time.mktime(timetpl) 46 os.utime(filename,(time.time(),modtime))
47 48 #------------------------------------------------------------------------------ 49
50 -def locateDateDir(date_v, searchDirs):
51 dateDir = [] 52 for direc in searchDirs: 53 dirList = dc.listdir(direc) 54 if date_v in dirList: 55 dateDir.append( os.path.join(direc, date_v)) 56 return dateDir
57 58 #------------------------------------------------------------------------------ 59
60 -def runScript(updDir):
61 cmd = ''.join([os.path.join(sysc.casuCodePerlPath(), "updateheader.pl"), 62 " '", os.path.join(updDir, 63 "*" + sysc.catSuffix() \ 64 + sysc.catType()), "'"]) 65 return os.popen(cmd).readlines()
66 67 #------------------------------------------------------------------------------ 68
69 -def make_log(logList, logText):
70 """print log to screen and append to list""" 71 print logText 72 logList.append(logText) 73 return logList
74 75 #------------------------------------------------------------------------------ 76
77 -def write_log(logFile, logList):
78 """write log to file""" 79 lffh = open(logFile,'w') 80 for entry in logList: 81 lffh.write(entry.rstrip() + '\n') 82 lffh.close()
83 84 #------------------------------------------------------------------------------ 85
86 -def usage():
87 print "usage: ./UpdateStacks.py [-d/--dir dirlist] [-o/--outdir outdir] date versionnr\n" 88 print " date is the date which is to be updated." 89 print " versionnr is the versionnr for which the update should take place." 90 print " dirlist is a list of directories where '<date>_v<versionnr> may reside\n (default: [massStorageRaidFileSystem()/fin_dir()])." 91 print " outdir: log file directory (default: " + \ 92 sysc.cambInfoPath() + ")"
93 94 #------------------------------------------------------------------------------ 95
96 -def main(argv):
97 """ Update stacked image headers with the final catalogue header 98 calibration info and some extra QC stuff. 99 100 date is the date to be update. 101 version is the version number. 102 """ 103 104 try: 105 opts, args = getopt.getopt(argv[1:], "d:o:h:",["dir=","outdir=","help="]) 106 except getopt.GetoptError: 107 # print help information and exit: 108 print argv[1:] 109 usage() 110 sys.exit(2) 111 112 logLines = [] 113 resultLog = [] 114 logOutDir = sysc.cambInfoPath() 115 mainDirs = [os.path.join(disk, sysc.fitsDir()) 116 for disk in sysc.massStorageRaidFileSystem()] 117 118 for o, a in opts: 119 if o in ("-d","--dir"): 120 mainDirs = a.split(',') 121 if o in ("-h","--help"): 122 usage() 123 sys.exit(0) 124 if o in ("-o","--outdir"): 125 logOutDir = a 126 127 if len(args) != 2: 128 print args 129 usage() 130 sys.exit(2) 131 132 update = args[0] 133 versionnr = args[1] 134 date_v = '_v'.join([update, versionnr]) 135 136 dateDirs = locateDateDir(date_v, mainDirs) 137 utils.ensureDirExist(logOutDir) 138 logFileName = os.path.join(logOutDir,"updateStacks" + \ 139 utils.makeMssqlTimeStamp()+".log") 140 updatedFiles = {} 141 if not dateDirs: 142 txt = ''.join([date_v, " not found in ", mainDirs]) 143 make_log(logLines, txt) 144 else: 145 for updateDir in dateDirs: 146 txt = ''.join(["Updating", updateDir, ": ", 147 utils.makeMssqlTimeStamp()]) 148 make_log(logLines, txt) 149 resultLog = runScript(updateDir) 150 logLines.extend(resultLog) 151 for line in logLines: 152 if "Updating" in line and "with" in line and \ 153 sysc.stackSuffix() + sysc.mefType() in line: 154 fileName = line.split()[1] 155 updatedFiles[fileName] = [] 156 txt = ''.join(["Done:", utils.makeMssqlTimeStamp()]) 157 make_log(logLines, txt) 158 dirInfo = getFileInfo(updateDir) 159 for fileName in updatedFiles: 160 updatedFiles[fileName] = dirInfo[fits.getCatalogue(fileName)] 161 fixTime(updatedFiles) 162 163 # write the log 164 write_log(logFileName, logLines)
165 166 #------------------------------------------------------------------------------ 167 # 168 if __name__ == '__main__': 169 main(sys.argv) 170 171 #------------------------------------------------------------------------------ 172 # Change log: 173 # 174 # 9-Oct-2006, ETWS: Original version. 175