Project

General

Profile

how to determine the values for the scale_factor and scale_factor

Added by pooran khedri about 5 years ago

Hi everybody,
I need ta add the add_offset and scale_factor to a netcdf file, this file contains meterological data. I do not know how to set the correct value for them.
could you please explain to me how to determine the values for the scale_factor and scale_factor?

Any help would be appreciated.
Pooran


Replies (1)

RE: how to determine the values for the scale_factor and scale_factor - Added by Karin Meier-Fleischer about 5 years ago

Hi pooran,

see UNIDATA's documentation about packed data values.

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 that

        unpacked_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 ;

To add attributes to a variable use the CDO operator setattribute.

-Karin

    (1-1/1)