pot-for-runs.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys, os
3 import getopt
4 import shlex, subprocess
5 import re, urllib2, csv
6 from datetime import date, datetime, timedelta
7 import psycopg2
8 import psycopg2.extras
9 
10 def read_list(file):
11  f = open(file, 'r')
12  runs=[]
13  subruns=[]
14  while (1):
15  line = f.readline()
16  if (line==''):
17  break
18  r = line.split("\t")
19  runs.append(int(r[0]))
20  subruns.append( (int(r[0]),int(r[1])) )
21  runs.sort()
22  return (runs,subruns)
23 
24 #.......................................................................
25 
26 def run_minmax(runs):
27  run_min = runs[0]
28  run_max = runs[0]
29  for r in runs:
30  if (r<run_min):
31  run_min = r
32  if (r>run_max):
33  run_max = r
34  return (run_min, run_max)
35 
36 #.......................................................................
37 
38 def read_file_list(file):
39  print 'Read file list:',file
40  f = open(file,'r')
41  runs=[]
42  subruns=[]
43  while (1):
44  r = f.readline()
45  if (r==''):
46  break;
47  #
48  # Fragile! Assume file names are fixed length formats
49  #
50  run = int(r[64:72])
51  subrun = int(r[74:76])
52  runs.append(run)
53  subruns.append( (run,subrun) )
54  runs = set(runs)
55  runs = list(runs)
56  runs.sort()
57 
58  return (runs,subruns)
59 
60 #.......................................................................
61 
62 def runs_db(rmin, rmax):
63 
64  DB_NAME = os.environ['NOVADBNAME'] # 'nova_prod'
65  DB_HOST = os.environ['NOVADBHOST'] # 'ifdbrep.fnal.gov'
66  DB_USER = os.environ['NOVADBUSER'] # 'nova_reader'
67  DB_PASS = open(os.environ['NOVADBPWDFILE'], 'r').readlines()[0].strip()
68  DB_PORT = os.environ['NOVADBPORT'] # '5433'
69 
70  try:
71  conn = psycopg2.connect(\
72  "dbname=%s host=%s user=%s password=%s port=%s" % \
73  (DB_NAME, DB_HOST, DB_USER, DB_PASS, DB_PORT))
74  except:
75  print "I am unable to connect to the runs database"
76 
77  db_cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
78 
79  sql = "select run,nsubruns,tstart,tstop from fardet.runs where partition=1 and tstop is not null and run>=%d and run<=%d order by tstart"%(rmin,rmax)
80 
81  db_cursor.execute(sql)
82  runs = db_cursor.fetchall();
83 
84  return runs
85 
86 #.......................................................................
87 
88 def in_unix(input):
89  """ Convert a datetime to UNIX time
90 
91  Args:
92  input: A datetime object
93 
94  Returns:
95  A floating points number of seconds since Jan. 1, 1970 (UNIX standard)
96 
97  """
98  start = datetime(year=1970,month=1,day=1,hour=0,minute=0,second=0)
99  diff = input - start
100  return diff.total_seconds()
101 
102 #.......................................................................
103 
104 def spills_between(t1, t2):
105  """
106  Return spill information (unix time and POT) for a datetime range
107 
108  Args:
109  t1 (datetime): Start time
110  t2 (datetime): End time
111 
112  Returns:
113  List of (time,pot) tuples
114  """
115 
116  spills = [];
117 
118  tf1 = in_unix(t1)
119  tf2 = in_unix(t2)
120  webaddress = 'http://ifb-data.fnal.gov:8099'\
121  '/ifbeam/data/data?'\
122  'v=E:TRTGTD&e=e,a9&t0={}&t1={}&&f=csv'.\
123  format(tf1,tf2)
124 
125  dd = None
126  #
127  # Make three tries and then bail...
128  #
129  try:
130  dd = urllib2.urlopen(webaddress,None,600)
131  except:
132  try:
133  dd = urllib2.urlopen(webaddress,None,600)
134  except:
135  try:
136  dd = urllib2.urlopen(webaddress,None,600)
137  except:
138  return spills
139 
140  if (dd==None):
141  return spills
142 
143  csvdata = csv.reader(dd)
144 
145  for row in csvdata:
146  if (row.count('E:TRTGTD')>0):
147  if (row[4]!='null'):
148  spills.append( (float(row[2])/1000., float(row[4])) )
149 
150  return spills
151 
152 #.......................................................................
153 
154 def count_subruns(subruns, run):
155  count = 0
156  for (r,s) in subruns:
157  if (run==r):
158  count = count + 1
159  return count
160 
161 #.......................................................................
162 
163 def usage():
164  print ""
165  print "pot-for-runs takes a list of files or subruns and prints a table of:"
166  print "1. run number"
167  print "2. total number of subruns recorded for the run"
168  print "3. number of subruns appearing in the list"
169  print "4. run start time"
170  print "5. run end time"
171  print "6. total POT for the run"
172  print "7. estimated POT for subruns in list, scaled by ratio of col3/col2 above."
173  print ""
174  print "Usage:"
175  print ""
176  print "% pot-for-runs -h -r [subrun list] -f [file list]"
177  print ""
178  print "Where:"
179  print ""
180  print "-h prints this help"
181  print "-s skips runs that are not in list. Otherwise steps sequentially by run number."
182  print "-r file.txt points to a text file of [run subrun] pairs separated by a space."
183  print "-f file.txt points to a text file of file names in standard format"
184  print ""
185  print "You cannot give both -r and -f together."
186  print ""
187 
188 #.......................................................................
189 
190 def main():
191  file = None
192  fmt = None
193  skip = True
194  try:
195  opts, args = getopt.getopt(sys.argv[1:],
196  "hsr:f:",
197  ["help","sequential","runs=","files="])
198  except getopt.GetoptError as err:
199  print str(err)
200  usage()
201  sys.exit(2)
202  for o, a in opts:
203  if (o in ("-h","--help")):
204  usage()
205  sys.exit(0)
206  if (o in ("-s","--sequential")):
207  skip = False
208  if (o in ("-r","--runs")):
209  file = a
210  fmt = 1
211  if (o in ("-f","--files")):
212  file = a
213  fmt = 2
214  if (file==None):
215  usage()
216  sys.exit(1)
217  if (fmt==1):
218  (runs, subruns) = read_list(file)
219  if (fmt==2):
220  (runs, subruns) = read_file_list(file)
221 
222  (rmin, rmax) = run_minmax(runs)
223  runs = runs_db(rmin, rmax)
224 
225  fmt = "%d/%m/%y %H:%M:%S"
226 
227  pot_total = 0
228  pot_total_scaled = 0
229  for r in runs:
230  run = r['run']
231  t1 = r['tstart']
232  t2 = r['tstop']
233  nsub = r['nsubruns']
234 
235  spills = spills_between(t1, t2)
236  pot_run = 0
237  for (t,q) in spills:
238  pot_run += q
239  pot_total += pot_run
240 
241  count = count_subruns(subruns, run)
242  if (skip & (count==0) ):
243  continue
244 
245  pot_run_scaled = pot_run*float(count)/float(nsub)
246  pot_total_scaled += pot_run_scaled
247 
248  print run,'\t',nsub,'\t',count,'\t',t1.strftime(fmt),'\t',t2.strftime(fmt),'\t',pot_run,'\t',pot_run_scaled
249 
250  print "TOTAL POT ", pot_total
251  print "TOTAL SCALED POT ", pot_total_scaled
252 
253  return
254 
255 #.......................................................................
256 
257 if __name__=="__main__":
258  main()
259 
260 ########################################################################
def in_unix(input)
Definition: pot-for-runs.py:88
def read_list(file)
Definition: pot-for-runs.py:10
def read_file_list(file)
Definition: pot-for-runs.py:38
def count_subruns(subruns, run)
def spills_between(t1, t2)
def run_minmax(runs)
Definition: pot-for-runs.py:26
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
procfile open("FD_BRL_v0.txt")
def runs_db(rmin, rmax)
Definition: pot-for-runs.py:62