netcdf-c/nc_test4/tst_unlim_vars.c
2020-02-12 07:44:54 -07:00

196 lines
7.8 KiB
C

/* This is part of the netCDF package.
Copyright 2018 University Corporation for Atmospheric Research/Unidata
See COPYRIGHT file for conditions of use.
Test netcdf-4 variables with unlimited dimensions.
$Id: tst_unlim_vars.c,v 1.9 2010/05/25 13:53:04 ed Exp $
*/
#include <config.h>
#include <nc_tests.h>
#include "err_macros.h"
#define FILE_NAME "tst_unlim_vars.nc"
#define SFC_TEMP_NAME "surface_temperature"
#define LAT_NAME "lat"
#define LAT_LEN 2
#define LON_NAME "lon"
#define LON_LEN 3
#define TIME_NAME "time"
#define NUM_TIMESTEPS 4
#define NDIM3 3
#define NDIM2 2
int
main(int argc, char **argv)
{
printf("\n*** Testing netcdf-4 variable with unlimited dimensions.\n");
printf("*** Testing file with one var, one unlim dim...");
{
int ncid, sfc_tempid;
float data_out[NUM_TIMESTEPS][LAT_LEN][LON_LEN], data_in[NUM_TIMESTEPS][LAT_LEN][LON_LEN];
int lat, lon, time;
int dimids[NDIM3];
nc_type xtype_in;
int ndims_in, dimids_in[10], natts_in, nvars_in, unlimdimid_in;
size_t len_in;
char name_in[NC_MAX_NAME+1];
size_t start[NDIM3], count[NDIM3];
int d;
/* Set up phony data. */
for (time = 0; time < NUM_TIMESTEPS; time++)
for (lat = 0; lat < LAT_LEN; lat++)
for (lon = 0; lon < LON_LEN; lon++)
data_out[time][lat][lon] = 25.5 + lat + lon + time;
/* Create a file with a 3D surface temp variable. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
/* Create three dims, one unlimited. */
if (nc_def_dim(ncid, TIME_NAME, NC_UNLIMITED, &dimids[0])) ERR;
if (nc_def_dim(ncid, LAT_NAME, LAT_LEN, &dimids[1])) ERR;
if (nc_def_dim(ncid, LON_NAME, LON_LEN, &dimids[2])) ERR;
/* Define a var. */
for (d = 0; d < NDIM3; d++)
dimids[d] = d;
if (nc_def_var(ncid, SFC_TEMP_NAME, NC_FLOAT, NDIM3, dimids, &sfc_tempid)) ERR;
/* Check some metadata. */
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != 3 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in,
&natts_in)) ERR;
if (strcmp(name_in, SFC_TEMP_NAME) || xtype_in != NC_FLOAT ||
ndims_in != 3 || natts_in != 0) ERR;
for (d = 0; d < NDIM3; d++)
if (dimids_in[d] != dimids[d]) ERR;
if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
if (len_in != 0 || strcmp(name_in, TIME_NAME)) ERR;
if (nc_inq_dim(ncid, 1, name_in, &len_in)) ERR;
if (len_in != LAT_LEN || strcmp(name_in, LAT_NAME)) ERR;
if (nc_inq_dim(ncid, 2, name_in, &len_in)) ERR;
if (len_in != LON_LEN || strcmp(name_in, LON_NAME)) ERR;
if (nc_close(ncid)) ERR;
if (nc_open(FILE_NAME, 0, &ncid)) ERR;
/* Check metadata. */
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != 3 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in,
&natts_in)) ERR;
if (strcmp(name_in, SFC_TEMP_NAME) || xtype_in != NC_FLOAT ||
ndims_in != 3 || natts_in != 0) ERR;
for (d = 0; d < NDIM3; d++)
if (dimids_in[d] != dimids[d]) ERR;
if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
if (len_in != 0 || strcmp(name_in, TIME_NAME)) ERR;
if (nc_inq_dim(ncid, 1, name_in, &len_in)) ERR;
if (len_in != LAT_LEN || strcmp(name_in, LAT_NAME)) ERR;
if (nc_inq_dim(ncid, 2, name_in, &len_in)) ERR;
if (len_in != LON_LEN || strcmp(name_in, LON_NAME)) ERR;
if (nc_close(ncid)) ERR;
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
/* Check metadata. */
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != 3 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in,
&natts_in)) ERR;
if (strcmp(name_in, SFC_TEMP_NAME) || xtype_in != NC_FLOAT ||
ndims_in != 3 || natts_in != 0) ERR;
for (d = 0; d < NDIM3; d++)
if (dimids_in[d] != dimids[d]) ERR;
if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
if (len_in != 0 || strcmp(name_in, TIME_NAME)) ERR;
if (nc_inq_dim(ncid, 1, name_in, &len_in)) ERR;
if (len_in != LAT_LEN || strcmp(name_in, LAT_NAME)) ERR;
if (nc_inq_dim(ncid, 2, name_in, &len_in)) ERR;
if (len_in != LON_LEN || strcmp(name_in, LON_NAME)) ERR;
if (nc_close(ncid)) ERR;
/* Write some data to the var.*/
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
for (d = 0; d < NDIM3; d++)
start[d] = 0;
count[0] = NUM_TIMESTEPS;
count[1] = LAT_LEN;
count[2] = LON_LEN;
if (nc_put_vara_float(ncid, 0, start, count, (float *)data_out)) ERR;
/* Check metadata. */
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != 3 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in,
&natts_in)) ERR;
if (strcmp(name_in, SFC_TEMP_NAME) || xtype_in != NC_FLOAT ||
ndims_in != 3 || natts_in != 0) ERR;
for (d = 0; d < NDIM3; d++)
if (dimids_in[d] != dimids[d]) ERR;
if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
if (len_in != NUM_TIMESTEPS || strcmp(name_in, TIME_NAME)) ERR;
if (nc_inq_dim(ncid, 1, name_in, &len_in)) ERR;
if (len_in != LAT_LEN || strcmp(name_in, LAT_NAME)) ERR;
if (nc_inq_dim(ncid, 2, name_in, &len_in)) ERR;
if (len_in != LON_LEN || strcmp(name_in, LON_NAME)) ERR;
if (nc_close(ncid)) ERR;
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
/* Check data. */
if (nc_get_vara_float(ncid, 0, start, count, (float *)data_in)) ERR;
for (time = 0; time < NUM_TIMESTEPS; time++)
for (lat = 0; lat < LAT_LEN; lat++)
for (lon = 0; lon < LON_LEN; lon++)
if (data_in[time][lat][lon] != data_out[time][lat][lon]) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** Testing netcdf-4 variable 2 unlimited dimensions...");
{
#define DIM0_NAME "Speed"
#define DIM1_NAME "Height"
#define VAR_NAME "Superman"
int ncid, varid, dimid[NDIM2];
int data = TEST_VAL_42;
size_t index[NDIM2] = {1, 1};
char name_in[NC_MAX_NAME + 1];
size_t len_in;
/* Create a file with 2 unlimited dims. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, DIM0_NAME, NC_UNLIMITED, &dimid[0])) ERR;
if (nc_def_dim(ncid, DIM1_NAME, NC_UNLIMITED, &dimid[1])) ERR;
if (nc_def_var(ncid, VAR_NAME, NC_INT, NDIM2, dimid, &varid)) ERR;
if (nc_close(ncid)) ERR;
/* Check the file. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
if (strcmp(name_in, DIM0_NAME) || len_in != 0) ERR;
if (nc_inq_dim(ncid, 1, name_in, &len_in)) ERR;
if (strcmp(name_in, DIM1_NAME) || len_in != 0) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file and add data. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_put_var1_int(ncid, 0, index, &data)) ERR;
if (nc_inq_dim(ncid, 0, NULL, &len_in)) ERR;
if (len_in != 2) ERR;
if (nc_inq_dim(ncid, 1, NULL, &len_in)) ERR;
if (len_in != 2) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}