Esempio n. 1
0
def delete_version_from_file(fname,
                             par,
                             ctype=gu.PIXEL_MASK,
                             vers=None,
                             cmt=None,
                             verb=False):
    """Delete specified version from calibration constants.
    
    Parameters
    
    - fname : full path to the hdf5 file
    - par   : psana.Event | psana.Env | float - tsec event time
    - ctype : gu.CTYPE - enumerated calibration type, e.g.: gu.PIXEL_MASK
    - vers  : int - calibration version
    - cmt   : str - comment
    - verb  : bool - verbousity

    See :py:class:`DCMethods`
    """

    metname = sys._getframe().f_code.co_name

    str_ctype = gu.dic_calib_type_to_name[ctype]
    if verb:        print '  %s.delete_version_from_file:  ctype: %s  vers: %s'%\
              (metname, str_ctype, vers)

    if not is_good_fname(fname, verb): return None

    cs = DCStore(fname)
    cs.load()

    ct = cs.ctypeobj(str_ctype)
    if ct is None: return None
    #ct.print_obj()

    tsec = dcu.par_to_tsec(par)
    cr = ct.range_for_tsec(tsec)
    if cr is None: return None

    v = vers if vers is not None else cr.vnum_last()

    vdel = cr.mark_version(vnum=vers, cmt=cmt)

    if verb: log.setPrintBits(02)  # 0377

    cs.save()

    if verb:
        print 50 * '_', '\nDCStore.print_obj() after delete version %s' % str(
            vdel)
        cs.print_obj()

    return vdel
Esempio n. 2
0
def get_constants_from_file(fname,
                            par,
                            ctype=gu.PIXEL_MASK,
                            vers=None,
                            verb=False):
    """Returns specified array of calibration constants.
    
    Parameters

    - fname : full path to the hdf5 file
    - par   : psana.Event | psana.Env | float - tsec event time
    - ctype : gu.CTYPE - enumerated calibration type, e.g.: gu.PIXEL_MASK
    - vers  : int - calibration version

    Returns

    - np.array - specified array of calibration constants

    See :py:class:`DCMethods`
    """
    if not is_good_fname(fname, verb): return None

    cs = DCStore(fname)
    cs.load()
    if verb:
        print 50 * '_', '\nDCStore.print_obj()'
        cs.print_obj()

    str_ctype = gu.dic_calib_type_to_name[ctype]
    ct = cs.ctypeobj(str_ctype)
    if ct is None: return None
    #ct.print_obj()

    tsec = dcu.par_to_tsec(par)
    #print 'XXX: get DCRange object for time = %.3f' % tsec
    cr = ct.range_for_tsec(tsec)
    #cr = ct.range_for_evt(evt)

    if cr is None: return None
    #cr.print_obj()

    cv = cr.version(vnum=vers)
    if cv is None: return None
    #cv.print_obj()

    return cv.data()
Esempio n. 3
0
def delete_range_from_file(fname,
                           ctype=gu.PIXEL_MASK,
                           range=None,
                           cmt=None,
                           verb=False):
    """Delete specified time range from calibration constants.
    
    Parameters
    
    - fname : full path to the hdf5 file
    - ctype : gu.CTYPE - enumerated calibration type, e.g.: gu.PIXEL_MASK
    - range : str - range, e.g. '1474587520-end'
    - cmt   : str - comment
    - verb  : bool - verbousity

    See :py:class:`DCMethods`
    """
    metname = sys._getframe().f_code.co_name

    str_ctype = gu.dic_calib_type_to_name[ctype]
    if verb:        print '  %s.delete_range_from_file  ctype: %s  range: %s'%\
              (metname, str_ctype, range)

    if not is_good_fname(fname, verb): return None

    cs = DCStore(fname)
    cs.load()
    #cs.print_obj()

    ct = cs.ctypeobj(str_ctype)
    if ct is None: return None

    rdel = ct.mark_range_for_key(range, cmt=cmt)
    if rdel is None: return None

    if verb: log.setPrintBits(02)  # 0377

    cs.save()

    if verb:
        print 50 * '_', '\nDCStore.print_obj() after delete range %s' % rdel
        cs.print_obj()

    return rdel
