Previous Scientific Data Formats: Hierarchical Data Format - HDF5 Next

Example: Reading Partial Datasets

To read a portion of a compound dataset or attribute, create a datatype that matches only the elements you wish to retrieve, and specify that datatype as the second argument to the H5D_READ function. The following example creates a simple HDF5 data file in the current directory, then opens the file and reads a portion of the data.

; Create sample data in an array of structures with two fields  
struct = {time:0.0, data:intarr(40)}  
r = REPLICATE(struct,20)  
r.time = RANDOMU(seed,20)*1000  
r.data = INDGEN(40,20)  
  
; Create a file  
file = 'h5_test.h5'  
fid = H5F_CREATE(file)  
; Create a datatype based on a single element of the arrary  
dt = H5T_IDL_CREATE(struct)  
; Create a 20 element dataspace  
ds = H5S_CREATE_SIMPLE(N_ELEMENTS(r))  
; Create and write the dataset  
d = H5D_CREATE(fid, 'dataset', dt, ds)  
H5D_WRITE, d, r  
; Close the file  
H5F_CLOSE, fid  
  
; Open the file for reading  
fid = H5F_OPEN(file)  
; Open the dataset  
d = H5D_OPEN(fid, 'dataset')  
; Define the data we want to read from the dataset  
struct = {data:intarr(40)}  
; Create datatype denoting the portion to be read  
dt = H5T_IDL_CREATE(struct)  
; Read only the data that matches our datatype. The  
; returned value will be a 20 element structure with only   
; one tag, 'DATA'. Each element of which will be a [40]   
; element integer array  
result = H5D_READ(d, dt)  
H5F_CLOSE, fid  

  IDL Online Help (March 06, 2007)