とらりもんHOME  Index  Search  Changes  Login

HDF5

(We assume you are using Ubuntu Linux 16.04)

What is HDF?

  • HDF is a data format which is common and popular for satellite data especially of NASA and JAXA.
  • There are HDF4 and HDF5.
  • HDF5 may be simpler and easier to understand and handle.
  • HDF4までのscientific dataset (SDS)はHDF5ではdatasetになったらしい。

If you want to use HDF4, go to HDF4 page!!

HDF5 documents

Installing HDF5 libraries:

  • HDF5
sudo apt-get install hdf5-tools hdf5-helpers libhdf5-dev libhdf5-doc libhdf5-serial-dev

HDF5 useful commands

... Perhaps "h5dump" works for most of your purpose!

h5dump -H HDF5_filename   ..... show HDF5 file headder info.
h5dump -n HDF5_filename   ..... show summary of datasets in HDF5 file.
h5dump -d データセット名 -b -o 出力ファイル名 HDF5ファイル名   .... HDF5ファイルの中のデータセットをシンプルなrawバイナリのファイルに書き出す。
h5ls HDF5ファイル名/グループ名/データ名 ... データの構造(配列のサイズなど)を表示
例: h5ls GPMCOR_DPR_1405281041_1214_001397_L2S_DD2_05A.h5/MS/PRE/sigmaZeroMeasured

h5dump command

詳細は, https://support.hdfgroup.org/HDF5/doc/RM/Tools/h5dump.htm

例: 表示する桁数が不十分なとき:

h5dump - A target_file
h5dump - A -m '%.3f' target_file

Example: How to find a unit of a product?

$ h5dump -n GPMMRG_MAP_2201_M_L3S_MCM_05A.h5
HDF5 "GPMMRG_MAP_2201_M_L3S_MCM_05A.h5" {
FILE_CONTENTS {
 group      /
 group      /Grid
 dataset    /Grid/Latitude
 dataset    /Grid/Longitude
 dataset    /Grid/gaugeQualityInfo
 dataset    /Grid/monthlyPrecipRate
 ...
 }
}

$ h5dump -a /Grid/monthlyPrecipRate/units GPMMRG_MAP_2201_M_L3S_MCM_05A.h5
HDF5 "GPMMRG_MAP_2201_M_L3S_MCM_05A.h5" {
ATTRIBUTE "units" {
  DATATYPE  H5T_STRING {
     STRSIZE 6;
     STRPAD H5T_STR_NULLTERM;
     CSET H5T_CSET_ASCII;
     CTYPE H5T_C_S1;
  }
  DATASPACE  SCALAR
  DATA {
  (0): "mm/hr"
  }
}
}

HDF5 to GeoTiff

$ gdal_translate 'HDF5:"GPMMRG_MAP_2101_M_L3S_MCM_04G.h5"://Grid/monthlyPrecipRateGC' 2021_01_monthlyPrecipRateGC.tiff
  • まずHDF5ファイルの構造を調べて, 抜き出したいdatasetを知る。例: $ gdalinfo GPMMRG_MAP_2101_M_L3S_MCM_04G.h5
  • その中のダブルクオートで書かれている部分(HDF5:.....)を抜き出し, gdal_translateに入れればOK。

Handle HDF5 file in C language and/or C++ language

/* H5test_write.c
 * Creating and closing a dataset.
 * editted on "h5_crtdat.c" taken from ftp://hdfgroup.org/HDF5/releases/hdf5-1.6/hdf5-1.6.7/src/unpacked/doc/html/Tutor/title.html
 * 2017/12/15 Kenlo Nasahara
 * compile: h5cc H5test_write.c
 */

# include <hdf5.h>
# define FILE "dset.h5"

int main() {
  hid_t       file_id, dataset_id, dataspace_id;
  hsize_t     dims[2] = {4, 6};
  herr_t      status;

  int         i, j, dset_data[4][6];

  /* Initialize the dataset. */
  for (i = 0; i < 4; i++)
     for (j = 0; j < 6; j++)
        dset_data[i][j] = i * 6 + j + 1;


  /* Create a new file using default properties. */
  file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

  /* Create the data space for the dataset. */
  dataspace_id = H5Screate_simple(2, dims, NULL);

  /* Create the dataset. */
  dataset_id=  H5Dcreate(file_id, "/dset", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

  /* Write the dataset. */
  status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dset_data);

  /* End access to the dataset and release resources used by it. */
  status = H5Dclose(dataset_id);

  /* Terminate access to the data space. */ 
  status = H5Sclose(dataspace_id);

  /* Close the file. */
  status = H5Fclose(file_id);
}
  • 上で作ったHDF5ファイルを読むサンプルプログラム(C言語)↓
