Exemple #1
0
def read_tbt(file_path):
    """
    Reads TbTData object from provided file_path
    Args:
        file_path: path to a file containing TbtData

    Returns:
        TbtData
    """
    if _is_ascii_file(file_path):
        matrices, date = _read_ascii(file_path)
        return handler.TbtData(matrices, date, [0], matrices[0]["X"].shape[1])

    sdds_file = sdds.read(file_path)
    nbunches = sdds_file.values[N_BUNCHES]
    bunch_ids = sdds_file.values[BUNCH_ID if BUNCH_ID in
                                 sdds_file.values else HOR_BUNCH_ID]
    if len(bunch_ids) > nbunches:
        bunch_ids = bunch_ids[:nbunches]
    nturns = sdds_file.values[N_TURNS]
    date = pytz.utc.localize(
        datetime.utcfromtimestamp(sdds_file.values[ACQ_STAMP] / 1e9))
    bpm_names = sdds_file.values[BPM_NAMES]
    nbpms = len(bpm_names)
    data = {
        k: sdds_file.values[POSITIONS[k]].reshape((nbpms, nbunches, nturns))
        for k in PLANES
    }
    matrices = [{
        k: pd.DataFrame(index=bpm_names, data=data[k][:, idx, :], dtype=float)
        for k in data
    } for idx in range(nbunches)]
    return handler.TbtData(matrices, date, bunch_ids, nturns)
Exemple #2
0
def read_tbt(file_path):
    """
    Reads TbTData object from provided file_path
    Args:
        file_path: path to a file containing TbtData

    Returns:
        TbtData
    """

    hdf_file = h5py.File(file_path, 'r')
    bunch_ids = [1]
    date = datetime.now()

    for vers in VERSIONS[::-1]:
        try:
            bpm_names = FUNCTIONS[vers]['get_bpm_names'](hdf_file)
            nturns = FUNCTIONS[vers]['get_nturns'](hdf_file, vers)
            matrices = [{k: pd.DataFrame(index=bpm_names,
                                         data=FUNCTIONS[vers]['get_tbtdata'](hdf_file, k, vers),
                                         dtype=float) for k in PLANES}]

            return handler.TbtData(matrices, date, bunch_ids, nturns)

        except TypeError:
            pass
Exemple #3
0
def read_tbt(file_path):
    """
    Reads TbtData object from PTC trackone output.

    """
    LOGGER.debug(f"Reading path: {file_path}")

    with open(file_path, "r") as tfs_data:
        lines = tfs_data.readlines()

    # header
    date, header_length = _read_header(lines)
    lines = lines[header_length:]

    # parameters
    bpms, particles, column_indices, n_turns, n_particles = _read_from_first_turn(lines)

    # data (read into dict first for speed, then convert to DF)
    matrices = [{p: {bpm: np.zeros(n_turns) for bpm in bpms} for p in PLANES} for _ in range(n_particles)]
    matrices = _read_data(lines, matrices, column_indices)
    for bunch in range(n_particles):
        for plane in PLANES:
            matrices[bunch][plane] = pd.DataFrame(matrices[bunch][plane]).transpose()

    LOGGER.debug(f"Read Tbt data from : {file_path}")
    return handler.TbtData(matrices, date, particles, n_turns)
Exemple #4
0
def test_compare_average_Tbtdata():
    npart = 10
    data = {
        plane: np.concatenate([[
            _create_data(np.linspace(1, 10, 10, endpoint=False, dtype=int), 2,
                         (lambda x: np.random.randn(len(x))))
        ] for _ in range(npart)],
                              axis=0)
        for plane in PLANES
    }

    origin = handler.TbtData(matrices=[{
        'X':
        pd.DataFrame(index=['IBPMA1C', 'IBPME2R'],
                     data=data['X'][i],
                     dtype=float),
        'Y':
        pd.DataFrame(index=['IBPMA1C', 'IBPME2R'],
                     data=data['Y'][i],
                     dtype=float)
    } for i in range(npart)],
                             date=datetime.now(),
                             bunch_ids=range(npart),
                             nturns=10)

    new = handler.TbtData(matrices=[{
        'X':
        pd.DataFrame(index=['IBPMA1C', 'IBPME2R'],
                     data=np.mean(data['X'], axis=0),
                     dtype=float),
        'Y':
        pd.DataFrame(index=['IBPMA1C', 'IBPME2R'],
                     data=np.mean(data['Y'], axis=0),
                     dtype=float)
    }],
                          date=datetime.now(),
                          bunch_ids=[1],
                          nturns=10)

    _compare_tbt(handler.generate_average_tbtdata(origin), new, False)
Exemple #5
0
def _original_trackone(track=False):
    names = np.array(["C1.BPM1"])
    matrix = [
        dict(
            X=pd.DataFrame(index=names,
                           data=[[0.001, -0.0003606, -0.00165823,
                                  -0.00266631]]),
            Y=pd.DataFrame(index=names,
                           data=[[0.001, 0.00070558, -0.00020681,
                                  -0.00093807]])),
        dict(X=pd.DataFrame(
            index=names,
            data=[[0.0011, -0.00039666, -0.00182406, -0.00293294]]),
             Y=pd.DataFrame(
                 index=names,
                 data=[[0.0011, 0.00077614, -0.00022749, -0.00103188]]))
    ]
    origin = handler.TbtData(matrix, None, [0, 1] if track else [1, 2], 4)
    return origin
Exemple #6
0
def test_tbt_read_hdf5_v2(_hdf5_file_v2):

    origin = handler.TbtData(matrices=[{
        'X':
        pd.DataFrame(index=['IBPMA1C', 'IBPME2R'],
                     data=_create_data(
                         np.linspace(-np.pi, np.pi, 2000, endpoint=False), 2,
                         np.sin),
                     dtype=float),
        'Y':
        pd.DataFrame(index=['IBPMA1C', 'IBPME2R'],
                     data=_create_data(
                         np.linspace(-np.pi, np.pi, 2000, endpoint=False), 2,
                         np.cos),
                     dtype=float)
    }],
                             date=datetime.now(),
                             bunch_ids=[1],
                             nturns=2000)
    new = reader_iota.read_tbt(_hdf5_file_v2)
    _compare_tbt(origin, new, False)