File size: 2,011 Bytes
b64b64f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Lightweight NumPy/h5py reader for the T4 structural-dynamics pilot."""

from __future__ import annotations

import json
from pathlib import Path

import h5py
import numpy as np


DEFAULT_PATH = (
    Path(__file__).resolve().parents[1]
    / "data"
    / "t4_structural_dynamics_pilot"
    / "pilot.h5"
)


def trajectory_ids(path: str | Path = DEFAULT_PATH) -> tuple[str, ...]:
    with h5py.File(path, "r") as h5:
        return tuple(sorted(h5["trajectories"]))


def load_trajectory(
    trajectory_id: str,
    path: str | Path = DEFAULT_PATH,
    *,
    include_fields: bool = True,
) -> dict[str, object]:
    with h5py.File(path, "r") as h5:
        group = h5[f"trajectories/{trajectory_id}"]
        arrays = {
            name: np.asarray(dataset)
            for name, dataset in group.items()
            if isinstance(dataset, h5py.Dataset)
        }
        if include_fields:
            arrays["field_time_s"] = np.asarray(group["fields/time_s"])
            arrays["displacement_m"] = np.asarray(group["fields/displacement_m"])
            arrays["velocity_m_per_s"] = np.asarray(group["fields/velocity_m_per_s"])
            arrays["acceleration_m_per_s2"] = np.asarray(
                group["fields/acceleration_m_per_s2"]
            )
            arrays["reference_geometry_m"] = np.asarray(
                h5["common/reference_geometry_m"]
            )
            arrays["topology"] = np.asarray(h5["common/topology"])
            arrays["sensor_coordinates_m"] = np.asarray(
                h5["common/sensor_coordinates_m"]
            )
        return {
            "trajectory_id": trajectory_id,
            "spec": json.loads(group.attrs["spec_json"]),
            "attributes": {name: group.attrs[name] for name in group.attrs},
            "arrays": arrays,
        }


if __name__ == "__main__":
    ids = trajectory_ids()
    sample = load_trajectory(ids[0], include_fields=False)
    print(ids)
    print({name: value.shape for name, value in sample["arrays"].items()})