This page follows the HDF4 library’s own headers in
hdf/src:
the file layout described in hfile_priv.h, the tags in
htags.h, the number types in hntdefs.h and the encoding
macros in hdf_priv.h, all read 2026-11-12. Numbers are big-endian (the
library’s encode macros write the high byte first).
Layout
| Item | Size | Fields |
|---|---|---|
| Magic | 4 | 0E 03 13 01 (^N^C^S^A). |
| DD block header | 6 | Number of descriptors in the block (2), offset of the next DD block (4, 0 for the last). |
| Data descriptor | 12 | Tag (2), reference number (2), offset of the data (4), length (4). |
The first DD block follows the magic; the library’s default is 16 descriptors per
block. A tag and reference number together name one object; tag 1
(DFTAG_NULL) marks an unused descriptor. Offsets and lengths are
unsigned 32-bit numbers. Tags 1 to 16,383 are the library’s ordinary tags, 16,384 to
32,767 its special tags, 32,768 to 64,999 user tags, and 65,000 and up reserved.
Number types
| Code | Type | Code | Type |
|---|---|---|---|
| 3 | DFNT_UCHAR8 | 22 | DFNT_INT16 |
| 4 | DFNT_CHAR8 | 23 | DFNT_UINT16 |
| 5 | DFNT_FLOAT32 | 24 | DFNT_INT32 |
| 6 | DFNT_FLOAT64 | 25 | DFNT_UINT32 |
| 20 | DFNT_INT8 | 26 | DFNT_INT64 |
| 21 | DFNT_UINT8 | 27 | DFNT_UINT64 |
The base values are the standard HDF format (DFNT_HDF, 0). Adding
0x1000 means the machine’s native format, 0x2000 a custom
format and 0x4000 little-endian.
Special elements
When a data element is stored in an unusual way (linked blocks, an external file,
compression, chunking), its descriptor’s tag has 0x4000 added and its
data starts with a 2-byte special type: 1 linked blocks, 2 external file, 3
compressed, 4 variable-length linked blocks, 5 chunked (and, inside the library,
6 buffered and 7 compressed raster). The rest describes where and how the real
data is stored.
Data layout of each tag
From the HDF Specification and Developer's Guide (SpecDevG.pdf, HDF 4.1r5, chapters 9 and 10, read 2026-11-13). Numbers are big-endian. A tag's data element is what its descriptor points to; “ref” is the 16-bit reference number in the descriptor. A basic tag is 1 to 16,383 (0x3FFF), extended tags 16,384 to 32,767, user tags 32,768 to 64,999, and 65,000 to 65,535 are reserved for expansion of the format; zero is not used. The spec was written for 4.1r3 and its numbers are those of the library headers except where noted.
| Tag | Data element |
|---|---|
| Utility | |
1 DFTAG_NULL | No data: empty descriptor; its offset and length must be 0 and its ref is always 0. |
30 DFTAG_VERSION | Major version (4), minor version (4), release (4), then a non-null-terminated ASCII string of any length (12 bytes plus the string): the library version that last wrote the file. (htags.h in the library calls the string 80 bytes.) |
106 DFTAG_NT | 4 bytes: version (1; currently 1), type (1: unsigned integer, signed integer, unsigned character, character, float, double), width (1: number of bits, all significant), class (1: a generic value read by type). Classes for floats: DFNTF_NONE 0, IEEE 1, VAX 2, CRAY 3, PC 4, CONVEX 5; for integers: DFNTI_MBO 1 (Motorola byte order), IBO 2 (Intel), VBO 4 (VAX); for characters: DFNTC_BYTE 0, ASCII 1, EBCDIC 2. Other tags should hold a reference to an NT instead of their own number type. |
107 DFTAG_MT | No data element: the 16-bit reference number itself is the code, four groups of 4 bits for double, float, integer and character encoding: double IEEE64, VAX64, CRAY128; float IEEE32, VAX32, CRAY64; integer VAX32, Intel16, Intel32, Motorola32, CRAY64; character ASCII, EBCDIC. It sets the default for values not constrained by an NT; bit-defined headers are not overridden. Without one, data is assumed to be of the local machine. |
| Annotation | |
100 DFTAG_FID, 101 DFTAG_FD | File identifier (a title) and file description (text): non-null-terminated ASCII of any length. |
102 DFTAG_TID, 103 DFTAG_TD | Tag identifier and tag description: the descriptor's ref slot holds the tag number being described, the data is a string or text. Meant for user-defined tags. |
104 DFTAG_DIL, 105 DFTAG_DIA | Data identifier label and annotation: the tag (2) and ref (2) of the object described, then a non-null-terminated string (label) or text (annotation). |
| Compression | |
11 DFTAG_RLE | No data; marks run-length encoding in a DFTAG_ID compression field. Each run starts with a count byte: the low 7 bits are n; a high bit of 1 means repeat the next byte n times, 0 means copy the next n bytes. |
12 DFTAG_IMC | No data; IMCOMP, a 4:1 area-averaging colour method that counts colour frequencies in 4×4 squares. |
13 DFTAG_JPEG, 14 DFTAG_GREYJPEG | Flags (the same ref as the DFTAG_CI they describe) that the compressed image is a 24-bit or 8-bit JPEG; parameters of any size. |
303 DFTAG_CI | A compressed raster image stream. For JPEG it holds the JFIF header up to the start-of-frame, and the rest of the image. |
| Raster images | |
306 DFTAG_RIG | A list of (tag, ref) pairs, 4 bytes each, of the objects needed to show an image: ID, RI, XYP, LD, LUT, MD, MA, CCN, CFM, AR. A reader uses those it understands. |
300 DFTAG_ID, 307 DFTAG_LD, 308 DFTAG_MD | 20 bytes, one layout: x dimension (4), y dimension (4), number type NT ref (2: a tag/ref of a DFTAG_NT), elements per entry (2), interlace (2: 0 components of a pixel together, 1 grouped by scan line, 2 by plane), compression tag (2) and its ref (2; tag 0 for none). They describe the image RI, palette LUT and matte MA respectively. |
302 DFTAG_RI | x × y × elements × NT size bytes, row-major, read per the interlace field. |
301 DFTAG_LUT | The palette; same size formula with the dimensions of its LD. A usual RGB palette is 256 × 1 with 3 elements, either RGB RGB … (interlace 0) or 256 reds, 256 greens, 256 blues (interlace 1). |
309 DFTAG_MA | Matte (transparency): 8-bit values from 0 (transparent) to 255 (opaque), linear by convention. |
310 DFTAG_CCN | 52 bytes: gamma, then x, y, z factors of the red, green, blue and white primaries, thirteen 32-bit IEEE floats. |
311 DFTAG_CFM | A string of upper-case characters: VALUE (a pseudo-colour value), RGB, XYZ, HSV, HSI, SPECTRAL. |
312 DFTAG_AR | 4 bytes: aspect ratio, width over height, a 32-bit IEEE float (1.0 square pixels). |
400 DFTAG_DRAW | A list of (tag, ref) pairs, 4 bytes each, of a composite image, displayed in order; may include instruction tags (XOR, source put, anti-aliasing). |
500 DFTAG_XYP | 8 bytes: x and y (32-bit integers) of the lower left corner on the screen; (0,0) is the lower left of the print area. |
602 DFTAG_T14, 603 DFTAG_T105 | Tektronix 4014 and 4105 vector streams; only the low 7 bits of each byte count. |
| Scientific data sets | |
720 DFTAG_NDG | A list of (tag, ref) pairs, 4 bytes each, of the members: SDD, SD, SDS, SDL, SDU, SDF, SDM, SDC, CAL, FV, LUT, LD, SDLNK. It replaces the old DFTAG_SDG (700), whose members are the same without CAL, FV, LUT, LD but with SDT (709). |
701 DFTAG_SDD | 6 + 8 × rank bytes: rank (2), the size of each dimension (4 each), then the data's NT ref (tag 106 and ref, 2 + 2) and one NT ref per dimension scale. |
702 DFTAG_SD | The array, NT size × product of the dimensions, row-major. Without an NT it is 32-bit IEEE float. |
703 DFTAG_SDS | Scales: rank flag bytes (0 or 1 per dimension: whether it has a scale), then each present scale's values, of the type in the SDD. |
704 DFTAG_SDL, 705 DFTAG_SDU, 706 DFTAG_SDF | Labels, units and output formats: consecutive NUL-terminated strings for the data and each dimension. |
707 DFTAG_SDM | Maximum then minimum, each of the data's number type. |
708 DFTAG_SDC | A NUL-terminated coordinate system string. |
709 DFTAG_SDT | No data: the array is column-major. No longer written. |
710 DFTAG_SDLNK | 8 bytes: NDG tag and ref, SDG tag and ref; ties an old SDG to a new NDG when the NDG holds 32-bit floats. |
731 DFTAG_CAL | 36 bytes: calibration factor (8, IEEE double), its error (8), offset (8), its error (8), effective data type (4: signed or unsigned 8, 16, 32-bit integer, 32 or 64-bit float). Real value = raw × factor + offset. |
732 DFTAG_FV | The fill value, sized by the data NT. |
| Vsets | |
1965 DFTAG_VG | Vgroup, 14 + 4 × nelt + name + class bytes: nelt (2), then nelt tag (2) and ref (2) members (any tag, including other Vgroups), name length (2) and name, class length (2) and class (not null-terminated), extension tag (2), extension ref (2), version (2), 2 zero bytes. |
1962 DFTAG_VH | Vdata description, 22 + 10 × nfields + names bytes: interlace (2), nvert records (4), ivsize record size (2), nfields (2); then per field: type (2), size in bytes (2), offset (2) and order (2); then per field a name length (2) and name; then Vdata name length and name, class length and class, extension tag (2), extension ref (2), version (2), 2 zero bytes. |
1963 DFTAG_VS | The records: nvert × (sum of field size × order), read with the description in its VH. |
| Obsolete | |
200 DFTAG_ID8 | x and y dimension, 16 bits each, of an 8-bit image. Superseded by ID. |
201 DFTAG_IP8 | 768 bytes: 256 RGB triples. Superseded by LUT. |
202 DFTAG_RI8 | x × y bytes, rows from the upper left. Superseded by RI. |
203 DFTAG_CI8 | Each row run-length encoded as above. Superseded by RLE; a CI8 and RI8 should not share a ref. |
204 DFTAG_II8 | A 4:1 IMCOMP image. Superseded by IMC. |
700 DFTAG_SDG | Old scientific data group, superseded by NDG since HDF 3.2. |
Extended tags and special elements
An element stored in a nonstandard way has an extended tag: the basic tag OR
0x4000 (adding 16,384), for example DFTAG_RI 302 becomes 16,686. The
descriptor then points to a description record, which starts with a 16-bit
special type and tells how to find the data. Four storage styles exist.
| Style | Description record |
|---|---|
Linked blocks (EXT_LINKED) | The element is a chain of blocks, all the same size except the first. The record holds the total length (4), the first block's length (4), the length of the other blocks (4), the number of blocks per block table (4) and the ref (2) of the first block table. A block table is stored under tag DFTAG_LINKED (20): its own ref (2) is the one named, then the ref of the next table (2; 0 means no more), then the refs of its data blocks (2 each). Each block is a DFTAG_LINKED element with the data. |
External (SPECIAL_EXT) | Length of the data in the external file (4), the offset in that file (4), and the file name as a non-null-terminated string. The spec says the structure of these elements is "still not completely defined" and gives this as a suggested layout. |
Chunked (SPECIAL_CHUNKED, a 16-bit constant) | The special tag descriptor (2) and length of this header (4), then: version (1), a flags field (4; only the low 8 bits are used, selecting additional specialness), the logical length of the whole element (4), chunk size (4), number type size (4), the tag (2) and ref (2) of the chunk table (a Vdata), a special-table tag and ref reserved for future ghost chunks (2 + 2), the number of dimensions (4), then per dimension: flag (4; low 8 bits the distribution type, 0 none or 1 block, only block supported and not checked; the next 8 bits 0 regular or 1 unlimited dimension), current length (4) and chunk length (4); then the fill value size (4) and the fill value. More specialness blocks may follow, each a descriptor (2), a header length (4) and a header. The chunk table has per chunk its origin in the chunk array (4 per dimension), a chunk tag (2; DFTAG_CHUNK 61, possibly DFTAG_CHUNKED 60 for nested chunking) and the chunk's ref (2). A chunk is a DFTAG_CHUNK element of chunk size × NT size bytes; ghost areas can make storage larger than the dataset. |
Compressed (SPECIAL_COMP) | The compression header: special tag (2), version (2), length of the uncompressed data (4), ref (2) of a DFTAG_COMPRESSED (40) element that holds the compressed bytes, model type (2; currently streaming I/O only), compression type (2). The rest depends on the type. N-bit run-length: number type (4), sign-extension flag (2), fill value (2), start bit (4), bit length (4), 30 bytes in all. Skipping Huffman: skipping unit size (4) and the number of bytes compressed (4; not used), 22 bytes. GNU zip (deflate): deflate level 0 to 9 (2), 16 bytes. The numeric codes of the compression types are in the library headers, not in the specification text. |
Compressed element: model and coder codes and parameters
From the HDF4 library source (HDFGroup/hdf4, master, read 2026-11-13): hdf/src/hcomp.h, hcomp.c (HCIwrite_header, HCPencode_header, HCPdecode_header, HCPquery_encode_header), cszip_priv.h, crle_priv.h, crle.c and htags.h. All numbers are big-endian.
The special code of a compressed element is SPECIAL_COMP = 3 (the list: 1 linked blocks, 2 external, 3 compressed, 4 variable-length linked blocks, 5 chunked, 6 buffered). The description record the library writes is: special code (int16) = 3, header version (uint16) = 0 (COMP_HEADER_VERSION), length of the uncompressed data (int32), reference number of the DFTAG_COMPRESSED (40) element that holds the compressed bytes (uint16), then the model type (uint16), the coder type (uint16) and the coder's parameters.
| Field | Values |
|---|---|
Model type (comp_model_t) | 0 COMP_MODEL_STDIO (standard C I/O model, the only one; it adds no parameter bytes). |
Coder type (comp_coder_t) | 0 COMP_CODE_NONE (just stored), 1 COMP_CODE_RLE, 2 COMP_CODE_NBIT, 3 COMP_CODE_SKPHUFF (skipping Huffman), 4 COMP_CODE_DEFLATE (gzip deflate), 5 COMP_CODE_SZIP, 6 COMP_CODE_INVALID (range check only), 7 COMP_CODE_JPEG (a hack so that GRsetcompress can create JPEG images; the JPEG tag path is separate), 12 COMP_CODE_IMCOMP (a hack so IMCOMP images can be inquired; it cannot be written and the header encoder rejects it). |
| Separate compression-type constants for images | COMP_NONE 0, COMP_JPEG 2, COMP_RLE 11, COMP_IMCOMP 12, mapped to tags by compress_map (COMP_JPEG to DFTAG_JPEG5, COMP_RLE to DFTAG_RLE); decoder and encoder availability flags COMP_DECODER_ENABLED 1 and COMP_ENCODER_ENABLED 2. |
| Coder | Parameter bytes after the coder type |
|---|---|
| none, RLE, JPEG | None. |
| N-bit (16 bytes) | number type of the data (int32); sign-extend flag (uint16); fill-with-ones flag (uint16, 0 fills with zeros); start bit, the offset of the first stored bit (int32); bit length, the number of bits stored (int32). |
| Skipping Huffman (8 bytes) | skip size, the size of the elements when skipping (uint32, must be at least 1); then the same value again (uint32, “number of bytes compressed, not used currently”). The decoder ignores the second. |
| Deflate (2 bytes) | deflate level (uint16, 0 to 9). |
| Szip (14 bytes) | pixels in the dataset or chunk (uint32); pixels per scanline (uint32); options mask OR-ed with SZ_H4_REV_2 = 0x10000 (uint32; this bit marks the revised storage and is removed again when the mask is returned to applications); bits per pixel (1 byte); pixels per block (1 byte). |
The header length is therefore 2 (model) + 2 (coder) plus the parameter bytes. RLE stream (crle.c): control bytes with the top bit (0x80, RUN_MASK) set start a run, the low 7 bits (COUNT_MASK 0x7F) plus the minimum run length 3 give the number of repeats of the one following byte (a run is at most 130, RLE_MAX_RUN); a control byte with the top bit clear starts a mix of literal bytes, count = low 7 bits + 1 (minimum RLE_MIN_MIX 1, at most 128 bytes, the encoder buffer being 128). The bit-level streams of N-bit, skipping Huffman, deflate and szip are those of their own sources (cnbit.c, cskphuff.c, cdeflate.c, cszip.c) and are not described here; deflate and szip use the zlib and szip libraries.
SD predefined attributes
From the HDF4.1r3 User's Guide, section 3.10 “Predefined Attributes” of the SD chapter (support.hdfgroup.org, read 2026-11-13). These names are reserved and understood by the library. A guide table prints cordsys and __FillValue where the definition table and the netCDF convention have _FillValue; the names below are those of the definition table (the guide itself spells the coordinate system attribute cordsys).
| Category | Name | Type, count | Applies to | Meaning |
|---|---|---|---|---|
| Label | long_name | DFNT_CHAR8, string length | SDS and dimension | Name of the array. |
| Unit | units | DFNT_CHAR8, string length | SDS and dimension | Units for all dimensions and data. |
| Format | format | DFNT_CHAR8, string length | SDS and dimension | Display format; the convention is FORTRAN-77 notation, for example F7.2. |
| Coordinate system | cordsys | DFNT_CHAR8, string length | SDS only | Coordinate system for interpretation: “cartesian”, “polar”, “spherical” are recommended. |
| Range | valid_range | any valid data type, 2 values | SDS only | Maximum and minimum of a selected valid range; the library does not check or update it as data changes. |
| Fill value | _FillValue | any valid data type, 1 value | SDS only | Value used for the empty locations between non-contiguous writes. |
| Calibration | scale_factor, scale_factor_err, add_offset, add_offset_err | DFNT_FLOAT64, 1 value each | SDS only | Value each array value is multiplied by; error introduced by scaling; value added; error introduced by the offset. |
| Calibration | calibrated_nt | DFNT_INT32, 1 value | SDS only | The data type of the calibrated (unpacked) data. |
Routines: SDsetdatastrs / SDgetdatastrs for the four string attributes (an attribute not set is returned as an empty string), plus the range, fill-value and calibration routines; predefined dimension attributes must be read and written with the dedicated routines, while dataset ones may also use the general attribute routines. An attribute created with different parameters than the definition table gives unpredictable results when read through the dedicated routine. Recommended, not predefined, netCDF conventions: missing_value (local to the array; different from the fill value), global title and history.
Specification chapters 5 (sets and groups), 7 (the SD model) and 8 (the GR model) and the Vdata attribute convention remain described only through the tag layouts above. The older guide read here is the 4.1r3 edition.