The HDF Group

HDF User’s Guide

Version 4.2r4


[Top] [Prev][Next]


an_ex1.c
#include "hdf.h" 
main( ) 
{ 
	int32 file_id; 
	intn status; 
	static char file_label[] = "This is a file label."; 
	static char file_desc[] = "This is a file description."; 
	/* Open the HDF file to write the annotations. */ 
	file_id = Hopen("Example1.hdf", DFACC_CREATE, 0); 
	/* Write the label to the file. */ 
	status = DFANaddfid(file_id, file_label); 
	/* Write the description to the file. */ 
	status = DFANaddfds(file_id, file_desc, strlen(file_desc)); 
	/* Close the file. */ 
	status = Hclose(file_id); 
} 
an_ex1.f
      PROGRAM CREATE ANNOTATION 
      character*50 file_label, file_desc 
      integer daafid, daafds, status, file_id, hopen, hclose 
      integer*4 DFACC_CREATE 
      parameter (DFACC_CREATE = 4) 
      file_label = "This is a file label." 
      file_desc = "This is a file description." 
C     Open the HDF file to write the annotations. 
      file_id = hopen(`Example1.hdf', DFACC_CREATE, 0) 
C     Write the label to the file. 
      status = daafid(file_id, file_label) 
C     Write the description to the file. 
      status = daafds(file_id, file_desc, 26) 
C     Close the file. 
      status = hclose(file_id) 
      end 
an_ex2.c
#include "hdf.h" 
#define X_LENGTH 3 
#define Y_LENGTH 2 
#define Z_LENGTH 5 
main( ) 
{ 
	/* Create the data array. */ 
	static float32 sds_data[X_LENGTH][Y_LENGTH][Z_LENGTH] = 
	{  1,  2,  3,  4,  5, 
	   6,  7,  8,  9, 10, 
	  11, 12, 13, 14, 15, 
	  16, 17, 18, 19, 20, 
	  21, 22, 23, 24, 25, 
	  26, 27, 28, 29, 30 }; 
	/*  
	* Create the array that will hold the dimensions of  
	* the data array. 
	*/ 
	int32 dims[3] = {X_LENGTH, Y_LENGTH, Z_LENGTH}; 
	intn refnum, status; 
	static char object_desc[] = "This is an object description."; 
	static char object_label[] = "This is an object label."; 
	/* Write the data to the HDF file. */ 
	status = DFSDadddata("Example1.hdf", 3, dims, (VOIDP)sds_data); 
	/* Get the reference number for the newly written data set. */ 
	refnum = DFSDlastref( ); 
	/* Assign the object label to the scientific data set. */ 
	status = DFANputlabel("Example1.hdf", DFTAG_NDG, refnum, \ 
					object_label);  
	/* Assign the object description to the scientific data set. */ 
	status = DFANputdesc("Example1.hdf", DFTAG_NDG, refnum, \ 
					object_desc, strlen(object_desc)); 
} 
an_ex2.f
      PROGRAM ANNOTATE OBJECT 
      integer dsadata, dims(3), status, refnum 
      integer daplab, dapdesc, dslref 
      integer*4 DFTAG_NDG, X_LENGTH, Y_LENGTH, Z_LENGTH 
      parameter(DFTAG_NDG = 720, 
     +          X_LENGTH = 5, 
     +          Y_LENGTH = 2, 
     +          Z_LENGTH = 3) 
C     Create the data array. 
      real*4 sds_data(X_LENGTH, Y_LENGTH, Z_LENGTH) 
      data sds_data /                             
     +          1,  2,  3,  4,  5,   
     +          6,  7,  8,  9, 10, 
     +         11, 12, 13, 14, 15, 
     +         16, 17, 18, 19, 20, 
     +         21, 22, 23, 24, 25, 
     +         26, 27, 28, 29, 30  / 
C     Create the array the will hold the dimensions of the data array. 
      data dims /X_LENGTH, Y_LENGTH, Z_LENGTH/ 
C     Write the data to the HDF file. 
      ref = dsadata(`Example1.hdf', 3, dims, sds_data) 
C     Get the reference number for the newly written data set.  
      refnum = dslref( ) 
C     Assign the object label to the scientific data set. 
      status = daplab(`Example1.hdf', DFTAG_NDG, refnum,  
     +             `This is an object label.') 
C     Assign an object description to the scientific data set. 
      status = dapdesc(`Example1.hdf', DFTAG_NDG, refnum,  
     +              `This is an object description.', 30) 
      end 
an_ex3.c
#include "hdf.h" 
main( ) 
{ 
	int32 file_id, file_label_len; 
	char *file_label; 
	intn status; 
	/* Open the HDF file containing the annotation. */ 
	file_id = Hopen("Example1.hdf", DFACC_READ, 0); 
       
	/* Determine the length of the file label. */ 
	file_label_len = DFANgetfidlen(file_id, 1); 
     
	/* Allocated memory for the file label buffer. */ 
	file_label = HDgetspace(file_label_len); 
	/* Read the file label. */ 
	file_label_len = DFANgetfid(file_id, file_label, file_label_len, 1); 
	/* Close the file */ 
	status = Hclose(file_id); 
} 
an_ex3.f
      PROGRAM GET ANNOTATION 
       
      integer status, file_id, label_length 
      integer hopen, hclose, dagfidl, dagfid  
      character file_label(50) 
      integer*4 DFACC_READ 
      parameter(DFACC_READ = 1) 
C     Open the HDF file containing the file label. 
      file_id = hopen("Example1.hdf", DFACC_READ, 0) 
C     Determine the length of the file label. 
      label_length = dagfidl(file_id, 1) 
C     Read the file label. 
      status = dagfid(file_id, file_label, label_length, 1) 
C     Close the HDF file. 
      status = hclose(file_id) 
      end 
an_ex4.c
#include "hdf.h" 
main( ) 
{ 
	intn desc_length = -1, status; 
	char desc[50]; 
	int32 file_id; 
	uint16 tag = 0, ref = 0; 
	uint32 find_offset, find_length; 
	/* Open the file and initialize the searching parameters to 0. */ 
	file_id = Hopen("Example1.hdf", DFACC_READ, 0); 
	/* 
	* Start a sequential forward search for the first reference 
	* number assigned to a scientific data set. 
	*/ 
	while (Hfind(file_id, DFTAG_NDG, DFREF_WILDCARD, &tag, &ref, \ 
		&find_offset, &find_length, DF_FORWARD) != FAIL) { 
	/* 
	* After discovering a valid reference number, check for an 
	* object description by returning the length of the description. 
	* If the inquiry fails, continue searching for the next valid 
	* reference number assigned to a scientific data set. 
	*/ 
	if ((desc_length = DFANgetdesclen("Example1.hdf", tag, ref)) \ 
		== FAIL) 
		break; 
	/*  
	* If a description exists and it will fit in the description buffer, 
	* print it. 
	*/ 
	if (desc_length != FAIL && desc_length <= 50) { 
		status = DFANgetdesc("Example1.hdf", tag, ref, desc, desc_length); 
		printf("Description: %s\n", desc); 
	} 
	} 
	/* Close the file. */ 
	status = Hclose(file_id); 
} 
an_ex5.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"); 
} 
an_ex5.f
      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 

HDF4.2r4 - February 2009
Copyright
The HDF Group
www.hdfgroup.org
The HDF Group