home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / graphics.formats / hdf / NCSA_HDF / HDF.5.ascii.Z / HDF.5.ascii
Encoding:
Text File  |  1991-09-24  |  15.4 KB  |  391 lines

  1. 5.1NCSA HDF Calling Interfaces and Utilities
  2.  
  3. Annotating Data Objects and Files5.1
  4.  
  5. National Center for Supercomputing Applications
  6.  
  7. November 1989
  8.  
  9.  
  10.  
  11.  
  12.  
  13. 5.1NCSA HDF Calling Interfaces and Utilities
  14.  
  15. Annotating Data Objects and Files5.1
  16.  
  17. National Center for Supercomputing Applications
  18.  
  19. November 1989
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. Chapter 5Annotating Data Objects and Files
  30.  
  31.  
  32.  
  33. Chapter Overview
  34. Annotation Tags
  35. The Annotation Interface
  36. Writing Annotations to a File
  37. Reading Annotations from a File
  38. Getting Annotation Information from a File
  39. Listing All Labels for a Given Tag
  40.  
  41. Chapter Overview
  42.  
  43. This chapter describes the routines that are available for storing and retrieving data and file annotations.
  44.  
  45.  
  46. Annotation Tags
  47.  
  48. It is often useful to associate in human readable form information about an HDF file and its data contents, and to keep that information in the same file that contains the data. HDF provides this capability in the form of annotations. An HDF annotation is a sequence of ASCII characters that is associated with one of three types of objects:  (1) the file itself, (2) the individual data elements in the file, or (3) the tags that identify the data elements. The current annotation interface supports only the latter type of annotation.
  49.  
  50. HDF annotations can accommodate a wide variety of types of information, including titles, comments, variable names, parameters, formulas, and source code. Virtually any information that a user might normally put into a notebook concerning the collection, meaning, or use of a file or data can be put into a file's annotations.
  51.  
  52. Annotations are optionally supplied by a creator or user of an HDF file or data object. Annotations come in two forms:  labels,  which normally consist of a short string of characters, and descriptions,  which can be long and complex bodies of text. 
  53.  
  54. Since each data object in an HDF file is uniquely identified by the combination of a tag and reference number, an annotation is associated with a data object by storing the unique identifier (tag and reference number) together with the annotation. 
  55.  
  56. For example, suppose you wish to annotate a scientific dataset by storing the following annotation with it in the file: "Data from black hole experiment 8/18/87." This statement would be stored in an HDF file as an annotation, and it would have stored with it the tag and reference number for the corresponding scientific dataset. The tag that goes with a scientific dataset is DFTAG_SDG. If you assume that the reference number for this particular dataset is 3, then Figure 5.1 illustrates how the annotation would look in the file.
  57.  
  58. Figure 5.1Example of an Annotation
  59.  
  60.  
  61.  
  62. The Annotation Interface
  63.  
  64. The HDF library provides a number of routines for storing and retrieving annotations. These routines are callable from C and FORTRAN programs that have access to the HDF library, version 3.0 and later.
  65.  
  66. In the following explanations, the term label refers to a string that identifies a data element such as an image or floating-point dataset. Labels can contain only printable ASCII characters and are assumed to be strings in the formal sense. A description, on the other hand, can contain more than one string and therefore, can contain NULL characters (zero bytes), which are normally used to delimit strings in C. In those routines that read or write descriptions, it is always necessary to specify explicitly the lengths of the descriptions.
  67.  
  68. All of the callable annotations routines begin with the letters DFAN. Since there are some FORTRAN compilers that only accept identifiers with eight or fewer characters, an alternate set of short names has been devised that can be used when programming with one of these compilers. Table 5.1 contains the normal names of the annotation routines, as well as the shorter names. Both sets of names are supported on all HDF-supported machines.(Refer to Appendix E, ╥Rouitne Lists,╙ for a complete listing of HDF routines.)
  69.  
  70. Table 5.1Long and Short Names for Annotation Routines
  71. Long NameShort NamePurpose
  72.  
  73. DFANgetlablendagllengets length of label of tag/ref.
  74.  
  75. DFANgetlabeldaglabgets label of tag/ref.
  76.  
  77. DFANgetdesclendagdlen gets length of description of tag/ref.
  78.  
  79. DFANgetdescdagdescgets description of tag/ref.
  80.  
  81. DFANputlabeldaplabputs label of tag/ref.
  82.  
  83. DFANputdescdapdescputs description of tag/ref.
  84.  
  85. DFANlastref*returns ref of last annotation 
  86. read or written.
  87.  
  88. DFANlablistdallistgets list of labels for a 
  89. particular tag.
  90.  
  91.  
  92. Writing Annotations to an HDF File
  93.  
  94. DFANputlabel
  95. FORTRAN:
  96. INTEGER FUNCTION DFANputlabel(filename, tag, ref, label)
  97.  
  98. CHARACTER*(*) filename-name of HDF file to put label in
  99. CHARACTER*(*) label-space to return label in
  100. INTEGER tag, ref-tag/ref of item whose label we want
  101. to store
  102.  
  103. C:
  104. int DFANputlabel(filename, tag, ref, label)
  105.  
  106. char *filename;/* name of HDF file to put label in */
  107. uint16 tag, ref;/* tag/ref of item whose label we want to 
  108. store*/
  109. char *label;/* space to return label in */
  110.  
  111.  
  112. Purpose:  To write out a label for the data object with the given tag/ref.
  113.  
  114. Return value:  0 on success; ╨1 on failure.
  115.  
  116.  
  117. DFANputdesc
  118. FORTRAN:
  119. INTEGER FUNCTION DFANputdesc(filename, tag, ref, desc, desclen)
  120.  
  121. CHARACTER*(*) filename-name of HDF file to put descr in
  122. CHARACTER*(*) desc-description to write to the file
  123. INTEGER tag, ref-tag/ref of item whose description
  124. we want to store
  125. INTEGER desclen-length of description
  126.  
  127. C:
  128. int DFANputdesc(filename, tag, ref, desc, desclen)
  129.  
  130. char *filename;/* name of HDF file descr stored in */
  131. uint16 tag, ref;/* tag/ref of item whose descr we want to 
  132. store */
  133. char *desc;/* description to write to file */
  134. int32 desclen;/* length of description */
  135.  
  136.  
  137. Purpose:  To write out a description for the data object with the given tag/ref.
  138.  
  139. Return value:  0 on success; ╨1 on failure.
  140.  
  141. The parameter desclen gives the amount of space that is available for storing the annotation.
  142.  
  143.  
  144. Example:  Adding Annotations to a Scientific Dataset
  145. The example in Figure 5.2 illustrates the use of DFANputlabel and DFANputdesc to write to an HDF file a label and description for a scientific dataset. It is not necessary to write both a label and a description. One or the other or both may be written, depending on your needs.
  146.  
  147. Figure 5.2Adding Annotations to a Scientific Dataset
  148. C:
  149. #include "df.h"
  150. ...
  151. int i, rank, dimsizes[2];
  152. char s[50];
  153. float *data;
  154. ...
  155. DFSDadddata("myfile",rank,dimsizes,data);
  156. ...
  157. sprintf(s,"Data from black hole experiment\n8/18/87");
  158. i = DFSDlastref();
  159. DFANputlabel("myfile", DFTAG_SDG, i, "black hole");
  160. DFANputdesc("myfile", DFTAG_SDG, i, s, strlen(s));
  161.  
  162.  
  163.  
  164. Remarks:
  165.  
  166. ÑDFTAG_SDG is the tag that goes with a scientific data group. A scientific data group in an HDF file is a group of tag/refs that describe a scientific dataset.
  167.  
  168. ÑThe call to DFSDlastref (see the section in this chapter, ╥Getting Annotation Information from a File╙) returns the value of the last reference number written to the file. This call is needed to complete the tag/ref combination that uniquely identifies the desired scientific data group that is being annotated.
  169.  
  170. ÑThe file df.h that is included with the source contains the number that corresponds to the tag for a scientific dataset. Tags and their numbers are listed in Appendix A, ╥NCSA HDF Tags.╙
  171.  
  172.  
  173. Reading Annotations from an HDF File
  174.  
  175. DFANgetlablen
  176. FORTRAN:
  177. INTEGER FUNCTION DFANgetlablen(filename, tag, ref)
  178.  
  179. CHARACTER*(*) filename-HDF file with label is stored in
  180. INTEGER tag, ref,-tag/ref of item whose label we want
  181.  
  182. C:
  183. int32 DFANgetlablen(filename, tag, ref)
  184.  
  185. char *filename;/* name of HDF file label is stored in */
  186. uint16 tag, ref;/* tag/ref of item whose label we want */
  187.  
  188.  
  189. Purpose:  To get the length of a label of the data object with the given tag and reference number. This routine allows you to insure that there is enough space allocated for a label before actually loading it.
  190.  
  191. Return value:  The length of label on success; ╨1 on failure.
  192.  
  193.  
  194. DFANgetlabel
  195. FORTRAN:
  196. INTEGER FUNCTION DFANgetlabel(filename, tag, ref, label, maxlen)
  197.  
  198. CHARACTER*(*) filename,-name of HDF file label is stored in
  199. CHARACTER*(*) label-space to return label in
  200. INTEGER tag, ref-tag/ref of item whose label we want
  201. INTEGER maxlen-size of space to return label in
  202.  
  203. C:
  204. int DFANgetlabel(filename, tag, ref, label, maxlen)
  205.  
  206. char *filename;/*  name of HDF file label is stored in */
  207. uint16 tag, ref;/* tag/ref of item whose label we want */
  208. char *label;/* space to return label in */
  209. int32 maxlen;/* size of space to return label in */
  210.  
  211.  
  212. Purpose:  To read in the label of the data object with the given tag and reference number.
  213.  
  214. Return value:  0 on success; ╨1 on failure.
  215.  
  216. The parameter maxlen gives the amount of space that is available for storing the label. The length of maxlen must be at least one greater than the anticipated length of the label, because a NULL byte is appended to the annotation.
  217.  
  218.  
  219. DFANgetdesclen
  220. FORTRAN:
  221. INTEGER FUNCTION DFANgetdesclen(filename, tag, ref)
  222.  
  223. CHARACTER*(*) filename-name of HDF file descr is stored in 
  224. INTEGER tag, ref-tag/ref of item whose descr we want
  225.  
  226. C:
  227. int32 DFANgetdesclen(filename, tag, ref)
  228.  
  229. char *filename;/* name of HDF file descr is stored in */
  230. uint16 tag, ref;/*tag/ref of item whose descr we want */
  231.  
  232.  
  233. Purpose:  To get the length of a description gf the data object with the given tag and reference number. This routine allows you do insure that there is enough space allocated for a description before actually loading it.
  234.  
  235. Return value:  The length of description on success; ╨1 on failure.
  236.  
  237. DFANgetdesc
  238. FORTRAN:
  239. INTEGER FUNCTION DFANgetdesc(filename, tag, ref, desc, maxlen)
  240.  
  241. CHARACTER*(*) filename-name of HDF file descr is stored in
  242. CHARACTER*(*) desc-space to return description in 
  243. INTEGER tag, ref-tag/ref of item whose descr we want
  244. INTEGER maxlen-size of space to return descr in
  245.  
  246. C:
  247. int DFANgetdesc(filename, tag, ref, desc, maxlen)
  248.  
  249. char *filename;/* name of HDF file descr is stored in */
  250. uint16 tag, ref;/*tag/ref of item whose descr we want */
  251. char *desc;/*space to return description in */
  252. int32 maxlen;/*size of space to return descr in */
  253.  
  254.  
  255. Purpose:  To read in the description of the data object with the given tag and reference number.
  256.  
  257. Return value:  0 on success; ╨1 on failure.
  258.  
  259. The parameter maxlen gives the amount of space that is available for storing the description. The length of maxlen must be at least one greater than the anticipated length of the description, because a NULL byte is appended to the annotation.
  260.  
  261.  
  262. Example:  Reading a Label and Description
  263. This example illustrates the use of DFANgetlabel, DFANgetdesclen, and DFANgetdesc to read from an HDF file a label and description for a scientific dataset.
  264.  
  265. Figure  5.3Getting Annotations 
  266. from a Scientific 
  267. Dataset
  268. C:
  269. #include "df.h"
  270. extern uint16 DFfindnextref();
  271.  
  272. main()
  273. {
  274.     int    desclen;
  275.     uint16 ref;
  276.     char   label[20], *s;
  277.     DF     *dfile;       /* HDF file pointer */
  278.  
  279.     dfile = DFopen("myfile", DFACC_READ, -1);
  280.     ref = DFfindnextref(dfile, (unsigned short) DFTAG_SDG, 0);
  281.     if (ref < 0) {
  282.         printf("Unable to find scientific dataset.\n");
  283.         exit(1);
  284.     }
  285.     DFclose(dfile);
  286.  
  287.     DFANgetlabel("myfile", (uint16) DFTAG_SDG, ref, label, 11);
  288. Figure 5.3Getting Annotations froma Scientific Dataset (Continued)
  289.     printf("Label: %s\n", label);
  290.     desclen = DFANgetdesclen("myfile",(uint16) DFTAG_SDG, ref);
  291.     s = malloc( desclen+1);
  292.     DFANgetdesc("myfile", (uint16) DFTAG_SDG, ref, s, desclen);
  293.     printf("Description: %s\n", s);
  294. }
  295.  
  296.  
  297. Remarks:
  298.  
  299. ÑLower level routines DFopen, DFclose, and DFfindnextref are used here to find the ref number of the first occurrance of the SDG tag.
  300.  
  301. ÑIn the above example, DFANgetlabel assumes that  the label is not more than 10 bytes long. If the program needs to know the length of a label, it can call DFANgetlablen to find this out.
  302.  
  303. ÑSince the description could be very long, the routine DFANgetdesclen is called to find the space requirements for the description. This space is allocated before calling DFANgetdesc to get the description.
  304.  
  305.  
  306. Getting Annotation Information from a File
  307.  
  308. DFANlastref
  309.  
  310. Figure 5.4Getting Most Recent Reference Number Written
  311. C:
  312. int DFANlastref()
  313.  
  314.  
  315. Purpose: Return most recent reference number written or read.
  316.  
  317. Returnvalue:  The reference number on success; ╨1 on error.
  318.  
  319. This routine is useful when your program reads or writes a data object, then needs to know the corresponding reference number in order to read or write a corresponding label or description. Figure 5.4 illustrates the use of this routine.
  320.  
  321.  
  322. Listing All Labels for a Given Tag
  323.  
  324. DFANlablist
  325. FORTRAN:
  326. INTEGER FUNCTION DFANlablist(filename, tag, reflist, labellist,
  327. listsize, maxlen, startpos)
  328.  
  329. CHARACTER*(*) filename-name of HDF file labels stored in
  330. CHARACTER*(*) labellist-array of strings to place labels in
  331. INTEGER tag-tag to find labels for
  332. INTEGER reflist(*)-array to place refs in
  333. INTEGER listsize-size of ref and label lists
  334. INTEGER maxlen-maximum length allowed for label
  335. INTEGER startpos-beginning from the startpos'th 
  336. entry, up to listsize entries will
  337. be returned.
  338.  
  339. C:
  340. int DFANlablist(filename, tag, reflist, labellist, listsize, maxlen, startpos)
  341.  
  342. char *filename;/* name of HDF file labels stored in */
  343. uint16 tag; /* tag to find labels for */
  344. unit16 reflist[];/* array to place refs in */
  345. char *labellist;/* array of strings to place labels in */
  346. int listsize;/* size of ref and label lists */
  347. int maxlen;/* maximum length allowed for label */
  348. int startpos;/* beginning from the startpos'th entry,
  349. up to listsize entries will be
  350. returned.*/
  351.  
  352.  
  353. Purpose:  To return a list of all reference numbers and labels for a given tag. 
  354.  
  355. Return value:  The number of entries on success; ╨1 on error.
  356.  
  357.  
  358. Input parameters are filename, tag, listsize, maxlen and startpos. Listsize gives the number of available entries in the ref and label lists, maxlen is the maximum length allowed for a label, and startpos tells which label to start reading for the given tag. Beginning from the startpos entry and extending to the listsize parameter, entries will be returned. (If  startpos is 1, for instance, all labels will be read; if startpos=4, all but the first 3 labels will be read.)
  359.  
  360. Taken together, the reflist and labellist returned by DFANlablist constitute a directory of all labels for a given tag in a file. The list, labellist, can be displayed to show all of the labels for a given tag. Or, it can be searched to find the ref of a data object with a certain label. Once the ref for a given label is found, the corresponding data object can be accessed by invoking the routine DFgetelement. This routine provides you with a mechanism for direct access to data objects in HDF files.
  361.  
  362.  
  363. Example:  Getting a List of Labels for Images in a File
  364. The example in Figure 5.5 illustrates the use of DFANlablist to get a list of all labels used for scientific datasets in an HDF file.
  365.  
  366. Figure 5.5Getting a List of Labels from a File
  367. C:
  368. #include "df.h"
  369. ...
  370. int i, nlabels;
  371. uint16 reflist[20];
  372. char labellist[320];
  373. ...
  374. printf("\nLabels of scientific datasets in myfile:\n");
  375. nlabels = DFANlablist(argv[1],DFTAG_SDG, reflist, 
  376.                                   labellist, 20, 15, 1);
  377. for (i=0; i<nlabels; i++)
  378.     printf("\tRef number: %d\tLabel: %s\n",
  379.                            reflist[i], labellist+(i*15));
  380. }
  381.  
  382.  
  383. Remarks:
  384.  
  385. ÑIn the call to DFANlablist the arrays, reflist and labellist, have to be preallocated with enough space to hold the reference numbers and labels. 
  386.  
  387. ÑThe '20' in the parameter list tells the routine to read, at most, 20 labels and reference numbers. The '15' indicates that each label is to be stored in a 15-byte sequence in the array labellist. The '1' tells the routine to start with the first entry.
  388.  
  389. * DFANlastref is callable only by C routines.  There is no equivalent FORTRAN routine in the HDF library.
  390.  
  391.