これは何?  †
- HDF5は、たくさんのファイルを一つのファイルに格納するためのフォーマット
- 追記が簡単なんで、科学分野でデータログを記録するのに使われている
- 人工衛星は、地球を周回しながら、次々と地表の写真を撮像する。地上の基地局上空で、それらの画像を落とすときに HDF 形式の1ファイルにまとめる。
 
- Viewer
Python package のインストール  †
(jupyter-env) [~]$ conda install h5py
サンプルプログラム  †
- 直交行列をテキストで公開しているサイトから、行列を読み取って numpy.ndarray 型で、HDF に登録する
HDF5への書き込み  †
#!/usr/bin/env python
# coding: utf-8
"""
Create numpy formatted Orthogonal Arrays from text file.
This program create a hdf5 file contains many arrays.
These arrays are came from :
http://support.sas.com/techsup/technote/ts723_Designs.txt
"""
__author__ = "HONDOH Atsushi <kagyuu@hondou.homedns.org>"
__version__ = "1.00"
__date__ = "June 28 2018"
import h5py
import re
import numpy as np
from typing import List, Any
def read_header(line):
    header = line.split("n=")
    name = header[0].strip()
    rows = header[1].strip()
    cols: List[tuple[Any]] = []
    for col in name.split(" "):
        cols.append(tuple(col.split("^")))
    return name, rows, cols
def save_matrix(name, matrix, fd):
    print(name)
    print(np.array(matrix))
    fd.create_dataset(name, data=np.array(matrix))
def main():
    test_data = open("ts723_Designs.txt", "r")
    hdf5 = h5py.File("oamatrix.hdf5", 'w')
    lines = test_data.readlines()
    count = 0
    name = ""
    rows = ""
    cols = []
    matrix = None
    for line in lines:
        # this line is blank line
        if len(line.strip()) == 0:
            continue
        # this line is matrix header line, like as "2^12 12^1     n=24"
        if re.match(".*n=[0-9]+", line):
            if matrix is not None:
                save_matrix(name, matrix, hdf5)
            name, rows, cols = read_header(line)
            matrix = list()
            count += 1
            continue
        # this line is matrix row line, like as "101101000111 3"
        # convert this line to an array
        ary = []
        p = 0
        for col in cols:
            # the col is "s^n"
            s = col[0]
            n = col[1]
            # If col < 10 then this col is defined in 1 digit.
            # Because {0,1,2,3,4,5,6,7,8,9}
            digit: int = 1 if int(s) <= 10 else 2
            for cnt in range(0, int(n)):
                ary.append(int(line[p: p + digit]))
                p += digit
        matrix.append(ary)
    # save the last matrix
    # Because this program module save matrix each when the header of matrix were found.
    if matrix is not None:
        save_matrix(name, matrix, hdf5)
    test_data.close()
    hdf5.flush()
    hdf5.close()
if __name__ == '__main__':
    main()
- ファイルを開く
hdf5 = h5py.File("oamatrix.hdf5", 'w')
- 書き込む
hdf5.create_dataset(name, data=np.array(matrix)) name をキーに numpy の配列を格納する
- ファイルを閉じる
hdf5.flush()
hdf5.close() 
HDF5からの読み込み  †
import h5py
import numpy as np
def main() :
    hdf5 = h5py.File("oamatrix.hdf5", 'r')
    print(list(hdf5.keys()))
    print(hdf5["2^3"].value)
    hdf5.close()
if __name__ == '__main__':
    main()
- hdf5.keys() で、格納されているデータのキーが取得できる
- hdf5[key].value で、key をキーに格納されているデータを取得
Python