IMPORTANT NOTICE: An upgrade of the ICARE network architecture is scheduled on Thursday, September 27, 2018 from 9:30 AM to 4:00 PM CEST (UTC +2:00).
All ICARE data services will be unavailable during this operation. We apologize for any inconvenience this may cause and thank you for your patience.

Writing an HDF file with C, FORTRAN, Python, MATLAB and R

Language

HDF4

Python

import numpy
from pyhdf.SD import SD, SDC
 
# Open file for writing
fileName = "SDS.hdf"
filehdf  = SD(fileName, SDC.WRITE | SDC.CREATE)
 
# Init the data buffer to write : a vector of values from 0 to 24, separated by 1
data = numpy.arange(25)
# Make it a 2D array
data.shape = (5, 5)
 
# Create the dataset : set its name, data type et dimensions
sds_name = "SDStemplate"
sds = filehdf.create(sds_name, SDC.INT32, data.shape)
 
# Write the data to it
sds[:] = data
 
# Terminate access to the data set
sds.endaccess()
 
# Close the file
filehdf.end()

C

#include "mfhdf.h"
#define FILE_NAME "SDS.hdf"
#define SDS_NAME  "SDStemplate"
#define X_LENGTH  5
#define Y_LENGTH  5
 
main( )
{
    /************************* Variable declaration **************************/
    int32 sd_id, sds_id, sds_index;
    intn  status;
    int32 start[2], edges[2];
    int32 data[Y_LENGTH][X_LENGTH];
    int   i, j;
    /********************* End of variable declaration ***********************/
 
    /*
    * Data set data initialization.
    */
    for ( j = 0; j < Y_LENGTH; j++ ) {
        for ( i = 0; i < X_LENGTH; i++ )
            data[j][i] = ( i + j ) + 1;
    }
 
    /*
    * Open the file and initialize the SD interface.
    */
    sd_id = SDstart ( FILE_NAME, DFACC_WRITE );
    /*
     * Attach to the first data set.
    */
    sds_index = 0;
    sds_id = SDselect ( sd_id, sds_index );
    /*
    * Define the location and size of the data to be written to the data set.
    */
    start[0] = 0;
    start[1] = 0;
    edges[0] = Y_LENGTH;
    edges[1] = X_LENGTH;
 
    /*
    * Write the stored data to the data set. The third argument is set to NULL
    * to specify contiguous data elements. The last argument must
    * be explicitly cast to a generic pointer since SDwritedata is designed
    * to write generic data.
    */
    status = SDwritedata ( sds_id, start, NULL, edges, ( VOIDP ) data );
    /*
    * Terminate access to the data set.
    */
    status = SDendaccess ( sds_id );
    /*
    * Terminate access to the SD interface and close the file.
    */
    status = SDend ( sd_id );
}

FORTRAN

program  write_dataprogram  write_data
    implicit none
 
C
C     Parameter declaration.
C
    character * 7  FILE_NAME
    character * 11 SDS_NAME
    integer      X_LENGTH, Y_LENGTH, RANK
    parameter   ( FILE_NAME = "SDS.hdf",
    +             SDS_NAME  = "SDStemplate",
    +             X_LENGTH  = 5,
    +             Y_LENGTH  = 16,
    +             RANK      = 2 )
    integer       DFACC_WRITE, DFNT_INT32
    parameter   ( DFACC_WRITE = 2,
    +             DFNT_INT32  = 24 )
 
C
C     Function declaration.
C
    integer sfstart, sfselect, sfwdata, sfendacc, sfend
 
C**** Variable declaration *******************************************
C
    integer sd_id, sds_id, sds_index, status
    integer start ( 2 ), edges ( 2 ), stride ( 2 )
    integer i, j
    integer data( X_LENGTH, Y_LENGTH )
C
C**** End of variable declaration ************************************
C
 
C
C     Data set data initialization.
C
    do  20 j = 1, Y_LENGTH
        do  10 i = 1, X_LENGTH
            data ( i, j ) = i + j - 1
 10         continue
 20     continue
 
C
C     Open the file and initialize the SD interface.
C
    sd_id = sfstart( FILE_NAME, DFACC_WRITE )
C
C     Attach to the first data set.
C
    sds_index = 0
    sds_id = sfselect(sd_id, sds_index)
