Creating and reading phicore HDF5 filesΒΆ

In thus example we will create a sample phicore HDF5 files then read it back as xarray.DataArray

import numpy as np
import xarray as xr

from phicore.io import PhiDataFile


rng = np.random.RandomState(42)

Nx, Ny, Nw = 5, 5, 3

X = xr.DataArray(rng.rand(Nx, Ny, Nw),
                 dims=['x', 'y', 'w'],
                 coords={'x': np.arange(Nx), 'y': np.arange(Ny),
                         'w': np.linspace(2.3, 2.5, Nw)},
                 attrs={'scale_units': {'x': 'px', 'y': 'px', 'w': 'PHz.rad'}},
                 name='X')

The xarray.DataArray that we will use as an example, is as follows,

print(X)


fh = PhiDataFile('test_file.h5', 'w', force=True)
fh.write_xarray(X)

Out:

<xarray.DataArray 'X' (x: 5, y: 5, w: 3)>
array([[[0.37454 , 0.950714, 0.731994],
        [0.598658, 0.156019, 0.155995],
        [0.058084, 0.866176, 0.601115],
        [0.708073, 0.020584, 0.96991 ],
        [0.832443, 0.212339, 0.181825]],

       [[0.183405, 0.304242, 0.524756],
        [0.431945, 0.291229, 0.611853],
        [0.139494, 0.292145, 0.366362],
        [0.45607 , 0.785176, 0.199674],
        [0.514234, 0.592415, 0.04645 ]],

       [[0.607545, 0.170524, 0.065052],
        [0.948886, 0.965632, 0.808397],
        [0.304614, 0.097672, 0.684233],
        [0.440152, 0.122038, 0.495177],
        [0.034389, 0.90932 , 0.25878 ]],

       [[0.662522, 0.311711, 0.520068],
        [0.54671 , 0.184854, 0.969585],
        [0.775133, 0.939499, 0.894827],
        [0.5979  , 0.921874, 0.088493],
        [0.195983, 0.045227, 0.32533 ]],

       [[0.388677, 0.271349, 0.828738],
        [0.356753, 0.280935, 0.542696],
        [0.140924, 0.802197, 0.074551],
        [0.986887, 0.772245, 0.198716],
        [0.005522, 0.815461, 0.706857]]])
Coordinates:
  * x        (x) int64 0 1 2 3 4
  * y        (y) int64 0 1 2 3 4
  * w        (w) float64 2.3 2.4 2.5
Attributes:
    scale_units:  {'x': 'px', 'y': 'px', 'w': 'PHz.rad'}

Now we will load this data back,

fh = PhiDataFile('test_file.h5', 'r')


X_out = fh.read_xarray('/data/X')

print(X)

Out:

<xarray.DataArray 'X' (x: 5, y: 5, w: 3)>
array([[[0.37454 , 0.950714, 0.731994],
        [0.598658, 0.156019, 0.155995],
        [0.058084, 0.866176, 0.601115],
        [0.708073, 0.020584, 0.96991 ],
        [0.832443, 0.212339, 0.181825]],

       [[0.183405, 0.304242, 0.524756],
        [0.431945, 0.291229, 0.611853],
        [0.139494, 0.292145, 0.366362],
        [0.45607 , 0.785176, 0.199674],
        [0.514234, 0.592415, 0.04645 ]],

       [[0.607545, 0.170524, 0.065052],
        [0.948886, 0.965632, 0.808397],
        [0.304614, 0.097672, 0.684233],
        [0.440152, 0.122038, 0.495177],
        [0.034389, 0.90932 , 0.25878 ]],

       [[0.662522, 0.311711, 0.520068],
        [0.54671 , 0.184854, 0.969585],
        [0.775133, 0.939499, 0.894827],
        [0.5979  , 0.921874, 0.088493],
        [0.195983, 0.045227, 0.32533 ]],

       [[0.388677, 0.271349, 0.828738],
        [0.356753, 0.280935, 0.542696],
        [0.140924, 0.802197, 0.074551],
        [0.986887, 0.772245, 0.198716],
        [0.005522, 0.815461, 0.706857]]])
Coordinates:
  * x        (x) int64 0 1 2 3 4
  * y        (y) int64 0 1 2 3 4
  * w        (w) float64 2.3 2.4 2.5
Attributes:
    scale_units:  {'x': 'px', 'y': 'px', 'w': 'PHz.rad'}

Total running time of the script: ( 0 minutes 0.295 seconds)

Gallery generated by Sphinx-Gallery