#!/usr/bin/env python # coding: utf-8 # # PyTimber Tutorial #
# # Accessing the CERN logging database and extracting machine parameters. # In[3]: import pytimber import time # In[4]: db = pytimber.LoggingDB() # ## Search for parameters # # Use the wildcard % # In[5]: db.search("%BEAM_INTENSITY%") # ## Explore the parameter tree # # use the ipython autocomplete (TAB) to explorer the parameter tree interactively # In[6]: 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[7]: db.get("HX:FILLN",time.time()) # ### Scaling algorithms # # getScaled can be used to exploit the time scaling functionality of Timber. Specify # - the scale Interval (an integer) # - the scaleSize (one of ['SECOND', 'MINUTE', 'HOUR', 'DAY', 'WEEK', 'MONTH', 'YEAR']) # - the scaling algorithm (one of ['MAX','MIN','AVG','COUNT','SUM','REPEAT','INTERPOLATE']) # In[8]: db.getScaled('MSC01.ZT8.107:COUNTS','2016-08-03 16:30:00.000','2016-08-03 18:30:00.000', timescaleAlgorithm='SUM', scaleSize='MINUTE', timescaleInterval='1') ## 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[9]: now=time.time() "The unixtime `%.3f` correspond to `%s` local time."%(now,pytimber.dumpdate(now)) # In[10]: db.get("HX:FILLN",time.time()) # In[11]: db.get("HX:FILLN",'2016-08-03 16:30:00.000') # In[12]: db.get("HX:FILLN",'2016-08-03 16:30:00.000',unixtime=False) # In[13]: db.get("HX:FILLN",'2016-08-02 16:30:00.000','next') # In[14]: 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[15]: db.get("LHC.BCTDC.A6R4.B1:BEAM_INTENSITY",now) # In[16]: db.get("LHC.BCTDC.A6R4.B%:BEAM_INTENSITY",now) # In[17]: 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[18]: # prepare for plotting get_ipython().run_line_magic('matplotlib', 'notebook') import matplotlib.pyplot as pl # In[19]: 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[20]: 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[21]: pl.figure() pl.imshow(valuesb1,aspect='auto',origin='bottom') pl.ylabel('seconds'); pl.xlabel("slot number") pl.colorbar(); pl.clim(9e10,12e10) # In[22]: pl.figure() pl.imshow(valuesb2,aspect='auto',origin='bottom') pl.ylabel('seconds'); pl.xlabel("slot number") pl.colorbar(); pl.clim(9e10,12e10)