1   
  2   
  3   
  4  """ 
  5   Interface for configuring pre/post icetray scripts 
  6   
  7   copyright  (c) 2005 the icecube collaboration 
  8   
  9   @version: $Revision: $ 
 10   @date: $Date: $ 
 11   @author: Juan Carlos Diaz Velez <juancarlos@icecube.wisc.edu> 
 12  """ 
 13   
 14  import os 
 15  import re 
 16  import sys 
 17  import math 
 18  import dircache 
 19  import time 
 20  import string 
 21  import shutil 
 22  import cPickle 
 23  from ipmodule import IPBaseClass 
 24  import logging 
 25   
 27      """ 
 28      This class provides an interface for preprocessing files in iceprod 
 29      """ 
 30   
 32          IPBaseClass.__init__(self) 
 33          self.cleanup = [] 
 34          self.AddParameter("DriverFileDirectory", 
 35                         "photonics driver file parent directory", 
 36                         "$PWD/"); 
 37   
 38          self.AddParameter("PhotonicsAngularSelectionHigh", 
 39                         "Maximum theta angle of tables to stage", 
 40                         180.0); 
 41   
 42          self.AddParameter("PhotonicsAngularSelectionLow", 
 43                         "Minimum theta angle of tables to stage", 
 44                         0.0); 
 45   
 46          self.AddParameter("TablesDirectory", 
 47                         "Location where tables should be found or staged if not found", 
 48                         "$system(photontablesdir)"); 
 49   
 50          self.AddParameter("TablesRepository", 
 51                         "Location where tables should be fetched from if not found", 
 52                         "$system(photontablesrepo)"); 
 53   
 54          self.AddParameter("PhotonicsLevel1DriverFile","","$steering(PHOTONTABLES::L1_IC)"); 
 55          self.AddParameter("PhotonicsLevel2DriverFile","","$steering(PHOTONTABLES::L2_IC)"); 
 56   
 57          self.logger = logging.getLogger('iceprod::StagePhotoTables') 
  58   
 59   
 61          if not IPBaseClass.Execute(self,stats): return 0 
 62          self.cleanup = [] 
 63   
 64           
 65           
 66          pt_amax = self.GetParameter("PhotonicsAngularSelectionHigh"); 
 67          pt_amin = self.GetParameter("PhotonicsAngularSelectionLow"); 
 68          pt_l1   = self.parser.parse( self.GetParameter("PhotonicsLevel1DriverFile") ); 
 69          pt_l2   = self.parser.parse( self.GetParameter("PhotonicsLevel2DriverFile") ); 
 70          pt_dir  = self.parser.parse( self.GetParameter("TablesDirectory") ); 
 71          pt_repo = self.parser.parse( self.GetParameter("TablesRepository") ); 
 72          pt_driver_dir  = self.parser.parse( self.GetParameter("DriverFileDirectory") ); 
 73   
 74          if not os.path.isdir(pt_dir):  
 75              os.makedirs(pt_dir) 
 76   
 77          l1 = open(os.path.join(pt_driver_dir,pt_l1),'r') 
 78          for line in l1.readlines(): 
 79              if line.startswith('#'): continue 
 80              line = line.split() 
 81              pt_file = line[0].strip() 
 82              amax    = float(line[-1].strip()) 
 83              amin    = float(line[-2].strip()) 
 84              zmax    = float(line[-3].strip()) 
 85              zmin    = float(line[-4].strip()) 
 86   
 87               
 88               
 89              if (amin < pt_amin) or (amin > pt_amax): continue 
 90   
 91              for s in ['.abs','.prob']: 
 92                 if not os.path.exists(os.path.join(pt_dir,pt_file+s)): 
 93                    self.logger.debug('copying %s' % pt_file+s) 
 94                    if not os.path.exists(os.path.dirname(os.path.join(pt_dir,pt_file+s))): 
 95                       os.makedirs(os.path.dirname(os.path.join(pt_dir,pt_file))) 
 96                    if os.system('cp %s %s' % (os.path.join(pt_repo,pt_file+s),os.path.join(pt_dir,pt_file+s))): 
 97                       raise Exception, "cannot copy photon tables from repository" 
 98                    self.cleanup.append(os.path.join(pt_dir,pt_file+s)) 
 99          l1.close() 
100   
101          l2 = open(os.path.join(pt_driver_dir,pt_l2),'r') 
102          for line in l2.readlines(): 
103   
104              if line.startswith('#'): continue 
105   
106              line = line.split() 
107              pt_file = line[0].strip() 
108              pt_file = pt_file.replace('@starting:','') 
109              pt_file = pt_file.replace('@stopping:','') 
110              amax    = float(line[-1].strip()) 
111              amin    = float(line[-2].strip()) 
112              zmax    = float(line[-3].strip()) 
113              zmin    = float(line[-4].strip()) 
114   
115               
116               
117              if (amin < pt_amin) or (amin > pt_amax): continue 
118   
119              for s in ['.abs','.prob']: 
120                 if not os.path.exists(os.path.join(pt_dir,pt_file+s)): 
121                    self.logger.debug('copying %s' % pt_file+s) 
122                    if not os.path.exists(os.path.dirname(os.path.join(pt_dir,pt_file+s))): 
123                       os.makedirs(os.path.dirname(os.path.join(pt_dir,pt_file))) 
124                    if os.system('cp %s %s' % (os.path.join(pt_repo,pt_file+s),os.path.join(pt_dir,pt_file+s))): 
125                       raise Exception, "cannot copy photon tables from repository" 
126                    self.cleanup.append(os.path.join(pt_dir,pt_file+s)) 
127          l2.close() 
128   
129          return 0 
 130   
132          if not IPBaseClass.Execute(self,stats): return 0 
133          for f in self.cleanup: 
134              if os.system('rm -f %s' % f): 
135                   raise Exception, "cannot remove cached photon tables" 
136          return 0 
  137