Esempio n. 4
0
def print_content_from_file(fname):
    """Prints content of the file.
    
    Parameters
    
    - fname : str - full path to the file

    See :py:class:`DCMethods`
    """
    metname = sys._getframe().f_code.co_name

    if not is_good_fname(fname, True): return

    cs = DCStore(fname)

    t0_sec = time()
    cs.load()
    print 'File content loading time (sec) = %.6f' % (time() - t0_sec)

    print 50 * '_', '\nDCStore.print_obj()'
    cs.print_obj()
Esempio n. 5
0
def add_constants_to_file(data, fname, par, env=None, ctype=gu.PIXEL_MASK,\
                          vers=None,\
                          pred=None,\
                          succ=None,\
                          cmt=None,\
                          verb=False) :
    """Adds specified numpy array to the hdf5 file.

    Parameters
    
    - data : numpy.array or str - array or string of calibration constants/data to save in file
    - fname: full path to the hdf5 file
    - par  : psana.Event | psana.Env | float - tsec event time
    - env  : psana.Env -> is used to get exp=env.experiment() for comments etc.
    - ctype: gu.CTYPE - enumerated calibration type, e.g.: gu.PIXEL_MASK
    - vers : int - calibration version
    - pred : str - predecessor name
    - succ : str - successor name
    - cmt  : str - comment saved as a history record within DCRange
    - verb : bool - verbosity, default=False - do not print any message

    See :py:class:`DCMethods`
    """
    metname = sys._getframe().f_code.co_name

    str_ctype = gu.dic_calib_type_to_name[ctype]

    if verb:        print '  %s.add_constants_to_file  ctype: %s  vers: %s'%\
              (metname, str_ctype, vers)

    if fname is None:
        if verb: print 'WARNING: file name is not defined - return None'
        return

    tsec_ev = dcu.par_to_tsec(par)

    cs = DCStore(fname)

    if verb: log.setPrintBits(0377)  # 0377

    if os.path.exists(fname): cs.load()

    cs.set_tscfile(tsec=tsec_ev)
    cs.set_predecessor(pred)
    cs.set_successor(succ)

    msg = 'detname:%s predecessor:%s successor:%s ts:%.0f' %\
          (cs.detname(), cs.predecessor(), cs.successor(), cs.tscfile())
    #cs.add_history_record('%s for %s' % (metname, msg))
    #cs.add_par('par-1-in-DCStore', 1)

    ct = cs.add_ctype(str_ctype, cmt='')
    if ct is None: return
    #ct.add_history_record('%s - add DCType %s' % (metname,str_ctype))
    #ct.add_par('par-1-in-DCType', 1)

    exp = env.experiment() if env is not None else ''
    exp = exp if exp != '' else 'unknown'
    runnum = par.run() if not isinstance(par, float) else 0
    msg = 'exp=%s:run=%s' % (exp, str(runnum))
    cr = ct.add_range(tsec_ev, end=None, cmt=msg)
    if cr is None: return
    cr.add_par('experiment', exp)
    cr.add_par('run', str(runnum))
    #cr.set_vnum_def(vnum=None)

    msg = '' if cmt is None else cmt
    cv = cr.add_version(vnum=vers, tsec_prod=time(), nda=data, cmt=msg)
    if cv is None: return
    #v = cr.vnum_last() if vers is None else vers
    #rec='%s vers=%d: %s' % (metname, v, cmt if cmt is not None else 'no-comments')
    #cr.add_history_record(rec)

    if verb:
        print 50 * '_', '\nIn %s:' % metname
        cs.print_obj()

    if verb: log.setPrintBits(02)  # 0377

    cs.save()