How to programmatically access HDF-4 SD dataset index values and names
ENVI can be used to open a variety of HDF-4 datasets such as ASTER, MODIS, Hyperion and others. ENVI's custom file readers are designed to open the most commonly used datasets that typically include only the spectral bands. However, HDF-4 files can contain many more datasets that users may wish to access or their particular dataset may not have a custom file reader in ENVI. In this case a user could open the file as Generic HDF and select the dataset they wish to open.
In order to perform this same task programmatically, you would use ENVI_OPEN_DATA_FILE with the HDFSD_DATASET and HDFSD_INTERLEAVE keywords. HDFSD_DATASET requires the dataset index of the HDF file which may not be known. One way to access this information programmatically from the HDF file is to use IDL's HDF_SD routines. The below function is an example of how to use these routines to access the SD index and dataset names from the file which can then be used to determine which datasets you wish to open using ENVI_OPEN_DATA_FILE.
The following function can be used access HDF-4 files (such as ASTER or MODIS) to find the dataset names contained in the HDF file and the associated index number. The index number can be used to open select HDF datasets in ENVI using ENVI_OPEN_DATA_FILE with the HDFSD_DATASET keyword.
=================================
function hdf_sd_getnameandindex, hdf_file
compile_opt idl2
;check to see if the HDF-4 file is valid
if hdf_file eq !null || ~hdf_ishdf(hdf_file) then begin
message, 'Not an HDF4 file. Returning.', /info
return, 0
endif
;initialize HDF SD interface
hdf_id = hdf_sd_start(hdf_file, /read)
if ~hdf_id then return, 0
;retrieve HDF datasets and attributes
hdf_sd_fileinfo, hdf_id, n_sds, n_attr
var_index = lonarr(n_sds)
var_name = strarr(n_sds)
;loop through datasets to get index values and names of datasets
for i = 0, n_sds-1 do begin
var_id = hdf_sd_select(hdf_id, i)
hdf_sd_getinfo, var_id, name=vn
var_index[i] = hdf_sd_nametoindex(hdf_id, vn)
var_name[i] = vn
endfor
hdf_sd_end, hdf_id
sd_info = {index:var_index, name:var_name}
return, sd_info
end
======================================
Call the routine at the command line using:
ENVI> result = hdf_sd_getnameandindex(hdf_file)
The 'result' will show the SD indicies and names of the datasets so that they can be searched and used to open the desired HDF-4 dataset using the ENVI routines.
Review on 12/31/2013 MM