Package helpers :: Module ResetStacks
[hide private]

Source Code for Module helpers.ResetStacks

 1  #! /usr/bin/env python 
 2  #------------------------------------------------------------------------------ 
 3  #$Id: ResetStacks.py 7245 2010-07-20 12:52:03Z RossCollins $ 
 4  """ 
 5     Remove arithmetic changes to stacks used to create CASU catalogues. 
 6   
 7     @author: N.J. Cross 
 8     @org:    WFAU, IfA, University of Edinburgh 
 9   
10     @newfield contributors: Contributors, Contributors (Alphabetical Order) 
11     @contributors: R.S. Collins 
12  """ 
13  #------------------------------------------------------------------------------ 
14  from __future__ import division, print_function 
15   
16  import numpy 
17  import os 
18   
19  from   wsatools.CLI         import CLI 
20  import wsatools.FitsUtils       as fits 
21  from   wsatools.Logger      import Logger 
22  import wsatools.SystemConstants as sysc 
23  #------------------------------------------------------------------------------ 
24  # Entry point for script. 
25   
26  # Allow module to be imported as well as executed from the command line 
27  if __name__ == "__main__": 
28      CLI.progArgs.append(CLI.Argument("directory", "/disk22/wsa/ingest/fits/")) 
29      cli = CLI("ResetStack", "$Revision: 7245 $", __doc__) 
30      Logger.isVerbose = False 
31      Logger.addMessage(cli.getProgDetails()) 
32   
33      expComment = "Exposure time scaled appropriately" 
34      multComment = "Multiplication factor to scale CASU stack." 
35      subComment = "Amount subtracted from CASU values." 
36      # PyFITS needs the values to be in the correct data-type. 
37      multFactDef = 1.0 
38      subtFactDef = 0.0 
39   
40      dirPath = cli.getArg("directory") 
41      for fileName in os.listdir(dirPath): 
42          if fileName.endswith(sysc.mefType()) \ 
43            and sysc.confSuffix() not in fileName: 
44              Logger.addMessage(fileName) 
45              filePath = os.path.join(dirPath, fileName) 
46              fits.uncompressFits([filePath]) 
47   
48              fitsFile = fits.open(filePath, "update") 
49              for extNum, hdu in enumerate(fitsFile): 
50                  if extNum != 0: 
51                      multFact = hdu.header.get("MULTFACT", multFactDef) 
52                      subtFact = hdu.header.get("SUBTFACT", subtFactDef) 
53                      hdu.data = numpy.add(hdu.data, subtFact) 
54                      hdu.data = numpy.divide(hdu.data, multFact) 
55                      hdu.header.update("SUBTFACT", subtFactDef, subComment) 
56                      hdu.header.update("MULTFACT", multFactDef, multComment) 
57   
58              expTime = fitsFile[0].header["EXP_TIME"] / multFact 
59              fitsFile[0].header.update("EXP_TIME", expTime, expComment) 
60              fitsFile.close() 
61              fits.compressFits([filePath]) 
62   
63  #------------------------------------------------------------------------------ 
64  # Change log: 
65  # 
66  #  9-Jul-2006,  NJC: Original version. 
67  #  9-Jul-2008,  NJC: Replaced numarray with numpy. 
68