Create Dimension scales on for a HDF5 dataset using Python through h5py

Overview:

  • A dataset can have one or more scales attached it on each of its dimensions.
  • For example, if a HDF5 dataset has a dimension of (100x100) denoted as (d1, d2) – then dimension d1 can have one or many scales attached to it.
  • Each scale will have units running from Unit 1 to Unit ‘n’. The HDF5 dataset is expected to have a subset (typically) of these values.
  • If a dimension is to have several scales, it is for the application that produces the data to define which scale to use. For example, the scale to be used can be specified in an attribute.
  • HDF5 defines the association between the dataset and the dimension scale to be loosely coupled to the maximum extent possible.

 

Example:

# Example Python program that creates dimension scales

# and attaches it to a HDF5 dataset

import h5py

import random

 

# Create hdf5 file

file        = h5py.File("Hdata.hdf5", "w");

 

# Create a group

grp         = file.create_group("2019|Dec|8-14");

 

# Create a dataset

file["observations"]   = grp.create_dataset("o", (5, 5));

 

# Label the dimensions of the dataset

file['observations'].dims[0].label = 'xlabel';

file['observations'].dims[1].label = 'xlabel';

 

# Create units for scales(i.e, datasets)

file["scale1units"] = [10, 20, 30, 40, 50];

file["scale2units"] = [0, 15, 30, 60, 90 ]

 

# Make the units into scales

file['scale1units'].make_scale('x')

file['scale2units'].make_scale('y')

 

# Attach scales to units

file["observations"].dims[0].attach_scale(file["scale1units"]);

file["observations"].dims[1].attach_scale(file["scale2units"]);

 

# Populate data

for i in range (0, 5):

    for j in range (0, 5):

        file["observations"][i][j] = random.randrange(10, 60, 10);

 

# Dimension labels

print("Dimension labels:");

for dim in file["observations"].dims:

    print(dim);

 

# Dimension scales

print("Name of the Dimension scales:");

print(file["observations"].dims[0].keys());

print(file["observations"].dims[1].keys());

 

file.close();

 

Output:

Dimension labels:

<"xlabel" dimension 0 of HDF5 dataset at 4421111392>

<"xlabel" dimension 1 of HDF5 dataset at 4421111392>

Name of the Dimension scales:

['x']

['y']


Copyright 2024 © pythontic.com