Package helpers :: Module RemoveFitsKeys
[hide private]

Source Code for Module helpers.RemoveFitsKeys

  1  #! /usr/bin/env python 
  2  #------------------------------------------------------------------------------ 
  3  #$Id: RemoveFitsKeys.py 7245 2010-07-20 12:52:03Z RossCollins $ 
  4  """ 
  5     Removes FITS keywords from FITS headers of files in a given file list. 
  6   
  7     @author: E. Sutorius 
  8     @org:    WFAU, IfA, University of Edinburgh 
  9  """ 
 10  #------------------------------------------------------------------------------ 
 11  #  The first line of the filelist should contain a list of keywords 
 12  #  and a tuple of the hdr numbers from which the keyword should be deleted, 
 13  #  eg.: 
 14  # 
 15  #  # begin remove.log 
 16  #  KEY1(0);KEY2(1,2,3,4);KEY3(4) 
 17  #  path/to/file.fit 
 18  #  path/to/another.fits 
 19  #  # end of remove.log 
 20  # 
 21  #  will delete KEY1 from the primary HDU, KEY2 from all extension HDUs 
 22  #  and KEY3 from the fourth extension HDU for the given FITS files. 
 23  #------------------------------------------------------------------------------ 
 24  import os 
 25  import sys 
 26  import time 
 27   
 28  import wsatools.FitsUtils as fits 
 29  #------------------------------------------------------------------------------ 
 30   
31 -def readfiles(filename):
32 """Read filenames from log file.""" 33 xobj = open(filename,mode='r') 34 flist = xobj.readlines() 35 xobj.close() 36 filelist = [] 37 for line in flist: 38 if line.find('#') < 0: 39 filelist.append(line[:-1]) 40 return filelist
41 42 #------------------------------------------------------------------------------ 43
44 -def printkey(header, keyword):
45 """Print the keyword and the one before and after to the screen 46 (in case you need to restore them later) 47 """ 48 cardlist = header.ascardlist() 49 index = -1 50 # get the position of the key 51 for card in cardlist: 52 if repr(card).startswith(keyword): 53 index = cardlist.index(card) 54 55 if index >= 0: 56 if index > 0: 57 print cardlist[index-1] 58 print ">>>>",cardlist[index] 59 if index < len(cardlist)-1: 60 print cardlist[index+1]
61 62 #------------------------------------------------------------------------------
63 -def main(argv):
64 65 if len(argv) !=2: 66 print "argv: ", argv 67 print "usage: RemoveFitsKeys.py removelog" 68 raise SystemExit 69 70 tflog = argv[1] 71 72 filelist = readfiles(tflog) 73 fitskeyline = filelist.pop(0) 74 fitskeys = fitskeyline.replace('(','!(').split(';') 75 fitskeydict = {} 76 for entry in fitskeys: 77 key, hdus = entry.split('!') 78 fitskeydict[key] = hdus[1:-1].split(',') 79 80 print fitskeydict 81 print "You are to delete:" 82 for key in fitskeydict.iterkeys(): 83 print key,"from extension(s)",fitskeydict[key] 84 answer = raw_input("Proceed?[N]").upper() 85 if answer != 'Y': 86 raise SystemExit 87 88 # open file in update mode 89 for filename in filelist: 90 print filename 91 print "="*len(filename) 92 fimgptr = fits.open(filename,"update") 93 for key in fitskeydict.iterkeys(): 94 for hdu in fitskeydict[key]: 95 if int(hdu) < len(fimgptr): 96 hdr = fimgptr[int(hdu)].header 97 printkey(hdr,key) 98 if hdr.has_key(key): 99 del hdr[key] 100 print " ",key,"deleted\n" 101 fimgptr.flush() 102 103 # if file was modified, set the modification date back 104 # to the original one 105 getTime = False 106 if fimgptr[0].header.has_key("WSA_TIME"): 107 wsaTimestamp = fimgptr[0].header.get("WSA_TIME") 108 timetpl = time.strptime(wsaTimestamp[6:],'%y%m%d%H%M%S') 109 getTime = True 110 else: 111 if fimgptr[1].header.has_key("DATE"): 112 timestamp = fimgptr[1].header.get("DATE") 113 timetpl = time.strptime(timestamp,'%Y-%m-%dT%H:%M:%S') 114 getTime = True 115 116 if getTime == True: 117 modtime = time.mktime(timetpl) 118 os.utime(filename,(time.time(),modtime)) 119 print "file time reset done\n" 120 else: 121 print "WARNING: No WSA_TIME or DATE available, file time not modified.\n" 122 123 # close file 124 fimgptr.close()
125 126 #------------------------------------------------------------------------------ 127 # 128 if __name__ == '__main__': 129 main(sys.argv) 130 131 #------------------------------------------------------------------------------ 132 # Change log: 133 # 134 # 3-Feb-2006, ETWS: first version 135 # 10-Jul-2006, RSC: Replaced string module functions with str() equivalents as 136 # the string module with be obsoleted in Python 3.0 137