#!/usr/bin/env python
#------------------------------
"""
  For specified instrument loop over all experiments in the DB
  and search for runs where specified detector (name pattern like in DetInfo object) is used.  
  Both parameters are case non-sensitive.  

  Created on 2018-04-19 by Mikhail Dubrovin
"""
from __future__ import print_function
#------------------------------

import sys
from CalibManager.RegDBUtils import test_find_detector_runs_for_instrument

INSTRS = ('AMO','SXR','XPP','XCS','CXI','MEC','MFX','DIA','DET','MOB') # /reg/d/psdm/
INSTR = sys.argv[1] if len(sys.argv) > 1 else None

DETS = ('jungfrau', 'cspad.', 'cspad2x2', 'opal', 'pnccd', 'etc...')
DET = sys.argv[2] if len(sys.argv) > 2 else 'jungfrau'

#------------------------------

def usage():
    return  'Usage: find_detector_runs <INS> <DET>\n'\
           +'       where <INS> instrument from the list %s\n' % ', '.join(INSTRS)\
           +'             <DET> is like one of the list %s' % ', '.join(DETS)

#------------------------------

def find_detector_runs(ins=INSTR, dtype=DET) :    
    print('%s\nList of experiments and runs containing detector "%s" in instrument %s' % (80*'_', dtype, ins))
    if INSTR is None or not(INSTR.upper() in INSTRS) : 
        print(usage())
        sys.exit()
    print('%s\n%s:' % (80*'_', ins))
    test_find_detector_runs_for_instrument(ins, dtype)

#------------------------------

find_detector_runs()

#------------------------------