C
C     Define the location and size of the data to be written
C     to the data set. Note that setting values of the array stride to 1
C     specifies the contiguous writing of data.
C
    start  ( 1 ) = 0
    start  ( 2 ) = 0
    edges  ( 1 ) = X_LENGTH
    edges  ( 2 ) = Y_LENGTH
    stride ( 1 ) = 1
    stride ( 2 ) = 1
C
C     Write the stored data to the data set named in SDS_NAME.
C     Note that the routine sfwdata is used instead of sfwcdata
C     to write the numeric data.
C
    status = sfwdata ( sds_id, start, stride, edges, data )
C
C     Terminate access to the data set.
C
    status = sfendacc ( sds_id )
C
C     Terminate access to the SD interface and close the file.
C
    status = sfend ( sd_id )
    end

MATLAB

file_name = 'sds.hdf';
sds_name = 'a_sds';
 
% Open an existing hdf file or create it
sd_id = hdfsd( 'start',file_name , 'write');
if sd_id == -1
    sd_id = hdfsd( 'start', file_name, 'DFACC_CREATE');
end
 
 
% Create a dataset with its name, data type, rank and dimensions
data = [0 0 0; 1 1 1; 2 2 2; 3 3 3];
data_type = 'double';
rank = ndims(data);
dimsizes  = fliplr(size(data));
sds_id = hdfsd('create', sd_id, sds_name, data_type, rank, dimsizes);
 
 
% Write data to hdf file
sds_start = zeros(1,  ndims(data));
sds_stride = [] ;
sds_edges = fliplr(size(data));
status = hdfsd('writedata', sds_id, sds_start, sds_stride,  sds_edges, data) ;
 
 
% Close access to the sds
status = hdfsd('endaccess',sds_id);
 
 
% Close access to the hdf file
status = hdfsd('end',sd_id);

HDF5

MATLAB

filename = 'test.h5';
 
% Create the file
fid = H5F.create(filename);
% Create a group
gid = H5G.create(fid, 'group','H5P_DEFAULT');
% Create a dataset
fid = H5F.open(filename);
h5create(filename, '/group/sds1', [2 7],'Datatype', 'double','FillValue',5.5);
 
% Add attributes
h5writeatt(filename, '/group', 'Description', 'this is a group');
h5writeatt(filename, '/', 'Description', 'this is an example');
 
% Write data
h5write(filename,  '/group/sds1', [1 2 1 1 1 1 1 ;3  5 0 0 0 0 0]);
h5disp(filename);
 
% Close access
H5G.close(gid)
H5F.close(fid)

R

#To install the package rhdf5 , you need a current version (>2.15.0) of R
#After installing R you can run the following commands from the R command shell to install the bioconductor package rhdf5.
#source("http://bioconductor.org/biocLite.R")
#biocLite("rhdf5")
 
library(rhdf5)
 
filename="/home/Projets/R/myExample.h5"
groupname = "group"
 
 
# Create an h5 file
h5createFile(filename)
 
# Create a group in the h5 file
h5createGroup(filename, groupname)
 
# Create datasets in the h5 file
h5createDataset(filename, "group/toto", c(5,5))
h5createDataset(filename, "sds", c(4,2), storage.mode="integer")
 
# Write data in dataset named "group/tata" and "sds"
vec <- c(1,2,3,4,5,6) 
h5write(vec, filename, "group/tata")
# Or
fid <- H5Fopen(filename)
sdsid <- H5Dopen(fid, "sds")
H5Dwrite(sdsid, c(1,2,3,4,5,6,7,8))
H5Dclose(sdsid)
H5Fclose(fid)
 
# Create attributes
fid <- H5Fopen(filename)
sdsid <- H5Dopen(fid, "sds")
h5createAttribute(sdsid, "unit", 1,1)
h5createAttribute(fid, "Description", 1, 1,storage.mode="character", size = 22)
 
 
# Write attribute value
aid <- H5Aopen(sdsid, "unit")
H5Awrite(aid, 69)
H5Aclose(aid)
aid <- H5Aopen(fid, "Description")
H5Awrite(aid, "this is an example")
H5Aclose(aid)
 
# Close access
H5Dclose(sdsid)
H5Fclose(fid)