|
NEP 4.3.0
NetCDF Expansion Pack
|
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. | |
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:
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:
Key Concepts:
Key API functions:
Related Examples:
| #define ERR | ( | e | ) |
| #define ERRCODE 2 |
| #define NX 1536 |
Length of the grid_xt dimension of the GDAS sample.
| #define NY 768 |
Length of the grid_yt dimension of the GDAS sample.
|
static |
Read a null-padded text attribute into a malloc'ed buffer.
| ncid | NetCDF file ID. |
| varid | Variable ID. |
| name | Attribute name. |
| text | Returned buffer (caller frees); NULL if absent. |
| len | Returned attribute length; 0 if absent. |
| 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.
| argc | Argument count; must be 2. |
| argv | argv[1] is the path to gdas_sfc_sample.nc. |
|
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.
| path | Output file path. |
| xtype | Variable type (NC_FLOAT or NC_FLOAT16). |
| data | Data buffer (float or uint16_t, NVALS elements). |
| grid_yt | Y coordinates (NY doubles). |
| grid_xt | X coordinates (NX doubles). |
| long_name | long_name attribute text. |
| long_name_len | Length of long_name. |
| units | units attribute text. |
| units_len | Length of units. |