Functions
GetSpillData Namespace Reference

Functions

def unix_timestamp (input)
 
def merge_max_mappings (dicts)
 
def main ()
 MAIN FUNCTION. More...
 

Detailed Description

  Return spill information for variety of MC7 devices.
  Teresa Lackey (lackey32@fnal.gov)

Function Documentation

def GetSpillData.main ( )

MAIN FUNCTION.

1D plots hits vs w //

Definition at line 55 of file GetSpillData.py.

References check_time_usage.float, novadaq::HexUtils.format(), merge_max_mappings(), print, PandAna.Demos.demo1.range, and unix_timestamp().

55 def main():
56 
57  spills = {}
58 
59  # define times for query
60  t0 = datetime(2019,7,1,13)
61  t1 = datetime(2019,7,6,13,1)
62 
63  t0 = unix_timestamp(t0)
64  t1 = unix_timestamp(t1)
65 
66  # device list you want to query
67  devices = ["F:MC6IC","F:MC7SC1","F:MC7U16","F:MC7U17","F:MC7U18","F:MC7U15","Z:NOVTSC31","Z:NOVTSC01","Z:NOVTSC00","Z:NOVTSC07","Z:NOVTSC24","Z:NOVTSC25","Z:NOVTSC26","Z:NOVTSC27","Z:NOVTSC28","Z:NOVTSC29","Z:NOVTSC30","Z:NOVTSC16","Z:NOVTSC17","Z:NOVTSC18","Z:NOVTSC19"]
68  event = "e,36"
69 
70 
71  print "Querying database from {} to {}".format(datetime.utcfromtimestamp(t0).strftime('%Y-%m-%d %H:%M:%S UTC'),datetime.utcfromtimestamp(t1).strftime('%Y-%m-%d %H:%M:%S UTC'))
72 
73  # loop over devices
74  for device in devices:
75 
76  print "Querying {}".format(device)
77 
78  # query IF Beam DB
79  url = 'http://ifb-data.fnal.gov:8099/ifbeam/data/data?'\
80  'v={}&e={}&t0={}&t1={}&f=csv'.format(device,event,t0,t1)
81 
82  response = None
83 
84  try:
85  response = urllib2.urlopen(url, None, 200)
86  except:
87  try:
88  response = urllib2.urlopen(url, None, 200)
89  except:
90  try:
91  response = urllib2.urlopen(url, None, 200)
92  except:
93  return spills
94 
95  csvdata = csv.reader(response)
96 
97  # write value into dictionary
98  for row in csvdata:
99  if (row.count(device)>0):
100  if (row[4] != 'null'):
101  spill_time = float(row[2]) / 1000.
102  if (spill_time in spills.keys()):
103  time_array = spills[spill_time]
104  else:
105  time_array = []
106 
107  time_array.append( {'device': device, 'value': float(row[4])} )
108  spills[spill_time] = time_array
109 
110  # Reformat dictionary to list of times and device values
111  spill_array = []
112  for key in spills.keys():
113  tmp_row = {}
114  tmp_row['unix time [s]'] = key
115  for device in spills[key]:
116  tmp_row[device['device']] = device['value']
117  spill_array.append(tmp_row)
118 
119  # sort by time
120  spill_array = sorted(spill_array, key=itemgetter('unix time [s]'))
121 
122  # merge lines that are from the same spill
123  # (Sets of devices recorded at slightly different times,
124  # usually within 10s of ns of each other)
125  spill_array_merged = []
126  skipNextRow = False
127  for i in range(len(spill_array)-1):
128  if skipNextRow:
129  skipNextRow = False
130  continue
131 
132  row1 = spill_array[i]
133  row2 = spill_array[i+1]
134 
135  if row2['unix time [s]'] - row1['unix time [s]'] < 1:
136  spill_array_merged.append(merge_max_mappings(row1,row2))
137  skipNextRow = True
138  else:
139  spill_array_merged.append(row1)
140 
141 
142  # write out info to csv
143  csv_cols = devices
144  csv_cols.insert(0,'unix time [s]')
145  csv_file = "MC7_scalars.csv"
146 
147  try:
148  with open (csv_file, 'w') as csvfile:
149  writer = csv.DictWriter(csvfile, fieldnames=csv_cols)
150  writer.writeheader()
151  for data in spill_array_merged:
152  writer.writerow(data)
153  except IOError:
154  print("I/O error")
155 
156 
157 #.......................................................................
158 
def unix_timestamp(input)
Definition: GetSpillData.py:14
bool print
def merge_max_mappings(dicts)
Definition: GetSpillData.py:31
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
def main()
MAIN FUNCTION.
Definition: GetSpillData.py:55
def GetSpillData.merge_max_mappings (   dicts)
  Merges an arbitrary number of dictionaries based on the
  maximum value in a given mapping.

  Parameters
  ----------
  dicts : Dict[Any, Comparable]
  
  Returns
  -------
  Dict[Any, Comparable]
  The merged dictionary

Definition at line 31 of file GetSpillData.py.

Referenced by main().

31 def merge_max_mappings(*dicts):
32  """
33  Merges an arbitrary number of dictionaries based on the
34  maximum value in a given mapping.
35 
36  Parameters
37  ----------
38  dicts : Dict[Any, Comparable]
39 
40  Returns
41  -------
42  Dict[Any, Comparable]
43  The merged dictionary
44  """
45 
46  merged = {}
47  for d in dicts: # `dicts` is a tuple storing the input dictionaries
48  for key in d:
49  if key not in merged or d[key] > merged[key]:
50  merged[key] = d[key]
51  return merged
52 
53 #.......................................................................
54 
def merge_max_mappings(dicts)
Definition: GetSpillData.py:31
def GetSpillData.unix_timestamp (   input)
  Convert a datetime to a unix timestamp
  Args:
  input : A datetime object

  Returns:
  difference : A unix timestamp

Definition at line 14 of file GetSpillData.py.

Referenced by main().

14 def unix_timestamp(input):
15  """
16  Convert a datetime to a unix timestamp
17  Args:
18  input : A datetime object
19 
20  Returns:
21  difference : A unix timestamp
22  """
23 
24  t0 = datetime(year = 1970, month = 1, day = 1)
25  difference = input - t0
26 
27  # Return unix timestamp
28  return difference.total_seconds()
29 
30 #.......................................................................
def unix_timestamp(input)
Definition: GetSpillData.py:14