Package helpers :: Module FitsDiff
[hide private]

Source Code for Module helpers.FitsDiff

  1  #! /usr/bin/env python 
  2  #------------------------------------------------------------------------------ 
  3  #$Id: FitsDiff.py 8320 2011-06-02 20:08:35Z EckhardSutorius $ 
  4  """ 
  5     Checks differences in metadata of two FITS files. 
  6     Run in screen at least 200 chars wide. 
  7   
  8     @author: E. Sutorius 
  9     @org:    WFAU, IfA, University of Edinburgh 
 10  """ 
 11  #------------------------------------------------------------------------------ 
 12  from   collections import defaultdict 
 13  import os 
 14   
 15  from   wsatools.CLI                    import CLI 
 16  import wsatools.FitsUtils                  as fits 
 17  from   wsatools.Logger                 import Logger 
 18  from   wsatools.SystemConstants        import SystemConstants 
 19   
 20  #------------------------------------------------------------------------------ 
 21  # Allow module to be imported as well as executed from the command line 
 22  if __name__ == "__main__": 
 23      CLI.progArgs["comment"] = "Diffing FITS files" 
 24      CLI.progArgs += [ 
 25          CLI.Argument("fileone", ''), 
 26          CLI.Argument("filetwo", '') 
 27          ] 
 28       
 29      CLI.progOpts += [ 
 30          CLI.Option('c', "commhist", "check also HISTORY and COMMENT"), 
 31          CLI.Option('e', "extnum", "check only these extensions", 
 32                     "LIST", '') 
 33          ] 
 34   
 35      cli = CLI("FitsDiff", "$Revision: 8320 $") 
 36      Logger.isVerbose = False 
 37      Logger.addMessage(cli.getProgDetails()) 
 38   
 39      sysc = SystemConstants(cli.getArg("archive")) 
 40      fileOne = cli.getArg("fileone") 
 41      fileTwo = cli.getArg("filetwo") 
 42      checkHist = cli.getOpt("commhist") 
 43       
 44      # check that files are comparable 
 45      if not fileOne or not fileTwo or fileOne == fileTwo: 
 46          raise SystemExit("You need to provide 2 different file names.") 
 47           
 48      if os.path.splitext(fileOne)[-1] != os.path.splitext(fileTwo)[-1]: 
 49          raise SystemExit("You need to compare 2 pixel or 2 catalogue files.") 
 50   
 51      # open both files 
 52      hduListOne = fits.open(fileOne) 
 53      hduListTwo = fits.open(fileTwo) 
 54   
 55      # set extensions 
 56      extNums = (map(int, cli.getOpt("extnum").split(',')) if cli.getOpt("extnum") 
 57                 else range(len(hduListOne))) 
 58   
 59      # setup the COMMENT/HISTORY check 
 60      commHistDict = {"COMMENT": defaultdict(list), "HISTORY": defaultdict(list)} 
 61      notChecked = ([] if checkHist else commHistDict.keys()) 
 62   
 63      for hduNum, hdu in enumerate(hduListOne): 
 64          # check extension order 
 65          if hduNum > 0 and hdu.header["CAMNUM"] in extNums: 
 66              if hdu.header["CAMNUM"] != hduListTwo[hduNum].header["CAMNUM"]: 
 67                  Logger.addMessage("HDU mixup in %s" % fileTwo) 
 68                  break 
 69           
 70          if (hduNum == 0 and 0 in extNums) \ 
 71             or (hduNum > 0 and hdu.header["CAMNUM"] in extNums): 
 72              # create cardlists 
 73              cardlistOne = hdu.header.ascardlist() 
 74              cardlistTwo = hduListTwo[hduNum].header.ascardlist() 
 75   
 76              # check fileTwo against fileOne 
 77              for keyInOne in set(cardlistOne.keys()).difference(notChecked): 
 78                  cardInOne = cardlistOne[keyInOne] 
 79                  # save COMMENT/HISTORY for later check 
 80                  if keyInOne in commHistDict: 
 81                      commHistDict[keyInOne][fileOne].append(cardInOne.value) 
 82                      continue 
 83                  # check if key is in second files 
 84                  if keyInOne not in cardlistTwo.keys(): 
 85                      Logger.addMessage("%s missing in %s[%d]" % ( 
 86                          keyInOne, fileTwo, hduNum)) 
 87                      continue 
 88   
 89                  cardInTwo = cardlistTwo[keyInOne] 
 90                  # check values and comments 
 91                  if cardInOne.value != cardInTwo.value: 
 92                      Logger.addMessage("Card value differs[%d]: %s <=> %s" % ( 
 93                          hduNum, cardInOne, cardInTwo)) 
 94                  if cardInOne.comment != cardInTwo.comment: 
 95                      Logger.addMessage("Card value differs[%d]: %s <=> %s" % ( 
 96                          hduNum, cardInOne, cardInTwo)) 
 97   
 98              # check fileOne against fileTwo 
 99              for keyInTwo in set(cardlistTwo.keys()).difference(notChecked): 
100                  cardInTwo = cardlistTwo[keyInTwo] 
101                  # save COMMENT/HISTORY for later check 
102                  if keyInTwo in commHistDict: 
103                      commHistDict[keyInTwo][fileTwo].append(cardInTwo.value) 
104                      continue 
105                  # check if key is in first files 
106                  if keyInTwo not in cardlistOne.keys(): 
107                      Logger.addMessage("%s missing in %s[%d]" % ( 
108                          keyInTwo, fileTwo, hduNum)) 
109               
110              # compare COMMENT/HISTORY         
111              for key in commHistDict: 
112                  for entry in commHistDict[key][fileOne]: 
113                      if entry not in commHistDict[key][fileTwo]: 
114                          Logger.addMessage("%s missing in %s[%d]" % ( 
115                              entry, fileTwo, hduNum)) 
116                  for entry in commHistDict[key][fileTwo]: 
117                      if entry not in commHistDict[key][fileOne]: 
118                          Logger.addMessage("%s missing in %s[%d]" % ( 
119                              entry, fileOne, hduNum)) 
120