uptime_graphic.py
Go to the documentation of this file.
1 import sys
2 from datetime import date, time, datetime, timedelta
3 from nova_uptime import connect_to_database
4 from nova_uptime import runs_for_period
5 from nova_uptime import nactive_chans
6 from nova_uptime import spills_between
7 from nova_uptime import pot_between
8 
9 HTML_FILE = None
10 #
11 # Table format is
12 # row[0] = date/time
13 # row[1] = run number
14 # row[2] = active detector mass rounded to nearest kt
15 # row[3] = 10 minute running average NuMI power in kW
16 #
17 DELTA_T_MIN = 20.
18 DELTA_T = timedelta(minutes=DELTA_T_MIN)
19 HALF_DELTA_T = timedelta(minutes=DELTA_T_MIN/2)
20 
22  HTML_FILE.write(\
23  "<html>"
24  "<head>"
25  "<script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script>"
26  "<script type=\"text/javascript\">"
27  "google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});"
28  "google.setOnLoadCallback(drawChart);"
29  "function drawChart() {"
30  "var data = google.visualization.arrayToDataTable(["
31  )
32 
34  HTML_FILE.write(\
35  "]);"
36  "var options = {"
37  "explorer: { axis: 'horizontal', maxZoomIn: 0.02857, maxZoomOut: 1.1}"
38  "};"
39  "var chart = new google.visualization.LineChart(document.getElementById('chart_div'));"
40  "chart.draw(data, options);"
41  "}"
42  "</script>"
43  "</head>"
44  "<body>"
45  " <div id=\"chart_div\" style=\"width: 900px; height: 500px;\"></div>"
46  "</body>"
47  "</html>"
48  )
49 
50 def print_table(table):
51  for row in table:
52  line = "[ new Date("+\
53  str(row[0].year)+","+\
54  str(row[0].month-1)+","+\
55  str(row[0].day)+","+\
56  str(row[0].hour)+","+\
57  str(row[0].minute)+","+\
58  str(row[0].second)+"),"+\
59  str(row[1])+","+\
60  str(row[2])+","+\
61  str(row[3])+"],"
62  HTML_FILE.write(line)
63 
64 def fill_table(table,run,t1,t2):
65  if ((t2-t1)>DELTA_T):
66  tr = t1;
67  while (1):
68  tr += DELTA_T
69  table.append([tr,run,0,0])
70  if (tr>t2):
71  break
72 
73 def do_fd_runs(t0,runs):
74  tgap = timedelta(minutes=1)
75  table = []
76  tlast = t0
77  runlast = 0
78  for r in runs:
79  t1 = r['tstart']
80  t2 = r['tstop']
81  run = r['run']
82  #
83  # Check if we have a gap between this run and the previous one
84  #
85  if ((t1-tlast)>tgap):
86  #
87  # Close out the last run
88  #
89  table.append([tlast, runlast, 0, 0]) # Complete last run
90  table.append([tlast, 0, 0, 0]) # Start downtime
91  fill_table(table, 0, tlast, t1) # Fill report intervals
92  table.append([t1, 0, 0, 0]) # End of downtime
93  #
94  # Make entry for start of new run
95  #
96  table.append([t1,run,0,0])
97  fill_table(table, run, t1, t2)
98  #
99  # Prepare for next iteration
100  #
101  runlast = run
102  tlast = t2
103  return table
104 
105 def do_pot(table):
106  spills = None
107  last_spill = datetime(1970,1,1)
108  for row in table:
109  t1 = row[0]-HALF_DELTA_T
110  t2 = row[0]+HALF_DELTA_T
111  #
112  # To optimize fetches from the beams database, grab spills 1
113  # day at a time
114  #
115  if ( (last_spill==None)|(t2>last_spill) ):
116  last_spill = t1 + timedelta(days=1)
117  if (last_spill<t2):
118  last_spill=t2
119  spills = spills_between(t1, last_spill)
120  pot = pot_between(t1, t2, spills)
121  #
122  # Normalize to kW. 700 kW is 1.3E17 POT/60 minutes. Our
123  # 'pot' number is in units of 1e18 pot. Then scale x100 so numbers
124  # fit on the plots with run numbers which are in 10000's range
125  #
126  row[3] = pot*(700.0/0.13)*(60./DELTA_T_MIN)*100.
127 
128 def do_kiloton(table):
129  """ Add active kilotons to the table. Round to the nearest kt
130  """
131  nchanmx = 14*12*64*32
132  for row in table:
133  if (row[1]==0):
134  row[2] = 0
135  else:
136  n = nactive_chans(row[1])
137  m = 1000*round(14*n/nchanmx+0.5,0)
138  row[2] = m
139 
140 def main():
141  global HTML_FILE
142 
144 
145  #
146  # By default, process the current month-1 week through "now"
147  #
148  t1 = datetime.utcnow()
149  t0 = t1 - timedelta(days=33)
150  if (len(sys.argv)>1):
151  #
152  # Set the default behavior - take the date given (like
153  # 2014/08/01) and report for 1 days prior and for the rest of
154  # the month extending beyong by one day
155  #
156  t00 = datetime.strptime(sys.argv[1], "%Y/%m/%d")
157  t0 = t00 - timedelta(days=1)
158  t1 = t00 + timedelta(days=33)
159 
160  outfile_name = "uptime_graphic_%02d.%02d.html" % (t1.year, t1.month)
161  HTML_FILE = open(outfile_name,'w')
162  print_header()
163 
164  HTML_FILE.write("['Date', 'FD Run Number', "\
165  "'FD Mass (tons)', 'NuMI (kW x 100)'],")
166 
167  runs = runs_for_period(t0, t1)
168  table = do_fd_runs(t0, runs);
169  do_pot(table)
170  do_kiloton(table)
171  print_table(table)
172 
173  print_footer()
174  HTML_FILE.close()
175 
176 if __name__ == "__main__":
177  main()
def do_fd_runs(t0, runs)
def print_table(table)
def do_kiloton(table)
fvar< T > round(const fvar< T > &x)
Definition: round.hpp:23
def spills_between(t1, t2)
Definition: nova_uptime.py:257
def runs_for_period(day0, day1)
Definition: nova_uptime.py:140
def fill_table(table, run, t1, t2)
def nactive_chans(run)
Definition: nova_uptime.py:92
def connect_to_database()
Definition: nova_uptime.py:52
procfile open("FD_BRL_v0.txt")
def pot_between(t0, t1, spills)
Definition: POTGetData.py:309
def do_pot(table)