/* H5test_read.c
 * Reading a dataset.
 * editted on "h5_rdwt.c" taken from ftp://hdfgroup.org/HDF5/releases/hdf5-1.6/hdf5-1.6.7/src/unpacked/doc/html/Tutor/title.html
 * 2017/12/15 Kenlo Nasahara
 * compile: h5cc H5test_read.c
 */

#include <hdf5.h>
#include<stdio.h>
#include<stdlib.h>
#define FILE "dset.h5"

int main() {
  hid_t       file_id, dataset_id, dataspace_id, filespace_id;  // identifiers
  herr_t      status;
  int         *dset_data;
  int         x, y;
  int         rank;
  hsize_t     dims[2];

  /* Open an existing file. */
  file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);

  /* Open an existing dataset. */
  dataset_id = H5Dopen(file_id, "/dset", H5P_DEFAULT);

  /* Read the dataset. */
  filespace_id= H5Dget_space (dataset_id);    // just for reading rand and dimensions
  rank        = H5Sget_simple_extent_ndims(filespace_id);               // read rank
  status      = H5Sget_simple_extent_dims (filespace_id, dims, NULL);   // read dimensions
  dset_data   = calloc(sizeof(int), dims[0]*dims[1]);    // array for data
  status      = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dset_data);

  for (y = 0; y < dims[0]; y++)
     for (x = 0; x < dims[1]; x++)
        printf("%d ", dset_data[y*dims[1]+x]);

  /* Close the dataset. */
  status = H5Dclose(dataset_id);

  /* Close the file. */
  status = H5Fclose(file_id);
  free(dset_data);
}
  • HDF5ファイルを作るサンプルプログラム(C++)
/* H5test_write.cpp
 * Creating and closing a dataset.
 * editted on "H5test_write.c"
 * 2017/12/15 Kenlo Nasahara
 * compile: h5c++ H5test_write.cpp
 */

# include <iostream>
# include <H5Cpp.h>
# define FILE "dset.h5"

using namespace std;
 
int main() {
  H5::H5File    file(FILE, H5F_ACC_TRUNC);
  H5::IntType   datatype( H5::PredType::NATIVE_INT );
  datatype.setOrder( H5T_ORDER_LE );
  hsize_t       dims[2] = {4, 6};
  H5::DataSpace dataspace( 2, dims );
  H5::DataSet   dataset   = file.createDataSet( "/dset", datatype, dataspace );
  int         i, j, dset_data[4][6];
  for (i = 0; i < 4; i++)
     for (j = 0; j < 6; j++)
        dset_data[i][j] = i * 6 + j + 1;
  dataset.write(dset_data, datatype );
  return 0;  // successfully terminated
}
  • 上で作ったHDF5ファイルを読むサンプルプログラム(C++)↓
/* H5test_read.cpp
 * Reading a dataset.
 * editted on "H5test_read.c"
 * 2017/12/15 Kenlo Nasahara
 * compile: h5c++ H5test_read.cpp
 */

# include <iostream>
# include <H5Cpp.h>
# include <stdlib.h>
# define FILE "dset.h5"

using namespace std;

int main() {
  H5::H5File    file(FILE, H5F_ACC_RDONLY);
  H5::DataSet   dataset   = file.openDataSet("/dset");
  H5::DataSpace dataspace = dataset.getSpace();
  int           rank=dataspace.getSimpleExtentNdims();
  hsize_t       dims[2];
  int ndims=dataspace.getSimpleExtentDims(dims, NULL);

  int         *dset_data;
  dset_data   = (int*)calloc(sizeof(int), dims[0]*dims[1]);

  dataset.read(dset_data, H5::PredType::NATIVE_INT);

  int x, y;
  for (y = 0; y < dims[0]; y++)
     for (x = 0; x < dims[1]; x++)
        printf("%d ", dset_data[y*dims[1]+x]);

  free(dset_data);
}

Handle HDF5 file with python (Ubuntu)

$ sudo apt-get install python-h5py python3-h5py
$ ipython3
import h5py
import numpy as np
fn='GPMTRM_PR1_1311302305_0038_091382_L2S_PU2_8b18_mod.h5'
infh=h5py.File(fn, 'r')
za=infh['/NS/PRE/localZenithAngle'].value

Last modified:2022/11/11 14:44:09
Keyword(s):
References:[とらりもんHOME] [HDF4]