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

Build a nxCDF NC_BFLOAT16 copy of the GDAS sample file. More...

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

Macros

#define ERRCODE   2
#define ERR(e)
#define HISTORY   "Data fields converted from gdas_sfc_sample.nc to NC_BFLOAT16 with nc_get_var_bfloat16 (round to nearest, ties to even) by make_bfloat16_sample"

Functions

static size_t type_size (nc_type xtype)
 Return the size in bytes of an atomic NetCDF type.
static int copy_att (int ncid_in, int varid_in, const char *name, int ncid_out, int varid_out)
 Copy one attribute between files, keeping type and length.
static int copy_global_atts (int ncid_in, int ncid_out)
 Copy the global attributes and add the history attribute.
static int def_dims (int ncid_in, int ncid_out)
 Define the output dimensions in fixed order.
static int def_vars (int ncid_in, int ncid_out)
 Define all output variables and copy their attributes.
static int copy_data (int ncid_in, int ncid_out)
 Copy the data of every variable.
int main (int argc, char **argv)
 Main entry point.

Detailed Description

Build a nxCDF NC_BFLOAT16 copy of the GDAS sample file.

Reads test/data/netCDF-4/gdas_sfc_sample.nc (an ordinary NetCDF-4 file) and writes a nxCDF file in which every NC_FLOAT data variable is stored as NC_BFLOAT16. The conversion is done by nc_get_var_bfloat16() on the source variables, which rounds to nearest, ties to even; fill points (9.99e+20) are converted like any other value. The NC_DOUBLE coordinate variables are copied unchanged. The output is uncompressed and contiguous; run make_bfloat16_sample.sh to apply the shuffle+zstd layout with h5repack.

Differences from the source: time is a fixed dimension of length 1, and the bfloat16 variables carry no _FillValue or missing_value attributes (NC_BFLOAT16 fill values are not supported, issue #377). A history global attribute records the conversion.

Usage:

make_bfloat16_sample <input.nc> <output.nc>
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

◆ HISTORY

#define HISTORY   "Data fields converted from gdas_sfc_sample.nc to NC_BFLOAT16 with nc_get_var_bfloat16 (round to nearest, ties to even) by make_bfloat16_sample"

Value of the history global attribute added to the output.

Function Documentation

◆ copy_att()

int copy_att ( int ncid_in,
int varid_in,
const char * name,
int ncid_out,
int varid_out )
static

Copy one attribute between files, keeping type and length.

Parameters
ncid_inInput file.
varid_inVariable the attribute is attached to (NC_GLOBAL ok).
nameAttribute name.
ncid_outOutput file.
varid_outOutput variable (NC_GLOBAL ok).
Returns
0 on success, exits on NetCDF error.

◆ copy_data()

int copy_data ( int ncid_in,
int ncid_out )
static

Copy the data of every variable.

NC_DOUBLE variables are copied with nc_get_var_double()/ nc_put_var_double(). NC_BFLOAT16 variables are read from the source NC_FLOAT variable with nc_get_var_bfloat16() into a uint16_t bit buffer (NEP rounds to nearest, ties to even) and written with nc_put_var_bfloat16(). An NC_ERANGE from the get is an error, because the GDAS sample has no out-of-range values.

Parameters
ncid_inInput file.
ncid_outOutput file.
Returns
0 on success, 1 on NC_ERANGE, exits on NetCDF error.

◆ copy_global_atts()

int copy_global_atts ( int ncid_in,
int ncid_out )
static

Copy the global attributes and add the history attribute.

Parameters
ncid_inInput file.
ncid_outOutput file.
Returns
0 on success, exits on NetCDF error.

◆ def_dims()

int def_dims ( int ncid_in,
int ncid_out )
static

Define the output dimensions in fixed order.

Defines grid_xt, grid_yt and a fixed-length time, with the lengths taken from the same-named input dimensions.

Parameters
ncid_inInput file.
ncid_outOutput file.
Returns
0 on success, exits on NetCDF error.

◆ def_vars()

int def_vars ( int ncid_in,
int ncid_out )
static

Define all output variables and copy their attributes.

For every input variable, in input order: NC_DOUBLE variables are defined as NC_DOUBLE (the coordinates) and NC_FLOAT variables as NC_BFLOAT16 with no fill value. Any other type is an error. Output dims are mapped by name. Every variable attribute is copied except _FillValue and missing_value.

Parameters
ncid_inInput file.
ncid_outOutput file.
Returns
0 on success, 1 on an unexpected variable type, exits on NetCDF error.

◆ main()

int main ( int argc,
char ** argv )

Main entry point.

Parameters
argcArgument count; must be 3.
argvargv[1] is the input netCDF-4 file, argv[2] the output nxCDF file.
Returns
0 on success, 1 on an unexpected type or NC_ERANGE, 2 on usage or NetCDF error.

◆ type_size()

size_t type_size ( nc_type xtype)
static

Return the size in bytes of an atomic NetCDF type.

Parameters
xtypeThe nc_type.
Returns
Element size in bytes, or 0 for an unknown type.