Ejemplo n.º 1
0
def load_ndarray_from_file(path):
    ext = os.path.splitext(path)[1]
    if ext in ('.npy', ):
        import numpy as np
        return np.load(path)
    elif ext in ('.txt', '.data', '.dat'):
        from psana.pscalib.calib.NDArrIO import load_txt  #, save_txt
        return load_txt(path)
    else:
        logger.debug(
            'not recognized file extension "%s", try to load as text file' %
            ext)
        from psana.pscalib.calib.NDArrIO import load_txt
        return load_txt(path)
Ejemplo n.º 2
0
    def data_geo(ntest):
        """Method for tests: returns test data numpy array and geometry object
    """
        from time import time
        from psana.pscalib.calib.NDArrIO import save_txt, load_txt
        from psana.pscalib.geometry.GeometryAccess import GeometryAccess

        dir = '/reg/g/psdm/detector/alignment/cspad/calib-cxi-camera2-2016-02-05'
        #fname_nda = '%s/nda-water-ring-cxij4716-r0022-e000001-CxiDs2-0-Cspad-0-ave.txt' % dir
        #fname_nda = '%s/nda-water-ring-cxij4716-r0022-e014636-CxiDs2-0-Cspad-0-ave.txt' % dir
        #fname_nda = '%s/nda-lysozyme-cxi02416-r0010-e052421-CxiDs2-0-Cspad-0-max.txt' % dir
        fname_nda = '%s/nda-lysozyme-cxi01516-r0026-e093480-CxiDs2-0-Cspad-0-max.txt'%dir if ntest in (21,28,29,30)\
                    else '%s/nda-water-ring-cxij4716-r0022-e014636-CxiDs2-0-Cspad-0-ave.txt'%dir
        fname_geo = '%s/calib/CsPad::CalibV1/CxiDs2.0:Cspad.0/geometry/geo-cxi01516-2016-02-18-Ag-behenate-tuned.data' % dir
        #fname_geo = '%s/geo-cxi02416-r0010-2016-03-11.txt' % dir
        fname_gain = '%s/calib/CsPad::CalibV1/CxiDs2.0:Cspad.0/pixel_gain/cxi01516-r0016-2016-02-18-FeKalpha.data' % dir

        # load n-d array with averaged water ring
        arr = load_txt(fname_nda)
        #arr *= load_txt(fname_gain)
        #print_ndarr(arr,'water ring')
        arr.shape = (arr.size, )  # (32*185*388,)

        # retrieve geometry
        t0_sec = time()
        geo = GeometryAccess(fname_geo)
        geo.move_geo('CSPAD:V1', 0, 1600, 0, 0)
        geo.move_geo('QUAD:V1', 2, -100, 0, 0)
        #geo.get_geo('QUAD:V1', 3).print_geo()
        print('Time to load geometry %.3f sec from file\n%s' %
              (time() - t0_sec, fname_geo))

        return arr, geo
Ejemplo n.º 3
0
def add_calib_file_to_cdb(exp, dircalib, calibvers, detname, cftype, fname,
                          cfdir, listdicts, **kwargs):
    """
    """
    d = history_dict_for_file(listdicts, fname)

    resp = parse_calib_file_name(fname)
    begin, end, ext = resp if resp is not None else (None, None, None)
    if begin is not None: begin = int(begin)

    if None in (begin, end, ext): return

    fpath = '%s/%s' % (cfdir, fname)

    verbose = kwargs.get('verbose', False)

    data = gu.load_textfile(fpath, verbose) if cftype in ('geometry','code_geometry') else\
           load_xtcav_calib_file(fpath) if is_xtcav(calibvers, cftype) else\
           load_txt(fpath) # using NDArrIO

    if isinstance(data, dict):
        serialize_dict(data)
        logger.debug(info_dict(data))
        #print_dict(data)
        #data = json.dumps(data) # (data,ensure_ascii=True) json.dumps converts dict -> str # .replace("'", '"') for json
        data = str(data)

    if isinstance(data, np.ndarray):
        check_data_shape(data, detname, cftype)

    begin_time, end_time = run_begin_end_time(exp, int(begin))

    if verbose:
        ndu.print_ndarr(data, 'scan calib: data')
        msg = 'scan calib: %s %s %s %s %s %s %s %s %s' % (
            exp, cfdir, fname, begin, end, ext, calibvers, detname, cftype)
        logger.info(msg)
        logger.info('begin_time: %s end_time: %s' % (begin_time, end_time))

    if data is None:
        msg = 'data is None, conversion is dropped for for file: %s' % fpath
        logger.warning(msg)
        return

    kwargs['run'] = begin
    kwargs['run_end'] = end
    kwargs['detector'] = detname
    kwargs['ctype'] = cftype
    kwargs['time_sec'] = begin_time
    kwargs['end_time'] = end_time
    kwargs['time_stamp'] = dbu._timestamp(begin_time)
    kwargs['extpars'] = d if d is not None else {
    }  # just in case save entire history dict
    #kwargs['comment']    = 'HISTORY: %s' % d.get('comment', '')

    dbu.insert_calib_data(data, **kwargs)
