7.8 Packed Data Values

Conventions for packing numeric data to save space have some
subtleties.

Packed data is stored in a netCDF file using a smaller data type
than the original data, for example, packing doubles into shorts. The
netCDF library itself does not do packing and unpacking, but the Java-netCDF library will do
automatic unpacking, see class **VariableDS**.

Recommendations

- For each variable with packed data, add two attributes called
`scale_factor`

and`add_offset`

, such thatunpacked_value = packed_value * scale_factor + add_offset

- The type of the stored variable is the type of the packed data type, typically byte, short, or int.
- The type of the
`scale_factor`

and`add_offset`

attributes should be the type that you want the unpacked data to be, typically float or double. - To compute the scale and offset for maximum precision packing of a set of
numbers, use:
add_offset = dataMin scale_factor = (dataMax - dataMin) / (2^n - 1)

where`n`

is the number of bits of the packed (integer) data type. - To avoid introducing a bias into the unpacked values due to
truncation when packing, round to the nearest integer rather than
just truncating towards zero:
packed_value = nint((unpacked_value - add_offset) / scale_factor)

- The precision of the data will be
`1.0 / scale_factor`

. - Example, packing 32-bit floats into 16-bit shorts:
variables: short data(z, y, x) ; data:scale_offset = 34.02f ; data:add_offset = 1.54f ;