This page follows Unidata’s NetCDF File Format Specification, a grammar for the classic format (CDF-1) and the 64-bit offset format (CDF-2), and PnetCDF’s CDF-5 specification for the 64-bit data format, both read 2026-11-12. All numbers are big-endian, and every item is padded to a multiple of 4 bytes.
Header
| Item | Form |
|---|---|
| magic | C D F and a version byte: 01 classic, 02 64-bit offset, 05 64-bit data. |
| numrecs | Number of records: 4 bytes (8 in CDF-5). FFFFFFFF means streaming: count unknown. |
| dim_list | Absent, or NC_DIMENSION (0000000A), a count, and dimensions: a name and a length. Length 0 marks the record (unlimited) dimension; there is at most one. |
| gatt_list | Absent, or NC_ATTRIBUTE (0000000C), a count, and attributes. |
| var_list | Absent, or NC_VARIABLE (0000000B), a count, and variables. |
“Absent” is written as two zero words (ZERO ZERO); in CDF-5 the
second is 8 bytes. Counts are 4 bytes, 8 in CDF-5.
| Element | Fields |
|---|---|
| name | Length, then the UTF-8 name (normalised to NFC), padded with zeros to 4 bytes. |
| attribute | Name, type, number of values, the values padded to 4 bytes. |
| variable | Name; number of dimensions and their dimension IDs (a record variable is one whose first dimension is the record dimension); its attributes (a vatt_list, same form as gatt_list); type; vsize; begin. |
vsize | Bytes of one variable (or of one record of it), padded to 4. The field is redundant; a size over 232 − 4 bytes is written as 232 − 1. |
begin | File offset of the variable’s data: 4 bytes in CDF-1, 8 in CDF-2 and CDF-5. |
Types
| Code | Type | Size | Default fill value |
|---|---|---|---|
| 1 | NC_BYTE | 1, signed | −127 |
| 2 | NC_CHAR | 1 | 0 |
| 3 | NC_SHORT | 2 | −32,767 |
| 4 | NC_INT | 4 | −2,147,483,647 |
| 5 | NC_FLOAT | 4, IEEE | 9.9692099683868690 × 1036 |
| 6 | NC_DOUBLE | 8, IEEE | 9.9692099683868690 × 1036 |
| 7 | NC_UBYTE | 1 (CDF-5 only) | 255 |
| 8 | NC_USHORT | 2 (CDF-5 only) | 65,535 |
| 9 | NC_UINT | 4 (CDF-5 only) | 4,294,967,295 |
| 10 | NC_INT64 | 8 (CDF-5 only) | −9,223,372,036,854,775,806 |
| 11 | NC_UINT64 | 8 (CDF-5 only) | 18,446,744,073,709,551,614 |
A _FillValue attribute on a variable replaces the default.
Names, padding, fill and offsets
From the notes under Unidata's grammar (read 2026-11-13).
- Names. The first character is alphanumeric, a multi-byte UTF-8 character, or
_(reserved for names with meaning to implementations, such as_FillValue). Later characters may also be printing special characters, but never/; a name must not end with a space. In the grammar,IDNis alphanumeric,special1(_ . @ + -) orspecial2; older libraries enforced a narrower set for new names but read any bytes. Names must be normalised to Unicode NFC before encoding as UTF-8. - Padding. Padding in the header is NUL bytes. Padding in data uses the variable's fill value. Special case: with exactly one record variable and its type char, byte or short, there is no padding between records, so records after the first need not start on a 4-byte boundary; readers should then ignore
vsizeand assume no padding, while writers still storevsizeas if padding were there. - Fill values. Defaults: char 0, byte 0x81 (−127), short 0x8001, int 0x80000001, float
7CF00000(9.96921 × 1036), double479E000000000000. A_FillValueattribute (scalar, of the variable's type) replaces them. Fill is not required: libraries have long had a no-fill mode that skips initialising data, which is faster but means never-written values cannot be detected. - Reserved attributes.
_Unsignedis reserved for saying that byte data is unsigned (there is no standard convention, and the default reading is signed);_Encodingis reserved for the encoding of char data (which may be other than UTF-8). - Streaming.
numrecs=FFFFFFFFmeans an indeterminate record count, to be found by reading or by computing from the file length, with the header never updated. The specification says this is not yet implemented. - 64-bit offset limit. The format differs from classic only in the version byte (
02) and the 64-bit offset field. Each fixed-size variable, and one record's worth of each record variable, is still limited to a little under 4 GiB, so that a whole variable or record can be read at once on 32-bit machines. - Smallest file. No dimensions, variables or attributes: 32 bytes, the magic number
CDF01, then seven 32-bit zeros: the record count, then the three absent lists (dimensions, global attributes, variables), each written as two zero words.
Computing an offset. The extsize of a value is 1 for byte and char, 2
for short, 4 for int and float, 8 for double. Form a product vector of the
variable's dimension lengths from right to left, leaving out the leftmost (record)
dimension of a record variable: lengths [5 3 2 7] give
[210 42 14 7], and a record variable [0 2 9 4] gives
[0 72 36 4]. The leftmost product rounded up to a multiple of 4 is
vsize (212 for the first example, 72 for the second). The offset of the
value at zero-based coordinates coord is
begin + (coord · productVector) × extsize, and for a record
variable also coord[0] × recsize, where recsize is the
sum of the vsize of all record variables (using the true size when the
field is too small).
CDF-5 field widths and differences
From the PnetCDF “CDF-5 file format specification” (Appendix G of the PnetCDF C Interface Guide; text read 2026-11-13 at cucis.ece.northwestern.edu/projects/PnetCDF/CDF-5.html). CDF-5 keeps the grammar of CDF-1 and CDF-2 and changes these items.
- Version byte
05(“64-bit data format”). NON_NEGis a 64-bit integer (in CDF-1 and CDF-2 it is 32-bit). This applies to every use of it:numrecs, everynelems(the counts of dimensions, attributes, variables, values, and of characters in a name),dim_length, eachdimidandvsize.begin(OFFSET) is 64-bit in both CDF-2 and CDF-5 (32-bit in CDF-1).- Fixed 32-bit items stay 32 bits: the version-independent list tags
NC_DIMENSION(0000000A),NC_VARIABLE(0000000B),NC_ATTRIBUTE(0000000C) and thenc_typecodes. - ABSENT is
ZERO ZERO64: a 4-byte zero tag followed by an 8-byte zero count, 12 bytes (CDF-1 and CDF-2: two 4-byte zeros). STREAMING isFF FF FF FF FF FF FF FFin CDF-5 (4 bytes in CDF-1 and CDF-2); the “not yet implemented” remark is kept. The 8-byte form comes from PnetCDF's CDF-5 specification (the Unidata text defines only the 4-byte one); netcdf-c, PnetCDF and scipy read all ones as the number it spells, and the viewer does the same unless its option asks for STREAMING. - Types. The type list adds
NC_UBYTE7,NC_USHORT8,NC_UINT9,NC_INT6410 andNC_UINT6411 (all big-endian; unsigned values are two's-complement-style fixed-width integers; bytes, chars, shorts and ushorts are padded to a multiple of 4 like the older small types; ints, uints, int64s, uint64s, floats and doubles need no padding), with the new default fill valuesFF(UBYTE 255),FFFF(USHORT 65535),FFFFFFFF(UINT 4294967295),80 00 00 00 00 00 00 02(INT64 −9223372036854775806) andFF FF FF FF FF FF FF FE(UINT64 18446744073709551614). The extsize values for the new types are 1, 2, 4, 8, 8. - Names. The CDF-5 grammar writes the first-character rule as
ID1 = alphanumeric | '_'andspecial1 = '_' | '.' | '@' | '+' | '-'; Unidata's classic grammar has the same two rules (ID1 = alphanumeric | '_', the samespecial1), and the grammar comment gives the regular expression([a-zA-Z0-9_]|{MUTF8})([^\\x00-\\x1F/\\x7F-\\xFF]|{MUTF8})*({MUTF8}is a multi-byte UTF-8 character); the notes are the same: first character alphanumeric, multi-byte UTF-8 or_, no/, no trailing spaces, NFC normalisation. - vsize. The note is unchanged:
232 − 1invsizemarks a variable too large for 32 bits in CDF-1 and CDF-2; the CDF-5 grammar makesvsizea 64-bit field. Offset computation, the product vector and the one-record-variable exception are as in CDF-1 and CDF-2. - PnetCDF writer detail. The PnetCDF tool
ncmpigen(as opposed toncgen) pads the header to a default minimum size of 512 bytes, sobeginis 512 in its output.
Worked examples
From the Unidata specification (Appendix B, netCDF 4.10.1 documentation, read 2026-11-13) and the CDF-5 specification above.
The empty file
netcdf empty { } is 32 bytes in CDF-1 and CDF-2, 48 bytes in CDF-5 (the same layout with 64-bit counts and ZERO64):
43 44 46 01 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
magic CDF 1 | numrecs 0 | dim_list ABSENT (two zero words)
| gatt_list ABSENT | var_list ABSENT
The tiny file
netcdf tiny {
dimensions: dim = 5;
variables: short vx(dim);
data: vx = 3, 1, 4, 1, 5 ;
}
CDF-1, 92 bytes (offset, bytes, meaning):
00 43 44 46 01 magic, version 1
04 00 00 00 00 numrecs 0
08 00 00 00 0A NC_DIMENSION
0C 00 00 00 01 1 dimension
10 00 00 00 03 64 69 6D 00 name length 3, "dim" + 1 padding byte
18 00 00 00 05 dim_length 5
1C 00 00 00 00 00 00 00 00 gatt_list ABSENT
24 00 00 00 0B NC_VARIABLE
28 00 00 00 01 1 variable
2C 00 00 00 02 76 78 00 00 name length 2, "vx" + 2 padding bytes
34 00 00 00 01 1 dimension in the variable
38 00 00 00 00 dimension ID 0
3C 00 00 00 00 00 00 00 00 vatt_list ABSENT
44 00 00 00 03 type NC_SHORT
48 00 00 00 0C vsize 12
4C 00 00 00 50 begin 80
50 00 03 00 01 00 04 00 01 00 05 80 01 data 3 1 4 1 5 and the short fill value 0x8001 as padding
CDF-2, 96 bytes is the same except for the version byte 02 and the 64-bit begin, which is 84 (00 00 00 00 00 00 00 54).
CDF-5, 140 bytes (the header is 128 bytes, the 12-byte data starts at begin = 128): 43 44 46 05; numrecs 00 × 8; NC_DIMENSION 00 00 00 0A; nelems 00 00 00 00 00 00 00 01; name length 8 bytes = 3, 64 69 6D and 1 padding byte (names pad to a multiple of 4 bytes, as in CDF-1 and CDF-2); dim_length 8 bytes = 5; gatt_list ABSENT as 00 00 00 00 plus 8 zero bytes; NC_VARIABLE 00 00 00 0B; nelems 1 (8 bytes); name length 2 (8 bytes), 76 78 00 00; rank 1 (8 bytes); dimension ID 0 (8 bytes); vatt_list ABSENT (12 bytes); type 00 00 00 03 (4 bytes); vsize 12 (8 bytes); begin 128 (8 bytes); then the 12 data bytes 00 03 00 01 00 04 00 01 00 05 80 01. Created with ncgen -v 5 -b tiny.cdl (-v 2 for CDF-2).
Data
- Fixed-size variables come first, each stored whole at its
begin, in row-major order, padded to 4 bytes. - Then the records: for each step of the record dimension, one slice of every
record variable in turn. The record size is the sum of their
vsizes. - When there is exactly one record variable, its slices are not padded, so byte and short data is packed tightly.