Example #1
0
def test_append_3d():
    from fact.io import initialize_h5py, append_to_h5py

    array = np.zeros(100, dtype=[('x', 'float64', (3, 3))])

    with tempfile.NamedTemporaryFile(suffix='.hdf5') as f:
        with h5py.File(f.name, 'w') as h5file:
            initialize_h5py(h5file, array, key='events')

            append_to_h5py(h5file, array, key='events')
            append_to_h5py(h5file, array, key='events')
Example #2
0
def test_initialize_h5py():
    from fact.io import initialize_h5py

    df = pd.DataFrame({
        'x': [1, 2, 3],
        'name': ['Crab', 'Mrk 501', 'Test'],
        't': ['2017-10-01 21:22', '2017-10-01 21:23', '2017-10-01 21:24'],
        's': [[0, 1], [0, 2], [0, 3]],
    })
    df['t'] = pd.to_datetime(df['t'])

    with tempfile.NamedTemporaryFile(suffix='.hdf5') as f:
        with h5py.File(f.name, 'w') as h5file:
            initialize_h5py(h5file, df.to_records(index=False), key='events')

        with h5py.File(f.name, 'r') as h5file:
            assert h5file['events']['name'].dtype == np.dtype('O')
            assert h5file['events']['x'].dtype == np.dtype('int64')
            assert h5file['events']['t'].dtype == np.dtype('S48')
            assert h5file['events']['s'].shape == (0, 2)
Example #3
0
def write_to_hdf(recarray, filename, key='events'):
    with h5py.File(filename, mode='a') as f:
        if key not in f:
            initialize_h5py(f, recarray, key=key)
        append_to_h5py(f, recarray, key=key)
Example #4
0
def write_fits_to_hdf5(outputfile,
                       inputfiles,
                       mode='a',
                       compression='gzip',
                       progress=True,
                       key='events'):

    initialized = False

    version = None
    with h5py.File(outputfile, mode) as hdf_file:

        for inputfile in tqdm(inputfiles, disable=not progress):
            with fits.open(inputfile) as f:

                if version is None:
                    version = f[0].header['VERSION']
                    hdf_file.attrs['fact_tools_version'] = version
                else:
                    if version != f[0].header['VERSION']:
                        raise ValueError(
                            'Merging output of different FACT-Tools versions not allowed'
                        )

                if len(f) < 2:
                    continue

                array = np.array(f[1].data[:])

                # convert all names to snake case
                array.dtype.names = rename_columns(array.dtype.names)

                # add columns with theta in degrees
                arrays = []
                names = []
                for in_col, out_col in zip(theta_columns, theta_deg_columns):
                    if in_col in array.dtype.names:
                        arrays.append(camera_distance_mm_to_deg(array[in_col]))
                        names.append(out_col)

                if len(names) > 0:
                    array = recfunctions.append_fields(
                        array,
                        names=names,
                        data=arrays,
                        usemask=False,
                    )

                if not initialized:
                    initialize_h5py(
                        hdf_file,
                        array,
                        key=key,
                        compression=compression,
                    )
                    initialized = True

                append_to_h5py(hdf_file, array, key=key)

            if 'timestamp' in array.dtype.names:
                hdf_file[key]['timestamp'].attrs['timeformat'] = 'iso'