.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_read_write_phicore_hdf5.py: Creating and reading phicore HDF5 files ======================================= In thus example we will create a sample phicore HDF5 files then read it back as ``xarray.DataArray`` .. code-block:: python 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, .. code-block:: python print(X) fh = PhiDataFile('test_file.h5', 'w', force=True) fh.write_xarray(X) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 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, .. code-block:: python fh = PhiDataFile('test_file.h5', 'r') X_out = fh.read_xarray('/data/X') print(X) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 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) .. _sphx_glr_download_examples_read_write_phicore_hdf5.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: read_write_phicore_hdf5.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: read_write_phicore_hdf5.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_