Ejemplo n.º 4
0
    def add(self):
        """Adds calibration constants to database from file.
        """
        kwargs = self.kwargs
        fname = kwargs.get('iofname', 'None')
        ctype = kwargs.get('ctype', 'None')
        assert os.path.exists(fname), 'File "%s" DOES NOT EXIST' % fname

        ext = os.path.splitext(fname)[-1]

        data = gu.load_textfile(fname, verb=False) if ctype == 'geometry' else\
               np.load(fname) if ext == '.npy' else\
               load_txt(fname)

        dbu.insert_calib_data(data, **kwargs)
Ejemplo n.º 5
0
    def add(self):
        """Adds calibration constants to database from file.
        """
        kwargs = self.kwargs
        fname = kwargs.get('iofname', 'None')
        ctype = kwargs.get('ctype', 'None')
        dtype = kwargs.get('dtype', 'None')
        verb = self.loglevel == 'DEBUG'
        assert os.path.exists(fname), 'File "%s" DOES NOT EXIST' % fname

        ext = os.path.splitext(fname)[-1]
        data = gu.load_textfile(fname, verb=verb) if ctype == 'geometry' or dtype in ('str', 'txt', 'text') else\
               load_xtcav_calib_file(fname)       if dtype == 'xtcav' else\
               np.load(fname)                     if ext == '.npy' else\
               gu.load_json(fname)                if ext == '.json' or dtype == 'json' else\
               gu.load_pickle(fname)              if ext == '.pkl' or dtype in ('pkl', 'pickle') else\
               load_txt(fname) # input NDArrIO

        dbu.insert_calib_data(data, **kwargs)
Ejemplo n.º 6
0
def add_calib_file_to_cdb(exp, dircalib, calibvers, detname, cftype, fname, cfdir, listdicts, **kwargs) :
    """
    """
    d = history_dict_for_file(listdicts, fname)

    resp = parse_calib_file_name(fname)
    begin, end, ext = resp if resp is not None else (None, None, None)

    if None in (begin, end, ext) : return

    fpath = '%s/%s' % (cfdir, fname)

    verbose = kwargs.get('verbose', False)

    data = gu.load_textfile(fpath, verbose) if cftype in ('geometry','code_geometry') else\
           load_xtcav_calib_file(fpath) if is_xtcav(calibvers, cftype) else\
           load_txt(fpath) # using NDArrIO

    begin_time, end_time = run_begin_end_time(exp, int(begin))

    if verbose :
        ndu.print_ndarr(data, 'scan calib: data')
        msg = 'scan calib: %s %s %s %s %s %s %s %s %s' % (exp, cfdir, fname, begin, end, ext, calibvers, detname, cftype)
        logger.info(msg)
        logger.info('begin_time: %s end_time: %s' % (begin_time, end_time))

    if data is None :
        msg = 'data is None, conversion is dropped for for file: %s' % fpath
        logger.warning(msg)        
        return

    kwargs['run']        = begin
    kwargs['run_end']    = end
    kwargs['detector']   = detname
    kwargs['ctype']      = cftype
    kwargs['time_sec']   = begin_time
    kwargs['end_time']   = end_time
    kwargs['time_stamp'] = dbu._timestamp(begin_time)
    kwargs['extpars']    = d # just in case save entire history dict
    #kwargs['comment']    = 'HISTORY: %s' % d.get('comment', '')

    dbu.insert_calib_data(data, **kwargs)
Ejemplo n.º 7
0
 def load_nda_from_file(self, path):
     ext = os.path.splitext(path)[1]
     nda = np.load(path) if ext in ('.npy', ) else load_txt(path)
     return nda