📄 t_nc.c
字号:
/* * Copyright 1988 University Corporation for Atmospheric Research * See netcdf/COPYRIGHT file for copying and redistribution conditions. *//* $Id: t_nc.c 2501 2007-11-20 02:33:29Z benkirk $ *//* * Program to create a cdf, exercise all cdf functions. * Creates cdf, stuff it full of numbers, closes it. Then * reopens it, and checks for consistency. * Leaves the file around afterwards. * * Based on a program to test the nasa look-alike program, * so not the most appropropriate test. See ../nctest for a * complete spec test. */#define REDEF/* #define SYNCDEBUG */#undef NDEBUG /* always active assert() in this file */#include <config.h>#include <stdio.h>#include <stddef.h>#include <stdlib.h>#include <string.h>#include <assert.h>#ifdef USE_NETCDF4#include <netcdf3.h>#include <nc3convert.h>#else#include "netcdf.h"#endif#define MAXSHORT 32767#define MAXINT 2147483647#define MAXBYTE 127#define FNAME "test.nc"#define NUM_DIMS 3#define DONT_CARE -1/* make these numbers big when you want to give this a real workout */#define NUM_RECS 8#define SIZE_1 7#define SIZE_2 8static struct { int num_dims; int num_vars; int num_attrs; int xtendim;} cdesc[1];static struct { char mnem[NC_MAX_NAME]; nc_type type; int ndims; int dims[NC_MAX_DIMS]; int num_attrs;} vdesc[1];static struct { char mnem[NC_MAX_NAME]; nc_type type; size_t len;} adesc[1];union getret{ char by[8]; short sh[4]; int in[2]; float fl[2]; double dbl;};static voidchkgot(nc_type type, union getret got, double check){ switch(type){ case NC_BYTE : assert( (char)check == got.by[0] ); break; case NC_CHAR : /* TODO */ assert( (char)check == got.by[0] ); break; case NC_SHORT : assert( (short)check == got.sh[0] ); break; case NC_INT : assert( (int)check == got.in[0] ); break; case NC_FLOAT : assert( (float)check == got.fl[0] ); break; case NC_DOUBLE : assert( check == got.dbl ); break; default: break; }}static const char *fname = FNAME;static size_t num_dims = NUM_DIMS;static size_t sizes[] = { NC_UNLIMITED, SIZE_1 , SIZE_2 };static const char * const dim_names[] = { "record", "ixx", "iyy"};static voidcreatetestdims(int cdfid, size_t num_dims, const size_t *sizes, const char * const dim_names[]){ int dimid; while(num_dims-- != 0) { assert( nc_def_dim(cdfid, *dim_names++, *sizes, &dimid) == NC_NOERR); sizes++; }}static voidtestdims(int cdfid, size_t num_dims, size_t *sizes, const char * const dim_names[]){ int ii; size_t size; char cp[NC_MAX_NAME]; for(ii=0; (size_t) ii < num_dims; ii++, sizes++) { assert( nc_inq_dim(cdfid, ii, cp, &size) == NC_NOERR); if( size != *sizes) (void) fprintf(stderr, "%d: %lu != %lu\n", ii, (unsigned long)size, (unsigned long)*sizes); assert( size == *sizes); assert( strcmp(cp, *dim_names++) == 0); }}static const char * const reqattr[] = { "UNITS", "VALIDMIN", "VALIDMAX", "SCALEMIN", "SCALEMAX", "FIELDNAM", _FillValue};#define NUM_RATTRS 6static struct tcdfvar { const char *mnem; nc_type type; const char *fieldnam; double validmin; double validmax; double scalemin; double scalemax; const char *units; int ndims; int dims[NUM_DIMS];} const testvars[] = {#define Byte_id 0 { "Byte", NC_BYTE, "Byte sized integer variable", -MAXBYTE, MAXBYTE, -MAXBYTE, MAXBYTE , "ones", 2, {0,1,DONT_CARE} },#define Char_id 1 { "Char", NC_CHAR, "char (string) variable", DONT_CARE, DONT_CARE, DONT_CARE, DONT_CARE, "(unitless)", 2, {0,2,DONT_CARE} },#define Short_id 2 { "Short", NC_SHORT, "Short variable", -MAXSHORT, MAXSHORT, -MAXSHORT, MAXSHORT , "ones", 2, {0, 2, DONT_CARE }},#define Long_id 3 { "Long", NC_INT, "Long Integer variable", /* 2.x backward strings */ -MAXINT, MAXINT, -MAXINT, MAXINT, "ones", 2, {1, 2, DONT_CARE}},#define Float_id 4 { "Float", NC_FLOAT, "Single Precision Floating Point variable", -MAXINT, MAXINT, -MAXINT, MAXINT, "flots", 3, {0, 1, 2 }},#define Double_id 5 { "Double", NC_DOUBLE, "Double Precision Floating Point variable", -MAXINT, MAXINT, -MAXINT, MAXINT, "dflots", 3, {0, 1, 2 }},};#define NUM_TESTVARS 6static voidcreatetestvars(int id, const struct tcdfvar *testvars, size_t count){ int ii; int varid; const struct tcdfvar *vp = testvars; for(ii = 0; (size_t) ii < count; ii++, vp++ ) { assert(nc_def_var(id, vp->mnem, vp->type, vp->ndims, vp->dims, &varid) == NC_NOERR ); assert( nc_put_att_text(id,ii,reqattr[0],strlen(vp->units), vp->units) == NC_NOERR); assert( nc_put_att_double(id,ii,reqattr[1],NC_DOUBLE,1, &vp->validmin) == NC_NOERR); assert( nc_put_att_double(id,ii,reqattr[2],NC_DOUBLE,1, &vp->validmax) == NC_NOERR); assert( nc_put_att_double(id,ii,reqattr[3],NC_DOUBLE,1, &vp->scalemin) == NC_NOERR); assert( nc_put_att_double(id,ii,reqattr[4],NC_DOUBLE,1, &vp->scalemax) == NC_NOERR); assert( nc_put_att_text(id,ii,reqattr[5],strlen(vp->fieldnam), vp->fieldnam) == NC_NOERR); }}static voidparray(const char *label, size_t count, const size_t array[]){ (void) fprintf(stdout, "%s", label); (void) fputc('\t',stdout); for(; count != 0; count--, array++) (void) fprintf(stdout," %lu", (unsigned long) *array);}static voidfill_seq(int id){ float values[NUM_RECS * SIZE_1 * SIZE_2]; size_t vindices[NUM_DIMS]; { size_t ii = 0; for(; ii < sizeof(values)/sizeof(values[0]); ii++) { values[ii] = (float) ii; } } /* zero the vindices */ { size_t *cc = vindices; while (cc < &vindices[num_dims]) *cc++ = 0; } sizes[0] = NUM_RECS; assert( nc_put_vara_float(id, Float_id, vindices, sizes, values)== NC_NOERR);}static voidcheck_fill_seq(int id){ size_t vindices[NUM_DIMS]; size_t *cc, *mm; union getret got; int ii = 0; float val; sizes[0] = NUM_RECS; cc = vindices; while (cc < &vindices[num_dims]) *cc++ = 0; /* ripple counter */ cc = vindices; mm = sizes; while (*vindices < *sizes) { while (*cc < *mm) { if (mm == &sizes[num_dims - 1]) { if(nc_get_var1_float(id, Float_id, vindices, &got.fl[0]) == -1) goto bad_ret; val = (float) ii; if(val != got.fl[0]) { parray("indices", NUM_DIMS, vindices); (void) printf("\t%f != %f\n", val, got.fl[0]); } (*cc)++; ii++; continue; } cc++; mm++; } if(cc == vindices) break; *cc = 0; cc--; mm--; (*cc)++; } return;bad_ret : (void) printf("couldn't get a var in check_fill_seq() %d\n", ii); return;}static const size_t indices[][3] = { {0, 1, 3}, {0, 3, 0}, {1, 2, 3}, {3, 2, 1}, {2, 1, 3}, {1, 0, 0},
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -