#!/usr/bin/env python
# coding: utf-8
# # PyTimber Tutorial
#
#
# Accessing the CERN logging database and extracting machine parameters.
# In[1]:
import pytimber
import time
# In[2]:
db = pytimber.LoggingDB()
# ## Search for parameters
#
# Use the wildcard %
# In[3]:
db.search("%BEAM_INTENSITY%")
# ## Explore the parameter tree
#
# use the ipython autocomplete (TAB) to explorer the parameter tree interactively
# In[4]:
db.tree.LHC.Beam_Quality.Beam_1.get_vars()
# ## Get Data
#
# Specify a variable name and a timestamp or an interval. Data is always returned as a dictionary of timestamp, values arrays.
#
# In[5]:
db.get("HX:FILLN",time.time())
# ## Timestamps
#
# Timestamps can be give as floating point number, strings, datatime objects.
#
# If only one timestamp is given the last value logged prior to the timestamp is given.
#
# If the second argument is 'next' the first value logged after the timestamp is given.
#
# If two timestamps are given the values logged in between (inclusively) are given.
#
# Timestamp are returned as unix timestamp (seconds and fraction from 1970-01-01 00:00:00 GMT) or optionally as datetime object.
# In[6]:
now=time.time()
"The unixtime `%.3f` correspond to `%s` local time."%(now,pytimber.dumpdate(now))
# In[7]:
db.get("HX:FILLN",time.time())
# In[8]:
db.get("HX:FILLN",'2016-08-03 16:30:00.000')
# In[9]:
db.get("HX:FILLN",'2016-08-03 16:30:00.000',unixtime=False)
# In[10]:
db.get("HX:FILLN",'2016-08-02 16:30:00.000','next')
# In[11]:
db.get("HX:FILLN",'2016-08-02 16:30:00.000','2016-08-03 16:30:00.000')
# ## Variables
#
# Variables can be given as a string, as a pattern, as a list of strings.
# In[12]:
db.get("LHC.BCTDC.A6R4.B1:BEAM_INTENSITY",now)
# In[13]:
db.get("LHC.BCTDC.A6R4.B%:BEAM_INTENSITY",now)
# In[14]:
db.get(["LHC.BCTDC.A6R4.B1:BEAM_INTENSITY","LHC.BCTDC.A6R4.B2:BEAM_INTENSITY"],now)
# ## Values
#
# Values can be scalar (floating point values or string) or vectors. If in a query the length of the vectors is the same, as 2D array is returned, else a list of arrays is returned instead.
# In[15]:
# prepare for plotting
get_ipython().run_line_magic('matplotlib', 'notebook')
import matplotlib.pyplot as pl
# In[16]:
ts=pytimber.parsedate("2016-07-01 03:10:15.000")
ib1="LHC.BCTFR.A6R4.B1:BUNCH_INTENSITY"
ib2="LHC.BCTFR.A6R4.B2:BUNCH_INTENSITY"
data=db.get([ib1,ib2],ts,'next')
timestamps,valuesb1=data[ib1]
timestamps,valuesb2=data[ib2]
pl.figure()
pl.plot(valuesb1[0])
pl.plot(valuesb2[0])
pl.xlabel("slot number")
pl.ylabel("protons per bunch")
# In[17]:
ts=pytimber.parsedate("2016-07-01 03:10:15.000")
ib1="LHC.BCTFR.A6R4.B1:BUNCH_INTENSITY"
ib2="LHC.BCTFR.A6R4.B2:BUNCH_INTENSITY"
data=db.get([ib1,ib2],ts,ts+60)
timestamps,valuesb1=data[ib1]
timestamps,valuesb2=data[ib2]
# In[18]:
pl.figure()
pl.imshow(valuesb1,aspect='auto',origin='bottom')
pl.ylabel('seconds'); pl.xlabel("slot number")
pl.colorbar(); pl.clim(9e10,12e10)
# In[19]:
pl.figure()
pl.imshow(valuesb2,aspect='auto',origin='bottom')
pl.ylabel('seconds'); pl.xlabel("slot number")
pl.colorbar(); pl.clim(9e10,12e10)