[Top] [Prev] [Next] [Bottom]
10.8 Determining Reference Numbers
It is advisable to check the reference number before attempting to assign an object annotation, as the overwriting of reference numbers isn't prevented by the HDF library routines.
There are three ways to check a reference number for an object:
10.8.1 Determine a Reference Number for the Last Object Accessed: DF*lastref
There are two methods of obtaining a reference number through the use of a DF*lastref call. The first approach is to obtain and store the reference number of an object immediately after the object is created:
- 1. Create the data object.
- 2. Call DF*lastref to determine its reference number.
- 3. Read or write an object annotation.
The second approach is to determine the reference number at some time after the data object is created. This approach requires repeated DF*read calls until the appropriate object is accessed, followed by a call to DF*lastref:
- 1. Read the appropriate data object.
- 2. Call DF*lastref to determine its reference number.
- 3. Read or write and object annotation.
Most HDF interfaces provide one routine that assigns a specified reference number to a data object and another routine that returns the reference number for the last data object accessed. (See Table 10H.) However, the SD interface doesn't. Also, the DFAN annotation doesn't include a DF*lastref routine.
Although DF*writeref calls are designed to assign specific reference numbers, they are not recommended for general use because there is no protection against reassigning an existing reference number and overwriting data. In general, it is better to determine a reference number for a data object by calling DF*lastref immediately after reading or writing a data object.
The DF*lastref routines have no parameters. The DF*writeref routines have two: filename, which is the name of the file that contains the data object, and ref, which is the reference number for the next data object read operation.
TABLE 10H List and Descriptions of the DF*writeref and DF*lastref Routines
10.8.2 Querying a List of Reference Numbers for a Given Tag: DFANlablist
Given a tag and two buffers, DFANlablist will fill one buffer with all reference numbers for the given tag and the other with all labels assigned to the given tag. The programming model for determining a list of reference numbers is as follows:
- 1. Determine the number of reference numbers that exist for a given tag.
- 2. Allocate a buffer to store the reference numbers.
- 3. Specify the maximum label length.
- 4. Allocate a buffer to store the labels.
- 5. Store the list of reference numbers and their labels.
To create a list of reference numbers and their labels for a given tag, the following routines should be called:
C: num_refs = Hnumber(file_id, tag);
ref_buf = HDmalloc(sizeof(uint16*)*num_refs);
max_lab_len = 16;
label_buf = HDmalloc(max_lab_len * num_refs);
start_pos = 0;
num_of_refs = DFANlablist(filename, tag, ref_buf, label_buf,
num_refs, max_lab_len,
start_pos);
FORTRAN: num_refs = hnumber(file_id, tag)
max_lab_len = 16
start_pos = 0
num_of_refs = dallist(filename, tag, ref_buf, label_buf,
num_refs, max_lab_len, start_pos)
Hnumber determines how many objects with the specified tag are in a file. It is described in Chapter 2, titled HDF Fundamentals.
DFANlablist has seven parameters: filename, tag, ref_list, label_buf, num_refs, max_lab_len, and start_pos. The filename parameter specifies the name of the file to search and tag specifies the search tag to use when creating the reference and label list. The ref_buf and label_buf parameters are buffers used to store the reference numbers and labels associated with tag. The num_ref parameter specifies the length of the reference number list and the max_lab_len parameter specifies the maximum length of a label. The start_pos parameter specifies the first label to read. For instance, if start_pos has a value of 1 all labels will be read - if it has a value of 4, all but the first three labels will be read.
Taken together, the ref_list and label_list constitute a directory of all objects and their labels for a given tag. The contents of label_list can be displayed to show all of the labels for a given tag or it can be searched to find the reference number of a data object with a certain label. Once the reference number for a given label is found, the corresponding data object can be accessed by invoking other HDF routines. Therefore, this routine provides a mechanism for direct access to data objects in HDF files.
TABLE 10I DFANlablist Parameter List
EXAMPLE 7. Getting a List of Labels for All Scientific Data Sets
These examples illustrate the method used to get a list of all labels used in scientific data sets in an HDF file using DFANlablist. The DFS_MAXLEN definition is located in the "hlimits.h" include file.
C:
#include "hdf.h"
#define LISTSIZE 20
main( )
{
int i, num_of_labels, start_position = 1, list_length = 10;
uint16 ref_list[LISTSIZE];
char label_list[DFS_MAXLEN*LISTSIZE-1];
/* Get the total number of labels in the "Example1.hdf" file. */
num_of_labels = DFANlablist("Example1.hdf", DFTAG_NDG, ref_list, \
label_list, list_length, DFS_MAXLEN, \
start_position);
/*
* Print the reference numbers and label names for each label
* in the list.
*/
for (i = 0; i < num_of_labels; i++)
printf("\n\t%d\tRef number: %d\tLabel: %s", i+1, ref_list[i], \
label_list - (i * 13));
printf("\n");
}
FORTRAN:
PROGRAM GET LABEL LIST
integer dallist
integer*4 DFTAG_NDG, LISTSIZE, DFS_MAXLEN
parameter (DFTAG_NDG = 720,
+ LISTSIZE = 20,
+ DFS_MAXLEN = 255)
character*60 label_list(DFS_MAXLEN*LISTSIZE)
integer i, num_of_labels, start_position, ref_list(DFS_MAXLEN)
start_position = 1
num_of_labels = dallist('Example1.hdf', DFTAG_NDG, ref_list,
+ label_list, 10, DFS_MAXLEN,
+ start_position)
do 10 i = 1, num_of_labels
print *,' Ref number: ',ref_list(i),
+ ' Label: ',label_list(i)
10 continue
end
10.8.3 Locate an Object by Its Tag and Reference Number: Hfind
Instead of using DFANlablist to create a list of reference numbers to search, HDF provides a general search routine called Hfind. Hfind is described in Chapter 2, titled HDF Fundamentals.
[Top] [Prev] [Next] [Bottom]
hdfhelp@ncsa.uiuc.edu
HDF User's Guide - 06/04/97, NCSA HDF
Development Group.