def calib_constants_all_types(det, exp=None, run=None, time_sec=None, vers=None, url=cc.URL): """ returns constants for all ctype-s """ ctype = None db_det, db_exp, colname, query = mu.dbnames_collection_query( det, exp, ctype, run, time_sec, vers) dbname = db_det if exp is None else db_exp docs = find_docs(dbname, colname, query, url) #logger.debug('find_docs: number of docs found: %d' % len(docs)) if docs is None: return None ctypes = set([d.get('ctype', None) for d in docs]) ctypes.discard(None) logger.debug('calib_constants_all_types - found ctypes: %s' % str(ctypes)) resp = {} for ct in ctypes: docs_for_type = [d for d in docs if d.get('ctype', None) == ct] doc = select_latest_doc(docs_for_type, query) if doc is None: continue resp[ct] = (get_data_for_doc(dbname, colname, doc, url), doc) return resp
def calib_constants(det, exp=None, ctype='pedestals', run=None, time_sec=None, vers=None, url=cc.URL): """Returns calibration constants and document with metadata for specified parameters. To get meaningful constants, at least a few parameters must be specified, e.g.: - det, ctype, time_sec - det, ctype, version - det, exp, ctype, run - det, exp, ctype, time_sec - det, exp, ctype, run, version etc... """ db_det, db_exp, colname, query = mu.dbnames_collection_query( det, exp, ctype, run, time_sec, vers) logger.debug('get_constants: %s %s %s %s' % (db_det, db_exp, colname, str(query))) dbname = db_det if exp is None else db_exp doc = find_doc(dbname, colname, query, url) if doc is None: # commented out by cpo since this happens routinely the way # that Mona is fetching calibration constants in psana. #logger.warning('document is not available for query: %s' % str(query)) return (None, None) return (get_data_for_doc(dbname, colname, doc, url), doc)
def dbnames_collection_query(det, exp=None, ctype='pedestals', run=None, time_sec=None, vers=None, dtype=None, dbsuffix=''): """wrapper for MDBUtils.dbnames_collection_query, - which should receive short detector name, othervice uses direct interface to DB """ short = pro_detector_name(det) logger.debug('short: %s dbsuffix: %s' % (short, dbsuffix)) resp = list(mu.dbnames_collection_query(short, exp, ctype, run, time_sec, vers, dtype)) if dbsuffix: resp[0] = detector_dbname(short, dbsuffix=dbsuffix) return resp
def dbnames_collection_query(det, exp=None, ctype='pedestals', run=None, time_sec=None, vers=None, dtype=None): """wrapper for MDBUtils.dbnames_collection_query, - which should receive short detector name, othervice uses direct interface to DB """ short = pro_detector_name(det) logger.debug('short: %s' % short) return mu.dbnames_collection_query(short, exp, ctype, run, time_sec, vers, dtype)
def get(self): """Gets constans from DB and saves them in file. """ mode, kwargs = self.mode, self.kwargs defs = self.defs host = kwargs.get('host', None) port = kwargs.get('port', None) exp = kwargs.get('experiment', None) det = kwargs.get('detector', None) ctype = kwargs.get('ctype', None) run = kwargs.get('run', None) run_end = kwargs.get('run_end', None) tsec = kwargs.get('time_sec', None) if is_in_command_line( '-s', '--time_sec') else None tstamp = kwargs.get('time_stamp', None) if is_in_command_line( '-t', '--time_stamp') else None vers = kwargs.get('version', None) prefix = kwargs.get('iofname', None) verb = self.loglevel == 'DEBUG' db_det, db_exp, colname, query = dbu.dbnames_collection_query( det, exp, ctype, run, tsec, vers) logger.debug('get: %s %s %s %s' % (db_det, db_exp, colname, str(query))) dbname = db_det if exp is None else db_exp client = self.client() if not self.check_database(client, dbname): return db, fs = dbu.db_and_fs(client, dbname) colnames = dbu.collection_names(db) if not (colname in colnames): # dbu.collection_exists(db, colname) logger.warning('db "%s" does not have collection "%s"' % (db.name, str(colname))) return col = dbu.collection(db, colname) logger.debug('Search document in db "%s" collection "%s"' % (dbname, colname)) doc = dbu.find_doc(col, query) if doc is None: logger.warning('Can not find document for query: %s' % str(query)) return logger.debug('get doc:', doc) data = dbu.get_data_for_doc(fs, doc) if data is None: logger.warning('Can not load data for doc: %s' % str(doc)) return if prefix is None: prefix = dbu.out_fname_prefix(**doc) dbu.save_doc_and_data_in_file(doc, data, prefix, control={ 'data': True, 'meta': True })
def test_dbnames_collection_query(): det='cspad_0001' db_det, db_exp, colname, query = mu.dbnames_collection_query(det, exp=None, ctype='pedestals', run=50, time_sec=None, vers=None) print('test_dbnames_collection_query:', db_det, db_exp, colname, query)