def setup_tof_calibration(self): """ the MC does not have a real number associate tof calibration to the geometry download run number if specified get the geometry download run number get the beamline date for this run setup the cards Note: the get_calibration_download_parameters already gets run number for scifi Hence, all we want to do is setup the tof calibration date based on run start time """ calib_date = "current" for i in range(5): try: print " Contacting CDB" if self.test_mode: bl_service = cdb.Beamline( "http://preprodcdb.mice.rl.ac.uk") else: bl_service = cdb.Beamline() print " Accessed beamline service" blrun = bl_service.get_beamline_for_run( self.geo_run_number)[int(self.geo_run_number)] run_date = blrun['start_time'] + datetime.timedelta(0, 1) calib_date = run_date.strftime("%Y-%m-%d %H:%M:%S") break except cdb.CdbTemporaryError: print "CDB BL lookup failed on attempt", i + 1 time.sleep(1) except cdb.CdbPermanentError: raise DownloadError("Failed to connect to the CDB") with open(self.sim_cards, 'a+') as incards: incards.write('\nTOF_calib_by = \"%s\"' % "date") incards.write('\nTOF_calib_date_from = \"%s\"' % calib_date) return
def download_beamline_for_tag( self, tag, downloadpath): #pylint: disable = R0201, C0301 """ @Method download geometry for run This method gets the geometry, for the given run number, from the database then passes the string to the unpack method which unpacks it. @param id The long ID run number for the desired geometry. @param downloadedpath The path location where the files will be unpacked to. """ if os.path.exists(downloadpath) == False: raise OSError('Path ' + downloadpath + ' does not exist') else: beamline_cdb = cdb.Beamline() tag_list = beamline_cdb.list_tags() tag_found = [i for i, j in enumerate(tag_list) if j == tag] if len(tag_found) > 0: try: downloaded = beamline_cdb.get_beamline_for_tag(tag) path = downloadpath + '/Beamline.gdml' downloadedfile = self.generate_beamline_xml_from_string(\ downloaded[tag]) fout = open(path, 'w') fout.write(downloadedfile) fout.close() except RuntimeError: exit(1) else: print "Beamline tag does not exist. Ignoring Beamline tag."
def test_read_access(cls): """Test that we can access the CDB with read access""" # throw exception if not available for url in READ_URL_LIST: cdb.AlarmHandler(url) cdb.Beamline(url) cdb.Cabling(url) cdb.Calibration(url) cdb.Geometry(url) cdb.PIDCtrl(url) cdb.StateMachine(url) cdb.Target(url)
def __init__(self): """ initialize """ self._cc = cdb.CoolingChannel(CDBURL) self._bl = cdb.Beamline(CDBURL) self.absdict = {} self.rundict = {} self.build_absorber_tags() self.abs_tag = None self.cc_tag = None self.bl_tag = None self._cctagdict = {} self._NCOILS = 11
def download_beamline_for_run( self, run_id, downloadpath): #pylint: disable = R0201, C0301, W0613 """ @Method download geometry for run This method gets the geometry, for the given run number, from the database then passes the string to the unpack method which unpacks it. @param id The long ID run number for the desired geometry. @param downloadedpath The path location where the files will be unpacked to. """ if os.path.exists(downloadpath) == False: raise OSError('Path ' + downloadpath + ' does not exist') else: beamline_cdb = cdb.Beamline() downloadedfile = beamline_cdb.get_beamline_for_run_xml(run_id) path = downloadpath + '/Beamline.gdml' fout = open(path, 'w') fout.write(downloadedfile) fout.close() # also want to get the geometry ID for record keeping downloadedmap = beamline_cdb.get_beamline_for_run(int(run_id)) if len(downloadedmap) == 0: raise OSError('Beamline not found for run') else: start_time = downloadedmap[int(run_id)]['start_time'] stop_time = downloadedmap[int(run_id)]['end_time'] ids = self.geometry_cdb.get_ids(start_time, stop_time) if len(ids) == 0: raise OSError('Geometry ID does not exist for run number') else: # the latest applicable key uploaded should be the # first key appearing geoid = ids.keys()[0] # in the event that this is not the case run # through the other values to see if there is a # later applicable creation date sortedids = sorted(ids.items(), \ key=lambda x:x[1]['created'], reverse=True) geoid = sortedids[0][0] return geoid
def get_run_summary(a_file): global optics_keys keys = optics_keys run_number = a_file.split("-") run_number = [item for item in run_number if "96" in item or "97" in item][0] run_number = run_number.split("/")[0] ell_file = a_file+"/ellipse_summary.txt" fin = open(ell_file) data = {'run':long(run_number)} for line in fin.readlines(): for key in optics_keys: if key in line: data[key] = line.split()[1] if "tku cut: all" in line: break bl = cdb.Beamline() bl_dict = bl.get_beamline_for_run(data['run'])[data['run']] for key in bl_dict: if type(bl_dict[key]) != type({}): data[key] = bl_dict[key] for key in bl_dict["magnets"]: data[key] = bl_dict["magnets"][key]["set_current"] return data
def run_update(namespace): print "Initializing..." print # CDB Interface cdb_channel = cdb.CoolingChannel(CDB_SERVER) cdb_beamline = cdb.Beamline(CDB_SERVER) # Get relevant run list all_runs = sorted(cdb_beamline.get_run_numbers()) the_runs = [] for run in all_runs: if int(run) > namespace.start_run: the_runs.append(run) # Some pretty output sys.stdout.write("Updating: 0%\b\b\b\b") sys.stdout.flush() total = len(the_runs) percent = int(-1) helical_runs = [] straight_runs = [] not_used = 0 num_errors = 0 prog = re.compile("([0-9]+)-([0-9]+)\+M.+-.*") prog = re.compile("([0-9]+)-([0-9]+)\+M.+-*") # Loop over all relevant run numbers for counter, run in enumerate(the_runs): run = run.zfill(5) # print(run) int_run = int(run) try: run_channel = cdb_channel.get_coolingchannel_for_run(run) run_beamline = cdb_beamline.get_beamline_for_run(run)[int_run] ### unused: run_absorber = cdb_channel.get_absorber_for_run(run).keys()[0][1] match = prog.match(run_beamline['optics']) if match is None: print "Not found: ", run_beamline['optics'], run continue print "Found: ", run_beamline['optics'], run beamline = match.group(0) emittance = match.group(1) momentum = match.group(2) new_percent = int((float(counter) / float(total)) * 100.0) if new_percent != percent: sys.stdout.write("{0:>3}".format(new_percent) + "\b\b\b") sys.stdout.flush() percent = new_percent if type(run_channel) == list: continue magnets = run_channel['magnets'] upstream_on = False downstream_on = False for magnet in magnets: if magnet['name'] == "SSU": for coil in magnet['coils']: if coil['name'] == "SSU-C": if float(coil['iset']) > 10.0: upstream_on = True if magnet['name'] == "SSD": for coil in magnet['coils']: if coil['name'] == "SSD-C": if float(coil['iset']) > 10.0: downstream_on = True if upstream_on and downstream_on: helical_runs.append( (run_beamline['run_number'], beamline, emittance, momentum, run_beamline['start_time'], run_beamline['end_time'])) elif (not upstream_on) and (not downstream_on): straight_runs.append( (run_beamline['run_number'], beamline, emittance, momentum, run_beamline['start_time'], run_beamline['end_time'])) else: not_used += 1 except cdb._exceptions.CdbPermanentError as ex: print "CDB Exception in Run:", run num_errors += 1 except cdb._exceptions.CdbTemporaryError as ex: print print "An Temporary Error Occured. Please try Again Later" print return print print "\rComplete. Found:" print print "Helical Runs : ", len(helical_runs) print "Straight Runs : ", len(straight_runs) print "Non-Analysis Runs : ", not_used print print "CDB Errors : ", num_errors print with open("helical_run_numbers.dat", "w") as outfile: for run_number, beamline, emittance, momentum, start, end in helical_runs: if end is None: end = datetime.datetime.min outfile.write("{0:s} {1:s} {2:s} {3:s} {4:s} {5:s}\n".format( str(run_number), beamline, emittance, momentum, start.isoformat(" "), end.isoformat(" "))) with open("straight_run_numbers.dat", "w") as outfile: for run_number, beamline, emittance, momentum, start, end in straight_runs: if end is None: end = datetime.datetime.min outfile.write("{0:s} {1:s} {2:s} {3:s} {4:s} {5:s}\n".format( str(run_number), beamline, emittance, momentum, start.isoformat(" "), end.isoformat(" ")))
def get_fields_for_tag(tag): print "Getting fields for beamline tag", tag bl = cdb.Beamline() beamline_all = bl.get_beamline_for_tag(tag)[tag] beamline_all = massage_tag(beamline_all, polarity = 1) get_beamline(beamline_all, str(tag))
def get_fields_for_beamline(run_number): print "Getting fields for beamline run", run_number bl = cdb.Beamline() beamline_all = bl.get_beamline_for_run(run_number)[run_number] get_beamline(beamline_all, str(run_number))
def get_beamline(run_number): bl = cdb.Beamline() beamline_all = bl.get_beamline_for_run(run_number)[run_number] return beamline_all
def get_run_comment(run): bl = cdb.Beamline() bl_list = bl.get_beamline_for_run(run)[long(run)] #print bl_list.keys() return bl_list['end_notes']
def main(): """ This is the executbale which gets information relating to beamline stored on the configuration Database. It will print either to screen or to file the details of the beamline or whether it is on the database or not. It takes arguments in the form of which method you wish to call the cdb. It currently only has a query of run number and a time frame. These arguments can be specified in the ConfigurationDefaults file. """ configuration = Configreader() server = cdb.Beamline() print "Server status is " + server.get_status() if configuration.get_beamline_by == 'all_entries': start_time = "2009-01-01 09:00:00" stop_time = datetime.today() id_dict = server.get_beamlines_for_dates(start_time, stop_time) keys = id_dict.viewkeys() ids_list = [] ids_list = list(keys) length = len(ids_list) print 'The CDB has information on the following runs;' for i in range(0, length): print ids_list[i] print 'If the run you are looking for is not here ' + \ 'then it is most likely not on the CDB.\n Reasons for '+\ 'this are;\n Run was not uploaded from the control room;\n '+\ 'The CDB was still under construction or in testing '+\ 'at the time.' elif configuration.get_beamline_by == 'run_number': run = configuration.get_beamline_run_number id_dict = server.get_beamline_for_run(run) if str(id_dict.keys()) == '[]': print '!WARNING! No beamline information on the CDB for ' \ + str(configuration.get_beamline_run_number) print 'If the run you are looking for is not here ' + \ 'then it is most likely not on the CDB.\n Reasons for '+\ 'this are;\n Run was not uploaded from the control room;\n '+\ 'The CDB was still under construction or in testing '+\ 'at the time.' else: print 'There is information for run number ' \ + str(configuration.get_beamline_run_number) + ' on the CDB' elif configuration.get_beamline_by == 'dates': if configuration.get_beamline_start_time == "": raise IOError('Start time not entered!' + \ ' Have you used a configuration file?') if configuration.get_beamline_stop_time == "": stop_time = None else: stop_time = configuration.get_beamline_stop_time start_time = configuration.get_beamline_start_time id_dict = server.get_beamlines_for_dates(start_time, stop_time) keys = id_dict.viewkeys() ids_list = [] ids_list = list(keys) length = len(ids_list) print 'The CDB has information on the following runs;' for i in range(0, length): print ids_list[i] print 'If the run you are looking for is not here ' + \ 'then it is most likely not on the CDB.\n Reasons for '+\ 'this are;\n Run was not uploaded from the control room;\n '+\ 'The CDB was still under construction or in testing '+\ 'at the time.' else: raise KeyError("Didn't recognise 'get_beamline_by' option '"+\ configuration.geometry_download_by+"'. Should be on of\n"+\ "all_entries\nrun_number\ndates\n")