|
|
from enum import IntEnum |
|
|
from pathlib import Path |
|
|
|
|
|
import numpy as np |
|
|
from h5py import Dataset, File as H5File, Group |
|
|
|
|
|
|
|
|
class StatisticType(IntEnum): |
|
|
AVERAGE = 0 |
|
|
TOTAL = 1 |
|
|
MIN = 2 |
|
|
MAX = 3 |
|
|
|
|
|
|
|
|
class ImageMetadata: |
|
|
name: str |
|
|
group_path: str |
|
|
extents: np.ndarray |
|
|
origin: np.ndarray |
|
|
shape: tuple[int] |
|
|
|
|
|
def __init__(self, name: str, group_path: str): |
|
|
self.name = name |
|
|
self.group_path = group_path |
|
|
|
|
|
def get_dataset(self, hdf5File: H5File, time_index: int) -> Dataset: |
|
|
image_group: Group = hdf5File[self.group_path] |
|
|
image_ds: Dataset = image_group[f'time{time_index:06d}'] |
|
|
return image_ds |
|
|
|
|
|
def read(self, f: H5File): |
|
|
image_group: Group = f[self.group_path] |
|
|
|
|
|
|
|
|
extents_list = [] |
|
|
origin_list = [] |
|
|
if "ExtentX" in image_group.attrs: |
|
|
extents_list.append(image_group.attrs['ExtentX']) |
|
|
if "ExtentY" in image_group.attrs: |
|
|
extents_list.append(image_group.attrs['ExtentY']) |
|
|
if "ExtentZ" in image_group.attrs: |
|
|
extents_list.append(image_group.attrs['ExtentZ']) |
|
|
if "OriginX" in image_group.attrs: |
|
|
origin_list.append(image_group.attrs['OriginX']) |
|
|
if "OriginY" in image_group.attrs: |
|
|
origin_list.append(image_group.attrs['OriginY']) |
|
|
if "OriginZ" in image_group.attrs: |
|
|
origin_list.append(image_group.attrs['OriginZ']) |
|
|
self.extents = np.array(extents_list) |
|
|
self.origin = np.array(origin_list) |
|
|
self.shape = self.get_dataset(f, 0).shape |
|
|
|
|
|
|
|
|
class VariableInfo: |
|
|
var_index: int |
|
|
var_name: str |
|
|
stat_channel: int |
|
|
statistic_type: StatisticType |
|
|
stat_var_name: str |
|
|
stat_var_unit: str |
|
|
|
|
|
def __init__(self, stat_var_name: str, stat_var_unit: str, stat_channel: int, var_index: int): |
|
|
self.stat_var_name = stat_var_name |
|
|
self.stat_var_unit = stat_var_unit |
|
|
self.stat_channel = stat_channel |
|
|
self.var_index = var_index |
|
|
|
|
|
stat_type_raw = stat_var_name.split("_")[-1] |
|
|
self.statistic_type = StatisticType[stat_type_raw.upper()] |
|
|
self.var_name = stat_var_name.replace("_"+stat_type_raw, "") |
|
|
|
|
|
|
|
|
class PostProcessing: |
|
|
postprocessing_hdf5_path: Path |
|
|
times: np.ndarray |
|
|
variables: list[VariableInfo] |
|
|
statistics: np.ndarray |
|
|
image_metadata: list[ImageMetadata] |
|
|
|
|
|
def __init__(self, postprocessing_hdf5_path: Path): |
|
|
self.postprocessing_hdf5_path = postprocessing_hdf5_path |
|
|
self.variables = [] |
|
|
self.image_metadata = [] |
|
|
|
|
|
|
|
|
def read(self) -> None: |
|
|
|
|
|
with H5File(self.postprocessing_hdf5_path, 'r') as f: |
|
|
|
|
|
times_ds: Dataset = f['/PostProcessing/Times'] |
|
|
|
|
|
self.times = times_ds[()] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var_stats_grp = f['/PostProcessing/VariableStatistics'] |
|
|
|
|
|
var_name_by_channel: dict[int, str] = {} |
|
|
var_unit_by_channel: dict[int, str] = {} |
|
|
for k, v in var_stats_grp.attrs.items(): |
|
|
parts = k.split('_') |
|
|
channel = int(parts[1]) |
|
|
value = v.decode('utf-8') |
|
|
if parts[2] == "name": |
|
|
var_name_by_channel[channel] = value |
|
|
elif parts[2] == "unit": |
|
|
var_unit_by_channel[channel] = value |
|
|
|
|
|
self.variables = [VariableInfo(stat_var_name=var_name_by_channel[i], |
|
|
stat_var_unit=var_unit_by_channel[i], |
|
|
stat_channel=i, |
|
|
var_index=i//4) |
|
|
for i in range(len(var_name_by_channel))] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
statistics_raw: np.ndarray = np.zeros((len(self.times), len(self.variables))) |
|
|
for time_index in range(len(self.times)): |
|
|
time_ds: Dataset = var_stats_grp[f'time{time_index:06d}'] |
|
|
statistics_raw[time_index, :] = time_ds[()] |
|
|
|
|
|
|
|
|
self.statistics = statistics_raw.reshape((len(self.times), len(self.variables)//4, 4)) |
|
|
|
|
|
|
|
|
|
|
|
image_groups = [k for k in f['/PostProcessing'].keys() if k not in ['Times', 'VariableStatistics']] |
|
|
|
|
|
|
|
|
for image_group in image_groups: |
|
|
metadata = ImageMetadata(group_path=f"/PostProcessing/{image_group}", name=image_group) |
|
|
metadata.read(f) |
|
|
self.image_metadata.append(metadata) |
|
|
|
|
|
def read_image_data(self, image_metadata: ImageMetadata, time_index: int) -> np.ndarray: |
|
|
with H5File(self.postprocessing_hdf5_path, 'r') as f: |
|
|
image_ds: Dataset = image_metadata.get_dataset(hdf5File=f, time_index=time_index) |
|
|
return image_ds[()] |
|
|
|