Package helpers :: Module CalcMagLmt
[hide private]

Source Code for Module helpers.CalcMagLmt

  1  #! /usr/bin/env python 
  2  #------------------------------------------------------------------------------ 
  3  #$Id: CalcMagLmt.py 9408 2012-09-19 21:56:38Z NicholasCross $ 
  4  """ 
  5     Tests new stacks and catalogues of given programme and database release. 
  6   
  7     @author: N.J. Cross 
  8     @org:    WFAU, IfA, University of Edinburgh 
  9   
 10     @requires: PyFITS 
 11  """ 
 12  #------------------------------------------------------------------------------ 
 13  import math 
 14  import numpy 
 15  import os 
 16  import time 
 17  from   Scientific.Functions import LeastSquares as LSQ 
 18  from   collections import defaultdict 
 19   
 20  from   wsatools.CLI                 import CLI 
 21  import wsatools.DataFactory             as df 
 22  from   wsatools.DbConnect.CuSession import CuSession 
 23  import wsatools.DbConnect.DbConstants   as dbc 
 24  import wsatools.FitsUtils               as fits 
 25  from   wsatools.Logger              import Logger, ForLoopMonitor 
 26  from   invocations.cu13.cu13        import ProductDefinition, Cu13 
 27  from   wsatools.SystemConstants     import DepCodes 
 28  import wsatools.DbConnect.CommonQueries as queries 
 29  #------------------------------------------------------------------------------ 
 30   
 31   
 32  #------------------------------------------------------------------------------ 
 33  # Entry point for script. 
 34   
35 -class CalcMagLim(Cu13):
36 """ Tests maglim code for set of created products 37 """ 38 #-------------------------------------------------------------------------- 39 # Class constants (access as CalcMagLim.varName) 40 41 #: Curation Unit number 42 cuNum = -9999 43 #-------------------------------------------------------------------------- 44 45 releaseNum = 0 46 reprocess = False 47 useDeep = False 48 numStack = None 49 notDepOnly = False 50 existingProducts = None 51 dateRange = CuSession.sysc.obsCal.dateRange() #: Nights to stack. 52 dirDate = time.strftime("%Y%m%d", time.gmtime()) #: Output dir date. 53 newProductsOfType = None 54 _tmpWorkDir = None 55 imageProductDict = None 56 idImageProductDict = None 57 deepProdMfIDDict = None 58 #-------------------------------------------------------------------------- 59
60 - def _onRun(self):
61 """ Calculate magnitude limits 62 """ 63 64 filterTable = df.Table("Filter", self.archive) 65 self.reqProdsOfType = \ 66 queries.findRequiredDeepProducts(self.programme, self.minProdID, 67 self.maxProdID, self.reqProdIDs) 68 self.prodDirectory = defaultdict(dict) 69 for productType in self.programme.getProductTypes(): 70 self.pType = productType 71 self.setUpProductsInfo() 72 self.existingProducts = self.archive.query( 73 selectStr="productID, M.frameType, releaseNum, " 74 "M.multiframeID, M.fileName AS imgName, " 75 "M2.fileName AS confName, M.catName", 76 fromStr="ProgrammeFrame P, Multiframe M, Multiframe M2", 77 whereStr="M.multiframeID=P.multiframeID AND programmeID=%s" 78 " AND M.%s AND M2.multiframeID=M.confID" 79 % (self.programmeID, DepCodes.selectNonDeprecated) 80 + " AND productID>0 AND M.frameType LIKE '%stack'") 81 productIDs = self.deepProductTable["productID"] 82 # Set up everything necessary for ProductDefinition... 83 for productID in productIDs: 84 self.prodDirectory[productType][productID] = [ 85 os.path.dirname(prod.imgName) for prod in self.existingProducts 86 if prod.productID == productID].pop() 87 mfID = [prod.multiframeID for prod in self.existingProducts 88 if prod.productID == productID].pop() 89 product = ProductDefinition(self, productID, filterTable) 90 # @TODO: is scos? 91 self.testProduct(product, calcOnly=(not self.archive.isRealRun)) 92 Logger.addMessage("ProductID: %s, filename = %s, " 93 "AB magLimit = %s" % (productID, 94 product.fileName(), self.abMagLimit)) 95 # UpdateDB 96 if not self.archive.isTrialRun: 97 for extNum in self.abMagLimit: 98 self.archive.update("MultiframeDetector", 99 [("abMagLim", self.abMagLimit[extNum])], 100 "multiframeID=%s and extNum=%s" % (mfID, extNum))
101 102 # Allow module to be imported as well as executed from the command line 103 if __name__ == '__main__': 104 CLI.progArgs.append(CLI.Argument('programmeID', 'DXS')) 105 CLI.progArgs.append(CLI.Argument('releaseNum', '3', 106 isValOK=lambda x: x.isdigit())) 107 CLI.progOpts.append(CLI.Option('n', 'minProdID', 108 'start at this productID', 'NUMBER', 109 isValOK=lambda x: x.isdigit())) 110 CLI.progOpts.append(CLI.Option('x', 'maxProdID', 111 'finish from this productID', 'NUMBER', 112 isValOK=lambda x: x.isdigit())) 113 CLI.progOpts.append(CLI.Option('v', 'isVerbose', 114 'Log all information')) 115 # @@ TODO. Put this into cu13/14 code 116 # Improve it 117 # ExpML calculator not really working. Why?? 118 cli = CLI(CalcMagLim, "$Revision: 9408 $") 119 Logger.isVerbose = cli.getOpt("verbose") 120 Logger.addMessage(cli.getProgDetails()) 121 cu = CalcMagLim(cli.getArg("programmeID"), cli=cli) 122 if cli.getOpt("maxProdID"): 123 cu.maxProdID = int(cli.getOpt("maxProdID")) 124 125 if cli.getOpt("minProdID"): 126 cu.minProdID = int(cli.getOpt("minProdID")) 127 cu.releaseNum = int(cli.getArg("releaseNum")) 128 cu.run() 129 #------------------------------------------------------------------------------ 130 # Change log: 131 # 132 # 9-Jul-2006, NJC: Original version. 133 # 8-Feb-2007, NJC: Refactored to put all the database selection at 134 # the beginning and to reduce the number of transactions. 135 # Removed repetitions, added more information. Improved 136 # speed. 137 # 8-Feb-2007, NJC: Corrected 2nd method of calculating magnitude limit. 138 # Added average ellipticity measure. 139 # 8-Feb-2007, NJC: Made sure selecting stars only. 140 # 9-Feb-2007, NJC: Added in code to produce simplified tex statistics table. 141 # It is not quite right, but needs very simple adjustments 142 # to produce tex readable table. 143 # 26-Feb-2007, NJC: Removed obsolete inputs to getRobustLimit 144 # 13-Nov-2007, NJC: Edited control, logging: now searches for catalogue files 145 # correctly, like cu14. 146 # 13-Nov-2007, NJC: Made executable. Sorted out tex file. 147 # 14-Nov-2007, NJC: Fixed control structures so that they work better with 148 # fitsutils.findStacksMosaic. 149 # 29-Nov-2007, NJC: Corrected to work with mosaics, use probable stars as 150 # well as stars to calculate mag limit. 151 # 17-Apr-2008, NJC: More options. Used TEXPTIME from header. Checks to 152 # see if catalogue present. Updated to work with changes 153 # to FitsUtils. 154