NEP 4.3.0
NetCDF Expansion Pack
Loading...
Searching...
No Matches
float16.c File Reference

Store a real meteorological field as NC_FLOAT16 in a nxCDF file. More...

#include <netcdf.h>
#include <nep.h>
#include "nxcdf/nxcdf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "float16_conv.h"

Macros

#define NY   768
#define NX   1536
#define NVALS   ((size_t)NY * NX)
#define ERRCODE   2
#define ERR(e)

Functions

static void get_att_text (int ncid, int varid, const char *name, char **text, size_t *len)
 Read a null-padded text attribute into a malloc'ed buffer.
static int write_file (const char *path, nc_type xtype, const void *data, const double *grid_yt, const double *grid_xt, const char *long_name, size_t long_name_len, const char *units, size_t units_len)
 Write one field and the coordinate variables to a nxCDF file.
int main (int argc, char **argv)
 Main entry point.

Detailed Description

Store a real meteorological field as NC_FLOAT16 in a nxCDF file.

IEEE 754 binary16 is a 16-bit floating-point format with 1 sign bit, 5 exponent bits and 10 mantissa bits. It has about 3 significant decimal digits of precision, a maximum of 65504 and a smallest normal value of about 6.1e-5, and takes half the storage of float32. It suits data where a relative error of up to 2^-11 (about 0.05%) is acceptable and every value fits its narrow range.

This program reads the tmp2m (2 m temperature) field and its coordinate variables from the GDAS sample file test/data/netCDF-4/gdas_sfc_sample.nc, writes it twice to nxCDF files - once as NC_FLOAT and once as NC_FLOAT16 - then reads the float16 file back, checks the bits round-tripped exactly, and reports file sizes and the maximum conversion error.

Usage:

HDF5_PLUGIN_PATH=<dir with one zstd plugin> \
NCRCENV_RC=<build>/.ncrc NETCDF_RC=<build> \
./float16 gdas_sfc_sample.nc

The input file is zstd-compressed, so HDF5_PLUGIN_PATH must point to a directory holding the zstd HDF5 plugin. The nxCDF backend is loaded through the .ncrc UDF9 autoload mechanism, so NCRCENV_RC and NETCDF_RC must name the NEP build directory that contains the generated .ncrc.

Learning Objectives:

  • Create a nxCDF file with nc_create() and NC_NXCDF
  • Define an NC_FLOAT16 variable
  • Read and write float16 data with the nc_*_float16() functions
  • Read NC_FLOAT16 data as float with nc_get_vara_float()
  • Understand the precision and range trade-offs of binary16

Key Concepts:

  • Raw bit patterns: NC_FLOAT16 data is exchanged with the library as raw uint16_t bit patterns
  • Conversion by NEP: the nc_*_float16() functions convert between float16 and float/double variables in the library; float_to_float16() stays as the reference conversion and is used here as a cross-check; the typed float/double calls use the same NEP round-to-nearest-even through the internal engine
  • Native mode only: NC_FLOAT16 is only available in nxCDF native mode
  • No fill value: float16 variables are defined without a fill value

Key API functions:

  • nc_create() Create a nxCDF file with NC_NXCDF
  • nc_def_var() Define a variable with NC_FLOAT16
  • nc_get_var_float16() Read a float variable as float16 (NEP converts)
  • nc_put_vara_float16() Write float16 bits to an NC_FLOAT16 variable
  • nc_get_vara_float16() Read float16 bits from an NC_FLOAT16 variable
  • nc_get_vara_float() Read float16 data converted to float

Related Examples:

  • bfloat16.c - The bfloat16 version of this tutorial
  • float16_perf.c - Benchmark of float32 vs float16 size and speed
  • compression.c - NetCDF-4 compression tutorial
Author
Edward Hartnett, Intelligent Data Design, Inc.

Macro Definition Documentation

◆ ERR

#define ERR ( e)
Value:
{printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
#define ERRCODE
Definition bfloat16.c:84

◆ ERRCODE

#define ERRCODE   2

◆ NVALS

#define NVALS   ((size_t)NY * NX)

Number of values in one field.

◆ NX

#define NX   1536

Length of the grid_xt dimension of the GDAS sample.

◆ NY

#define NY   768

Length of the grid_yt dimension of the GDAS sample.

Function Documentation

◆ get_att_text()

void get_att_text ( int ncid,
int varid,
const char * name,
char ** text,
size_t * len )
static

Read a null-padded text attribute into a malloc'ed buffer.

Parameters
ncidNetCDF file ID.
varidVariable ID.
nameAttribute name.
textReturned buffer (caller frees); NULL if absent.
lenReturned attribute length; 0 if absent.

◆ main()

int main ( int argc,
char ** argv )

Main entry point.

Reads tmp2m and its coordinate variables from the GDAS sample, writes float_tmp2m.nc (NC_FLOAT) and float16_tmp2m.nc (NC_FLOAT16), verifies the float16 bits round-trip exactly, and prints file sizes and conversion errors.

Parameters
argcArgument count; must be 2.
argvargv[1] is the path to gdas_sfc_sample.nc.
Returns
0 on success, 1 on verification failure, 2 on usage or NetCDF error.

◆ write_file()

int write_file ( const char * path,
nc_type xtype,
const void * data,
const double * grid_yt,
const double * grid_xt,
const char * long_name,
size_t long_name_len,
const char * units,
size_t units_len )
static

Write one field and the coordinate variables to a nxCDF file.

Creates path with nc_create(NC_NXCDF | NC_CLOBBER), defines fixed dims time = 1, grid_yt = NY and grid_xt = NX, the NC_DOUBLE coordinate variables grid_yt and grid_xt, and the variable tmp2m(time, grid_yt, grid_xt) of type xtype with the long_name and units attributes copied from the input. The variable gets no fill value.

Parameters
pathOutput file path.
xtypeVariable type (NC_FLOAT or NC_FLOAT16).
dataData buffer (float or uint16_t, NVALS elements).
grid_ytY coordinates (NY doubles).
grid_xtX coordinates (NX doubles).
long_namelong_name attribute text.
long_name_lenLength of long_name.
unitsunits attribute text.
units_lenLength of units.
Returns
0 on success, exits on NetCDF error.